Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add TS types * use types from swagger shchema * remove typings field * Fix typo in pkgjson
- Loading branch information
1 parent
42671e6
commit f142c94
Showing
4 changed files
with
122 additions
and
1 deletion.
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 |
---|---|---|
@@ -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; |
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
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,10 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es6", | ||
"module": "commonjs", | ||
"noEmit": true, | ||
"strict": true, | ||
"noImplicitAny": true | ||
}, | ||
"files": ["./types.test.ts"] | ||
} |
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,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(); | ||
}); |