mirror of
https://github.com/wassname/DefinitelyTyped.git
synced 2026-09-07 16:30:12 +08:00
Add definition for custom interceptors
This commit is contained in:
+51
-14
@@ -1,5 +1,6 @@
|
||||
/// <reference path="./rest.d.ts" />
|
||||
|
||||
import when = require('when');
|
||||
import rest = require('rest');
|
||||
|
||||
import defaultRequest = require('rest/interceptor/defaultRequest');
|
||||
@@ -17,6 +18,7 @@ import jsonp = require('rest/interceptor/jsonp');
|
||||
import xdomain = require('rest/interceptor/ie/xdomain');
|
||||
import xhr = require('rest/interceptor/ie/xhr');
|
||||
|
||||
import interceptor = require('rest/interceptor');
|
||||
import registry = require('rest/mime/registry');
|
||||
|
||||
rest('/').then(function(response) {
|
||||
@@ -52,18 +54,53 @@ registry.register('application/vnd.com.example', {
|
||||
}
|
||||
});
|
||||
|
||||
var noop = interceptor({
|
||||
init: (config: any) => {
|
||||
return config;
|
||||
},
|
||||
request: (request: rest.Request, config: any, meta: rest.Meta) => {
|
||||
return request;
|
||||
},
|
||||
response: (response: rest.Response, config: any, meta: rest.Meta) => {
|
||||
return response;
|
||||
},
|
||||
success: (response: rest.Response, config: any, meta: rest.Meta) => {
|
||||
return response;
|
||||
},
|
||||
error: (response: rest.Response, config: any, meta: rest.Meta) => {
|
||||
return response;
|
||||
}
|
||||
});
|
||||
|
||||
var fail = interceptor({
|
||||
success: (response: rest.Response) => when.reject<rest.Response>(response),
|
||||
});
|
||||
|
||||
var succeed = interceptor({
|
||||
error: (response: rest.Response) => when(response),
|
||||
});
|
||||
|
||||
var defaulted = interceptor({
|
||||
init: (config: any) => {
|
||||
config.prop = config.prop || 'default-value';
|
||||
return config;
|
||||
},
|
||||
});
|
||||
|
||||
client = rest
|
||||
.wrap(defaultRequest)
|
||||
.wrap(hateoas)
|
||||
.wrap(location)
|
||||
.wrap(mime)
|
||||
.wrap(pathPrefix)
|
||||
.wrap(basicAuth)
|
||||
.wrap(oAuth)
|
||||
.wrap(csrf)
|
||||
.wrap(errorCode)
|
||||
.wrap(retry)
|
||||
.wrap(timeout)
|
||||
.wrap(jsonp)
|
||||
.wrap(xdomain)
|
||||
.wrap(xhr);
|
||||
.wrap(defaultRequest)
|
||||
.wrap(hateoas)
|
||||
.wrap(location)
|
||||
.wrap(mime)
|
||||
.wrap(pathPrefix)
|
||||
.wrap(basicAuth)
|
||||
.wrap(oAuth)
|
||||
.wrap(csrf)
|
||||
.wrap(errorCode)
|
||||
.wrap(retry)
|
||||
.wrap(timeout)
|
||||
.wrap(jsonp)
|
||||
.wrap(xdomain)
|
||||
.wrap(xhr)
|
||||
.wrap(noop)
|
||||
.wrap(fail);
|
||||
|
||||
Vendored
+33
@@ -59,9 +59,42 @@ declare module "rest" {
|
||||
skip(): Client;
|
||||
wrap(interceptor: Interceptor, config?: any): Client;
|
||||
}
|
||||
|
||||
export interface Meta {
|
||||
client: Client;
|
||||
arguments: any;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
declare module "rest/interceptor" {
|
||||
import when = require("when");
|
||||
import rest = require("rest");
|
||||
|
||||
// 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>;
|
||||
}
|
||||
|
||||
function interceptor(config: Config): rest.Interceptor;
|
||||
function interceptor(config: PromiseConfig): rest.Interceptor;
|
||||
|
||||
export = interceptor;
|
||||
}
|
||||
|
||||
declare module "rest/interceptor/defaultRequest" {
|
||||
import rest = require("rest");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user