fastify-extract-definitions

Retrieves TypeScript definitions from Fastify router schema


Keywords
fastify, extract, definitions, ts, typescript, typings, JSON Schema, router, routes
License
MIT
Install
npm install fastify-extract-definitions@3.1.0

Documentation

fastify-extract-definitions

NPM version Tests

Automatically extracts TypeScript definitions from Fastify router schema and generates server and client typings. Based on json-schema-to-typescript package.

For fastify@>=4.2 use fastify-extract-definitions@1.3.0.

For fastify@3 use fastify-extract-definitions@0.0.4.

Pre requirements

  • node.js: >=10.*
  • fastify: >=3.*

Installation

Install it with yarn:

yarn add fastify-extract-definitions

Or with npm:

npm install fastify-extract-definitions

Usage

Add it to your project with register, pass it some options, and you are done!

⚠️ Note! Use this plugin for development only, make sure it is disabled in production.

import Fastify from 'fastify';
import extractor from 'fastify-extract-definitions';

const fastify = Fastify();

fastify.register(extractor, {
  enabled: process.env.NODE_ENV === 'development',
  outputs: {
    './types.d.ts': {
      target: 'serverTypes',
    },
  },
});

See example for more details.

Options

key type default description
enabled boolean false Is the plugin enabled?
ignoreHead boolean false Ignore HEAD endpoints
outputs object {} Outputs config
compilerOptions object See json-schema-to-typescript default options json-schema-to-typescript options

Outputs config

[outputPath: string]: {
  target: 'serverTypes' | 'clientTypes';
};

License

MIT