diff --git a/rest/rest-tests.ts b/rest/rest-tests.ts index be3887246..26346389f 100644 --- a/rest/rest-tests.ts +++ b/rest/rest-tests.ts @@ -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, { 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' }); diff --git a/rest/rest.d.ts b/rest/rest.d.ts index d8126b72d..37041e150 100644 --- a/rest/rest.d.ts +++ b/rest/rest.d.ts @@ -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(interceptor: Interceptor, config?: T): Client; export interface Request { method?: string; @@ -48,8 +48,8 @@ declare module "rest" { header(headerName: string): when.Promise; // string or string[] } - export interface Interceptor { - (parent?: Client, config?: any): Client; + export interface Interceptor { + (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(interceptor: Interceptor, 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(config: interceptor.Config): 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; - response?: (response: rest.Response, config: any, meta: rest.Meta) => when.Promise; - success?: (response: rest.Response, config: any, meta: rest.Meta) => when.Promise; - error?: (response: rest.Response, config: any, meta: rest.Meta) => when.Promise; + interface Config { + init?: (config: T) => U; + request?: (request: rest.Request, config: U, meta: rest.Meta) => rest.Request | when.Promise; + response?: (response: rest.Response, config: U, meta: rest.Meta) => rest.Response | when.Promise; + success?: (response: rest.Response, config: U, meta: rest.Meta) => rest.Response | when.Promise; + error?: (response: rest.Response, config: U, meta: rest.Meta) => rest.Response | when.Promise; } } @@ -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; + + 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; + + 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; + + 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; + + 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; + + 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; + + 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; + + 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; + + 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; + + 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; + + 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; + + 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; + + 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; - write(value: any): any; // string or when.Promise; + var registry: registry.Registry; + + module registry { + interface MIMEConverter { + read(value: string): any | when.Promise; + write(value: any): string | when.Promise; + } + + interface Registry { + lookup(mimeType: string): when.Promise; + register(mimeType: string, converter: MIMEConverter): void; + } } - export function lookup(mimeType: string): when.Promise; - - export function register(mimeType: string, converter: MIMEConverter): void; + export = registry; }