midway-scalar 1.0.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.
package/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # midway-scalar
2
+
3
+ Midway.js 组件,用于集成 Scalar API Reference,提供美观的 API 文档展示界面。
4
+
5
+ ## 功能特性
6
+
7
+ - 集成 Scalar API Reference 到 Midway.js 应用
8
+ - 自动读取 Swagger 配置生成 API 文档
9
+ - 支持自定义 Scalar 配置选项
10
+ - 提供 `/scalar` 路由访问 API 文档
11
+
12
+ ## 安装
13
+
14
+ ```bash
15
+ npm install midway-scalar
16
+ # 或
17
+ pnpm add midway-scalar
18
+ ```
19
+
20
+ ## 使用
21
+
22
+ ### 1. 配置组件
23
+
24
+ 在 `src/configuration.ts` 中引入组件:
25
+
26
+ ```typescript
27
+ import { Configuration } from '@midwayjs/core';
28
+ import * as scalar from 'midway-scalar';
29
+
30
+ @Configuration({
31
+ imports: [scalar],
32
+ })
33
+ export class MainConfiguration {}
34
+ ```
35
+
36
+ ### 2. 配置 Scalar
37
+
38
+ 在 `src/config/config.default.ts` 中配置:
39
+
40
+ ```typescript
41
+ export default {
42
+ swagger: {
43
+ title: 'API 文档',
44
+ description: 'API 接口文档',
45
+ version: '1.0.0',
46
+ },
47
+ scalar: {
48
+ theme: 'purple',
49
+ darkMode: true,
50
+ },
51
+ } as MidwayConfig;
52
+ ```
53
+
54
+ ### 3. 访问文档
55
+
56
+ 启动应用后,访问 `http://localhost:7001/scalar/` 查看 API 文档。
57
+
58
+ ## 配置选项
59
+
60
+ 更多配置选项请参考官方文档:[Scalar](https://guides.scalar.com/products/api-references/configuration#configuration__configuration-options)。
61
+
62
+ ## License
63
+
64
+ MIT
@@ -0,0 +1 @@
1
+ export declare const scalar: {};
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.scalar = void 0;
4
+ exports.scalar = {};
@@ -0,0 +1,3 @@
1
+ export declare class ScalarConfiguration {
2
+ onReady(): Promise<void>;
3
+ }
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.ScalarConfiguration = void 0;
10
+ const core_1 = require("@midwayjs/core");
11
+ const swagger = require("@midwayjs/swagger");
12
+ const DefaultConfig = require("./config/config.default");
13
+ let ScalarConfiguration = class ScalarConfiguration {
14
+ async onReady() {
15
+ // TODO something
16
+ }
17
+ };
18
+ ScalarConfiguration = __decorate([
19
+ (0, core_1.Configuration)({
20
+ namespace: 'scalar',
21
+ imports: [swagger],
22
+ importConfigs: [
23
+ {
24
+ default: DefaultConfig,
25
+ },
26
+ ],
27
+ })
28
+ ], ScalarConfiguration);
29
+ exports.ScalarConfiguration = ScalarConfiguration;
@@ -0,0 +1,5 @@
1
+ import { ScalarService } from '../service/scalar.service';
2
+ export declare class ScalarController {
3
+ scalarService: ScalarService;
4
+ getApiReference(): Promise<string>;
5
+ }
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.ScalarController = void 0;
13
+ const core_1 = require("@midwayjs/core");
14
+ const swagger_1 = require("@midwayjs/swagger");
15
+ const scalar_service_1 = require("../service/scalar.service");
16
+ let ScalarController = class ScalarController {
17
+ async getApiReference() {
18
+ return this.scalarService.render();
19
+ }
20
+ };
21
+ __decorate([
22
+ (0, core_1.Inject)(),
23
+ __metadata("design:type", scalar_service_1.ScalarService)
24
+ ], ScalarController.prototype, "scalarService", void 0);
25
+ __decorate([
26
+ (0, core_1.Get)('/'),
27
+ __metadata("design:type", Function),
28
+ __metadata("design:paramtypes", []),
29
+ __metadata("design:returntype", Promise)
30
+ ], ScalarController.prototype, "getApiReference", null);
31
+ ScalarController = __decorate([
32
+ (0, swagger_1.ApiExcludeController)(),
33
+ (0, core_1.Controller)('/scalar')
34
+ ], ScalarController);
35
+ exports.ScalarController = ScalarController;
@@ -0,0 +1,4 @@
1
+ export { ScalarConfiguration as Configuration } from './configuration';
2
+ export * from './controller/scalar.controller';
3
+ export * from './interface';
4
+ export * from './service/scalar.service';
package/dist/index.js ADDED
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.Configuration = void 0;
18
+ var configuration_1 = require("./configuration");
19
+ Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.ScalarConfiguration; } });
20
+ __exportStar(require("./controller/scalar.controller"), exports);
21
+ __exportStar(require("./interface"), exports);
22
+ __exportStar(require("./service/scalar.service"), exports);
@@ -0,0 +1,157 @@
1
+ export interface ScalarConfig {
2
+ url?: string;
3
+ content?: string | Record<string, any> | (() => Record<string, any>);
4
+ proxyUrl?: string;
5
+ authentication?: {
6
+ preferredSecurityScheme?: string | string[] | string[][];
7
+ securitySchemes?: {
8
+ [key: string]: {
9
+ name: string;
10
+ in: 'header' | 'query' | 'cookie';
11
+ value?: string;
12
+ } | {
13
+ token: string;
14
+ } | {
15
+ username: string;
16
+ password: string;
17
+ } | {
18
+ flows?: {
19
+ authorizationCode?: {
20
+ token?: string;
21
+ 'x-scalar-client-id'?: string;
22
+ clientSecret?: string;
23
+ authorizationUrl?: string;
24
+ tokenUrl?: string;
25
+ 'x-scalar-redirect-uri'?: string;
26
+ 'x-usePkce'?: 'SHA-256' | 'plain' | 'no';
27
+ selectedScopes?: string[];
28
+ 'x-scalar-security-query'?: Record<string, any>;
29
+ 'x-scalar-security-body'?: Record<string, any>;
30
+ 'x-tokenName'?: string;
31
+ 'x-scalar-credentials-location'?: 'header' | 'body';
32
+ };
33
+ clientCredentials?: {
34
+ token?: string;
35
+ 'x-scalar-client-id'?: string;
36
+ clientSecret?: string;
37
+ tokenUrl?: string;
38
+ selectedScopes?: string[];
39
+ 'x-tokenName'?: string;
40
+ 'x-scalar-credentials-location'?: 'header' | 'body';
41
+ };
42
+ implicit?: {
43
+ token?: string;
44
+ 'x-scalar-client-id'?: string;
45
+ authorizationUrl?: string;
46
+ 'x-scalar-redirect-uri'?: string;
47
+ selectedScopes?: string[];
48
+ 'x-tokenName'?: string;
49
+ };
50
+ password?: {
51
+ token?: string;
52
+ 'x-scalar-client-id'?: string;
53
+ clientSecret?: string;
54
+ tokenUrl?: string;
55
+ username?: string;
56
+ password?: string;
57
+ selectedScopes?: string[];
58
+ 'x-tokenName'?: string;
59
+ 'x-scalar-credentials-location'?: 'header' | 'body';
60
+ };
61
+ };
62
+ 'x-default-scopes'?: string[];
63
+ };
64
+ };
65
+ };
66
+ baseServerURL?: string;
67
+ customCss?: string;
68
+ darkMode?: boolean;
69
+ defaultHttpClient?: {
70
+ targetKey: string;
71
+ clientKey: string;
72
+ };
73
+ defaultOpenAllTags?: boolean;
74
+ documentDownloadType?: 'json' | 'yaml' | 'both' | 'direct' | 'none';
75
+ expandAllModelSections?: boolean;
76
+ expandAllResponses?: boolean;
77
+ favicon?: string;
78
+ forceDarkModeState?: 'dark' | 'light';
79
+ hideClientButton?: boolean;
80
+ hideDarkModeToggle?: boolean;
81
+ showOperationId?: boolean;
82
+ hideModels?: boolean;
83
+ hideSearch?: boolean;
84
+ hideTestRequestButton?: boolean;
85
+ hiddenClients?: string[] | true | Record<string, boolean | string[]>;
86
+ isLoading?: boolean;
87
+ layout?: 'modern' | 'classic';
88
+ metaData?: Record<string, any>;
89
+ operationTitleSource?: 'summary' | 'path';
90
+ orderRequiredPropertiesFirst?: boolean;
91
+ orderSchemaPropertiesBy?: 'alpha' | 'preserve';
92
+ pathRouting?: {
93
+ basePath: string;
94
+ };
95
+ persistAuth?: boolean;
96
+ telemetry?: boolean;
97
+ plugins?: any[];
98
+ searchHotKey?: string;
99
+ servers?: Array<{
100
+ url: string;
101
+ description?: string;
102
+ variables?: Record<string, {
103
+ default: string;
104
+ description?: string;
105
+ enum?: string[];
106
+ }>;
107
+ }>;
108
+ showSidebar?: boolean;
109
+ showDeveloperTools?: 'always' | 'localhost' | 'never';
110
+ theme?: string;
111
+ withDefaultFonts?: boolean;
112
+ fetch?: (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
113
+ generateHeadingSlug?: (heading: {
114
+ slug: string;
115
+ value: string;
116
+ }) => string;
117
+ generateModelSlug?: (model: {
118
+ name: string;
119
+ }) => string;
120
+ generateOperationSlug?: (operation: {
121
+ method: string;
122
+ path: string;
123
+ operationId?: string;
124
+ summary?: string;
125
+ }) => string;
126
+ generateTagSlug?: (tag: {
127
+ name: string;
128
+ }) => string;
129
+ generateWebhookSlug?: (webhook: {
130
+ name: string;
131
+ method?: string;
132
+ }) => string;
133
+ tagsSorter?: 'alpha' | ((a: {
134
+ name: string;
135
+ }, b: {
136
+ name: string;
137
+ }) => number);
138
+ operationsSorter?: 'alpha' | 'method' | ((a: {
139
+ method: string;
140
+ path: string;
141
+ }, b: {
142
+ method: string;
143
+ path: string;
144
+ }) => number);
145
+ redirect?: (path: string) => string | null | undefined;
146
+ onBeforeRequest?: ({ request }: {
147
+ request: Request;
148
+ }) => void | Promise<void>;
149
+ onDocumentSelect?: () => Promise<void> | void;
150
+ onLoaded?: (slug: string) => Promise<void> | void;
151
+ onRequestSent?: (request: string) => void;
152
+ onServerChange?: (server: string) => void;
153
+ onShowMore?: (tagId: string) => void | Promise<void>;
154
+ onSidebarClick?: (href: string) => void | Promise<void>;
155
+ onSpecUpdate?: (spec: string) => void;
156
+ [key: string]: any;
157
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,5 @@
1
+ import { MidwayConfig } from '@midwayjs/core';
2
+ export declare class ScalarService {
3
+ config: MidwayConfig;
4
+ render(): Promise<string>;
5
+ }
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.ScalarService = void 0;
13
+ const core_1 = require("@midwayjs/core");
14
+ let ScalarService = class ScalarService {
15
+ async render() {
16
+ var _a;
17
+ const swaggerPath = ((_a = this.config.swagger) === null || _a === void 0 ? void 0 : _a.swaggerPath) || '/swagger-ui';
18
+ const specUrl = `${swaggerPath}/index.json`;
19
+ const scalarConfig = this.config.scalar;
20
+ const config = {
21
+ url: specUrl,
22
+ ...scalarConfig,
23
+ };
24
+ return `<!DOCTYPE html>
25
+ <html>
26
+ <head>
27
+ <title>Scalar API Reference</title>
28
+ <meta charset="utf-8" />
29
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
30
+ </head>
31
+ <body>
32
+ <div id="app"></div>
33
+ <script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
34
+ <script>
35
+ Scalar.createApiReference('#app', ${JSON.stringify(config)});
36
+ </script>
37
+ </body>
38
+ </html>`;
39
+ }
40
+ };
41
+ __decorate([
42
+ (0, core_1.Config)(core_1.ALL),
43
+ __metadata("design:type", Object)
44
+ ], ScalarService.prototype, "config", void 0);
45
+ ScalarService = __decorate([
46
+ (0, core_1.Provide)()
47
+ ], ScalarService);
48
+ exports.ScalarService = ScalarService;
package/index.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ export * from './dist/index';
2
+ import { ScalarConfig } from './src/interface';
3
+
4
+ declare module '@midwayjs/core/dist/interface' {
5
+ interface MidwayConfig {
6
+ scalar?: ScalarConfig;
7
+ }
8
+ }
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "midway-scalar",
3
+ "version": "1.0.0",
4
+ "description": "Midway.js component for Scalar API Reference integration",
5
+ "main": "dist/index.js",
6
+ "typings": "index.d.ts",
7
+ "scripts": {
8
+ "build": "mwtsc --cleanOutDir",
9
+ "test": "cross-env NODE_ENV=unittest jest",
10
+ "cov": "jest --coverage",
11
+ "lint": "mwts check",
12
+ "lint:fix": "mwts fix"
13
+ },
14
+ "keywords": ["midway", "scalar", "api-reference", "swagger", "openapi"],
15
+ "author": "nobu121",
16
+ "files": [
17
+ "dist/**/*.js",
18
+ "dist/**/*.d.ts",
19
+ "index.d.ts"
20
+ ],
21
+ "license": "MIT",
22
+ "publishConfig": {
23
+ "registry": "https://registry.npmjs.org/"
24
+ },
25
+ "devDependencies": {
26
+ "@midwayjs/core": "^3.12.0",
27
+ "@midwayjs/logger": "^3.1.0",
28
+ "@midwayjs/mock": "^3.12.0",
29
+ "@types/jest": "^29.2.0",
30
+ "@types/node": "^16.11.22",
31
+ "cross-env": "^6.0.0",
32
+ "jest": "^29.2.2",
33
+ "mwts": "^1.3.0",
34
+ "mwtsc": "^1.4.0",
35
+ "ts-jest": "^29.0.3",
36
+ "typescript": "~4.8.0"
37
+ },
38
+ "dependencies": {
39
+ "@midwayjs/swagger": "^3.20.19",
40
+ "@scalar/core": "^0.3.30"
41
+ }
42
+ }