Merge pull request #3485 from Nemo157/rest-interceptor-1.4

Rest interceptor 1.4
This commit is contained in:
Masahiro Wakame
2015-01-19 22:41:21 +09:00
2 changed files with 173 additions and 44 deletions
+33 -2
View File
@@ -31,7 +31,7 @@ client({ path: '/data.json' }).then(function(response) {
console.log('response: ', response);
});
client = rest.wrap(mime, { mime: 'application/json' }).wrap(errorCode, { code: 500 });
client = rest.wrap<mime.Config>(mime, { mime: 'application/json' }).wrap(errorCode, { code: 500 });
client({ path: '/data.json' }).then(
function(response) {
console.log('response: ', response);
@@ -87,6 +87,35 @@ var defaulted = interceptor({
},
});
interface KnownConfig {
prop: string;
}
var knownConfig = interceptor({
success: (response: rest.Response, config: KnownConfig) => {
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;
},
});
var promiseOrResponse = interceptor({
success: (response: rest.Response) => {
return response;
},
error: (response: rest.Response) => {
return when(response);
},
});
client = rest
.wrap(defaultRequest)
.wrap(hateoas)
@@ -103,4 +132,6 @@ client = rest
.wrap(xdomain)
.wrap(xhr)
.wrap(noop)
.wrap(fail);
.wrap(fail)
.wrap(knownConfig, { prop: 'value' })
.wrap(transformedConfig, { prop: 'value' });
+140 -42
View File
@@ -14,7 +14,7 @@ declare module "rest" {
function rest(request: rest.Request): rest.ResponsePromise;
module rest {
export function wrap(interceptor: Interceptor, config?: any): Client;
export function wrap<T>(interceptor: Interceptor<T>, config?: T): Client;
export interface Request {
method?: string;
@@ -48,8 +48,8 @@ declare module "rest" {
header(headerName: string): when.Promise<any>; // string or string[]
}
export interface Interceptor {
(parent?: Client, config?: any): Client;
export interface Interceptor<T> {
(parent?: Client, config?: T): Client;
}
export interface Client {
@@ -57,7 +57,7 @@ declare module "rest" {
(request: Request): ResponsePromise;
skip(): Client;
wrap(interceptor: Interceptor, config?: any): Client;
wrap<T>(interceptor: Interceptor<T>, config?: T): Client;
}
export interface Meta {
@@ -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, U>(config: interceptor.Config<T, U>): rest.Interceptor<T>;
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, 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>;
}
}
@@ -100,7 +89,18 @@ declare module "rest/interceptor" {
declare module "rest/interceptor/defaultRequest" {
import rest = require("rest");
var defaultRequest: rest.Interceptor;
var defaultRequest: rest.Interceptor<defaultRequest.Config>;
module defaultRequest {
interface Config {
method?: string;
path?: string;
params?: any;
headers?: any;
entity?: any;
mixin?: any;
}
}
export = defaultRequest;
}
@@ -108,7 +108,14 @@ declare module "rest/interceptor/defaultRequest" {
declare module "rest/interceptor/hateoas" {
import rest = require("rest");
var hateoas: rest.Interceptor;
var hateoas: rest.Interceptor<hateoas.Config>;
module hateoas {
interface Config {
target?: string;
client?: rest.Client;
}
}
export = hateoas;
}
@@ -116,15 +123,32 @@ declare module "rest/interceptor/hateoas" {
declare module "rest/interceptor/location" {
import rest = require("rest");
var location: rest.Interceptor;
var location: rest.Interceptor<location.Config>;
module location {
interface Config {
client?: rest.Client;
code?: number;
}
}
export = location;
}
declare module "rest/interceptor/mime" {
import rest = require("rest");
import registry = require("rest/mime/registry");
var mime: rest.Interceptor;
var mime: rest.Interceptor<mime.Config>;
module mime {
interface Config {
mime?: string;
accept?: string;
registry?: registry.Registry;
permissive?: boolean;
}
}
export = mime;
}
@@ -132,7 +156,13 @@ declare module "rest/interceptor/mime" {
declare module "rest/interceptor/pathPrefix" {
import rest = require("rest");
var pathPrefix: rest.Interceptor;
var pathPrefix: rest.Interceptor<pathPrefix.Config>;
module pathPrefix {
interface Config {
prefix?: string;
}
}
export = pathPrefix;
}
@@ -140,7 +170,14 @@ declare module "rest/interceptor/pathPrefix" {
declare module "rest/interceptor/basicAuth" {
import rest = require("rest");
var basicAuth: rest.Interceptor;
var basicAuth: rest.Interceptor<basicAuth.Config>;
module basicAuth {
interface Config {
username?: string;
password?: string;
}
}
export = basicAuth;
}
@@ -148,7 +185,23 @@ declare module "rest/interceptor/basicAuth" {
declare module "rest/interceptor/oAuth" {
import rest = require("rest");
var oAuth: rest.Interceptor;
var oAuth: rest.Interceptor<oAuth.Config>;
module oAuth {
interface DismissWindow {
(): void;
}
interface Config {
token?: string;
clientId?: string;
scope?: string;
authorizationUrl?: string;
redirectUrl?: string;
windowStrategy?: (url: string) => DismissWindow;
oAuthCallback?: (hash: string) => void;
oAuthCallbackName?: string;
}
}
export = oAuth;
}
@@ -156,7 +209,14 @@ declare module "rest/interceptor/oAuth" {
declare module "rest/interceptor/csrf" {
import rest = require("rest");
var csrf: rest.Interceptor;
var csrf: rest.Interceptor<csrf.Config>;
module csrf {
interface Config {
name?: string;
token?: string;
}
}
export = csrf;
}
@@ -164,7 +224,13 @@ declare module "rest/interceptor/csrf" {
declare module "rest/interceptor/errorCode" {
import rest = require("rest");
var errorCode: rest.Interceptor;
var errorCode: rest.Interceptor<errorCode.Config>;
module errorCode {
interface Config {
code?: number;
}
}
export = errorCode;
}
@@ -172,7 +238,15 @@ declare module "rest/interceptor/errorCode" {
declare module "rest/interceptor/retry" {
import rest = require("rest");
var retry: rest.Interceptor;
var retry: rest.Interceptor<retry.Config>;
module retry {
interface Config {
initial?: number;
multiplier?: number;
max?: number;
}
}
export = retry;
}
@@ -180,7 +254,14 @@ declare module "rest/interceptor/retry" {
declare module "rest/interceptor/timeout" {
import rest = require("rest");
var timeout: rest.Interceptor;
var timeout: rest.Interceptor<timeout.Config>;
module timeout {
interface Config {
timeout?: number;
transient?: boolean;
}
}
export = timeout;
}
@@ -188,7 +269,17 @@ declare module "rest/interceptor/timeout" {
declare module "rest/interceptor/jsonp" {
import rest = require("rest");
var jsonp: rest.Interceptor;
var jsonp: rest.Interceptor<jsonp.Config>;
module jsonp {
interface Config {
callback?: {
param?: string;
prefix?: string;
name?: string;
}
}
}
export = jsonp;
}
@@ -196,7 +287,7 @@ declare module "rest/interceptor/jsonp" {
declare module "rest/interceptor/ie/xdomain" {
import rest = require("rest");
var xdomain: rest.Interceptor;
var xdomain: rest.Interceptor<{}>;
export = xdomain;
}
@@ -204,7 +295,7 @@ declare module "rest/interceptor/ie/xdomain" {
declare module "rest/interceptor/ie/xhr" {
import rest = require("rest");
var xhr: rest.Interceptor;
var xhr: rest.Interceptor<{}>;
export = xhr;
}
@@ -212,12 +303,19 @@ declare module "rest/interceptor/ie/xhr" {
declare module "rest/mime/registry" {
import when = require("when");
export interface MIMEConverter {
read(value: string): any; // any or when.Promise<any>;
write(value: any): any; // string or when.Promise<string>;
var registry: registry.Registry;
module registry {
interface MIMEConverter {
read(value: string): any | when.Promise<any>;
write(value: any): string | when.Promise<string>;
}
interface Registry {
lookup(mimeType: string): when.Promise<MIMEConverter>;
register(mimeType: string, converter: MIMEConverter): void;
}
}
export function lookup(mimeType: string): when.Promise<MIMEConverter>;
export function register(mimeType: string, converter: MIMEConverter): void;
export = registry;
}