@tsed/swagger 8.2.0 → 8.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/lib/esm/SwaggerModule.js +27 -83
  2. package/lib/esm/SwaggerModule.js.map +1 -1
  3. package/lib/esm/constants.js +2 -1
  4. package/lib/esm/constants.js.map +1 -1
  5. package/lib/esm/exports.js +2 -0
  6. package/lib/esm/exports.js.map +1 -0
  7. package/lib/esm/index.js +1 -8
  8. package/lib/esm/index.js.map +1 -1
  9. package/lib/esm/middlewares/indexMiddleware.js +9 -5
  10. package/lib/esm/middlewares/indexMiddleware.js.map +1 -1
  11. package/lib/types/SwaggerModule.d.ts +13 -23
  12. package/lib/types/exports.d.ts +1 -0
  13. package/lib/types/index.d.ts +1 -8
  14. package/lib/types/interfaces/SwaggerSettings.d.ts +6 -82
  15. package/lib/types/middlewares/indexMiddleware.d.ts +1 -2
  16. package/lib/views/assets/swagger.css +16 -0
  17. package/lib/views/index.ejs +4 -53
  18. package/package.json +17 -19
  19. package/readme.md +8 -8
  20. package/src/SwaggerModule.spec.ts +7 -7
  21. package/src/SwaggerModule.ts +37 -104
  22. package/src/constants.ts +2 -1
  23. package/src/exports.ts +1 -0
  24. package/src/index.ts +1 -8
  25. package/src/interfaces/SwaggerSettings.ts +6 -85
  26. package/src/middlewares/indexMiddleware.spec.ts +5 -3
  27. package/src/middlewares/indexMiddleware.ts +10 -5
  28. package/views/assets/swagger.css +16 -0
  29. package/views/index.ejs +4 -53
  30. package/lib/esm/decorators/docs.js +0 -14
  31. package/lib/esm/decorators/docs.js.map +0 -1
  32. package/lib/esm/middlewares/cssMiddleware.js +0 -18
  33. package/lib/esm/middlewares/cssMiddleware.js.map +0 -1
  34. package/lib/esm/middlewares/jsMiddleware.js +0 -13
  35. package/lib/esm/middlewares/jsMiddleware.js.map +0 -1
  36. package/lib/esm/middlewares/redirectMiddleware.js +0 -12
  37. package/lib/esm/middlewares/redirectMiddleware.js.map +0 -1
  38. package/lib/esm/services/SwaggerService.js +0 -34
  39. package/lib/esm/services/SwaggerService.js.map +0 -1
  40. package/lib/esm/utils/includeRoute.js +0 -16
  41. package/lib/esm/utils/includeRoute.js.map +0 -1
  42. package/lib/esm/utils/matchPath.js +0 -5
  43. package/lib/esm/utils/matchPath.js.map +0 -1
  44. package/lib/esm/utils/readSpec.js +0 -12
  45. package/lib/esm/utils/readSpec.js.map +0 -1
  46. package/lib/types/decorators/docs.d.ts +0 -10
  47. package/lib/types/middlewares/cssMiddleware.d.ts +0 -6
  48. package/lib/types/middlewares/jsMiddleware.d.ts +0 -6
  49. package/lib/types/middlewares/redirectMiddleware.d.ts +0 -6
  50. package/lib/types/services/SwaggerService.d.ts +0 -15
  51. package/lib/types/utils/includeRoute.d.ts +0 -3
  52. package/lib/types/utils/matchPath.d.ts +0 -1
  53. package/lib/types/utils/readSpec.d.ts +0 -1
  54. package/src/decorators/docs.ts +0 -14
  55. package/src/middlewares/cssMiddleware.spec.ts +0 -24
  56. package/src/middlewares/cssMiddleware.ts +0 -19
  57. package/src/middlewares/jsMiddleware.spec.ts +0 -24
  58. package/src/middlewares/jsMiddleware.ts +0 -14
  59. package/src/middlewares/redirectMiddleware.spec.ts +0 -31
  60. package/src/middlewares/redirectMiddleware.ts +0 -13
  61. package/src/services/SwaggerService.spec.ts +0 -19
  62. package/src/services/SwaggerService.ts +0 -51
  63. package/src/services/__snapshots__/SwaggerService.spec.ts.snap +0 -11
  64. package/src/utils/includeRoute.spec.ts +0 -111
  65. package/src/utils/includeRoute.ts +0 -22
  66. package/src/utils/matchPath.spec.ts +0 -16
  67. package/src/utils/matchPath.ts +0 -5
  68. package/src/utils/readSpec.ts +0 -11
