diff --git a/packages/specs/openapi-utils/.npmignore b/packages/specs/openapi-utils/.npmignore new file mode 100644 index 00000000000..0887e21d43e --- /dev/null +++ b/packages/specs/openapi-utils/.npmignore @@ -0,0 +1,7 @@ +test +coverage +tsconfig.json +tsconfig.*.json +__mock__ +*.spec.js +*.tsbuildinfo diff --git a/packages/specs/openapi-utils/package.json b/packages/specs/openapi-utils/package.json new file mode 100644 index 00000000000..0b92b3f12a0 --- /dev/null +++ b/packages/specs/openapi-utils/package.json @@ -0,0 +1,52 @@ +{ + "name": "@tsed/openapi-utils", + "description": "OpenAPI utils to build @tsed/swagger package or @tsed/scalar", + "type": "module", + "version": "8.2.0", + "source": "./src/index.ts", + "main": "./lib/esm/index.js", + "module": "./lib/esm/index.js", + "typings": "./lib/types/index.d.ts", + "exports": { + ".": { + "@tsed/source": "./src/index.ts", + "types": "./lib/types/index.d.ts", + "import": "./lib/esm/index.js", + "default": "./lib/esm/index.js" + } + }, + "scripts": { + "clean": "rm -rf lib", + "build": "yarn clean && yarn barrels && yarn build:ts", + "barrels": "barrels", + "test": "vitest run", + "build:ts": "tsc --build tsconfig.json", + "test:ci": "vitest run --coverage.thresholds.autoUpdate=true" + }, + "dependencies": { + "@tsed/openspec": "workspace:*", + "micromatch": "4.0.8", + "tslib": "2.7.0" + }, + "devDependencies": { + "@tsed/barrels": "workspace:*", + "@tsed/core": "workspace:*", + "@tsed/di": "workspace:*", + "@tsed/json-mapper": "workspace:*", + "@tsed/openspec": "workspace:*", + "@tsed/platform-http": "workspace:*", + "@tsed/schema": "workspace:*", + "@tsed/typescript": "workspace:*", + "eslint": "9.12.0", + "typescript": "5.4.5", + "vitest": "2.1.2" + }, + "peerDependencies": { + "@tsed/platform-http": "8.2.0" + }, + "peerDependenciesMeta": { + "@tsed/platform-http": { + "optional": false + } + } +} diff --git a/packages/specs/openapi-utils/readme.md b/packages/specs/openapi-utils/readme.md new file mode 100644 index 00000000000..595516eb1ca --- /dev/null +++ b/packages/specs/openapi-utils/readme.md @@ -0,0 +1,257 @@ +

+ Ts.ED logo +

+ +
+

Swagger

