diff --git a/README.md b/README.md index 23284a2..872e1a3 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![NPM version](https://img.shields.io/npm/v/@fastify/swagger-ui.svg?style=flat)](https://www.npmjs.com/package/@fastify/swagger-ui) [![CI](https://github.com/fastify/fastify-swagger-ui/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/fastify/fastify-swagger-ui/actions/workflows/ci.yml) -[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/) +[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard) A Fastify plugin for serving [Swagger UI](https://swagger.io/tools/swagger-ui/). diff --git a/eslint.config.js b/eslint.config.js new file mode 100644 index 0000000..d4b6032 --- /dev/null +++ b/eslint.config.js @@ -0,0 +1,9 @@ +'use strict' + +module.exports = require('neostandard')({ + ignores: [ + ...require('neostandard').resolveIgnoresFromGitignore(), + 'dist' + ], + ts: true +}) diff --git a/package.json b/package.json index f3a2148..cb246ec 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,8 @@ "type": "commonjs", "types": "types/index.d.ts", "scripts": { - "lint": "standard", - "lint:fix": "npm run lint -- --fix", + "lint": "eslint", + "lint:fix": "eslint --fix", "prepare": "node scripts/prepare-swagger-ui", "prepublishOnly": "npm run prepare", "test": "npm run prepare && npm run unit && npm run typescript", @@ -54,8 +54,8 @@ "c8": "^10.1.2", "fastify": "^5.0.0", "fs-extra": "^11.2.0", + "neostandard": "^0.11.9", "qs": "^6.12.1", - "standard": "^17.1.0", "swagger-ui-dist": "5.18.2", "tsd": "^0.31.0" }, @@ -66,11 +66,6 @@ "rfdc": "^1.3.1", "yaml": "^2.4.1" }, - "standard": { - "ignore": [ - "dist" - ] - }, "tsd": { "directory": "types" }, diff --git a/types/http2-types.test-d.ts b/types/http2-types.test-d.ts index 64d8766..bb15752 100644 --- a/types/http2-types.test-d.ts +++ b/types/http2-types.test-d.ts @@ -1,26 +1,26 @@ -import fastify from 'fastify'; -import fastifySwagger from '@fastify/swagger'; -import fastifySwaggerUi from '..'; +import fastify from 'fastify' +import fastifySwagger from '@fastify/swagger' +import fastifySwaggerUi from '..' const app = fastify({ http2: true -}); +}) -app.register(fastifySwagger); -app.register(fastifySwaggerUi); -app.register(fastifySwaggerUi, {}); +app.register(fastifySwagger) +app.register(fastifySwaggerUi) +app.register(fastifySwaggerUi, {}) app.register(fastifySwaggerUi, { routePrefix: '/documentation', -}); +}) app.put('/some-route/:id', { schema: { description: 'put me some data', tags: ['user', 'code'], summary: 'qwerty', - security: [{ apiKey: []}] + security: [{ apiKey: [] }] } -}, (req, reply) => {}); +}, (req, reply) => {}) app.get('/public/route', { schema: { @@ -28,9 +28,9 @@ app.get('/public/route', { summary: 'qwerty', security: [] } -}, (_req, _reply) => {}); +}, (_req, _reply) => {}) app .register(fastifySwaggerUi, { routePrefix: '/documentation', - }) \ No newline at end of file + }) diff --git a/types/imports.test-d.ts b/types/imports.test-d.ts index 717cfdf..e4ee73d 100644 --- a/types/imports.test-d.ts +++ b/types/imports.test-d.ts @@ -1,14 +1,14 @@ -import fastify from "fastify"; +import fastify from 'fastify' -import swaggerDefault, { fastifySwaggerUi, FastifySwaggerUiOptions } from ".."; -import * as fastifySwaggerStar from ".."; +import swaggerDefault, { fastifySwaggerUi, FastifySwaggerUiOptions } from '..' +import * as fastifySwaggerStar from '..' -const app = fastify(); +const app = fastify() const fastifySwaggerOptions: FastifySwaggerUiOptions = { - routePrefix: "/documentation", -}; + routePrefix: '/documentation', +} -app.register(swaggerDefault, fastifySwaggerOptions); -app.register(fastifySwaggerUi, fastifySwaggerOptions); -app.register(fastifySwaggerStar.default, fastifySwaggerOptions); -app.register(fastifySwaggerStar.fastifySwaggerUi, fastifySwaggerOptions); +app.register(swaggerDefault, fastifySwaggerOptions) +app.register(fastifySwaggerUi, fastifySwaggerOptions) +app.register(fastifySwaggerStar.default, fastifySwaggerOptions) +app.register(fastifySwaggerStar.fastifySwaggerUi, fastifySwaggerOptions) diff --git a/types/index.d.ts b/types/index.d.ts index dd47358..fb394c4 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -7,7 +7,7 @@ import { onRequestHookHandler, preHandlerHookHandler, RegisterOptions, -} from 'fastify'; +} from 'fastify' /** * Swagger-UI Vendor Extensions @@ -35,7 +35,7 @@ declare module 'fastify' { } } -type FastifySwaggerUi = FastifyPluginCallback; +type FastifySwaggerUi = FastifyPluginCallback declare namespace fastifySwaggerUi { export interface FastifySwaggerUiOptions extends Omit { @@ -101,7 +101,7 @@ declare namespace fastifySwaggerUi { target?: '_blank' | '_parent' | '_self' | '_top'; } - type SupportedHTTPMethods = "get" | "put" | "post" | "delete" | "options" | "head" | "patch" | "trace"; + type SupportedHTTPMethods = 'get' | 'put' | 'post' | 'delete' | 'options' | 'head' | 'patch' | 'trace' interface PluginsOptions { /** @@ -114,14 +114,14 @@ declare namespace fastifySwaggerUi { pluginLoadType?: PluginLoadType; } - type PluginLoadType = 'legacy' | 'chain'; + type PluginLoadType = 'legacy' | 'chain' type SorterLike = - | "alpha" - | "method" + | 'alpha' + | 'method' | { (name1: string, name2: string): number; - }; + } interface Request { [prop: string]: any; @@ -247,7 +247,7 @@ declare namespace fastifySwaggerUi { * (The user can always switch the rendering for a given model by clicking the * 'Model' and 'Example Value' links.) */ - defaultModelRendering?: "example" | "model" | undefined; + defaultModelRendering?: 'example' | 'model' | undefined; /** * Controls the display of the request duration (in milliseconds) for "Try it out" requests. @@ -259,7 +259,7 @@ declare namespace fastifySwaggerUi { * It can be 'list' (expands only the tags), 'full' (expands the tags and operations) * or 'none' (expands nothing). */ - docExpansion?: "list" | "full" | "none" | undefined; + docExpansion?: 'list' | 'full' | 'none' | undefined; /** * If set, enables filtering. @@ -332,7 +332,7 @@ declare namespace fastifySwaggerUi { /** * Highlight.js syntax coloring theme to use. (Only these 6 styles are available.) */ - theme?: "agate" | "arta" | "monokai" | "nord" | "obsidian" | "tomorrow-night" | undefined; + theme?: 'agate' | 'arta' | 'monokai' | 'nord' | 'obsidian' | 'tomorrow-night' | undefined; } | undefined; /** * Controls whether the "Try it out" section should be enabled by default. @@ -506,6 +506,6 @@ declare namespace fastifySwaggerUi { export const fastifySwaggerUi: FastifySwaggerUi export { fastifySwaggerUi as default } } -declare function fastifySwaggerUi(...params: Parameters): ReturnType +declare function fastifySwaggerUi (...params: Parameters): ReturnType -export = fastifySwaggerUi; +export = fastifySwaggerUi diff --git a/types/swagger-ui-vendor-extensions.test-d.ts b/types/swagger-ui-vendor-extensions.test-d.ts index 5e92b2d..b0724d3 100644 --- a/types/swagger-ui-vendor-extensions.test-d.ts +++ b/types/swagger-ui-vendor-extensions.test-d.ts @@ -1,10 +1,11 @@ import { OpenAPIV2, OpenAPIV3 } from 'openapi-types' +import { expectAssignable } from 'tsd' -const xTokenNameOpenAPIv3: OpenAPIV3.Document = { +expectAssignable({ openapi: '3.0.0', info: { - 'version': '1.0.0', - 'title': 'Test OpenApiv3 specification', + version: '1.0.0', + title: 'Test OpenApiv3 specification', }, components: { securitySchemes: { @@ -13,7 +14,7 @@ const xTokenNameOpenAPIv3: OpenAPIV3.Document = { 'x-tokenName': 'id_token', flows: { implicit: { - authorizationUrl: `http.../login/oauth/authorize`, + authorizationUrl: 'http.../login/oauth/authorize', scopes: {}, }, }, @@ -21,9 +22,9 @@ const xTokenNameOpenAPIv3: OpenAPIV3.Document = { } }, paths: {} -} +}) -const xTokenNameOpenAPIv2: OpenAPIV2.Document = { +expectAssignable({ swagger: '2.0.0', info: { title: 'Test OpenApiv2 specification', @@ -31,77 +32,77 @@ const xTokenNameOpenAPIv2: OpenAPIV2.Document = { }, securityDefinitions: { OAuth2AccessCodeFlow: { - type: "oauth2", - flow: "accessCode", - authorizationUrl: "https://example.com/oauth/authorize", - tokenUrl: "https://example.com/oauth/token", - "x-tokenName": 'id_token', + type: 'oauth2', + flow: 'accessCode', + authorizationUrl: 'https://example.com/oauth/authorize', + tokenUrl: 'https://example.com/oauth/token', + 'x-tokenName': 'id_token', scopes: { } }, OAuth2ApplicationFlow: { - type: "oauth2", - flow: "application", - tokenUrl: "https://example.com/oauth/token", - "x-tokenName": 'id_token', + type: 'oauth2', + flow: 'application', + tokenUrl: 'https://example.com/oauth/token', + 'x-tokenName': 'id_token', scopes: { } }, OAuth2ImplicitFlow: { - type: "oauth2", - flow: "implicit", - authorizationUrl: "https://example.com/oauth/authorize", - "x-tokenName": 'id_token', + type: 'oauth2', + flow: 'implicit', + authorizationUrl: 'https://example.com/oauth/authorize', + 'x-tokenName': 'id_token', scopes: { } }, OAuth2PasswordFlow: { - type: "oauth2", - flow: "password", - tokenUrl: "https://example.com/oauth/token", - "x-tokenName": 'id_token', + type: 'oauth2', + flow: 'password', + tokenUrl: 'https://example.com/oauth/token', + 'x-tokenName': 'id_token', scopes: { } }, }, paths: {} -} +}) -const xExampleOpenAPIv2: OpenAPIV2.Document = { +expectAssignable({ swagger: '2.0.0', info: { title: 'Test OpenApiv2 specification', version: '2.0.0' }, paths: { - "/users/{userId}": { - 'get': { - summary: "Gets a user by ID.", + '/users/{userId}': { + get: { + summary: 'Gets a user by ID.', responses: { }, parameters: [ { - in: "path", - name: "userId", - type: "integer", + in: 'path', + name: 'userId', + type: 'integer', required: true, - description: "Numeric ID of the user to get.", + description: 'Numeric ID of the user to get.', 'x-example': 'BADC0FFEE' }, { - in: "query", - name: "offset", - type: "integer", - description: "The number of items to skip before starting to collect the result set.", + in: 'query', + name: 'offset', + type: 'integer', + description: 'The number of items to skip before starting to collect the result set.', 'x-example': 1337 }, { - in: "header", - name: "X-Request-ID", - type: "string", + in: 'header', + name: 'X-Request-ID', + type: 'string', required: true, 'x-example': 'wget' }, { - in: "formData", - name: "name", - type: "string", + in: 'formData', + name: 'name', + type: 'string', description: "A person's name.", 'x-example': 'John Doe' } @@ -109,4 +110,4 @@ const xExampleOpenAPIv2: OpenAPIV2.Document = { } } } -} \ No newline at end of file +}) diff --git a/types/types.test-d.ts b/types/types.test-d.ts index e111889..9d5053a 100644 --- a/types/types.test-d.ts +++ b/types/types.test-d.ts @@ -1,13 +1,13 @@ -import fastify, { FastifyReply, FastifyRequest } from 'fastify'; -import { expectType } from 'tsd'; +import fastify, { FastifyReply, FastifyRequest } from 'fastify' +import { expectType } from 'tsd' import fastifySwaggerUi, { FastifySwaggerUiOptions, FastifySwaggerInitOAuthOptions, FastifySwaggerUiConfigOptions, FastifySwaggerUiHooksOptions, -} from ".." +} from '..' -const app = fastify(); +const app = fastify() const uiConfig: FastifySwaggerUiConfigOptions = { deepLinking: true, defaultModelsExpandDepth: -1, @@ -16,49 +16,49 @@ const uiConfig: FastifySwaggerUiConfigOptions = { layout: 'BaseLayout', supportedSubmitMethods: ['get'], persistAuthorization: false, -}; +} const initOAuth: FastifySwaggerInitOAuthOptions = { scopes: ['openid', 'profile', 'email', 'offline_access'], -}; +} const uiHooks: FastifySwaggerUiHooksOptions = { - onRequest: (request, reply, done) => {done()}, - preHandler: (request, reply, done) => {done()}, + onRequest: (request, reply, done) => { done() }, + preHandler: (request, reply, done) => { done() }, } -app.register(fastifySwaggerUi); -app.register(fastifySwaggerUi, {}); +app.register(fastifySwaggerUi) +app.register(fastifySwaggerUi, {}) app.register(fastifySwaggerUi, { routePrefix: '/documentation', indexPrefix: '/custom-prefix' -}); +}) const fastifySwaggerOptions: FastifySwaggerUiOptions = { routePrefix: '/documentation', indexPrefix: '/custom-prefix' } -app.register(fastifySwaggerUi, fastifySwaggerOptions); +app.register(fastifySwaggerUi, fastifySwaggerOptions) app.get('/deprecated', { schema: { deprecated: true, hide: true } -}, (req, reply) => {}); +}, (req, reply) => {}) app.put('/some-route/:id', { - schema: { - description: 'put me some data', - tags: ['user', 'code'], - summary: 'qwerty', - consumes: ['application/json', 'multipart/form-data'], - security: [{ apiKey: []}], - operationId: 'opeId', - externalDocs: { - url: 'https://swagger.io', - description: 'Find more info here' - }, - } - }, (req, reply) => {}); + schema: { + description: 'put me some data', + tags: ['user', 'code'], + summary: 'qwerty', + consumes: ['application/json', 'multipart/form-data'], + security: [{ apiKey: [] }], + operationId: 'opeId', + externalDocs: { + url: 'https://swagger.io', + description: 'Find more info here' + }, + } +}, (req, reply) => {}) app.put('/image.png', { schema: { @@ -73,22 +73,23 @@ app.put('/image.png', { } } } -}, async (req, reply) => { reply +}, async (req, reply) => { + reply .type('image/png') - .send(Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAAAgSURBVBhXY/iPCkB8BgYkEiSIBICiCCEoB0SBwf///wGHRzXLSklJLQAAAABJRU5ErkJggg==', 'base64')); -}); + .send(Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAAAgSURBVBhXY/iPCkB8BgYkEiSIBICiCCEoB0SBwf///wGHRzXLSklJLQAAAABJRU5ErkJggg==', 'base64')) +}) app.get('/public/route', { - schema: { - description: 'returns 200 OK', - summary: 'qwerty', - security: [], - response: { 200: {} } - }, - links: { - 200: {'some-route': { operationId: 'opeId'}} - } - }, (req, reply) => {}); + schema: { + description: 'returns 200 OK', + summary: 'qwerty', + security: [], + response: { 200: {} } + }, + links: { + 200: { 'some-route': { operationId: 'opeId' } } + } +}, (req, reply) => {}) app .register(fastifySwaggerUi, { @@ -101,8 +102,9 @@ app initOAuth }) .ready((err) => { - app.swagger(); - }); + expectType(err) + app.swagger() + }) app.register(fastifySwaggerUi, { uiConfig @@ -124,7 +126,7 @@ app.register(fastifySwaggerUi, { }) app.register(fastifySwaggerUi, { staticCSP: true, - transformStaticCSP(header) { + transformStaticCSP (header) { return header } }) @@ -157,13 +159,13 @@ app.register(fastifySwaggerUi, { app.register(fastifySwaggerUi, { theme: { favicon: [ - { - filename: 'favicon-16x16.png', - rel: 'icon', - sizes: '16x16', - type: 'image/png', - content: Buffer.from('somethingsomething') - } + { + filename: 'favicon-16x16.png', + rel: 'icon', + sizes: '16x16', + type: 'image/png', + content: Buffer.from('somethingsomething') + } ], }, })