|
1 | 1 | import * as t from 'io-ts'; |
| 2 | +import { KeyToHttpStatus } from './statusCode'; |
2 | 3 |
|
3 | 4 | export type HttpResponse = { |
4 | 5 | [K: number | string]: t.Mixed; |
5 | 6 | }; |
6 | 7 |
|
| 8 | +const responseFunction = |
| 9 | + <S extends keyof KeyToHttpStatus>(status: S) => |
| 10 | + <T>(payload: T) => ({ type: KeyToHttpStatus[status] as KeyToHttpStatus[S], payload }); |
| 11 | + |
| 12 | +export const Response = { |
| 13 | + continue: responseFunction('continue'), |
| 14 | + switchingProtocols: responseFunction('switchingProtocols'), |
| 15 | + processing: responseFunction('processing'), |
| 16 | + ok: responseFunction('ok'), |
| 17 | + created: responseFunction('created'), |
| 18 | + accepted: responseFunction('accepted'), |
| 19 | + nonAuthoritativeInformation: responseFunction('nonAuthoritativeInformation'), |
| 20 | + noContent: responseFunction('noContent'), |
| 21 | + resetContent: responseFunction('resetContent'), |
| 22 | + partialContent: responseFunction('partialContent'), |
| 23 | + multiStatus: responseFunction('multiStatus'), |
| 24 | + alreadyReported: responseFunction('alreadyReported'), |
| 25 | + imUsed: responseFunction('imUsed'), |
| 26 | + multipleChoices: responseFunction('multipleChoices'), |
| 27 | + movedPermanently: responseFunction('movedPermanently'), |
| 28 | + found: responseFunction('found'), |
| 29 | + seeOther: responseFunction('seeOther'), |
| 30 | + notModified: responseFunction('notModified'), |
| 31 | + temporaryRedirect: responseFunction('temporaryRedirect'), |
| 32 | + permanentRedirect: responseFunction('permanentRedirect'), |
| 33 | + invalidRequest: responseFunction('invalidRequest'), |
| 34 | + unauthenticated: responseFunction('unauthenticated'), |
| 35 | + paymentRequired: responseFunction('paymentRequired'), |
| 36 | + permissionDenied: responseFunction('permissionDenied'), |
| 37 | + notFound: responseFunction('notFound'), |
| 38 | + methodNotAllowed: responseFunction('methodNotAllowed'), |
| 39 | + notAcceptable: responseFunction('notAcceptable'), |
| 40 | + proxyAuthenticationRequired: responseFunction('proxyAuthenticationRequired'), |
| 41 | + requestTimeout: responseFunction('requestTimeout'), |
| 42 | + conflict: responseFunction('conflict'), |
| 43 | + gone: responseFunction('gone'), |
| 44 | + lengthRequired: responseFunction('lengthRequired'), |
| 45 | + preconditionFailed: responseFunction('preconditionFailed'), |
| 46 | + contentTooLarge: responseFunction('contentTooLarge'), |
| 47 | + uriTooLong: responseFunction('uriTooLong'), |
| 48 | + unsupportedMediaType: responseFunction('unsupportedMediaType'), |
| 49 | + rangeNotSatisfiable: responseFunction('rangeNotSatisfiable'), |
| 50 | + exceptionFailed: responseFunction('exceptionFailed'), |
| 51 | + imATeapot: responseFunction('imATeapot'), |
| 52 | + misdirectedRequest: responseFunction('misdirectedRequest'), |
| 53 | + unprocessableContent: responseFunction('unprocessableContent'), |
| 54 | + locked: responseFunction('locked'), |
| 55 | + failedDependency: responseFunction('failedDependency'), |
| 56 | + tooEarly: responseFunction('tooEarly'), |
| 57 | + upgradeRequired: responseFunction('upgradeRequired'), |
| 58 | + preconditionRequired: responseFunction('preconditionRequired'), |
| 59 | + rateLimitExceeded: responseFunction('rateLimitExceeded'), |
| 60 | + requestHeaderFieldsTooLarge: responseFunction('requestHeaderFieldsTooLarge'), |
| 61 | + unavailableForLegalReasons: responseFunction('unavailableForLegalReasons'), |
| 62 | + internalError: responseFunction('internalError'), |
| 63 | + notImplemented: responseFunction('notImplemented'), |
| 64 | + badGateway: responseFunction('badGateway'), |
| 65 | + serviceUnavailable: responseFunction('serviceUnavailable'), |
| 66 | + gatewayTimeout: responseFunction('gatewayTimeout'), |
| 67 | + httpVersionNotSupported: responseFunction('httpVersionNotSupported'), |
| 68 | + variantAlsoNegotiates: responseFunction('variantAlsoNegotiates'), |
| 69 | + insufficientStorage: responseFunction('insufficientStorage'), |
| 70 | + loopDetected: responseFunction('loopDetected'), |
| 71 | + notExtended: responseFunction('notExtended'), |
| 72 | + networkAuthenticationRequired: responseFunction('networkAuthenticationRequired'), |
| 73 | +}; |
| 74 | + |
7 | 75 | export type ResponseTypeForStatus< |
8 | 76 | Response extends HttpResponse, |
9 | 77 | S extends keyof Response, |
|
0 commit comments