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: 2 additions & 0 deletions packages/io-ts-http/src/statusCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const HttpToKeyStatus = {
401: 'unauthenticated',
403: 'permissionDenied',
404: 'notFound',
409: 'conflict',
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎸

429: 'rateLimitExceeded',
500: 'internalError',
503: 'serviceUnavailable',
Expand All @@ -19,6 +20,7 @@ export const KeyToHttpStatus = {
unauthenticated: 401,
permissionDenied: 403,
notFound: 404,
conflict: 409,
rateLimitExceeded: 429,
internalError: 500,
serviceUnavailable: 503,
Expand Down
43 changes: 2 additions & 41 deletions packages/response/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,2 @@
/*
* @api-ts/response
*/

// HTTP | GRPC | Response
// ----------------------------|--------------------|---------------------------
// 400 (bad request) | INVALID_ARGUMENT | Response.invalidRequest
// 401 (unauthorized) | UNAUTHENTICATED | Response.unauthenticated
// 403 (forbidden) | PERMISSION_DENIED | Response.permissionDenied
// 404 (not found) | NOT_FOUND | Response.notFound
// 405 (method not allowed) | NOT_FOUND | Response.notFound
// 429 (rate-limit) | RESOURCE_EXHAUSTED | Response.rateLimitExceeded
// 500 (internal server error) | INTERNAL | Response.internalError
// 503 (service unavailable) | UNAVAILABLE | Response.serviceUnavailable

export type Status =
| 'ok'
| 'invalidRequest'
| 'unauthenticated'
| 'permissionDenied'
| 'notFound'
| 'rateLimitExceeded'
| 'internalError'
| 'serviceUnavailable';

export type Response = { type: Status; payload: unknown };

const responseFunction =
<S extends Status>(status: S) =>
<T>(payload: T) => ({ type: status, payload });

export const Response = {
ok: responseFunction('ok'),
invalidRequest: responseFunction('invalidRequest'),
unauthenticated: responseFunction('unauthenticated'),
permissionDenied: responseFunction('permissionDenied'),
notFound: responseFunction('notFound'),
rateLimitExceeded: responseFunction('rateLimitExceeded'),
internalError: responseFunction('internalError'),
serviceUnavailable: responseFunction('serviceUnavailable'),
};
export * from './response';
export * from './keyed-response';
44 changes: 44 additions & 0 deletions packages/response/src/keyed-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* @api-ts/response
*/

// HTTP | GRPC | Response
// ----------------------------|--------------------|---------------------------
// 400 (bad request) | INVALID_ARGUMENT | Response.invalidRequest
// 401 (unauthorized) | UNAUTHENTICATED | Response.unauthenticated
// 403 (forbidden) | PERMISSION_DENIED | Response.permissionDenied
// 404 (not found) | NOT_FOUND | Response.notFound
// 405 (method not allowed) | NOT_FOUND | Response.notFound
// 409 (conflict) | ALREADY_EXISTS | Response.conflict
// 429 (rate-limit) | RESOURCE_EXHAUSTED | Response.rateLimitExceeded
// 500 (internal server error) | INTERNAL | Response.internalError
// 503 (service unavailable) | UNAVAILABLE | Response.serviceUnavailable

export type KeyedStatus =
| 'ok'
| 'invalidRequest'
| 'unauthenticated'
| 'permissionDenied'
| 'notFound'
| 'conflict'
| 'rateLimitExceeded'
| 'internalError'
| 'serviceUnavailable';

export type KeyedResponse = { type: KeyedStatus; payload: unknown };

const responseFunction =
<S extends KeyedStatus>(status: S) =>
<T>(payload: T) => ({ type: status, payload });

export const KeyedResponse = {
ok: responseFunction('ok'),
invalidRequest: responseFunction('invalidRequest'),
unauthenticated: responseFunction('unauthenticated'),
permissionDenied: responseFunction('permissionDenied'),
notFound: responseFunction('notFound'),
conflict: responseFunction('conflict'),
rateLimitExceeded: responseFunction('rateLimitExceeded'),
internalError: responseFunction('internalError'),
serviceUnavailable: responseFunction('serviceUnavailable'),
};
35 changes: 35 additions & 0 deletions packages/response/src/response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* @api-ts/response
*/

// HTTP | GRPC | Response
// ----------------------------|--------------------|---------------------------
// 400 (bad request) | INVALID_ARGUMENT | Response.invalidRequest
// 401 (unauthorized) | UNAUTHENTICATED | Response.unauthenticated
// 403 (forbidden) | PERMISSION_DENIED | Response.permissionDenied
// 404 (not found) | NOT_FOUND | Response.notFound
// 405 (method not allowed) | NOT_FOUND | Response.notFound
// 409 (conflict) | ALREADY_EXISTS | Response.conflict
// 429 (rate-limit) | RESOURCE_EXHAUSTED | Response.rateLimitExceeded
// 500 (internal server error) | INTERNAL | Response.internalError
// 503 (service unavailable) | UNAVAILABLE | Response.serviceUnavailable

export type Status = 200 | 400 | 401 | 403 | 404 | 409 | 429 | 500 | 503;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a change req but wanted to point out that we specify 405 in the comment above but can't actually throw it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, this was here before fwiw

Copy link

@manav-c manav-c Feb 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I figured


export type Response = { type: Status; payload: unknown };

const ResponseFunction =
<S extends Status>(status: S) =>
<T>(payload: T) => ({ type: status, payload });

export const Response = {
ok: ResponseFunction(200),
invalidRequest: ResponseFunction(400),
unauthenticated: ResponseFunction(401),
permissionDenied: ResponseFunction(403),
notFound: ResponseFunction(404),
conflict: ResponseFunction(409),
rateLimitExceeded: ResponseFunction(429),
internalError: ResponseFunction(500),
serviceUnavailable: ResponseFunction(503),
};