Skip to content

Commit

Permalink
feat(typescript): add typescript definitions
Browse files Browse the repository at this point in the history
The default export is the fastifySensible plugin, whereas the http
errors are also being exported but namely.
  • Loading branch information
hspedro committed Jun 4, 2019
1 parent b8985e8 commit c91e4ae
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
61 changes: 61 additions & 0 deletions index.d.ts
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;
54 changes: 54 additions & 0 deletions lib/httpError.d.ts
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;
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
},
"homepage": "https://github.com/fastify/fastify-sensible#readme",
"devDependencies": {
"@types/http-errors": "^1.6.1",
"@types/proxy-addr": "^2.0.0",
"@types/vary": "^1.1.0",
"fastify": "^2.0.0",
"standard": "^12.0.1",
"tap": "^12.1.1"
Expand Down

0 comments on commit c91e4ae

Please sign in to comment.