Skip to content
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

feat(ts): Add types and ts documentation #1

Merged
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
15 changes: 15 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {
type FastifyInstance,
type FastifyBaseLogger,
type FastifyReply,
type FastifyRequest,
} from "fastify";

declare namespace fastifyasyncforge {
export function app<T extends FastifyInstance>(): T;
export function request<T extends FastifyRequest>(): T;
export function reply<T extends FastifyReply>(): T;
export function logger<T extends FastifyBaseLogger>(): T;
}

export = fastifyasyncforge;
45 changes: 45 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { expectAssignable, expectError, expectType } from "tsd";
import { app, logger, reply, request } from ".";
import fastify, {
type FastifyInstance,
type FastifyBaseLogger,
type FastifyRequest,
type FastifyReply,
type RawServerDefault,
type RouteGenericInterface,
} from "fastify";

const fastifyInstance = fastify();

// app
expectAssignable<FastifyInstance>(fastifyInstance);
expectAssignable<FastifyInstance>(app());
expectAssignable<FastifyInstance>(app<FastifyInstance<RawServerDefault>>());
expectError<FastifyInstance>(app<string>());
expectError<FastifyInstance>({});

// request
expectType<FastifyRequest>(request());
expectType<FastifyRequest>(request<FastifyRequest<RouteGenericInterface>>());
expectError<FastifyRequest>(request<boolean>());
expectType<unknown>(request().body);
expectType<boolean>(request().is404);
expectError<FastifyRequest>({});

// reply
expectType<FastifyReply>(reply());
expectType<FastifyReply>(reply<FastifyReply<RawServerDefault>>());
expectType<number>(reply().statusCode);
expectType<boolean>(reply().sent);
expectError<FastifyReply>(reply<number>());
expectError<FastifyReply>({});

// logger
expectType<FastifyBaseLogger>(fastifyInstance.log);
expectType<FastifyBaseLogger>(logger());
expectType<FastifyBaseLogger>(logger<FastifyBaseLogger>());
expectType<void>(logger().debug({ msg: "hey" }));
expectType<void>(logger().info({ msg: "oh!" }));
expectType<void>(logger().warn({ msg: "let's go!!!" }));
expectError<FastifyBaseLogger>(logger<object>());
expectError<FastifyBaseLogger>({});
Loading
Loading