-
Notifications
You must be signed in to change notification settings - Fork 22
feat(response)!: keyed/non-keyed responses #400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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'; |
| 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'), | ||
| }; |
| 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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch, this was here before fwiw There was a problem hiding this comment. Choose a reason for hiding this commentThe 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), | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎸