Skip to content

Commit

Permalink
Small refactoring for code consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl committed Apr 4, 2022
1 parent 02bfae0 commit e95e11b
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions packages/transport-commons/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const knownMethods: { [key: string]: string } = {

export function getServiceMethod (_httpMethod: string, id: unknown, headerOverride?: string) {
const httpMethod = _httpMethod.toLowerCase();

if (httpMethod === 'post' && headerOverride) {
return headerOverride;
}
Expand Down Expand Up @@ -55,32 +55,41 @@ export const argumentsFor = {
default: ({ data, params }: ServiceParams) => [ data, params ]
}

export function getResponse (context: HookContext) {
const http = context.http || {};
export function getStatusCode (context: HookContext, body: any, location: string|string[]) {
const { http = {} } = context;

let status = statusCodes.success;
let headers = http.headers || {};
let location = headers[ 'Location' ];
let body = context.result;
if (http.status) {
return http.status;
}

if (context.method === 'create') {
return statusCodes.created;
}

if (location !== undefined) {
return statusCodes.seeOther;
}

if (context.dispatch !== undefined) {
body = context.dispatch;
if (!body) {
return statusCodes.noContent;
}

return statusCodes.success;
}

export function getResponse (context: HookContext) {
const { http = {} } = context;
const body = context.dispatch !== undefined ? context.dispatch : context.result;

let headers = http.headers || {};
let location = headers.Location;

if (http.location !== undefined) {
location = encodeUrl(http.location);
headers = { ...headers, Location: location };
}

if (http.status) {
status = http.status;
} else if (context.method === 'create') {
status = statusCodes.created;
} else if (location !== undefined) {
status = statusCodes.seeOther;
} else if (!body) {
status = statusCodes.noContent;
}
const status = getStatusCode(context, body, location);

return { status, headers, body };
}

0 comments on commit e95e11b

Please # to comment.