Merge pull request #3445 from Nemo157/rest-interceptors

Add rest interceptor definitions
This commit is contained in:
Masahiro Wakame
2015-01-13 18:58:57 +09:00
2 changed files with 200 additions and 3 deletions
+66
View File
@@ -1,8 +1,24 @@
/// <reference path="./rest.d.ts" />
import when = require('when');
import rest = require('rest');
import defaultRequest = require('rest/interceptor/defaultRequest');
import hateoas = require('rest/interceptor/hateoas');
import location = require('rest/interceptor/location');
import mime = require('rest/interceptor/mime');
import pathPrefix = require('rest/interceptor/pathPrefix');
import basicAuth = require('rest/interceptor/basicAuth');
import oAuth = require('rest/interceptor/oAuth');
import csrf = require('rest/interceptor/csrf');
import errorCode = require('rest/interceptor/errorCode');
import retry = require('rest/interceptor/retry');
import timeout = require('rest/interceptor/timeout');
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) {
@@ -38,3 +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(noop)
.wrap(fail);
+134 -3
View File
@@ -59,9 +59,108 @@ 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");
function interceptor(config: interceptor.Config): rest.Interceptor;
function interceptor(config: interceptor.PromiseConfig): 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>;
}
}
export = interceptor;
}
declare module "rest/interceptor/defaultRequest" {
import rest = require("rest");
var defaultRequest: rest.Interceptor;
export = defaultRequest;
}
declare module "rest/interceptor/hateoas" {
import rest = require("rest");
var hateoas: rest.Interceptor;
export = hateoas;
}
declare module "rest/interceptor/location" {
import rest = require("rest");
var location: rest.Interceptor;
export = location;
}
declare module "rest/interceptor/mime" {
import rest = require("rest");
var mime: rest.Interceptor;
export = mime;
}
declare module "rest/interceptor/pathPrefix" {
import rest = require("rest");
var pathPrefix: rest.Interceptor;
export = pathPrefix;
}
declare module "rest/interceptor/basicAuth" {
import rest = require("rest");
var basicAuth: rest.Interceptor;
export = basicAuth;
}
declare module "rest/interceptor/oAuth" {
import rest = require("rest");
var oAuth: rest.Interceptor;
export = oAuth;
}
declare module "rest/interceptor/csrf" {
import rest = require("rest");
var csrf: rest.Interceptor;
export = csrf;
}
declare module "rest/interceptor/errorCode" {
import rest = require("rest");
@@ -70,12 +169,44 @@ declare module "rest/interceptor/errorCode" {
export = errorCode;
}
declare module "rest/interceptor/mime" {
declare module "rest/interceptor/retry" {
import rest = require("rest");
var mime: rest.Interceptor;
var retry: rest.Interceptor;
export = mime;
export = retry;
}
declare module "rest/interceptor/timeout" {
import rest = require("rest");
var timeout: rest.Interceptor;
export = timeout;
}
declare module "rest/interceptor/jsonp" {
import rest = require("rest");
var jsonp: rest.Interceptor;
export = jsonp;
}
declare module "rest/interceptor/ie/xdomain" {
import rest = require("rest");
var xdomain: rest.Interceptor;
export = xdomain;
}
declare module "rest/interceptor/ie/xhr" {
import rest = require("rest");
var xhr: rest.Interceptor;
export = xhr;
}
declare module "rest/mime/registry" {