Skip to content

Commit

Permalink
Merge pull request #1 from rozzilla/feat/ts/add-types-and-ts-document…
Browse files Browse the repository at this point in the history
…ation

feat(ts): Add types and ts documentation
  • Loading branch information
mcollina authored Apr 22, 2024
2 parents 1838e99 + ae95287 commit 3b148f5
Show file tree
Hide file tree
Showing 4 changed files with 6,438 additions and 4 deletions.
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

0 comments on commit 3b148f5

Please sign in to comment.