E5F6 feat(core): add `context.http` and move `statusCode` there by vonagam · Pull Request #2496 · feathersjs/feathers · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/express/src/rest.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
FF8
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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