-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(typescript): add typescript definitions
The default export is the fastifySensible plugin, whereas the http errors are also being exported but namely.
- Loading branch information
Showing
3 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import * as fastify from "fastify"; | ||
import * as http from "http"; | ||
import * as proxyAddr from "proxy-addr"; | ||
import { HttpErrors } from "./lib/httpError"; | ||
|
||
declare module "fastify" { | ||
namespace SensibleTypes { | ||
type ToType<T> = [Error, T]; | ||
} | ||
|
||
interface Assert { | ||
(condition: boolean, code: number, message: string): string; | ||
ok(condition: boolean, code: number, message: string): string; | ||
equal<T, U>(a: T, b: U, code: number, message: string): string; | ||
notEqual<T, U>(a: T, b: U, code: number, message: string): string; | ||
strictEqual<T, U>(a: T, b: U, code: number, message: string): string; | ||
notStrictEqual<T, U>(a: T, b: U, code: number, message: string): string; | ||
deepEqual<T, U>(a: T, b: U, code: number, message: string): string; | ||
notDeepEqual<T, U>(a: T, b: U, code: number, message: string): string; | ||
} | ||
|
||
interface To { | ||
<T>(to: Promise<T>): Promise<SensibleTypes.ToType<T>>; | ||
} | ||
|
||
interface FastifyInstance extends HttpErrors { | ||
assert: Assert; | ||
to: To; | ||
} | ||
|
||
interface VaryReply { | ||
vary(field: string | string[]): void; | ||
append(header: string, field: string | string[]): string; | ||
} | ||
|
||
interface FastifyReply<HttpResponse> { | ||
vary(field: string | string[]): void; | ||
} | ||
|
||
interface FastifyRequest<HttpRequest> { | ||
forwarded(): string[]; | ||
proxyaddr( | ||
trust: | ||
| proxyAddr.Address | ||
| proxyAddr.Address[] | ||
| ((addr: string, i: number) => boolean) | ||
): string; | ||
is(types: Array<string>): string | false | null; | ||
is(...types: Array<string>): string | false | null; | ||
} | ||
} | ||
|
||
declare const fastifySensible: fastify.Plugin< | ||
http.Server, | ||
http.IncomingMessage, | ||
http.ServerResponse, | ||
{} | ||
>; | ||
|
||
export { HttpErrorType } from "./lib/httpError"; | ||
export default fastifySensible; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import createHttpError from 'http-errors'; | ||
|
||
export type HttpErrorType = { | ||
badRequest(message?: string): createHttpError.HttpError; | ||
unauthorized(message?: string): createHttpError.HttpError; | ||
paymentRequired(message?: string): createHttpError.HttpError; | ||
forbidden(message?: string): createHttpError.HttpError; | ||
notFound(message?: string): createHttpError.HttpError; | ||
methodNotAllowed(message?: string): createHttpError.HttpError; | ||
notAcceptable(message?: string): createHttpError.HttpError; | ||
proxyAuthenticationRequired(message?: string): createHttpError.HttpError; | ||
requestTimeout(message?: string): createHttpError.HttpError; | ||
conflict(message?: string): createHttpError.HttpError; | ||
gone(message?: string): createHttpError.HttpError; | ||
lengthRequired(message?: string): createHttpError.HttpError; | ||
preconditionFailed(message?: string): createHttpError.HttpError; | ||
payloadTooLarge(message?: string): createHttpError.HttpError; | ||
uriTooLong(message?: string): createHttpError.HttpError; | ||
unsupportedMediaType(message?: string): createHttpError.HttpError; | ||
rangeNotSatisfiable(message?: string): createHttpError.HttpError; | ||
expectationFailed(message?: string): createHttpError.HttpError; | ||
imateapot(message?: string): createHttpError.HttpError; | ||
misdirectedRequest(message?: string): createHttpError.HttpError; | ||
unprocessableEntity(message?: string): createHttpError.HttpError; | ||
locked(message?: string): createHttpError.HttpError; | ||
failedDependency(message?: string): createHttpError.HttpError; | ||
unorderedCollection(message?: string): createHttpError.HttpError; | ||
upgradeRequired(message?: string): createHttpError.HttpError; | ||
preconditionRequired(message?: string): createHttpError.HttpError; | ||
tooManyRequests(message?: string): createHttpError.HttpError; | ||
requestHeaderFieldsTooLarge(message?: string): createHttpError.HttpError; | ||
unavailableForLegalReasons(message?: string): createHttpError.HttpError; | ||
internalServerError(message?: string): createHttpError.HttpError; | ||
notImplemented(message?: string): createHttpError.HttpError; | ||
badGateway(message?: string): createHttpError.HttpError; | ||
serviceUnavailable(message?: string): createHttpError.HttpError; | ||
gatewayTimeout(message?: string): createHttpError.HttpError; | ||
httpVersionNotSupported(message?: string): createHttpError.HttpError; | ||
variantAlsoNegotiates(message?: string): createHttpError.HttpError; | ||
insufficientStorage(message?: string): createHttpError.HttpError; | ||
loopDetected(message?: string): createHttpError.HttpError; | ||
bandwidthLimitExceeded(message?: string): createHttpError.HttpError; | ||
notExtended(message?: string): createHttpError.HttpError; | ||
networkAuthenticationRequired( | ||
message?: string, | ||
): createHttpError.HttpError; | ||
|
||
getHttpError: (code: number, message?: string) => string; | ||
HttpError: createHttpError.HttpErrorConstructor; | ||
}; | ||
|
||
export interface HttpErrors { | ||
httpError: HttpErrorType; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters