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
102 changes: 102 additions & 0 deletions packages/io-ts-http/src/statusCode.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,131 @@
// TODO: Enforce consistency at the type level

export const HttpToKeyStatus = {
100: 'continue',
101: 'switchingProtocols',
102: 'processing',
200: 'ok',
201: 'created',
202: 'accepted',
203: 'nonAuthoritativeInformation',
204: 'noContent',
205: 'resetContent',
206: 'partialContent',
207: 'multiStatus',
208: 'alreadyReported',
226: 'imUsed',
300: 'multipleChoices',
301: 'movedPermanently',
302: 'found',
303: 'seeOther',
304: 'notModified',
307: 'temporaryRedirect',
308: 'permanentRedirect',
400: 'invalidRequest',
401: 'unauthenticated',
402: 'paymentRequired',
403: 'permissionDenied',
404: 'notFound',
405: 'methodNotAllowed',
406: 'notAcceptable',
407: 'proxyAuthenticationRequired',
408: 'requestTimeout',
409: 'conflict',
410: 'gone',
411: 'lengthRequired',
412: 'preconditionFailed',
413: 'contentTooLarge',
414: 'uriTooLong',
415: 'unsupportedMediaType',
416: 'rangeNotSatisfiable',
417: 'exceptionFailed',
418: 'imATeapot',
421: 'misdirectedRequest',
422: 'unprocessableContent',
423: 'locked',
424: 'failedDependency',
425: 'tooEarly',
426: 'upgradeRequired',
428: 'preconditionRequired',
429: 'rateLimitExceeded',
431: 'requestHeaderFieldsTooLarge',
451: 'unavailableForLegalReasons',
500: 'internalError',
501: 'notImplemented',
502: 'badGateway',
503: 'serviceUnavailable',
504: 'gatewayTimeout',
505: 'httpVersionNotSupported',
506: 'variantAlsoNegotiates',
507: 'insufficientStorage',
508: 'loopDetected',
510: 'notExtended',
511: 'networkAuthenticationRequired',
} as const;

export type HttpToKeyStatus = typeof HttpToKeyStatus;

export const KeyToHttpStatus = {
continue: 100,
switchingProtocols: 101,
processing: 102,
ok: 200,
created: 201,
accepted: 202,
nonAuthoritativeInformation: 203,
noContent: 204,
resetContent: 205,
partialContent: 206,
multiStatus: 207,
alreadyReported: 208,
imUsed: 226,
multipleChoices: 300,
movedPermanently: 301,
found: 302,
seeOther: 303,
notModified: 304,
temporaryRedirect: 307,
permanentRedirect: 308,
invalidRequest: 400,
unauthenticated: 401,
paymentRequired: 402,
permissionDenied: 403,
notFound: 404,
methodNotAllowed: 405,
notAcceptable: 406,
proxyAuthenticationRequired: 407,
requestTimeout: 408,
conflict: 409,
gone: 410,
lengthRequired: 411,
preconditionFailed: 412,
contentTooLarge: 413,
uriTooLong: 414,
unsupportedMediaType: 415,
rangeNotSatisfiable: 416,
exceptionFailed: 417,
imATeapot: 418,
misdirectedRequest: 421,
unprocessableContent: 422,
locked: 423,
failedDependency: 424,
tooEarly: 425,
upgradeRequired: 426,
preconditionRequired: 428,
rateLimitExceeded: 429,
requestHeaderFieldsTooLarge: 431,
unavailableForLegalReasons: 451,
internalError: 500,
notImplemented: 501,
badGateway: 502,
serviceUnavailable: 503,
gatewayTimeout: 504,
httpVersionNotSupported: 505,
variantAlsoNegotiates: 506,
insufficientStorage: 507,
loopDetected: 508,
notExtended: 510,
networkAuthenticationRequired: 511,
} as const;

export type KeyToHttpStatus = typeof KeyToHttpStatus;
116 changes: 103 additions & 13 deletions packages/response/src/keyed-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,67 @@
* @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
Copy link
Contributor

