Skip to content

Commit 6a33aba

Browse files
Merge pull request #400 from jonball4/update-response-lib
feat(response)!: keyed/non-keyed responses
2 parents d6b9a38 + 4de6465 commit 6a33aba

File tree

4 files changed

+83
-41
lines changed

4 files changed

+83
-41
lines changed

packages/io-ts-http/src/statusCode.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const HttpToKeyStatus = {
66
401: 'unauthenticated',
77
403: 'permissionDenied',
88
404: 'notFound',
9+
409: 'conflict',
910
429: 'rateLimitExceeded',
1011
500: 'internalError',
1112
503: 'serviceUnavailable',
@@ -19,6 +20,7 @@ export const KeyToHttpStatus = {
1920
unauthenticated: 401,
2021
permissionDenied: 403,
2122
notFound: 404,
23+
conflict: 409,
2224
rateLimitExceeded: 429,
2325
internalError: 500,
2426
serviceUnavailable: 503,

packages/response/src/index.ts

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,2 @@
1-
/*
2-
* @api-ts/response
3-
*/
4-
5-
// HTTP | GRPC | Response
6-
// ----------------------------|--------------------|---------------------------
7-
// 400 (bad request) | INVALID_ARGUMENT | Response.invalidRequest
8-
// 401 (unauthorized) | UNAUTHENTICATED | Response.unauthenticated
9-
// 403 (forbidden) | PERMISSION_DENIED | Response.permissionDenied
10-
// 404 (not found) | NOT_FOUND | Response.notFound
11-
// 405 (method not allowed) | NOT_FOUND | Response.notFound
12-
// 429 (rate-limit) | RESOURCE_EXHAUSTED | Response.rateLimitExceeded
13-
// 500 (internal server error) | INTERNAL | Response.internalError
14-
// 503 (service unavailable) | UNAVAILABLE | Response.serviceUnavailable
15-
16-
export type Status =
17-
| 'ok'
18-
| 'invalidRequest'
19-
| 'unauthenticated'
20-
| 'permissionDenied'
21-
| 'notFound'
22-
| 'rateLimitExceeded'
23-
| 'internalError'
24-
| 'serviceUnavailable';
25-
26-
export type Response = { type: Status; payload: unknown };
27-
28-
const responseFunction =
29-
<S extends Status>(status: S) =>
30-
<T>(payload: T) => ({ type: status, payload });
31-
32-
export const Response = {
33-
ok: responseFunction('ok'),
34-
invalidRequest: responseFunction('invalidRequest'),
35-
unauthenticated: responseFunction('unauthenticated'),
36-
permissionDenied: responseFunction('permissionDenied'),
37-
notFound: responseFunction('notFound'),
38-
rateLimitExceeded: responseFunction('rateLimitExceeded'),
39-
internalError: responseFunction('internalError'),
40-
serviceUnavailable: responseFunction('serviceUnavailable'),
41-
};
1+
export * from './response';
2+
export * from './keyed-response';
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @api-ts/response
3+
*/
4+
5+
// HTTP | GRPC | Response
6+
// ----------------------------|--------------------|---------------------------
7+
// 400 (bad request) | INVALID_ARGUMENT | Response.invalidRequest
8+
// 401 (unauthorized) | UNAUTHENTICATED | Response.unauthenticated
9+
// 403 (forbidden) | PERMISSION_DENIED | Response.permissionDenied
10+
// 404 (not found) | NOT_FOUND | Response.notFound
11+
// 405 (method not allowed) | NOT_FOUND | Response.notFound
12+
// 409 (conflict) | ALREADY_EXISTS | Response.conflict
13+
// 429 (rate-limit) | RESOURCE_EXHAUSTED | Response.rateLimitExceeded
14+
// 500 (internal server error) | INTERNAL | Response.internalError
15+
// 503 (service unavailable) | UNAVAILABLE | Response.serviceUnavailable
16+
17+
export type KeyedStatus =
18+
| 'ok'
19+
| 'invalidRequest'
20+
| 'unauthenticated'
21+
| 'permissionDenied'
22+
| 'notFound'
23+
| 'conflict'
24+
| 'rateLimitExceeded'
25+
| 'internalError'
26+
| 'serviceUnavailable';
27+
28+
export type KeyedResponse = { type: KeyedStatus; payload: unknown };
29+
30+
const responseFunction =
31+
<S extends KeyedStatus>(status: S) =>
32+
<T>(payload: T) => ({ type: status, payload });
33+
34+
export const KeyedResponse = {
35+
ok: responseFunction('ok'),
36+
invalidRequest: responseFunction('invalidRequest'),
37+
unauthenticated: responseFunction('unauthenticated'),
38+
permissionDenied: responseFunction('permissionDenied'),
39+
notFound: responseFunction('notFound'),
40+
conflict: responseFunction('conflict'),
41+
rateLimitExceeded: responseFunction('rateLimitExceeded'),
42+
internalError: responseFunction('internalError'),
43+
serviceUnavailable: responseFunction('serviceUnavailable'),
44+
};

packages/response/src/response.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* @api-ts/response
3+
*/
4+
5+
// HTTP | GRPC | Response
6+
// ----------------------------|--------------------|---------------------------
7+
// 400 (bad request) | INVALID_ARGUMENT | Response.invalidRequest
8+
// 401 (unauthorized) | UNAUTHENTICATED | Response.unauthenticated
9+
// 403 (forbidden) | PERMISSION_DENIED | Response.permissionDenied
10+
// 404 (not found) | NOT_FOUND | Response.notFound
11+
// 405 (method not allowed) | NOT_FOUND | Response.notFound
12+
// 409 (conflict) | ALREADY_EXISTS | Response.conflict
13+
// 429 (rate-limit) | RESOURCE_EXHAUSTED | Response.rateLimitExceeded
14+
// 500 (internal server error) | INTERNAL | Response.internalError
15+
// 503 (service unavailable) | UNAVAILABLE | Response.serviceUnavailable
16+
17+
export type Status = 200 | 400 | 401 | 403 | 404 | 409 | 429 | 500 | 503;
18+
19+
export type Response = { type: Status; payload: unknown };
20+
21+
const ResponseFunction =
22+
<S extends Status>(status: S) =>
23+
<T>(payload: T) => ({ type: status, payload });
24+
25+
export const Response = {
26+
ok: ResponseFunction(200),
27+
invalidRequest: ResponseFunction(400),
28+
unauthenticated: ResponseFunction(401),
29+
permissionDenied: ResponseFunction(403),
30+
notFound: ResponseFunction(404),
31+
conflict: ResponseFunction(409),
32+
rateLimitExceeded: ResponseFunction(429),
33+
internalError: ResponseFunction(500),
34+
serviceUnavailable: ResponseFunction(503),
35+
};

0 commit comments

Comments
 (0)