diff --git a/packages/transport-commons/src/http.ts b/packages/transport-commons/src/http.ts index 262dde2176..f7114b238f 100644 --- a/packages/transport-commons/src/http.ts +++ b/packages/transport-commons/src/http.ts @@ -62,10 +62,6 @@ export function getStatusCode(context: HookContext, body: any, location: string return http.status } - if (context.method === 'create') { - return statusCodes.created - } - if (location !== undefined) { return statusCodes.seeOther } @@ -74,6 +70,10 @@ export function getStatusCode(context: HookContext, body: any, location: string return statusCodes.noContent } + if (context.method === 'create') { + return statusCodes.created + } + return statusCodes.success } diff --git a/packages/transport-commons/test/http.test.ts b/packages/transport-commons/test/http.test.ts index dfbe4ef74f..ca15f9c499 100644 --- a/packages/transport-commons/test/http.test.ts +++ b/packages/transport-commons/test/http.test.ts @@ -22,16 +22,29 @@ describe('@feathersjs/transport-commons HTTP helpers', () => { http: { status: 202 } } const createContext = { + method: 'create', + result: {} + } + const createEmptyContext = { method: 'create' } const redirectContext = { http: { location: '/' } } + const redirectCreateContext = { + method: 'create', + http: { location: '/' } + } assert.strictEqual(http.getResponse(statusContext as HookContext).status, 202) assert.strictEqual(http.getResponse(createContext as HookContext).status, http.statusCodes.created) assert.strictEqual(http.getResponse(redirectContext as HookContext).status, http.statusCodes.seeOther) + assert.strictEqual( + http.getResponse(redirectCreateContext as HookContext).status, + http.statusCodes.seeOther + ) assert.strictEqual(http.getResponse({} as HookContext).status, http.statusCodes.noContent) + assert.strictEqual(http.getResponse(createEmptyContext as HookContext).status, http.statusCodes.noContent) assert.strictEqual(http.getResponse({ result: true } as HookContext).status, http.statusCodes.success) })