From b701bf77fb83048aa1dffa492b3d77dd53f7b72b Mon Sep 17 00:00:00 2001 From: Dmitrii Maganov Date: Fri, 26 Nov 2021 03:21:26 +0300 Subject: [PATCH] feat(core): add `context.http` and move `statusCode` there (#2496) --- packages/express/src/rest.ts | 2 +- packages/express/test/rest.test.ts | 6 ++++-- packages/feathers/src/declarations.ts | 14 ++++++++++++++ packages/feathers/src/hooks/index.ts | 8 +++++++- packages/koa/src/rest.ts | 2 +- packages/transport-commons/src/http.ts | 4 ++-- packages/transport-commons/test/http.test.ts | 2 +- 7 files changed, 30 insertions(+), 8 deletions(-) diff --git a/packages/express/src/rest.ts b/packages/express/src/rest.ts index 2976d7fe16..132be382dc 100644 --- a/packages/express/src/rest.ts +++ b/packages/express/src/rest.ts @@ -66,7 +66,7 @@ export const serviceMethodHandler = ( } const args = getArgs(options); - const context = createContext(service, method); + const context = createContext(service, method, { http: {} }); res.hook = context; diff --git a/packages/express/test/rest.test.ts b/packages/express/test/rest.test.ts index 235dddc7b7..c1d6a2fb4f 100644 --- a/packages/express/test/rest.test.ts +++ b/packages/express/test/rest.test.ts @@ -158,6 +158,7 @@ describe('@feathersjs/express/rest provider', () => { type: null, method: 'get', path: 'hook', + http: {}, event: null, result: { description: 'You have to do dishes' }, addedProperty: true @@ -196,7 +197,7 @@ describe('@feathersjs/express/rest provider', () => { app.service('hook-status').hooks({ after (hook: HookContext) { - hook.statusCode = 206; + hook.http!.statusCode = 206; } }); @@ -244,7 +245,8 @@ describe('@feathersjs/express/rest provider', () => { type: null, event: null, method: 'get', - path: 'hook-error' + path: 'hook-error', + http: {} }, error: { message: 'I blew up' } }); diff --git a/packages/feathers/src/declarations.ts b/packages/feathers/src/declarations.ts index 4afbd61a21..73c5228169 100644 --- a/packages/feathers/src/declarations.ts +++ b/packages/feathers/src/declarations.ts @@ -240,6 +240,14 @@ export interface Params { [key: string]: any; // (JL) not sure if we want this } +export interface Http { + /** + * A writeable, optional property that allows to override the standard HTTP status + * code that should be returned. + */ + statusCode?: number; +} + export interface HookContext extends BaseHookContext> { /** * A read only property that contains the Feathers application object. This can be used to @@ -310,8 +318,14 @@ export interface HookContext extends BaseHookContext ( method, service, event: null, - type: null + type: null, + get statusCode() { + return this.http?.statusCode; + }, + set statusCode(value: number) { + (this.http ||= {}).statusCode = value; + } }); return res; diff --git a/packages/koa/src/rest.ts b/packages/koa/src/rest.ts index ab5e5df833..fc32d6cf9d 100644 --- a/packages/koa/src/rest.ts +++ b/packages/koa/src/rest.ts @@ -37,7 +37,7 @@ export function rest () { route }; const args = createArguments({ id, data, params }); - const hookContext = createContext(service, method); + const hookContext = createContext(service, method, { http: {} }); ctx.hook = hookContext as any; diff --git a/packages/transport-commons/src/http.ts b/packages/transport-commons/src/http.ts index 23ed293ec9..c61e51330a 100644 --- a/packages/transport-commons/src/http.ts +++ b/packages/transport-commons/src/http.ts @@ -60,8 +60,8 @@ export function getData (context: HookContext) { } export function getStatusCode (context: HookContext, data?: any) { - if (context.statusCode) { - return context.statusCode; + if (context.http?.statusCode) { + return context.http.statusCode; } if (context.method === 'create') { diff --git a/packages/transport-commons/test/http.test.ts b/packages/transport-commons/test/http.test.ts index c23da805e4..2856a70657 100644 --- a/packages/transport-commons/test/http.test.ts +++ b/packages/transport-commons/test/http.test.ts @@ -19,7 +19,7 @@ describe('@feathersjs/transport-commons HTTP helpers', () => { it('getStatusCode', async () => { const statusContext = { - statusCode: 202 + http: { statusCode: 202 } }; const createContext = { method: 'create'