@@ -1,5 +0,0 @@
1
- import micromatch from "micromatch";
2
- export function matchPath(path, patterns) {
3
- return patterns && patterns.length ? micromatch.isMatch(path, patterns) : true;
4
- }
5
- //# sourceMappingURL=matchPath.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"matchPath.js","sourceRoot":"","sources":["../../../src/utils/matchPath.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,YAAY,CAAC;AAEpC,MAAM,UAAU,SAAS,CAAC,IAAY,EAAE,QAAmB;IACzD,OAAO,QAAQ,IAAI,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,UAAU,CAAC,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;AACjF,CAAC"}
@@ -1,12 +0,0 @@
1
- export async function readSpec(path) {
2
- const { default: fs } = await import("fs-extra");
3
- if (fs.existsSync(path)) {
4
- try {
5
- return await fs.readJSON(path, { encoding: "utf8" });
6
- }
7
- catch (e) { }
8
- }
9
- /* istanbul ignore next */
10
- return {};
11
- }
12
- //# sourceMappingURL=readSpec.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"readSpec.js","sourceRoot":"","sources":["../../../src/utils/readSpec.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,IAAY;IACzC,MAAM,EAAC,OAAO,EAAE,EAAE,EAAC,GAAG,MAAM,MAAM,CAAC,UAAU,CAAC,CAAC;IAC/C,IAAI,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;QACxB,IAAI,CAAC;YACH,OAAO,MAAM,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,EAAC,QAAQ,EAAE,MAAM,EAAC,CAAC,CAAC;QACrD,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC,CAAA,CAAC;IAChB,CAAC;IAED,0BAA0B;IAC1B,OAAO,EAAE,CAAC;AACZ,CAAC"}
@@ -1,10 +0,0 @@
1
- /**
2
- * Set the type documentation for this class.
3
- *
4
- * See [swagger documentation](/tutorials/swagger.html)
5
- *
6
- * @decorator
7
- * @swagger
8
- * @param docs
9
- */
10
- export declare function Docs(...docs: string[]): Function;
@@ -1,6 +0,0 @@
1
- import { PlatformContext } from "@tsed/platform-http";
2
- /**
3
- * @ignore
4
- * @param path
5
- */
6
- export declare function cssMiddleware(path: string): (ctx: PlatformContext) => void;
@@ -1,6 +0,0 @@
1
- import { PlatformContext } from "@tsed/platform-http";
2
- /**
3
- * @ignore
4
- * @param path
5
- */
6
- export declare function jsMiddleware(path: string): (ctx: PlatformContext) => void;
@@ -1,6 +0,0 @@
1
- import { PlatformContext } from "@tsed/platform-http";
2
- /**
3
- * @ignore
4
- * @param path
5
- */
6
- export declare function redirectMiddleware(path: string): (ctx: PlatformContext) => void;
@@ -1,15 +0,0 @@
1
- import { OpenSpec2, OpenSpec3 } from "@tsed/openspec";
2
- import { Platform } from "@tsed/platform-http";
3
- import { SwaggerOS2Settings, SwaggerOS3Settings, SwaggerSettings } from "../interfaces/SwaggerSettings.js";
4
- export declare class SwaggerService {
5
- #private;
6
- protected platform: Platform;
7
- constructor();
8
- /**
9
- * Generate Spec for the given configuration
10
- * @returns {Spec}
11
- */
12
- getOpenAPISpec(conf: SwaggerOS3Settings): Promise<OpenSpec3>;
13
- getOpenAPISpec(conf: SwaggerOS2Settings): Promise<OpenSpec2>;
14
- getOpenAPISpec(conf: SwaggerSettings): Promise<OpenSpec2>;
15
- }
@@ -1,3 +0,0 @@
1
- import { Provider } from "@tsed/di";
2
- import { SwaggerSettings } from "../interfaces/SwaggerSettings.js";
3
- export declare function includeRoute(route: string, provider: Provider, conf: SwaggerSettings): boolean;
@@ -1 +0,0 @@
1
- export declare function matchPath(path: string, patterns?: string[]): boolean;
@@ -1 +0,0 @@
1
- export declare function readSpec(path: string): Promise<any>;
@@ -1,14 +0,0 @@
1
- import {StoreMerge} from "@tsed/core";
2
-
3
- /**
4
- * Set the type documentation for this class.
5
- *
6
- * See [swagger documentation](/tutorials/swagger.html)
7
- *
8
- * @decorator
9
- * @swagger
10
- * @param docs
11
- */
12
- export function Docs(...docs: string[]) {
13
- return StoreMerge("docs", docs);
14
- }
@@ -1,24 +0,0 @@
1
- import {PlatformTest} from "@tsed/platform-http/testing";
2
- import Fs from "fs";
3
-
4
- import {cssMiddleware} from "./cssMiddleware.js";
5
-
6
- describe("cssMiddleware", () => {
7
- beforeEach(PlatformTest.create);
8
- afterEach(PlatformTest.reset);
9
- beforeEach(() => {
10
- vi.spyOn(Fs, "readFileSync").mockReturnValue(".css{}");
11
- });
12
- it("should create a middleware", () => {
13
- const ctx = PlatformTest.createRequestContext();
14
-
15
- cssMiddleware("/path")(ctx);
16
-
17
- expect(ctx.response.raw.headers).toEqual({
18
- "content-type": "text/css",
19
- "x-request-id": "id"
20
- });
21
- expect(ctx.response.raw.statusCode).toEqual(200);
22
- expect(ctx.response.raw.data).toEqual(".css{}");
23
- });
24
- });
@@ -1,19 +0,0 @@
1
- import {PlatformContext} from "@tsed/platform-http";
2
- import Fs from "fs";
3
- import {resolve} from "path";
4
-
5
- /**
6
- * @ignore
7
- * @param path
8
- */
9
- export function cssMiddleware(path: string) {
10
- return (ctx: PlatformContext) => {
11
- const content = Fs.readFileSync(resolve(path), {encoding: "utf8"});
12
- ctx.response
13
- .setHeaders({
14
- "Content-Type": "text/css"
15
- })
16
- .status(200)
17
- .body(content);
18
- };
19
- }
@@ -1,24 +0,0 @@
1
- import {PlatformTest} from "@tsed/platform-http/testing";
2
- import Fs from "fs";
3
-
4
- import {jsMiddleware} from "./jsMiddleware.js";
5
-
6
- describe("jsMiddleware", () => {
7
- beforeEach(PlatformTest.create);
8
- afterEach(PlatformTest.reset);
9
- beforeEach(() => {
10
- vi.spyOn(Fs, "readFileSync").mockReturnValue("var test=1");
11
- });
12
- it("should create a middleware", () => {
13
- const ctx = PlatformTest.createRequestContext();
14
-
15
- jsMiddleware("/path")(ctx);
16
-
17
- expect(ctx.response.raw.headers).toEqual({
18
- "content-type": "application/javascript",
19
- "x-request-id": "id"
20
- });
21
- expect(ctx.response.raw.statusCode).toBe(200);
22
- expect(ctx.response.raw.data).toBe("var test=1");
23
- });
24
- });
@@ -1,14 +0,0 @@
1
- import {PlatformContext} from "@tsed/platform-http";
2
- import Fs from "fs";
3
- import {resolve} from "path";
4
-
5
- /**
6
- * @ignore
7
- * @param path
8
- */
9
- export function jsMiddleware(path: string) {
10
- return (ctx: PlatformContext) => {
11
- const content = Fs.readFileSync(resolve(path), {encoding: "utf8"});
12
- ctx.response.setHeaders({"Content-Type": "application/javascript"}).status(200).body(content);
13
- };
14
- }
@@ -1,31 +0,0 @@
1
- import {PlatformTest} from "@tsed/platform-http/testing";
2
-
3
- import {redirectMiddleware} from "./redirectMiddleware.js";
4
-
5
- describe("redirectMiddleware and redirect", () => {
6
- beforeEach(PlatformTest.create);
7
- afterEach(PlatformTest.reset);
8
- it("should create a middleware", () => {
9
- const ctx = PlatformTest.createRequestContext();
10
- vi.spyOn(ctx.response, "redirect").mockReturnValue(undefined as never);
11
-
12
- ctx.request.raw.url = "/path";
13
- ctx.request.raw.originalUrl = "/path";
14
- ctx.request.raw.method = "GET";
15
-
16
- redirectMiddleware("/path")(ctx);
17
-
18
- expect(ctx.response.redirect).toHaveBeenCalledWith(302, "/path/");
19
- });
20
- it("should create a middleware and call next", () => {
21
- const ctx = PlatformTest.createRequestContext();
22
- vi.spyOn(ctx.response, "redirect");
23
- ctx.request.raw.url = "/path/";
24
- ctx.request.raw.method = "GET";
25
- ctx.request.raw.originalUrl = "/path/";
26
-
27
- redirectMiddleware("/path")(ctx);
28
-
29
- expect(ctx.response.redirect).not.toHaveBeenCalled();
30
- });
31
- });
@@ -1,13 +0,0 @@
1
- import {PlatformContext} from "@tsed/platform-http";
2
-
3
- /**
4
- * @ignore
5
- * @param path
6
- */
7
- export function redirectMiddleware(path: string) {
8
- return (ctx: PlatformContext) => {
9
- if (ctx.request.method?.toUpperCase() === "GET" && ctx.request.url === path && !ctx.request.url.match(/\/$/)) {
10
- ctx.response.redirect(302, `${path}/`);
11
- }
12
- };
13
- }
@@ -1,19 +0,0 @@
1
- import {PlatformTest} from "@tsed/platform-http/testing";
2
-
3
- import {SwaggerService} from "../index.js";
4
-
5
- describe("SwaggerService", () => {
6
- beforeEach(() => PlatformTest.create());
7
- afterEach(() => PlatformTest.reset());
8
-
9
- describe("getOpenAPISpec()", () => {
10
- it("should compile spec only once time", async () => {
11
- const swaggerService = await PlatformTest.invoke<SwaggerService>(SwaggerService);
12
- const result1 = await swaggerService.getOpenAPISpec({specVersion: "3.0.1"} as any);
13
- const result2 = await swaggerService.getOpenAPISpec({specVersion: "3.0.1"} as any);
14
-
15
- expect(result1).toEqual(result2);
16
- expect(result1).toMatchSnapshot();
17
- });
18
- });
19
- });
@@ -1,51 +0,0 @@
1
- import type {Type} from "@tsed/core";
2
- import {constant, inject, injectable} from "@tsed/di";
3
- import {OpenSpec2, OpenSpec3} from "@tsed/openspec";
4
- import {Platform} from "@tsed/platform-http";
5
- import {generateSpec} from "@tsed/schema";
6
-
7
- import {SwaggerOS2Settings, SwaggerOS3Settings, SwaggerSettings} from "../interfaces/SwaggerSettings.js";
8
- import {includeRoute} from "../utils/includeRoute.js";
9
- import {readSpec} from "../utils/readSpec.js";
10
-
11
- export class SwaggerService {
12
- protected platform = inject(Platform);
13
-
14
- #specs: Map<string, OpenSpec3 | OpenSpec2> = new Map();
15
-
16
- constructor() {}
17
-
18
- /**
19
- * Generate Spec for the given configuration
20
- * @returns {Spec}
21
- */
22
- public async getOpenAPISpec(conf: SwaggerOS3Settings): Promise<OpenSpec3>;
23
- public async getOpenAPISpec(conf: SwaggerOS2Settings): Promise<OpenSpec2>;
24
- public async getOpenAPISpec(conf: SwaggerSettings): Promise<OpenSpec2>;
25
- public async getOpenAPISpec(conf: SwaggerSettings) {
26
- if (!this.#specs.has(conf.path)) {
27
- const version = constant("version", "1.0.0");
28
- const acceptMimes = constant<string>("acceptMimes");
29
- const specPath = conf.specPath;
30
-
31
- const tokens = this.platform
32
- .getMountedControllers()
33
- .filter(({routes, provider}) => [...routes.values()].some((route) => includeRoute(route, provider, conf)))
34
- .map(({route, provider}) => ({token: provider.token as Type, rootPath: route}));
35
-
36
- const spec = generateSpec({
37
- tokens,
38
- ...conf,
39
- fileSpec: specPath ? await readSpec(specPath) : {},
40
- version,
41
- acceptMimes
42
- });
43
-
44
- this.#specs.set(conf.path, spec);
45
- }
46
-
47
- return this.#specs.get(conf.path);
48
- }
49
- }
50
-
51
- injectable(SwaggerService);
@@ -1,11 +0,0 @@
1
- // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
-
3
- exports[`SwaggerService > getOpenAPISpec() > should compile spec only once time 1`] = `
4
- {
5
- "info": {
6
- "title": "Api documentation",
7
- "version": "1.0.0",
8
- },
9
- "openapi": "3.0.1",
10
- }
11
- `;
@@ -1,111 +0,0 @@
1
- import {ControllerProvider} from "@tsed/di";
2
- import {Hidden} from "@tsed/schema";
3
-
4
- import {Docs} from "../decorators/docs.js";
5
- import {includeRoute} from "./includeRoute.js";
6
-
7
- @Hidden()
8
- class HiddenCtrl {}
9
-
10
- @Docs("test")
11
- class DocsCtrl {}
12
-
13
- class PlainCtrl {}
14
-
15
- describe("includeRoute", () => {
16
- it.each([
17
- {
18
- label: "a simple configuration + HiddenCtrl are given",
19
- expected: false,
20
- route: "/test",
21
- conf: {
22
- path: "/swagger"
23
- },
24
- controller: HiddenCtrl
25
- },
26
- {
27
- label: "a simple configuration + PlainCtrl are given",
28
- expected: true,
29
- route: "/test",
30
- conf: {
31
- path: "/swagger"
32
- },
33
- controller: PlainCtrl
34
- },
35
- {
36
- label: "a given configuration with doc property doesn't match route",
37
- expected: false,
38
- route: "/test",
39
- conf: {
40
- path: "/swagger",
41
- doc: "notmatch"
42
- },
43
- controller: DocsCtrl
44
- },
45
- {
46
- label: "a given configuration with doc property matching route",
47
- expected: true,
48
- route: "/test",
49
- conf: {
50
- path: "/swagger",
51
- doc: "test"
52
- },
53
- controller: DocsCtrl
54
- },
55
- {
56
- label: "a given configuration with pathPatterns property matching route",
57
- expected: true,
58
- route: "/should/match",
59
- conf: {
60
- path: "/swagger",
61
- pathPatterns: ["/should/**"]
62
- },
63
- controller: PlainCtrl
64
- },
65
- {
66
- label: "a given configuration with pathPatterns property doesn't match route",
67
- expected: false,
68
- route: "/shouldnot/match",
69
- conf: {
70
- path: "/swagger",
71
- pathPatterns: ["/notmatch/**"]
72
- },
73
- controller: PlainCtrl
74
- },
75
- {
76
- label: "a given configuration with pathPatterns property doesn't match route - negation on pathPatterns",
77
- expected: false,
78
- route: "/should/match",
79
- conf: {
80
- path: "/swagger",
81
- pathPatterns: ["!/should/**"]
82
- },
83
- controller: PlainCtrl
84
- },
85
- {
86
- label: "a given configuration with pathPatterns/doc properties but only doc match",
87
- expected: true,
88
- route: "/shouldnot/match",
89
- conf: {
90
- path: "/swagger",
91
- doc: "test",
92
- pathPatterns: ["/notmatch/**"]
93
- },
94
- controller: DocsCtrl
95
- },
96
- {
97
- label: "a given configuration with pathPatterns/doc properties but only pathPattern match",
98
- expected: true,
99
- route: "/should/match",
100
- conf: {
101
- path: "/swagger",
102
- doc: "nomatch",
103
- pathPatterns: ["/should/**"]
104
- },
105
- controller: DocsCtrl
106
- }
107
- ])("should return $expected when $label ($route)", ({expected, conf, route, controller}) => {
108
- const provider = new ControllerProvider(controller);
109
- expect(includeRoute(route, provider, conf as any)).toBe(expected);
110
- });
111
- });
@@ -1,22 +0,0 @@
1
- import {Provider} from "@tsed/di";
2
-
3
- import {SwaggerSettings} from "../interfaces/SwaggerSettings.js";
4
- import {matchPath} from "./matchPath.js";
5
-
6
- export function includeRoute(route: string, provider: Provider, conf: SwaggerSettings): boolean {
7
- const hidden = provider.store.get("hidden");
8
- const docs = provider.store.get("docs") || [];
9
- const {doc, pathPatterns} = conf;
10
- const inDoc = doc && docs.indexOf(doc) > -1;
11
- const pathPatternMatch = pathPatterns && matchPath(route, pathPatterns);
12
-
13
- if (hidden) {
14
- return false;
15
- }
16
-
17
- if (!doc && !pathPatterns) {
18
- return true;
19
- }
20
-
21
- return !!(inDoc || pathPatternMatch);
22
- }
@@ -1,16 +0,0 @@
1
- import {matchPath} from "./matchPath.js";
2
-
3
- describe("matchPath", () => {
4
- it("should match route with the glob pattern", () => {
5
- expect(matchPath("/user", ["/user/**"])).toBe(true);
6
- expect(matchPath("/user", [])).toBe(true);
7
- expect(matchPath("/user")).toBe(true);
8
- expect(matchPath("/test", ["!/user/**"])).toBe(true);
9
- });
10
-
11
- it("should not match route with the glob pattern", () => {
12
- const result = matchPath("/test", ["/user/**"]);
13
-
14
- expect(result).toBe(false);
15
- });
16
- });
@@ -1,5 +0,0 @@
1
- import micromatch from "micromatch";
2
-
3
- export function matchPath(path: string, patterns?: string[]) {
4
- return patterns && patterns.length ? micromatch.isMatch(path, patterns) : true;
5
- }
@@ -1,11 +0,0 @@
1
- export async function readSpec(path: string) {
2
- const {default: fs} = await import("fs-extra");
3
- if (fs.existsSync(path)) {
4
- try {
5
- return await fs.readJSON(path, {encoding: "utf8"});
6
- } catch (e) {}
7
- }
8
-
9
- /* istanbul ignore next */
10
- return {};
11
- }