From 23b98eea8d2c96e35bbe22831bd8f856a34c1187 Mon Sep 17 00:00:00 2001 From: Wim Looman Date: Mon, 19 Jan 2015 16:25:04 +1300 Subject: [PATCH] Update rest interceptor function for TS 1.4 Use union type for return values, make config typed. --- rest/rest-tests.ts | 23 +++++++++++++++++++++++ rest/rest.d.ts | 25 +++++++------------------ 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/rest/rest-tests.ts b/rest/rest-tests.ts index be3887246..f6bcfa183 100644 --- a/rest/rest-tests.ts +++ b/rest/rest-tests.ts @@ -87,6 +87,29 @@ var defaulted = interceptor({ }, }); +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); + return response; + }, +}); + +var promiseOrResponse = interceptor({ + success: (response: rest.Response) => { + return response; + }, + error: (response: rest.Response) => { + return when(response); + }, +}); + client = rest .wrap(defaultRequest) .wrap(hateoas) diff --git a/rest/rest.d.ts b/rest/rest.d.ts index d8126b72d..194124420 100644 --- a/rest/rest.d.ts +++ b/rest/rest.d.ts @@ -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) => T; + request?: (request: rest.Request, config: T, meta: rest.Meta) => rest.Request | when.Promise; + response?: (response: rest.Response, config: T, meta: rest.Meta) => rest.Response | when.Promise; + success?: (response: rest.Response, config: T, meta: rest.Meta) => rest.Response | when.Promise; + error?: (response: rest.Response, config: T, meta: rest.Meta) => rest.Response | when.Promise; } }