-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add FastifyPluginAsyncJsonSchemaToTs and FastifyPluginCallbackJ…
…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
Showing
3 changed files
with
185 additions
and
3 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 |
---|---|---|
@@ -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; | ||
} | ||
); | ||
}; | ||
``` |
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 |
---|---|---|
@@ -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>; |
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,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); |