Skip to content

Commit 52b5347

Browse files
authored
refactor: update types (#72)
* refactor: update fastify * chore: bump version of fastify * fix: plugin test callback * test: use strict mode * chore: use single asterisk * chore: update requirements * chore: add tsconfig.json
1 parent fb30e21 commit 52b5347

File tree

4 files changed

+38
-15
lines changed

4 files changed

+38
-15
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
# @fastify/type-provider-json-schema-to-ts
22

3-
A Type Provider for json-schema-to-ts
3+
A Type Provider for [json-schema-to-ts](https://github.com/ThomasAribart/json-schema-to-ts)
44

55
## Install
66

77
```
88
npm i @fastify/type-provider-json-schema-to-ts
99
```
1010

11+
## TypeScript requirements
12+
13+
It is required to use `TypeScript@4.3` or above with
14+
[`strict`](https://www.typescriptlang.org/tsconfig#strict)
15+
mode enabled and
16+
[`noStrictGenericChecks`](https://www.typescriptlang.org/tsconfig#noStrictGenericChecks)
17+
disabled. You may take the below configuration (`tsconfig.json`)
18+
as an example.
19+
20+
```json
21+
{
22+
"compilerOptions": {
23+
"strict": true,
24+
"noStrictGenericChecks": false
25+
}
26+
}
27+
```
28+
1129
## Plugin definition
1230

1331
> **Note**

index.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import {
2-
FastifyTypeProvider,
2+
FastifyBaseLogger,
3+
FastifyPluginAsync,
4+
FastifyPluginCallback,
35
FastifyPluginOptions,
6+
FastifyTypeProvider,
47
RawServerBase,
5-
RawServerDefault,
6-
FastifyPluginCallback,
7-
FastifyPluginAsync
8+
RawServerDefault
89
} from 'fastify'
910

10-
import { JSONSchema, FromSchema, FromSchemaOptions, FromSchemaDefaultOptions } from 'json-schema-to-ts'
11+
import { FromSchema, FromSchemaDefaultOptions, FromSchemaOptions, JSONSchema } from 'json-schema-to-ts'
1112

1213
export interface JsonSchemaToTsProvider<Options extends FromSchemaOptions = FromSchemaDefaultOptions> extends FastifyTypeProvider {
1314
output: this['input'] extends JSONSchema ? FromSchema<this['input'], Options> : unknown;
@@ -27,8 +28,9 @@ export interface JsonSchemaToTsProvider<Options extends FromSchemaOptions = From
2728
*/
2829
export type FastifyPluginCallbackJsonSchemaToTs<
2930
Options extends FastifyPluginOptions = Record<never, never>,
30-
Server extends RawServerBase = RawServerDefault
31-
> = FastifyPluginCallback<Options, Server, JsonSchemaToTsProvider>;
31+
Server extends RawServerBase = RawServerDefault,
32+
Logger extends FastifyBaseLogger = FastifyBaseLogger,
33+
> = FastifyPluginCallback<Options, Server, JsonSchemaToTsProvider, Logger>;
3234

3335
/**
3436
* FastifyPluginAsync with JSON Schema to Typescript automatic type inference
@@ -43,5 +45,6 @@ export type FastifyPluginCallbackJsonSchemaToTs<
4345
*/
4446
export type FastifyPluginAsyncJsonSchemaToTs<
4547
Options extends FastifyPluginOptions = Record<never, never>,
46-
Server extends RawServerBase = RawServerDefault
47-
> = FastifyPluginAsync<Options, Server, JsonSchemaToTsProvider>;
48+
Server extends RawServerBase = RawServerDefault,
49+
Logger extends FastifyBaseLogger = FastifyBaseLogger,
50+
> = FastifyPluginAsync<Options, Server, JsonSchemaToTsProvider, Logger>;

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"test": "npm run build && npm run typescript",
1010
"lint": "eslint \"**/*.ts\"",
1111
"lint:fix": "eslint \"**/*.ts\" --fix",
12-
"typescript": "tsd",
12+
"typescript": "tsc --strict --noEmit types/*.ts && tsd",
1313
"prepublishOnly": "npm run build"
1414
},
1515
"repository": {
@@ -27,7 +27,7 @@
2727
},
2828
"homepage": "https://github.com/fastify/fastify-type-provider-json-schema-to-ts#readme",
2929
"peerDependencies": {
30-
"fastify": "^4.0.0",
30+
"fastify": "^4.25.2",
3131
"json-schema-to-ts": "^3.0.0"
3232
},
3333
"tsd": {

types/plugin.test-d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import Fastify, { FastifyPluginAsync, FastifyPluginCallback } from 'fastify'
2+
import { expectType } from 'tsd'
13
import {
24
FastifyPluginAsyncJsonSchemaToTs,
35
FastifyPluginCallbackJsonSchemaToTs
46
} from '../index'
5-
import { expectType } from 'tsd'
6-
import Fastify, { FastifyPluginAsync, FastifyPluginCallback } from 'fastify'
77

88
import { Http2Server } from 'http2'
99

@@ -21,7 +21,7 @@ export const pluginAsyncDefaults: FastifyPluginAsync = async (
2121
}
2222

2323
// Ensure the defaults of FastifyPluginCallbackJsonSchemaToTs are the same as FastifyPluginCallback
24-
export const pluginCallbackDefaults: FastifyPluginCallback = async (
24+
export const pluginCallbackDefaults: FastifyPluginCallback = (
2525
fastify,
2626
options,
2727
done
@@ -34,9 +34,11 @@ export const pluginCallbackDefaults: FastifyPluginCallback = async (
3434
) => {
3535
expectType<typeof fastifyWithJSONSchemaToTs['server']>(fastify.server)
3636
expectType<typeof optionsJSONSchemaToTs>(options)
37+
doneJSONSchemaToTs()
3738
}
3839

3940
fastify.register(pluginCallbackJSONSchemaToTsDefaults)
41+
done()
4042
}
4143

4244
const asyncPlugin: FastifyPluginAsyncJsonSchemaToTs<

0 commit comments

Comments
 (0)