Skip to content

Commit

Permalink
Add TypeScript types (#124)
Browse files Browse the repository at this point in the history
* add TS types

* use types from swagger shchema

* remove typings field

* Fix typo in pkgjson
  • Loading branch information
ricardo-devis-agullo authored and cemremengu committed Dec 7, 2018
1 parent 42671e6 commit f142c94
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 1 deletion.
61 changes: 61 additions & 0 deletions index.d.ts
@@ -0,0 +1,61 @@
import * as http from 'http';
import * as fastify from 'fastify';
import * as SwaggerSchema from 'swagger-schema-official';

declare namespace fastifySwagger {
interface FastifySwaggerOptions {
mode?: 'static' | 'dynamic';
}

interface FastifyDynamicSwaggerOptions extends FastifySwaggerOptions {
mode?: 'dynamic';
/**
* Overwrite the swagger url end-point
* @default /documentation
*/
routePrefix?: string;
/**
* To expose the documentation api
* @default false
*/
exposeRoute?: boolean;
swagger?: Partial<SwaggerSchema.Spec>;
}

interface StaticPathSpec {
path: string;
postProcessor?: (spec: SwaggerSchema.Spec) => SwaggerSchema.Spec;
}

interface StaticDocumentSpec {
document: string;
}

interface FastifyStaticSwaggerOptions extends FastifySwaggerOptions {
mode: 'static';
specification: StaticPathSpec | StaticDocumentSpec;
}
}

declare module 'fastify' {
interface FastifyInstance<
HttpServer = http.Server,
HttpRequest = http.IncomingMessage,
HttpResponse = http.ServerResponse
> {
swagger: (
opts?: {
yaml?: boolean;
}
) => SwaggerSchema.Spec;
}
}

declare let fastifySwagger: fastify.Plugin<
http.Server,
http.IncomingMessage,
http.ServerResponse,
fastifySwagger.FastifyStaticSwaggerOptions | fastifySwagger.FastifyDynamicSwaggerOptions
>;

export = fastifySwagger;
5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -6,7 +6,8 @@
"scripts": {
"prepare": "node prepare-swagger-ui",
"test": "standard && tap test/*.js",
"prepublishOnly": "npm run prepare"
"prepublishOnly": "npm run prepare",
"typescript": "tsc --project ./tsconfig.json"
},
"repository": {
"type": "git",
Expand All @@ -29,11 +30,13 @@
},
"homepage": "https://github.com/fastify/fastify-swagger#readme",
"devDependencies": {
"@types/swagger-schema-official": "^2.0.13",
"fastify": "^1.9.0",
"fs-extra": "^7.0.0",
"pre-commit": "^1.2.2",
"standard": "^12.0.1",
"swagger-parser": "^6.0.0",
"typescript": "^3.1.6",
"swagger-ui-dist": "3.20.1",
"tap": "^12.0.0"
},
Expand Down
10 changes: 10 additions & 0 deletions tsconfig.json
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"noEmit": true,
"strict": true,
"noImplicitAny": true
},
"files": ["./types.test.ts"]
}
47 changes: 47 additions & 0 deletions types.test.ts
@@ -0,0 +1,47 @@
import fastify = require('fastify');
import fastifySwagger = require('../fastify-swagger');

const app = fastify();

app.register(fastifySwagger);
app.register(fastifySwagger, {});
app.register(fastifySwagger, {
mode: 'static',
specification: {
document: 'path'
}
});

app
.register(fastifySwagger, {
routePrefix: '/documentation',
swagger: {
info: {
title: 'Test swagger',
description: 'testing the fastify swagger api',
version: '0.1.0'
},
externalDocs: {
url: 'https://swagger.io',
description: 'Find more info here'
},
host: 'localhost',
schemes: ['http'],
consumes: ['application/json'],
produces: ['application/json'],
tags: [
{ name: 'user', description: 'User related end-points' },
{ name: 'code', description: 'Code related end-points' }
],
securityDefinitions: {
apiKey: {
type: 'apiKey',
name: 'apiKey',
in: 'header'
}
}
}
})
.ready(err => {
app.swagger();
});

0 comments on commit f142c94

Please sign in to comment.