Skip to content

Commit

Permalink
refactor: update types (#72)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
climba03003 authored Jan 10, 2024
1 parent fb30e21 commit 52b5347
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
# @fastify/type-provider-json-schema-to-ts

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

## Install

```
npm i @fastify/type-provider-json-schema-to-ts
```

## TypeScript requirements

It is required to use `[email protected]` or above with
[`strict`](https://www.typescriptlang.org/tsconfig#strict)
mode enabled and
[`noStrictGenericChecks`](https://www.typescriptlang.org/tsconfig#noStrictGenericChecks)
disabled. You may take the below configuration (`tsconfig.json`)
as an example.

```json
{
"compilerOptions": {
"strict": true,
"noStrictGenericChecks": false
}
}
```

## Plugin definition

> **Note**
Expand Down
21 changes: 12 additions & 9 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {
FastifyTypeProvider,
FastifyBaseLogger,
FastifyPluginAsync,
FastifyPluginCallback,
FastifyPluginOptions,
FastifyTypeProvider,
RawServerBase,
RawServerDefault,
FastifyPluginCallback,
FastifyPluginAsync
RawServerDefault
} from 'fastify'

import { JSONSchema, FromSchema, FromSchemaOptions, FromSchemaDefaultOptions } from 'json-schema-to-ts'
import { FromSchema, FromSchemaDefaultOptions, FromSchemaOptions, JSONSchema } from 'json-schema-to-ts'

export interface JsonSchemaToTsProvider<Options extends FromSchemaOptions = FromSchemaDefaultOptions> extends FastifyTypeProvider {
output: this['input'] extends JSONSchema ? FromSchema<this['input'], Options> : unknown;
Expand All @@ -27,8 +28,9 @@ export interface JsonSchemaToTsProvider<Options extends FromSchemaOptions = From
*/
export type FastifyPluginCallbackJsonSchemaToTs<
Options extends FastifyPluginOptions = Record<never, never>,
Server extends RawServerBase = RawServerDefault
> = FastifyPluginCallback<Options, Server, JsonSchemaToTsProvider>;
Server extends RawServerBase = RawServerDefault,
Logger extends FastifyBaseLogger = FastifyBaseLogger,
> = FastifyPluginCallback<Options, Server, JsonSchemaToTsProvider, Logger>;

/**
* FastifyPluginAsync with JSON Schema to Typescript automatic type inference
Expand All @@ -43,5 +45,6 @@ export type FastifyPluginCallbackJsonSchemaToTs<
*/
export type FastifyPluginAsyncJsonSchemaToTs<
Options extends FastifyPluginOptions = Record<never, never>,
Server extends RawServerBase = RawServerDefault
> = FastifyPluginAsync<Options, Server, JsonSchemaToTsProvider>;
Server extends RawServerBase = RawServerDefault,
Logger extends FastifyBaseLogger = FastifyBaseLogger,
> = FastifyPluginAsync<Options, Server, JsonSchemaToTsProvider, Logger>;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"test": "npm run build && npm run typescript",
"lint": "eslint \"**/*.ts\"",
"lint:fix": "eslint \"**/*.ts\" --fix",
"typescript": "tsd",
"typescript": "tsc --strict --noEmit types/*.ts && tsd",
"prepublishOnly": "npm run build"
},
"repository": {
Expand All @@ -27,7 +27,7 @@
},
"homepage": "https://github.com/fastify/fastify-type-provider-json-schema-to-ts#readme",
"peerDependencies": {
"fastify": "^4.0.0",
"fastify": "^4.25.2",
"json-schema-to-ts": "^3.0.0"
},
"tsd": {
Expand Down
8 changes: 5 additions & 3 deletions types/plugin.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Fastify, { FastifyPluginAsync, FastifyPluginCallback } from 'fastify'
import { expectType } from 'tsd'
import {
FastifyPluginAsyncJsonSchemaToTs,
FastifyPluginCallbackJsonSchemaToTs
} from '../index'
import { expectType } from 'tsd'
import Fastify, { FastifyPluginAsync, FastifyPluginCallback } from 'fastify'

import { Http2Server } from 'http2'

Expand All @@ -21,7 +21,7 @@ export const pluginAsyncDefaults: FastifyPluginAsync = async (
}

// Ensure the defaults of FastifyPluginCallbackJsonSchemaToTs are the same as FastifyPluginCallback
export const pluginCallbackDefaults: FastifyPluginCallback = async (
export const pluginCallbackDefaults: FastifyPluginCallback = (
fastify,
options,
done
Expand All @@ -34,9 +34,11 @@ export const pluginCallbackDefaults: FastifyPluginCallback = async (
) => {
expectType<typeof fastifyWithJSONSchemaToTs['server']>(fastify.server)
expectType<typeof optionsJSONSchemaToTs>(options)
doneJSONSchemaToTs()
}

fastify.register(pluginCallbackJSONSchemaToTsDefaults)
done()
}

const asyncPlugin: FastifyPluginAsyncJsonSchemaToTs<
Expand Down

0 comments on commit 52b5347

Please sign in to comment.