Skip to content

Commit

Permalink
feat(core): add context.http and move statusCode there (#2496)
Browse files Browse the repository at this point in the history
  • Loading branch information
vonagam committed Nov 26, 2021
1 parent 9c22f70 commit b701bf7
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/express/src/rest.ts
Expand Up @@ -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;

Expand Down
6 changes: 4 additions & 2 deletions packages/express/test/rest.test.ts
Expand Up @@ -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
Expand Down Expand Up @@ -196,7 +197,7 @@ describe('@feathersjs/express/rest provider', () => {

app.service('hook-status').hooks({
after (hook: HookContext) {
hook.statusCode = 206;
hook.http!.statusCode = 206;
}
});

Expand Down Expand Up @@ -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' }
});
Expand Down
14 changes: 14 additions & 0 deletions packages/feathers/src/declarations.ts
Expand Up @@ -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<A = Application, S = any> extends BaseHookContext<ServiceGenericType<S>> {
/**
* A read only property that contains the Feathers application object. This can be used to
Expand Down Expand Up @@ -310,8 +318,14 @@ export interface HookContext<A = Application, S = any> extends BaseHookContext<S
/**
* A writeable, optional property that allows to override the standard HTTP status
* code that should be returned.
*
* @deprecated Use `http.statusCode` instead.
*/
statusCode?: number;
/**
* A writeable, optional property that contains options specific to HTTP transports.
*/
http?: Http;
/**
* The event emitted by this method. Can be set to `null` to skip event emitting.
*/
Expand Down
8 changes: 7 additions & 1 deletion packages/feathers/src/hooks/index.ts
Expand Up @@ -80,7 +80,13 @@ export function hookMixin<A> (
method,
service,
event: null,
type: null
type: null,
get statusCode() {
return this.http?.statusCode;
},
set statusCode(value: number) {
(this.http ||= {}).statusCode = value;
}
});

return res;
Expand Down
2 changes: 1 addition & 1 deletion packages/koa/src/rest.ts
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions packages/transport-commons/src/http.ts
Expand Up @@ -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') {
Expand Down
2 changes: 1 addition & 1 deletion packages/transport-commons/test/http.test.ts
Expand Up @@ -19,7 +19,7 @@ describe('@feathersjs/transport-commons HTTP helpers', () => {

it('getStatusCode', async () => {
const statusContext = {
statusCode: 202
http: { statusCode: 202 }
};
const createContext = {
method: 'create'
Expand Down

0 comments on commit b701bf7

Please sign in to comment.