Allow init to transform config

This commit is contained in:
Wim Looman
2015-01-19 16:44:07 +13:00
parent a2edb80689
commit 1685b0160b
2 changed files with 20 additions and 13 deletions
+13 -6
View File
@@ -91,12 +91,18 @@ 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);
console.log(config);
return response;
},
});
var transformedConfig = interceptor({
init: (config: KnownConfig) => {
return config.prop;
},
success: (response: rest.Response, config: string) => {
console.log(config);
return response;
},
});
@@ -127,4 +133,5 @@ client = rest
.wrap(xhr)
.wrap(noop)
.wrap(fail)
.wrap(knownConfig, { prop: 'value' });
.wrap(knownConfig, { prop: 'value' })
.wrap(transformedConfig, { prop: 'value' });
+7 -7
View File
@@ -71,15 +71,15 @@ declare module "rest/interceptor" {
import when = require("when");
import rest = require("rest");
function interceptor<T>(config: interceptor.Config<T>): rest.Interceptor<T>;
function interceptor<T, U>(config: interceptor.Config<T, U>): rest.Interceptor<T>;
module interceptor {
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>;
interface Config<T, U> {
init?: (config: T) => U;
request?: (request: rest.Request, config: U, meta: rest.Meta) => rest.Request | when.Promise<rest.Request>;
response?: (response: rest.Response, config: U, meta: rest.Meta) => rest.Response | when.Promise<rest.Response>;
success?: (response: rest.Response, config: U, meta: rest.Meta) => rest.Response | when.Promise<rest.Response>;
error?: (response: rest.Response, config: U, meta: rest.Meta) => rest.Response | when.Promise<rest.Response>;
}
}