Choose a reason for hiding this comment

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

Leaving a quick comment, this thought isn't fully formed/revived:

api-ts was originally conceived to be transport-layer agnostic, so you could use the same service handler but swap out the transport layer. For example, with no changes to business logic you could switch from HTTP to gRPC.

io-ts-http happens to be the HTTP-specific api-ts library. We haven't included any gRPC-specific code yet, but this PR proposes changes that undeniably couple the response library, and api-ts, to HTTP.

I'd love to hear @bitgopatmcl's thoughts!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The motivation here was that we keep having needs for different HTTP status codes popping up, and each time they are added in a one-off slowing down the ticket which needs the new status code. Given that there are a finite number of supported and recognized status codes, I thought it best to add all of them.

I thought this comment seemed a little misplaced as it is the only mention of GRPc in this lib is in this comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there any planned net-new development on these libs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I do feel like if it is to be transport-layer agnostic, it should have all status codes available to the lib user on HTTP, right?

Choose a reason for hiding this comment

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

just want to support the need for having more status codes

we've defined a bunch of these ourselves but having them in is io-ts-http definitely makes sense

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think these status codes also need to be defined here

I believe I have already modified that file as part of this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The @api-ts/response dependency is also not actually used by io-ts-http despite being listed in package.json. If projects are just pulling in response for the couple wrapper functions it exports, perhaps we can just export http-specific versions of those from io-ts-http leaving response protocol-agnostic (although if we do go to implement, say, io-ts-grpc in the future then we probably need to do some more work around what the role of this response library actually is).

To clarify is this a change request or simply a suggestion for the future? Want to note that this would introduce a breaking change if we were to pull this out of the response lib.

Also, it seems like there are already http statuses which don't map 1-1 with a gRPC status, for example 405 -> NOT_FOUND. Seems like response is already 'coupled' to http in some sense.

Copy link

@manav-c manav-c Mar 6, 2023

Choose a reason for hiding this comment

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

It feels like the response lib should just be called response-http. And we can have separate response libs for other protocols. This is not a change request. Just a suggestion

Copy link
Contributor

Choose a reason for hiding this comment

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

It feels like the response lib should just be called response-http. And we can have separate response libs for other protocols. This is not a change request. Just a suggestion

Yeah, either that or just merge it into io-ts-http. If we do eventually want to implement other protocols such as gRPC, then services that want to be protocol-agnostic can limit themselves to the intersection of response codes supported by all protocols.

This avoids needing to try and anticipate what the set of "generic" responses is, like this library currently does. Even then it only mentions gRPC, if we want to use something else then it could have an entirely different set of responses anyway.

With that in mind I think we can merge this with intent to deprecate the library as a follow-up.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, thank you Patrick! I can add a ticket for this if you haven't already done so


export type KeyedStatus =
| 'continue'
| 'switchingProtocols'
| 'processing'
| 'ok'
| 'created'
| 'accepted'
| 'nonAuthoritativeInformation'
| 'noContent'
| 'resetContent'
| 'partialContent'
| 'multiStatus'
| 'alreadyReported'
| 'imUsed'
| 'multipleChoices'
| 'movedPermanently'
| 'found'
| 'seeOther'
| 'notModified'
| 'temporaryRedirect'
| 'permanentRedirect'
| 'invalidRequest'
| 'unauthenticated'
| 'paymentRequired'
| 'permissionDenied'
| 'notFound'
| 'methodNotAllowed'
| 'notAcceptable'
| 'proxyAuthenticationRequired'
| 'requestTimeout'
| 'conflict'
| 'gone'
| 'lengthRequired'
| 'preconditionFailed'
| 'contentTooLarge'
| 'uriTooLong'
| 'unsupportedMediaType'
| 'rangeNotSatisfiable'
| 'exceptionFailed'
| 'imATeapot'
| 'misdirectedRequest'
| 'unprocessableContent'
| 'locked'
| 'failedDependency'
| 'tooEarly'
| 'upgradeRequired'
| 'preconditionRequired'
| 'rateLimitExceeded'
| 'requestHeaderFieldsTooLarge'
| 'unavailableForLegalReasons'
| 'internalError'
| 'serviceUnavailable';
| 'notImplemented'
| 'badGateway'
| 'serviceUnavailable'
| 'gatewayTimeout'
| 'httpVersionNotSupported'
| 'variantAlsoNegotiates'
| 'insufficientStorage'
| 'loopDetected'
| 'notExtended'
| 'networkAuthenticationRequired';

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