+ +[![Build & Release](https://github.com/tsedio/tsed/workflows/Build%20&%20Release/badge.svg)](https://github.com/tsedio/tsed/actions?query=workflow%3A%22Build+%26+Release%22) +[![PR Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/tsedio/tsed/blob/master/CONTRIBUTING.md) +[![npm version](https://badge.fury.io/js/%40tsed%2Fcommon.svg)](https://badge.fury.io/js/%40tsed%2Fcommon) +[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release) +[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier) +[![github](https://img.shields.io/static/v1?label=Github%20sponsor&message=%E2%9D%A4&logo=GitHub&color=%23fe8e86)](https://github.com/sponsors/romakita) +[![opencollective](https://img.shields.io/static/v1?label=OpenCollective%20sponsor&message=%E2%9D%A4&logo=OpenCollective&color=%23fe8e86)](https://opencollective.com/tsed) + +
+ +
+ Website +   •   + Getting started +   •   + Slack +   •   + Twitter +
+ +
+ +A package of Ts.ED framework. See website: https://tsed.io/tutorials/swagger + +## Installation + +Before using the Swagger, we have to install the [swagger-ui-express](https://www.npmjs.com/package/swagger-ui-express) module. + +```bash +npm install --save @tsed/swagger +``` + +Then add the following configuration in your Server: + +```typescript +import {Configuration} from "@tsed/di"; +import "@tsed/swagger"; // import swagger Ts.ED module +import {resolve} from "path"; + +@Configuration({ + swagger: [ + { + path: "/v2/docs", + specVersion: "2.0" + }, + { + path: "/v3/docs", + specVersion: "3.0.1" + } + ] +}) +export class Server {} +``` + +> The path option for swagger will be used to expose the documentation (ex: http://localhost:8000/api-docs). + +Normally, Swagger-ui is ready. You can start your server and check if it work fine. + +> Note: Ts.ED will print the swagger url in the console. + +### Swagger options + +Some options is available to configure Swagger-ui, Ts.ED and the default spec information. + +| Key | Example | Description | +| -------------------- | ------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------- | +| path | `/api-doc` | The url subpath to access to the documentation. | +| specVersion | `2.0`, `3.0.1` | The spec version. | +| doc | `hidden-doc` | The documentation key used by `@Docs` decorator to create several swagger documentations. | +| cssPath | `${rootDir}/spec/style.css` | The path to the CSS file. | +| jsPath | `${rootDir}/spec/main.js` | The path to the JS file. | +| showExplorer | `true` | Display the search field in the navbar. | +| spec | `{swagger: "2.0"}` | The default information spec. | +| specPath | `${rootDir}/spec/swagger.base.json` | Load the base spec documentation from the specified path. | +| outFile | `${rootDir}/spec/swagger.json` | Write the `swagger.json` spec documentation on the specified path. | +| hidden | `true` | Hide the documentation in the dropdown explorer list. | +| options | Swagger-UI options | SwaggerUI options. See (https://github.com/swagger-api/swagger-ui/blob/HEAD/docs/usage/configuration.md) | +| operationIdFormatter | `(name: string, propertyKey: string, path: string) => string` | A function to generate the operationId. | +| operationIdPattern | `%c_%m` | A pattern to generate the operationId. Format of operationId field (%c: class name, %m: method name). | + +### Multi documentations + +It also possible to create several swagger documentations with `doc` option: + +```typescript +import {Configuration} from "@tsed/di"; +import "@tsed/swagger"; // import swagger Ts.ED module + +@Configuration({ + swagger: [ + { + path: "/api-docs-v1", + doc: "api-v1" + }, + { + path: "/api-docs-v2", + doc: "api-v2" + } + ] +}) +export class Server {} +``` + +Then use `@Docs` decorators on your controllers to specify where the controllers should be displayed. + +```typescript +import {Controller} from "@tsed/di"; +import {Docs} from "@tsed/swagger"; + +@Controller("/calendars") +@Docs("api-v2") // display this controllers only for api-docs-v2 +export class CalendarCtrlV2 {} +// OR +@Controller("/calendars") +@Docs("api-v2", "api-v1") // display this controllers for api-docs-v2 and api-docs-v1 +export class CalendarCtrl {} +``` + +## Examples + +#### Model documentation + +One of the feature of Ts.ED is the model definition to serialize or deserialize a +JSON Object (see [converters section](https://tsed.devdocs/components/converters.html)). + +This model can used on a method controller along with [@BodyParams](https://tsed.devapi/common/filters/decorators/BodyParams.html) or other decorators. + +```typescript +import {JsonProperty, Title, Description, Example} from "@tsed/schema"; + +export class CalendarModel { + @Title("iD") + @Description("Description of calendar model id") + @Example("Example value") + @JsonProperty() + public id: string; + + @JsonProperty() + public name: string; +} +``` + +#### Endpoint documentation + +```typescript +import {Controller} from "@tsed/di"; +import {BodyParams, QueryParams} from "@tsed/platform-params"; +import {Get, Post, Returns, ReturnsArray, Description, Summary, Deprecated, Security} from "@tsed/schema"; +import {CalendarModel} from "../models/CalendarModel.js"; + +@Controller("/calendars") +export class Calendar { + @Get("/:id") + @Summary("Summary of this route") + @Description("Description of this route") + @Returns(CalendarModel) + @Returns(404, {description: "Not found"}) + async getCalendar(@QueryParams("id") id: string): Promise { + //... + } + + @Get("/v0/:id") + @Deprecated() + @Description("Deprecated route, use /rest/calendars/:id instead of.") + @Returns(CalendarModel) + @Returns(404, {description: "Not found"}) + getCalendarDeprecated(@QueryParams("id") id: string): Promise { + //... + } + + @Get("/") + @Description("Description of this route") + @ReturnsArray(CalendarModel) + getCalendars(): Promise { + // ... + } + + @Post("/") + @Security("calendar_auth", "write:calendar", "read:calendar") + @Returns(CalendarModel) + async createCalendar(@BodyParams() body: any): Promise { + //... + } +} +``` + +::: warninig +To update the swagger.json you need to reload the server before. +::: + +## Import Javascript + +It possible to import a Javascript in the Swagger-ui documentation. This script let you customize the swagger-ui instance. + +```typescript +import {Configuration} from "@tsed/di"; +import "@tsed/swagger"; // import swagger Ts.ED module + +@Configuration({ + swagger: [ + { + path: "/api-docs", + jsPath: "/spec/main.js" + } + ] +}) +export class Server {} +``` + +In your JavaScript file, you can handle Swagger-ui configuration and the instance: + +```javascript +console.log(SwaggerUIBuilder.config); //Swagger-ui config + +document.addEventListener("swagger.init", (evt) => { + console.log(SwaggerUIBuilder.ui); //Swagger-ui instance +}); +``` + +## Documentation + +See our documentation https://tsed.dev#/api/index + +## Contributors + +Please read [contributing guidelines here](https://tsed.devcontributing.html). + + + +## Backers + +Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/tsed#backer)] + + + +## Sponsors + +Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/tsed#sponsor)] + +## License + +The MIT License (MIT) + +Copyright (c) 2016 - 2022 Romain Lenzotti + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/packages/specs/swagger/src/decorators/docs.ts b/packages/specs/openapi-utils/src/decorators/docs.ts similarity index 100% rename from packages/specs/swagger/src/decorators/docs.ts rename to packages/specs/openapi-utils/src/decorators/docs.ts diff --git a/packages/specs/openapi-utils/src/index.ts b/packages/specs/openapi-utils/src/index.ts new file mode 100644 index 00000000000..b6add771590 --- /dev/null +++ b/packages/specs/openapi-utils/src/index.ts @@ -0,0 +1,14 @@ +/** + * @file Automatically generated by @tsed/barrels. + */ +export * from "./decorators/docs.js"; +export * from "./interfaces/OpenApiSettings.js"; +export * from "./middlewares/cssMiddleware.js"; +export * from "./middlewares/jsMiddleware.js"; +export * from "./middlewares/openApiMiddleware.js"; +export * from "./middlewares/redirectMiddleware.js"; +export * from "./services/OpenAPIBaseModule.js"; +export * from "./services/OpenAPIService.js"; +export * from "./utils/includeRoute.js"; +export * from "./utils/matchPath.js"; +export * from "./utils/readSpec.js"; diff --git a/packages/specs/openapi-utils/src/interfaces/OpenApiSettings.ts b/packages/specs/openapi-utils/src/interfaces/OpenApiSettings.ts new file mode 100644 index 00000000000..84abdbcc0cc --- /dev/null +++ b/packages/specs/openapi-utils/src/interfaces/OpenApiSettings.ts @@ -0,0 +1,98 @@ +import {OpenSpec2, OpenSpec3, OpenSpecVersions, OS2Versions, OS3Versions} from "@tsed/openspec"; + +export interface SwaggerUIOptions { + configUrl?: string; + url?: string; + urls?: {url: string; name: string; primaryName?: string}[]; + layout?: string; + validatorUrl?: string; + oauth?: any; + authorize?: any; + + [key: string]: any; +} + +export interface OpenApiSettingsBase { + /** + * The url subpath to access to the documentation. + */ + path: string; + /** + * Specify the spec version you want to generate. + */ + specVersion?: OpenSpecVersions; + /** + * Swagger file name. By default swagger.json + */ + fileName?: string; + /** + * The documentation key used by `@Docs` decorator to create several swagger documentations. + */ + doc?: string; + /** + * The path to the CSS file. + */ + cssPath?: string; + /** + * The path to the JS file. + */ + jsPath?: string; + /** + * The path to the ejs file to create html page. + */ + viewPath?: string | false; + /** + * Display the search field in the navbar. + */ + showExplorer?: boolean; + /** + * Load the base spec documentation from the specified path. + */ + specPath?: string; + /** + * Write the `swagger.json` spec documentation on the specified path. + */ + outFile?: string; + /** + * Display the documentation in the browser. + */ + hidden?: boolean; + /** + * Sort paths by alphabetical order + */ + sortPaths?: boolean; + /** + * A function to generate the operationId. + */ + operationIdFormatter?: (name: string, propertyKey: string, path: string) => string; + /** + * A pattern to generate the operationId. Format of operationId field (%c: class name, %m: method name). + */ + operationIdPattern?: string; + /** + * + */ + disableSpec?: boolean; + /** + * Include only controllers whose paths match the pattern list provided. + */ + pathPatterns?: string[]; +} + +export interface Swagger2Settings extends OpenApiSettingsBase { + specVersion?: OS2Versions; + /** + * OpenSpec 2 + */ + spec?: Partial; +} + +export interface OpenAPI3Settings extends OpenApiSettingsBase { + specVersion?: OS3Versions; + /** + * OpenSpec 3 + */ + spec?: Partial; +} + +export type OpenApiSettings = Swagger2Settings | OpenAPI3Settings; diff --git a/packages/specs/swagger/src/middlewares/cssMiddleware.spec.ts b/packages/specs/openapi-utils/src/middlewares/cssMiddleware.spec.ts similarity index 80% rename from packages/specs/swagger/src/middlewares/cssMiddleware.spec.ts rename to packages/specs/openapi-utils/src/middlewares/cssMiddleware.spec.ts index b87b191b1c9..dbdcd1433d3 100644 --- a/packages/specs/swagger/src/middlewares/cssMiddleware.spec.ts +++ b/packages/specs/openapi-utils/src/middlewares/cssMiddleware.spec.ts @@ -1,3 +1,4 @@ +import {runInContext} from "@tsed/di"; import {PlatformTest} from "@tsed/platform-http/testing"; import Fs from "fs"; @@ -9,10 +10,10 @@ describe("cssMiddleware", () => { beforeEach(() => { vi.spyOn(Fs, "readFileSync").mockReturnValue(".css{}"); }); - it("should create a middleware", () => { + it("should create a middleware", async () => { const ctx = PlatformTest.createRequestContext(); - cssMiddleware("/path")(ctx); + await runInContext(ctx, () => cssMiddleware("/path")()); expect(ctx.response.raw.headers).toEqual({ "content-type": "text/css", diff --git a/packages/specs/swagger/src/middlewares/cssMiddleware.ts b/packages/specs/openapi-utils/src/middlewares/cssMiddleware.ts similarity index 75% rename from packages/specs/swagger/src/middlewares/cssMiddleware.ts rename to packages/specs/openapi-utils/src/middlewares/cssMiddleware.ts index ae9b41816de..4964e6b6ce6 100644 --- a/packages/specs/swagger/src/middlewares/cssMiddleware.ts +++ b/packages/specs/openapi-utils/src/middlewares/cssMiddleware.ts @@ -1,14 +1,17 @@ -import {PlatformContext} from "@tsed/platform-http"; +import {context} from "@tsed/di"; import Fs from "fs"; import {resolve} from "path"; /** - * @ignore + * Expose a css file. * @param path */ export function cssMiddleware(path: string) { - return (ctx: PlatformContext) => { + return () => { + const ctx = context(); + const content = Fs.readFileSync(resolve(path), {encoding: "utf8"}); + ctx.response .setHeaders({ "Content-Type": "text/css" diff --git a/packages/specs/swagger/src/middlewares/jsMiddleware.spec.ts b/packages/specs/openapi-utils/src/middlewares/jsMiddleware.spec.ts similarity index 80% rename from packages/specs/swagger/src/middlewares/jsMiddleware.spec.ts rename to packages/specs/openapi-utils/src/middlewares/jsMiddleware.spec.ts index 235842fa396..371dca4b63e 100644 --- a/packages/specs/swagger/src/middlewares/jsMiddleware.spec.ts +++ b/packages/specs/openapi-utils/src/middlewares/jsMiddleware.spec.ts @@ -1,3 +1,4 @@ +import {runInContext} from "@tsed/di"; import {PlatformTest} from "@tsed/platform-http/testing"; import Fs from "fs"; @@ -9,10 +10,10 @@ describe("jsMiddleware", () => { beforeEach(() => { vi.spyOn(Fs, "readFileSync").mockReturnValue("var test=1"); }); - it("should create a middleware", () => { + it("should create a middleware", async () => { const ctx = PlatformTest.createRequestContext(); - jsMiddleware("/path")(ctx); + await runInContext(ctx, () => jsMiddleware("/path")()); expect(ctx.response.raw.headers).toEqual({ "content-type": "application/javascript", diff --git a/packages/specs/swagger/src/middlewares/jsMiddleware.ts b/packages/specs/openapi-utils/src/middlewares/jsMiddleware.ts similarity index 74% rename from packages/specs/swagger/src/middlewares/jsMiddleware.ts rename to packages/specs/openapi-utils/src/middlewares/jsMiddleware.ts index e79b0593e24..913d70d0841 100644 --- a/packages/specs/swagger/src/middlewares/jsMiddleware.ts +++ b/packages/specs/openapi-utils/src/middlewares/jsMiddleware.ts @@ -1,13 +1,15 @@ -import {PlatformContext} from "@tsed/platform-http"; +import {context} from "@tsed/di"; import Fs from "fs"; import {resolve} from "path"; /** - * @ignore + * Expose a js file. * @param path */ export function jsMiddleware(path: string) { - return (ctx: PlatformContext) => { + return () => { + const ctx = context(); + const content = Fs.readFileSync(resolve(path), {encoding: "utf8"}); ctx.response.setHeaders({"Content-Type": "application/javascript"}).status(200).body(content); }; diff --git a/packages/specs/openapi-utils/src/middlewares/openApiMiddleware.spec.ts b/packages/specs/openapi-utils/src/middlewares/openApiMiddleware.spec.ts new file mode 100644 index 00000000000..a7c5d092d19 --- /dev/null +++ b/packages/specs/openapi-utils/src/middlewares/openApiMiddleware.spec.ts @@ -0,0 +1,31 @@ +import {runInContext} from "@tsed/di"; +import {PlatformTest} from "@tsed/platform-http/testing"; +import Fs from "fs"; + +import {openApiMiddleware} from "./openApiMiddleware.js"; + +describe("openApiMiddleware", () => { + beforeEach(PlatformTest.create); + afterEach(PlatformTest.reset); + beforeEach(() => { + vi.spyOn(Fs, "readFileSync").mockReturnValue("var test=1"); + }); + it("should create a middleware", async () => { + const ctx = PlatformTest.createRequestContext(); + const conf = {specVersion: "3.0.1"}; + + await runInContext(ctx, () => openApiMiddleware(conf as never)()); + + expect(ctx.response.raw.headers).toEqual({ + "x-request-id": "id" + }); + expect(ctx.response.raw.statusCode).toBe(200); + expect(ctx.response.raw.data).toEqual({ + info: { + title: "Api documentation", + version: "1.0.0" + }, + openapi: "3.0.1" + }); + }); +}); diff --git a/packages/specs/openapi-utils/src/middlewares/openApiMiddleware.ts b/packages/specs/openapi-utils/src/middlewares/openApiMiddleware.ts new file mode 100644 index 00000000000..fe5b94fcd43 --- /dev/null +++ b/packages/specs/openapi-utils/src/middlewares/openApiMiddleware.ts @@ -0,0 +1,16 @@ +import {context, injector} from "@tsed/di"; + +import type {OpenApiSettings} from "../interfaces/OpenApiSettings.js"; +import {OpenAPIService} from "../services/OpenAPIService.js"; + +/** + * Return a middleware to expose the OpenAPI spec. + */ +export function openApiMiddleware(conf: OpenApiSettings) { + return async () => { + const ctx = context(); + const spec = await injector().get(OpenAPIService).getOpenAPISpec(conf); + + ctx.response.status(200).body(spec); + }; +} diff --git a/packages/specs/swagger/src/middlewares/redirectMiddleware.spec.ts b/packages/specs/openapi-utils/src/middlewares/redirectMiddleware.spec.ts similarity index 74% rename from packages/specs/swagger/src/middlewares/redirectMiddleware.spec.ts rename to packages/specs/openapi-utils/src/middlewares/redirectMiddleware.spec.ts index d24fb5edeff..1299c529b00 100644 --- a/packages/specs/swagger/src/middlewares/redirectMiddleware.spec.ts +++ b/packages/specs/openapi-utils/src/middlewares/redirectMiddleware.spec.ts @@ -1,3 +1,4 @@ +import {runInContext} from "@tsed/di"; import {PlatformTest} from "@tsed/platform-http/testing"; import {redirectMiddleware} from "./redirectMiddleware.js"; @@ -5,7 +6,7 @@ import {redirectMiddleware} from "./redirectMiddleware.js"; describe("redirectMiddleware and redirect", () => { beforeEach(PlatformTest.create); afterEach(PlatformTest.reset); - it("should create a middleware", () => { + it("should create a middleware", async () => { const ctx = PlatformTest.createRequestContext(); vi.spyOn(ctx.response, "redirect").mockReturnValue(undefined as never); @@ -13,18 +14,18 @@ describe("redirectMiddleware and redirect", () => { ctx.request.raw.originalUrl = "/path"; ctx.request.raw.method = "GET"; - redirectMiddleware("/path")(ctx); + await runInContext(ctx, () => redirectMiddleware("/path")()); expect(ctx.response.redirect).toHaveBeenCalledWith(302, "/path/"); }); - it("should create a middleware and call next", () => { + it("should create a middleware and call next", async () => { const ctx = PlatformTest.createRequestContext(); vi.spyOn(ctx.response, "redirect"); ctx.request.raw.url = "/path/"; ctx.request.raw.method = "GET"; ctx.request.raw.originalUrl = "/path/"; - redirectMiddleware("/path")(ctx); + await runInContext(ctx, () => redirectMiddleware("/path")()); expect(ctx.response.redirect).not.toHaveBeenCalled(); }); diff --git a/packages/specs/swagger/src/middlewares/redirectMiddleware.ts b/packages/specs/openapi-utils/src/middlewares/redirectMiddleware.ts similarity index 65% rename from packages/specs/swagger/src/middlewares/redirectMiddleware.ts rename to packages/specs/openapi-utils/src/middlewares/redirectMiddleware.ts index 4863f50e3a0..faef0182cce 100644 --- a/packages/specs/swagger/src/middlewares/redirectMiddleware.ts +++ b/packages/specs/openapi-utils/src/middlewares/redirectMiddleware.ts @@ -1,11 +1,13 @@ -import {PlatformContext} from "@tsed/platform-http"; +import {context} from "@tsed/di"; /** - * @ignore + * Redirect to the same path with a trailing slash * @param path */ export function redirectMiddleware(path: string) { - return (ctx: PlatformContext) => { + return () => { + const ctx = context(); + if (ctx.request.method?.toUpperCase() === "GET" && ctx.request.url === path && !ctx.request.url.match(/\/$/)) { ctx.response.redirect(302, `${path}/`); } diff --git a/packages/specs/openapi-utils/src/services/OpenAPIBaseModule.ts b/packages/specs/openapi-utils/src/services/OpenAPIBaseModule.ts new file mode 100644 index 00000000000..ad032685b7b --- /dev/null +++ b/packages/specs/openapi-utils/src/services/OpenAPIBaseModule.ts @@ -0,0 +1,91 @@ +import {basename} from "node:path"; + +import {Env} from "@tsed/core"; +import {configuration, constant, inject, logger} from "@tsed/di"; +import {normalizePath} from "@tsed/normalize-path"; +import {application, OnReady, OnRoutesInit} from "@tsed/platform-http"; +import {PlatformRouter, useContextHandler} from "@tsed/platform-router"; + +import {OpenAPIService} from "../.."; +import type {OpenApiSettings} from "../interfaces/OpenApiSettings.js"; +import {cssMiddleware} from "../middlewares/cssMiddleware.js"; +import {jsMiddleware} from "../middlewares/jsMiddleware.js"; +import {openApiMiddleware} from "../middlewares/openApiMiddleware.js"; +import {redirectMiddleware} from "../middlewares/redirectMiddleware.js"; + +export abstract class OpenAPIBaseModule implements OnRoutesInit, OnReady { + abstract name: string; + abstract rootDir: string; + abstract settings: OpenApiSettings[]; + protected openAPIService = inject(OpenAPIService); + protected env = constant("env"); + protected disableRoutesSummary = constant("logger.disableRoutesSummary"); + private loaded = false; + + $onRoutesInit() { + if (this.loaded) { + return; + } + + this.settings.forEach((conf: OpenApiSettings) => { + const {path = "/"} = conf; + + application().use(path, useContextHandler(redirectMiddleware(path))); + application().use(path, this.createRouter(conf)); + }); + + this.loaded = true; + } + + $onReady() { + // istanbul ignore next + if (configuration().getBestHost && !this.disableRoutesSummary) { + const host = configuration().getBestHost(); + const url = host.toString(); + + const displayLog = (conf: OpenApiSettings) => { + const {path, fileName, doc, specVersion} = conf; + + logger().info( + `[${doc || "default"}] ${specVersion === "2.0" ? "Swagger" : "OpenAPI"} JSON is available on ${url}${normalizePath(path, fileName!)}` + ); + logger().info(`[${doc || "default"}] ${this.name} UI is available on ${url}${path}/`); + }; + + this.settings.forEach((conf) => { + displayLog(conf); + }); + } + + this.generateSpecFiles(); + } + + generateSpecFiles() { + return Promise.all( + this.settings.map(async (conf) => { + this.openAPIService.writeOpenAPISpec(conf); + }) + ); + } + + protected createRouter(conf: OpenApiSettings) { + const {disableSpec, fileName, cssPath, jsPath, viewPath} = conf; + const router = new PlatformRouter(); + + if (!disableSpec) { + router.get(normalizePath("/", fileName!), useContextHandler(openApiMiddleware(conf))); + } + + if (viewPath) { + if (cssPath) { + router.get(`/${basename(cssPath)}`, useContextHandler(cssMiddleware(cssPath))); + } + + if (jsPath) { + router.get(`/${basename(jsPath)}`, useContextHandler(jsMiddleware(jsPath))); + } + } + + return router; + } +} diff --git a/packages/specs/openapi-utils/src/services/OpenAPIService.spec.ts b/packages/specs/openapi-utils/src/services/OpenAPIService.spec.ts new file mode 100644 index 00000000000..6ab6fb20998 --- /dev/null +++ b/packages/specs/openapi-utils/src/services/OpenAPIService.spec.ts @@ -0,0 +1,35 @@ +import {writeFile} from "node:fs/promises"; + +import {PlatformTest} from "@tsed/platform-http/testing"; + +import {OpenAPIService} from "../index.js"; + +vi.mock("node:fs/promises"); + +describe("SwaggerService", () => { + beforeEach(() => PlatformTest.create()); + afterEach(() => PlatformTest.reset()); + + describe("getOpenAPISpec()", () => { + it("should compile spec only once time", async () => { + const service = await PlatformTest.invoke(OpenAPIService); + const result1 = await service.getOpenAPISpec({specVersion: "3.0.1"} as any); + const result2 = await service.getOpenAPISpec({specVersion: "3.0.1"} as any); + + expect(result1).toEqual(result2); + expect(result1).toMatchSnapshot(); + }); + }); + + describe("writeOpenAPISpec()", () => { + it("should write spec only once time", async () => { + const service = await PlatformTest.invoke(OpenAPIService); + + await service.writeOpenAPISpec({specVersion: "3.0.1", outFile: "/path"} as any); + + expect(writeFile).toHaveBeenCalledWith("/path", expect.any(String), { + encoding: "utf8" + }); + }); + }); +}); diff --git a/packages/specs/swagger/src/services/SwaggerService.ts b/packages/specs/openapi-utils/src/services/OpenAPIService.ts similarity index 58% rename from packages/specs/swagger/src/services/SwaggerService.ts rename to packages/specs/openapi-utils/src/services/OpenAPIService.ts index 4c746fd4c71..c8a6dc33950 100644 --- a/packages/specs/swagger/src/services/SwaggerService.ts +++ b/packages/specs/openapi-utils/src/services/OpenAPIService.ts @@ -1,14 +1,16 @@ -import type {Type} from "@tsed/core"; +import {writeFile} from "node:fs/promises"; + +import {Env, type Type} from "@tsed/core"; import {constant, inject, injectable} from "@tsed/di"; import {OpenSpec2, OpenSpec3} from "@tsed/openspec"; import {Platform} from "@tsed/platform-http"; import {generateSpec} from "@tsed/schema"; -import {SwaggerOS2Settings, SwaggerOS3Settings, SwaggerSettings} from "../interfaces/SwaggerSettings.js"; +import {OpenAPI3Settings, OpenApiSettings, Swagger2Settings} from "../interfaces/OpenApiSettings.js"; import {includeRoute} from "../utils/includeRoute.js"; import {readSpec} from "../utils/readSpec.js"; -export class SwaggerService { +export class OpenAPIService { protected platform = inject(Platform); #specs: Map = new Map(); @@ -17,12 +19,11 @@ export class SwaggerService { /** * Generate Spec for the given configuration - * @returns {Spec} */ - public async getOpenAPISpec(conf: SwaggerOS3Settings): Promise; - public async getOpenAPISpec(conf: SwaggerOS2Settings): Promise; - public async getOpenAPISpec(conf: SwaggerSettings): Promise; - public async getOpenAPISpec(conf: SwaggerSettings) { + public async getOpenAPISpec(conf: OpenAPI3Settings): Promise; + public async getOpenAPISpec(conf: Swagger2Settings): Promise; + public async getOpenAPISpec(conf: OpenApiSettings): Promise; + public async getOpenAPISpec(conf: OpenApiSettings) { if (!this.#specs.has(conf.path)) { const version = constant("version", "1.0.0"); const acceptMimes = constant("acceptMimes"); @@ -46,6 +47,19 @@ export class SwaggerService { return this.#specs.get(conf.path); } + + async writeOpenAPISpec(conf: OpenApiSettings) { + const {outFile} = conf; + const env = constant("env"); + + if (env === Env.PROD || outFile) { + const spec = await this.getOpenAPISpec(conf); + + if (outFile) { + return writeFile(outFile, JSON.stringify(spec, null, 2), {encoding: "utf8"}); + } + } + } } -injectable(SwaggerService); +injectable(OpenAPIService); diff --git a/packages/specs/swagger/src/services/__snapshots__/SwaggerService.spec.ts.snap b/packages/specs/openapi-utils/src/services/__snapshots__/OpenAPIService.spec.ts.snap similarity index 100% rename from packages/specs/swagger/src/services/__snapshots__/SwaggerService.spec.ts.snap rename to packages/specs/openapi-utils/src/services/__snapshots__/OpenAPIService.spec.ts.snap diff --git a/packages/specs/openapi-utils/src/services/__snapshots__/SwaggerService.spec.ts.snap b/packages/specs/openapi-utils/src/services/__snapshots__/SwaggerService.spec.ts.snap new file mode 100644 index 00000000000..6a6d4a4c0c8 --- /dev/null +++ b/packages/specs/openapi-utils/src/services/__snapshots__/SwaggerService.spec.ts.snap @@ -0,0 +1,11 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`SwaggerService > getOpenAPISpec() > should compile spec only once time 1`] = ` +{ + "info": { + "title": "Api documentation", + "version": "1.0.0", + }, + "openapi": "3.0.1", +} +`; diff --git a/packages/specs/swagger/src/utils/includeRoute.spec.ts b/packages/specs/openapi-utils/src/utils/includeRoute.spec.ts similarity index 100% rename from packages/specs/swagger/src/utils/includeRoute.spec.ts rename to packages/specs/openapi-utils/src/utils/includeRoute.spec.ts diff --git a/packages/specs/swagger/src/utils/includeRoute.ts b/packages/specs/openapi-utils/src/utils/includeRoute.ts similarity index 84% rename from packages/specs/swagger/src/utils/includeRoute.ts rename to packages/specs/openapi-utils/src/utils/includeRoute.ts index 3fe2d922d59..c63e15fe83f 100644 --- a/packages/specs/swagger/src/utils/includeRoute.ts +++ b/packages/specs/openapi-utils/src/utils/includeRoute.ts @@ -1,9 +1,9 @@ import {Provider} from "@tsed/di"; -import {SwaggerSettings} from "../interfaces/SwaggerSettings.js"; +import {OpenApiSettings} from "../interfaces/OpenApiSettings.js"; import {matchPath} from "./matchPath.js"; -export function includeRoute(route: string, provider: Provider, conf: SwaggerSettings): boolean { +export function includeRoute(route: string, provider: Provider, conf: OpenApiSettings): boolean { const hidden = provider.store.get("hidden"); const docs = provider.store.get("docs") || []; const {doc, pathPatterns} = conf; diff --git a/packages/specs/swagger/src/utils/matchPath.ts b/packages/specs/openapi-utils/src/utils/matchPath.ts similarity index 100% rename from packages/specs/swagger/src/utils/matchPath.ts rename to packages/specs/openapi-utils/src/utils/matchPath.ts diff --git a/packages/specs/openapi-utils/src/utils/readSpec.ts b/packages/specs/openapi-utils/src/utils/readSpec.ts new file mode 100644 index 00000000000..98b4ffc7864 --- /dev/null +++ b/packages/specs/openapi-utils/src/utils/readSpec.ts @@ -0,0 +1,15 @@ +export async function readSpec(path: string) { + const {readFile} = await import("node:fs/promises"); + const {existsSync} = await import("node:fs"); + + if (existsSync(path)) { + try { + const response = await readFile(path, {encoding: "utf8"}); + + return JSON.parse(response); + } catch (e) {} + } + + /* istanbul ignore next */ + return {}; +} diff --git a/packages/specs/openapi-utils/tsconfig.esm.json b/packages/specs/openapi-utils/tsconfig.esm.json new file mode 100644 index 00000000000..8954049da4a --- /dev/null +++ b/packages/specs/openapi-utils/tsconfig.esm.json @@ -0,0 +1,26 @@ +{ + "extends": "@tsed/typescript/tsconfig.node.json", + "compilerOptions": { + "baseUrl": ".", + "rootDir": "src", + "outDir": "./lib/esm", + "declarationDir": "./lib/types", + "declaration": true, + "composite": true, + "noEmit": false + }, + "include": ["src/**/*.ts", "src/**/*.json"], + "exclude": [ + "node_modules", + "test", + "lib", + "benchmark", + "coverage", + "spec", + "**/*.benchmark.ts", + "**/*.spec.ts", + "keys", + "**/__mock__/**", + "webpack.config.js" + ] +} diff --git a/packages/specs/openapi-utils/tsconfig.json b/packages/specs/openapi-utils/tsconfig.json new file mode 100644 index 00000000000..cc938769408 --- /dev/null +++ b/packages/specs/openapi-utils/tsconfig.json @@ -0,0 +1,34 @@ +{ + "extends": "@tsed/typescript/tsconfig.node.json", + "compilerOptions": { + "baseUrl": ".", + "noEmit": true + }, + "include": [], + "references": [ + { + "path": "../../platform/platform-http/tsconfig.json" + }, + { + "path": "../../core/tsconfig.json" + }, + { + "path": "../../di/tsconfig.json" + }, + { + "path": "../json-mapper/tsconfig.json" + }, + { + "path": "../openspec/tsconfig.json" + }, + { + "path": "../schema/tsconfig.json" + }, + { + "path": "./tsconfig.esm.json" + }, + { + "path": "./tsconfig.spec.json" + } + ] +} diff --git a/packages/specs/openapi-utils/tsconfig.spec.json b/packages/specs/openapi-utils/tsconfig.spec.json new file mode 100644 index 00000000000..6f1ccc47c95 --- /dev/null +++ b/packages/specs/openapi-utils/tsconfig.spec.json @@ -0,0 +1,37 @@ +{ + "extends": "@tsed/typescript/tsconfig.node.json", + "compilerOptions": { + "baseUrl": ".", + "rootDir": "../..", + "declaration": false, + "composite": false, + "noEmit": true, + "paths": { + "@tsed/openspec": ["../openspec/src/index.ts"], + "@tsed/schema": ["../schema/src/index.ts"], + "@tsed/di": ["../../di/src/index.ts"], + "@tsed/exceptions": ["../exceptions/src/index.ts"], + "@tsed/json-mapper": ["../json-mapper/src/index.ts"], + "@tsed/platform-exceptions": ["../../platform/platform-exceptions/src/index.ts"], + "@tsed/platform-middlewares": ["../../platform/platform-middlewares/src/index.ts"], + "@tsed/platform-params": ["../../platform/platform-params/src/index.ts"], + "@tsed/platform-log-middleware": ["../../platform/platform-log-middleware/src/index.ts"], + "@tsed/platform-response-filter": ["../../platform/platform-response-filter/src/index.ts"], + "@tsed/platform-router": ["../../platform/platform-router/src/index.ts"], + "@tsed/platform-views": ["../../platform/platform-views/src/index.ts"], + "@tsed/normalize-path": ["../../utils/normalize-path/src/index.ts"], + "@tsed/components-scan": ["../../third-parties/components-scan/src/index.ts"], + "@tsed/platform-http": ["../../platform/platform-http/src/common/index.ts"], + "@tsed/platform-http/testing": ["../../platform/platform-http/src/testing/index.ts"], + "@tsed/ajv": ["../ajv/src/index.ts"], + "@tsed/platform-cache": ["../../platform/platform-cache/src/index.ts"], + "@tsed/platform-test-sdk": ["../../platform/platform-test-sdk/src/index.ts"], + "@tsed/platform-express": ["../../platform/platform-express/src/index.ts"], + "@tsed/platform-koa": ["../../platform/platform-koa/src/index.ts"], + "@tsed/mongoose": ["../../orm/mongoose/src/index.ts"] + }, + "types": ["vite/client", "vitest/globals"] + }, + "include": ["src/**/*.spec.ts", "test/**/*.spec.ts", "vitest.config.mts"], + "exclude": ["node_modules", "lib", "benchmark", "coverage"] +} diff --git a/packages/specs/openapi-utils/vitest.config.mts b/packages/specs/openapi-utils/vitest.config.mts new file mode 100644 index 00000000000..d759e817941 --- /dev/null +++ b/packages/specs/openapi-utils/vitest.config.mts @@ -0,0 +1,21 @@ +// @ts-ignore +import {presets} from "@tsed/vitest/presets"; +import {defineConfig} from "vitest/config"; + +export default defineConfig( + { + ...presets, + test: { + ...presets.test, + coverage: { + ...presets.test.coverage, + thresholds: { + statements: 0, + branches: 0, + functions: 0, + lines: 0 + } + } + } + } +);