Skip to content

Commit e95e11b

Browse files
committed
Small refactoring for code consistency
1 parent 02bfae0 commit e95e11b

File tree

1 file changed

+27
-18
lines changed
  • packages/transport-commons/src

1 file changed

+27
-18
lines changed

packages/transport-commons/src/http.ts

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const knownMethods: { [key: string]: string } = {
2727

2828
export function getServiceMethod (_httpMethod: string, id: unknown, headerOverride?: string) {
2929
const httpMethod = _httpMethod.toLowerCase();
30-
30+
3131
if (httpMethod === 'post' && headerOverride) {
3232
return headerOverride;
3333
}
@@ -55,32 +55,41 @@ export const argumentsFor = {
5555
default: ({ data, params }: ServiceParams) => [ data, params ]
5656
}
5757

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

61-
let status = statusCodes.success;
62-
let headers = http.headers || {};
63-
let location = headers[ 'Location' ];
64-
let body = context.result;
61+
if (http.status) {
62+
return http.status;
63+
}
64+
65+
if (context.method === 'create') {
66+
return statusCodes.created;
67+
}
68+
69+
if (location !== undefined) {
70+
return statusCodes.seeOther;
71+
}
6572

66-
if (context.dispatch !== undefined) {
67-
body = context.dispatch;
73+
if (!body) {
74+
return statusCodes.noContent;
6875
}
6976

77+
return statusCodes.success;
78+
}
79+
80+
export function getResponse (context: HookContext) {
81+
const { http = {} } = context;
82+
const body = context.dispatch !== undefined ? context.dispatch : context.result;
83+
84+
let headers = http.headers || {};
85+
let location = headers.Location;
86+
7087
if (http.location !== undefined) {
7188
location = encodeUrl(http.location);
7289
headers = { ...headers, Location: location };
7390
}
7491

75-
if (http.status) {
76-
status = http.status;
77-
} else if (context.method === 'create') {
78-
status = statusCodes.created;
79-
} else if (location !== undefined) {
80-
status = statusCodes.seeOther;
81-
} else if (!body) {
82-
status = statusCodes.noContent;
83-
}
92+
const status = getStatusCode(context, body, location);
8493

8594
return { status, headers, body };
8695
}

0 commit comments

Comments
 (0)