Expand All @@ -32,13 +71,64 @@ const responseFunction =
<T>(payload: T) => ({ type: status, payload });

export const KeyedResponse = {
continue: responseFunction('continue'),
switchingProtocols: responseFunction('switchingProtocols'),
processing: responseFunction('processing'),
ok: responseFunction('ok'),
created: responseFunction('created'),
accepted: responseFunction('accepted'),
nonAuthoritativeInformation: responseFunction('nonAuthoritativeInformation'),
noContent: responseFunction('noContent'),
resetContent: responseFunction('resetContent'),
partialContent: responseFunction('partialContent'),
multiStatus: responseFunction('multiStatus'),
alreadyReported: responseFunction('alreadyReported'),
imUsed: responseFunction('imUsed'),
multipleChoices: responseFunction('multipleChoices'),
movedPermanently: responseFunction('movedPermanently'),
found: responseFunction('found'),
seeOther: responseFunction('seeOther'),
notModified: responseFunction('notModified'),
temporaryRedirect: responseFunction('temporaryRedirect'),
permanentRedirect: responseFunction('permanentRedirect'),
invalidRequest: responseFunction('invalidRequest'),
unauthenticated: responseFunction('unauthenticated'),
paymentRequired: responseFunction('paymentRequired'),
permissionDenied: responseFunction('permissionDenied'),
notFound: responseFunction('notFound'),
methodNotAllowed: responseFunction('methodNotAllowed'),
notAcceptable: responseFunction('notAcceptable'),
proxyAuthenticationRequired: responseFunction('proxyAuthenticationRequired'),
requestTimeout: responseFunction('requestTimeout'),
conflict: responseFunction('conflict'),
gone: responseFunction('gone'),
lengthRequired: responseFunction('lengthRequired'),
preconditionFailed: responseFunction('preconditionFailed'),
contentTooLarge: responseFunction('contentTooLarge'),
uriTooLong: responseFunction('uriTooLong'),
unsupportedMediaType: responseFunction('unsupportedMediaType'),
rangeNotSatisfiable: responseFunction('rangeNotSatisfiable'),
exceptionFailed: responseFunction('exceptionFailed'),
imATeapot: responseFunction('imATeapot'),
misdirectedRequest: responseFunction('misdirectedRequest'),
unprocessableContent: responseFunction('unprocessableContent'),
locked: responseFunction('locked'),
failedDependency: responseFunction('failedDependency'),
tooEarly: responseFunction('tooEarly'),
upgradeRequired: responseFunction('upgradeRequired'),
preconditionRequired: responseFunction('preconditionRequired'),
rateLimitExceeded: responseFunction('rateLimitExceeded'),
requestHeaderFieldsTooLarge: responseFunction('requestHeaderFieldsTooLarge'),
unavailableForLegalReasons: responseFunction('unavailableForLegalReasons'),
internalError: responseFunction('internalError'),
notImplemented: responseFunction('notImplemented'),
badGateway: responseFunction('badGateway'),
serviceUnavailable: responseFunction('serviceUnavailable'),
gatewayTimeout: responseFunction('gatewayTimeout'),
httpVersionNotSupported: responseFunction('httpVersionNotSupported'),
variantAlsoNegotiates: responseFunction('variantAlsoNegotiates'),
insufficientStorage: responseFunction('insufficientStorage'),
loopDetected: responseFunction('loopDetected'),
notExtended: responseFunction('notExtended'),
networkAuthenticationRequired: responseFunction('networkAuthenticationRequired'),
};
Loading