Update rest interceptor function for TS 1.4

Use union type for return values, make config typed.
This commit is contained in:
Wim Looman
2015-01-19 16:25:04 +13:00
parent 56068d3354
commit 23b98eea8d
2 changed files with 30 additions and 18 deletions
+23
View File
@@ -87,6 +87,29 @@ var defaulted = interceptor({
},
});
interface KnownConfig {
prop: string;
}
var knownConfig = interceptor({
init: (config: KnownConfig) => {
config.prop = config.prop || 'default-value';
return config;
},
success: (response: rest.Response, config: KnownConfig) => {
console.log(config.prop);
return response;
},
});
var promiseOrResponse = interceptor({
success: (response: rest.Response) => {
return response;
},
error: (response: rest.Response) => {
return when(response);
},
});
client = rest
.wrap(defaultRequest)
.wrap(hateoas)
+7 -18
View File
@@ -71,26 +71,15 @@ declare module "rest/interceptor" {
import when = require("when");
import rest = require("rest");
function interceptor(config: interceptor.Config): rest.Interceptor;
function interceptor(config: interceptor.PromiseConfig): rest.Interceptor;
function interceptor<T>(config: interceptor.Config<T>): rest.Interceptor;
module interceptor {
// TODO: These two configs should be merged to use union output types.
interface Config {
init?: (config: any) => any;
request?: (request: rest.Request, config: any, meta: rest.Meta) => rest.Request;
response?: (response: rest.Response, config: any, meta: rest.Meta) => rest.Response;
success?: (response: rest.Response, config: any, meta: rest.Meta) => rest.Response;
error?: (response: rest.Response, config: any, meta: rest.Meta) => rest.Response;
}
interface PromiseConfig {
init?: (config: any) => any;
request?: (request: rest.Request, config: any, meta: rest.Meta) => when.Promise<rest.Request>;
response?: (response: rest.Response, config: any, meta: rest.Meta) => when.Promise<rest.Response>;
success?: (response: rest.Response, config: any, meta: rest.Meta) => when.Promise<rest.Response>;
error?: (response: rest.Response, config: any, meta: rest.Meta) => when.Promise<rest.Response>;
interface Config<T> {
init?: (config: T) => T;
request?: (request: rest.Request, config: T, meta: rest.Meta) => rest.Request | when.Promise<rest.Request>;
response?: (response: rest.Response, config: T, meta: rest.Meta) => rest.Response | when.Promise<rest.Response>;
success?: (response: rest.Response, config: T, meta: rest.Meta) => rest.Response | when.Promise<rest.Response>;
error?: (response: rest.Response, config: T, meta: rest.Meta) => rest.Response | when.Promise<rest.Response>;
}
}