Skip to content

Commit

Permalink
feat: add FastifyPluginAsyncJsonSchemaToTs and FastifyPluginCallbackJ…
Browse files Browse the repository at this point in the history
…sonSchemaToTs plugin types (#15)

* feat: add jstt plugin type

* me, no I would never copy and paste code

* elongate plugin type abbreviations

* remove all abbreviations

* remove abbreviations from readme

* remove abbreviations from index doc comments
  • Loading branch information
Liam-Tait authored Aug 4, 2022
1 parent 403e137 commit ec6b9c0
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 3 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,36 @@
# fastify-type-provider-json-schema-to-ts

A Type Provider for json-schema-to-ts

## Plugin definition

> **Note**
> When using plugin types, withTypeProvider is not required in order to register the plugin
```ts
const plugin: FastifyPluginAsyncJsonSchemaToTs = async function (
fastify,
_opts
) {
fastify.get(
"/",
{
schema: {
body: {
type: "object",
properties: {
x: { type: "string" },
y: { type: "number" },
z: { type: "boolean" },
},
required: ["x", "y", "z"],
} as const,
},
},
(req) => {
/// The `x`, `y`, and `z` types are automatically inferred
const { x, y, z } = req.body;
}
);
};
```
46 changes: 43 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,47 @@
import { FastifyTypeProvider } from 'fastify'
import {
FastifyTypeProvider,
FastifyPluginOptions,
RawServerBase,
RawServerDefault,
FastifyPluginCallback,
FastifyPluginAsync,
} from "fastify";

import { JSONSchema7, FromSchema } from 'json-schema-to-ts'
import { JSONSchema7, FromSchema } from "json-schema-to-ts";

export interface JsonSchemaToTsProvider extends FastifyTypeProvider {
output: this['input'] extends JSONSchema7 ? FromSchema<this['input']> : never
output: this["input"] extends JSONSchema7 ? FromSchema<this["input"]> : never;
}

/**
* FastifyPluginCallback with JSON Schema to Typescript automatic type inference
*
* @example
* ```typescript
* import { FastifyPluginCallbackJsonSchemaToTs } from "@fastify/type-provider-json-schema-to-ts"
*
* const plugin: FastifyPluginCallbackJsonSchemaToTs = (fastify, options, done) => {
* done()
* }
* ```
*/
export type FastifyPluginCallbackJsonSchemaToTs<
Options extends FastifyPluginOptions = Record<never, never>,
Server extends RawServerBase = RawServerDefault
> = FastifyPluginCallback<Options, Server, JsonSchemaToTsProvider>;

/**
* FastifyPluginAsync with JSON Schema to Typescript automatic type inference
*
* @example
* ```typescript
* import { FastifyPluginAsyncJsonSchemaToTs } fromg "@fastify/type-provider-json-schema-to-ts"
*
* const plugin: FastifyPluginAsyncJsonSchemaToTs = async (fastify, options) => {
* }
* ```
*/
export type FastifyPluginAsyncJsonSchemaToTs<
Options extends FastifyPluginOptions = Record<never, never>,
Server extends RawServerBase = RawServerDefault
> = FastifyPluginAsync<Options, Server, JsonSchemaToTsProvider>;
108 changes: 108 additions & 0 deletions types/plugin.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import {
FastifyPluginAsyncJsonSchemaToTs,
FastifyPluginCallbackJsonSchemaToTs,
} from "../index";
import { expectType } from "tsd";
import Fastify, { FastifyPluginAsync, FastifyPluginCallback } from "fastify";

import { Http2Server } from "http2";

// Ensure the defaults of FastifyPluginAsyncJsonSchemaToTs are the same as FastifyPluginAsync
export const pluginAsyncDefaults: FastifyPluginAsync = async (
fastify,
options
) => {
const pluginAsyncJSONSchemaToTsDefaults: FastifyPluginAsyncJsonSchemaToTs =
async (fastifyWithJSONSchemaToTs, optionsJSONSchemaToTs) => {
expectType<typeof fastifyWithJSONSchemaToTs["server"]>(fastify.server);
expectType<typeof optionsJSONSchemaToTs>(options);
};
fastify.register(pluginAsyncJSONSchemaToTsDefaults);
};

// Ensure the defaults of FastifyPluginCallbackJsonSchemaToTs are the same as FastifyPluginCallback
export const pluginCallbackDefaults: FastifyPluginCallback = async (
fastify,
options,
done
) => {
const pluginCallbackJSONSchemaToTsDefaults: FastifyPluginCallbackJsonSchemaToTs =
async (
fastifyWithJSONSchemaToTs,
optionsJSONSchemaToTs,
doneJSONSchemaToTs
) => {
expectType<typeof fastifyWithJSONSchemaToTs["server"]>(fastify.server);
expectType<typeof optionsJSONSchemaToTs>(options);
};

fastify.register(pluginCallbackJSONSchemaToTsDefaults);
};

const asyncPlugin: FastifyPluginAsyncJsonSchemaToTs<
{ optionA: string },
Http2Server
> = async (fastify, options) => {
expectType<Http2Server>(fastify.server);

expectType<string>(options.optionA);

fastify.get(
"/",
{
schema: {
body: {
type: "object",
properties: {
x: { type: "string" },
y: { type: "number" },
z: { type: "boolean" },
},
required: ["x", "y", "z"],
} as const,
},
},
(req) => {
expectType<boolean>(req.body.z);
expectType<number>(req.body.y);
expectType<string>(req.body.x);
}
);
};

const callbackPlugin: FastifyPluginCallbackJsonSchemaToTs<
{ optionA: string },
Http2Server
> = (fastify, options, done) => {
expectType<Http2Server>(fastify.server);

expectType<string>(options.optionA);

fastify.get(
"/",
{
schema: {
body: {
type: "object",
properties: {
x: { type: "string" },
y: { type: "number" },
z: { type: "boolean" },
},
required: ["x", "y", "z"],
} as const,
},
},
(req) => {
expectType<boolean>(req.body.z);
expectType<number>(req.body.y);
expectType<string>(req.body.x);
}
);
done();
};

const fastify = Fastify();

fastify.register(asyncPlugin);
fastify.register(callbackPlugin);

0 comments on commit ec6b9c0

Please sign in to comment.