@scalar/mock-server 0.1.61 → 0.1.63

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/dist/index.js CHANGED
@@ -2,140 +2,54 @@ import { getExampleFromSchema } from '@scalar/oas-utils';
2
2
  import { openapi } from '@scalar/openapi-parser';
3
3
  import { Hono } from 'hono';
4
4
 
5
- /******************************************************************************
6
- Copyright (c) Microsoft Corporation.
7
-
8
- Permission to use, copy, modify, and/or distribute this software for any
9
- purpose with or without fee is hereby granted.
10
-
11
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
- PERFORMANCE OF THIS SOFTWARE.
18
- ***************************************************************************** */
19
- /* global Reflect, Promise, SuppressedError, Symbol */
20
-
21
-
22
- function __awaiter(thisArg, _arguments, P, generator) {
23
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
24
- return new (P || (P = Promise))(function (resolve, reject) {
25
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
26
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
27
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
28
- step((generator = generator.apply(thisArg, _arguments || [])).next());
29
- });
30
- }
31
-
32
- function __generator(thisArg, body) {
33
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
34
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
35
- function verb(n) { return function (v) { return step([n, v]); }; }
36
- function step(op) {
37
- if (f) throw new TypeError("Generator is already executing.");
38
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
39
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
40
- if (y = 0, t) op = [op[0] & 2, t.value];
41
- switch (op[0]) {
42
- case 0: case 1: t = op; break;
43
- case 4: _.label++; return { value: op[1], done: false };
44
- case 5: _.label++; y = op[1]; op = [0]; continue;
45
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
46
- default:
47
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
48
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
49
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
50
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
51
- if (t[2]) _.ops.pop();
52
- _.trys.pop(); continue;
53
- }
54
- op = body.call(thisArg, _);
55
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
56
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
57
- }
58
- }
59
-
60
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
61
- var e = new Error(message);
62
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
63
- };
64
-
65
- /**
66
- * Normalize OpenAPI JSON string, YAML string … to object
67
- */
68
5
  function normalize(openapi) {
69
- if (typeof openapi === 'string') {
70
- openapi = JSON.parse(openapi);
71
- }
72
- return openapi;
6
+ if (typeof openapi === "string") {
7
+ openapi = JSON.parse(openapi);
8
+ }
9
+ return openapi;
73
10
  }
74
11
 
75
- /**
76
- * Convert path to route
77
- * Example: /posts/{id} -> /posts/:id
78
- */
79
12
  function routeFromPath(path) {
80
- return path.replace(/{/g, ':').replace(/}/g, '');
13
+ return path.replace(/{/g, ":").replace(/}/g, "");
81
14
  }
82
15
 
83
- /**
84
- * Create a mock server instance
85
- */
86
- function createMockServer(options) {
87
- return __awaiter(this, void 0, void 0, function () {
88
- var app, result;
89
- var _a;
90
- return __generator(this, function (_b) {
91
- switch (_b.label) {
92
- case 0:
93
- app = new Hono();
94
- return [4 /*yield*/, openapi().load(options.specification).resolve()
95
- // OpenAPI file
96
- ];
97
- case 1:
98
- result = _b.sent();
99
- // OpenAPI file
100
- app.get('/openapi.json', function (c) {
101
- if (!(options === null || options === void 0 ? void 0 : options.specification)) {
102
- return c.text('Not found', 404);
103
- }
104
- return c.json(options.specification);
105
- });
106
- // Paths
107
- Object.keys((_a = result.schema.paths) !== null && _a !== void 0 ? _a : {}).forEach(function (path) {
108
- // Request methods
109
- Object.keys(result.schema.paths[path]).forEach(function (method) {
110
- var route = routeFromPath(path);
111
- // Route
112
- app[method](route, function (c) {
113
- var _a, _b;
114
- // Call onRequest callback
115
- if (options === null || options === void 0 ? void 0 : options.onRequest) {
116
- options.onRequest({
117
- context: c,
118
- operation: result.schema.paths[path][method],
119
- });
120
- }
121
- // Response
122
- var operation = result.schema.paths[path][method];
123
- var jsonResponseConfiguration = (_b = (_a = operation.responses) === null || _a === void 0 ? void 0 : _a['200']) === null || _b === void 0 ? void 0 : _b.content['application/json'];
124
- var response = (jsonResponseConfiguration === null || jsonResponseConfiguration === void 0 ? void 0 : jsonResponseConfiguration.example)
125
- ? jsonResponseConfiguration.example
126
- : (jsonResponseConfiguration === null || jsonResponseConfiguration === void 0 ? void 0 : jsonResponseConfiguration.schema)
127
- ? getExampleFromSchema(jsonResponseConfiguration.schema, {
128
- emptyString: '…',
129
- })
130
- : null;
131
- return c.json(response);
132
- });
133
- });
134
- });
135
- return [2 /*return*/, app];
136
- }
137
- });
16
+ async function createMockServer(options) {
17
+ const app = new Hono();
18
+ const result = await openapi().load(options?.specification ?? {}).resolve();
19
+ app.get("/openapi.json", (c) => {
20
+ if (!options?.specification) {
21
+ return c.text("Not found", 404);
22
+ }
23
+ return c.json(openapi().load(options.specification).get());
24
+ });
25
+ app.get("/openapi.yaml", (c) => {
26
+ if (!options?.specification) {
27
+ return c.text("Not found", 404);
28
+ }
29
+ return c.text(openapi().load(options.specification).toYaml());
30
+ });
31
+ Object.keys(result.schema?.paths ?? {}).forEach((path) => {
32
+ Object.keys(result.schema?.paths?.[path] ?? {}).forEach((method) => {
33
+ const route = routeFromPath(path);
34
+ app[method](route, (c) => {
35
+ if (options?.onRequest) {
36
+ options.onRequest({
37
+ context: c,
38
+ // @ts-expect-error Needs a proper type
39
+ operation: result.schema.paths[path][method]
40
+ });
41
+ }
42
+ const operation = result.schema?.paths?.[path]?.[method];
43
+ const jsonResponseConfiguration = operation.responses?.["200"]?.content["application/json"];
44
+ const response = jsonResponseConfiguration?.example ? jsonResponseConfiguration.example : jsonResponseConfiguration?.schema ? getExampleFromSchema(jsonResponseConfiguration.schema, {
45
+ emptyString: "\u2026"
46
+ }) : null;
47
+ return c.json(response);
48
+ });
138
49
  });
50
+ });
51
+ return app;
139
52
  }
140
53
 
141
54
  export { createMockServer, normalize, routeFromPath };
55
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../src/utils/normalize.ts","../src/utils/routeFromPath.ts","../src/createMockServer.ts"],"sourcesContent":["/**\n * Normalize OpenAPI JSON string, YAML string … to object\n */\nexport function normalize(openapi: string | Record<string, any>) {\n if (typeof openapi === 'string') {\n openapi = JSON.parse(openapi)\n }\n\n return openapi\n}\n","/**\n * Convert path to route\n * Example: /posts/{id} -> /posts/:id\n */\nexport function routeFromPath(path: string) {\n return path.replace(/{/g, ':').replace(/}/g, '')\n}\n","import { type Operation, getExampleFromSchema } from '@scalar/oas-utils'\nimport { openapi } from '@scalar/openapi-parser'\nimport { type Context, Hono } from 'hono'\n\nimport { routeFromPath } from './utils'\n\n/**\n * Create a mock server instance\n */\nexport async function createMockServer(options?: {\n specification: string | Record<string, any>\n onRequest?: (data: { context: Context; operation: Operation }) => void\n}) {\n const app = new Hono()\n\n // Resolve references\n const result = await openapi()\n .load(options?.specification ?? {})\n .resolve()\n\n // OpenAPI JSON file\n app.get('/openapi.json', (c) => {\n if (!options?.specification) {\n return c.text('Not found', 404)\n }\n\n return c.json(openapi().load(options.specification).get())\n })\n\n // OpenAPI YAML file\n app.get('/openapi.yaml', (c) => {\n if (!options?.specification) {\n return c.text('Not found', 404)\n }\n\n return c.text(openapi().load(options.specification).toYaml())\n })\n\n // Paths\n Object.keys(result.schema?.paths ?? {}).forEach((path) => {\n // Request methods\n Object.keys(result.schema?.paths?.[path] ?? {}).forEach((method) => {\n const route = routeFromPath(path)\n\n // Route\n // @ts-expect-error Needs a proper type\n app[method](route, (c: Context) => {\n // Call onRequest callback\n if (options?.onRequest) {\n options.onRequest({\n context: c,\n // @ts-expect-error Needs a proper type\n operation: result.schema.paths[path][method],\n })\n }\n\n // Response\n // @ts-expect-error Needs a proper type\n const operation = result.schema?.paths?.[path]?.[method]\n\n const jsonResponseConfiguration =\n operation.responses?.['200']?.content['application/json']\n\n const response = jsonResponseConfiguration?.example\n ? jsonResponseConfiguration.example\n : jsonResponseConfiguration?.schema\n ? getExampleFromSchema(jsonResponseConfiguration.schema, {\n emptyString: '…',\n })\n : null\n\n return c.json(response)\n })\n })\n })\n\n return app\n}\n"],"names":[],"mappings":";;;;AAGO,SAAS,UAAU,OAAuC,EAAA;AAC/D,EAAI,IAAA,OAAO,YAAY,QAAU,EAAA;AAC/B,IAAU,OAAA,GAAA,IAAA,CAAK,MAAM,OAAO,CAAA,CAAA;AAAA,GAC9B;AAEA,EAAO,OAAA,OAAA,CAAA;AACT;;ACLO,SAAS,cAAc,IAAc,EAAA;AAC1C,EAAA,OAAO,KAAK,OAAQ,CAAA,IAAA,EAAM,GAAG,CAAE,CAAA,OAAA,CAAQ,MAAM,EAAE,CAAA,CAAA;AACjD;;ACGA,eAAsB,iBAAiB,OAGpC,EAAA;AACD,EAAM,MAAA,GAAA,GAAM,IAAI,IAAK,EAAA,CAAA;AAGrB,EAAM,MAAA,MAAA,GAAS,MAAM,OAAA,EAClB,CAAA,IAAA,CAAK,SAAS,aAAiB,IAAA,EAAE,CAAA,CACjC,OAAQ,EAAA,CAAA;AAGX,EAAI,GAAA,CAAA,GAAA,CAAI,eAAiB,EAAA,CAAC,CAAM,KAAA;AAC9B,IAAI,IAAA,CAAC,SAAS,aAAe,EAAA;AAC3B,MAAO,OAAA,CAAA,CAAE,IAAK,CAAA,WAAA,EAAa,GAAG,CAAA,CAAA;AAAA,KAChC;AAEA,IAAO,OAAA,CAAA,CAAE,KAAK,OAAQ,EAAA,CAAE,KAAK,OAAQ,CAAA,aAAa,CAAE,CAAA,GAAA,EAAK,CAAA,CAAA;AAAA,GAC1D,CAAA,CAAA;AAGD,EAAI,GAAA,CAAA,GAAA,CAAI,eAAiB,EAAA,CAAC,CAAM,KAAA;AAC9B,IAAI,IAAA,CAAC,SAAS,aAAe,EAAA;AAC3B,MAAO,OAAA,CAAA,CAAE,IAAK,CAAA,WAAA,EAAa,GAAG,CAAA,CAAA;AAAA,KAChC;AAEA,IAAO,OAAA,CAAA,CAAE,KAAK,OAAQ,EAAA,CAAE,KAAK,OAAQ,CAAA,aAAa,CAAE,CAAA,MAAA,EAAQ,CAAA,CAAA;AAAA,GAC7D,CAAA,CAAA;AAGD,EAAO,MAAA,CAAA,IAAA,CAAK,OAAO,MAAQ,EAAA,KAAA,IAAS,EAAE,CAAA,CAAE,OAAQ,CAAA,CAAC,IAAS,KAAA;AAExD,IAAO,MAAA,CAAA,IAAA,CAAK,MAAO,CAAA,MAAA,EAAQ,KAAQ,GAAA,IAAI,CAAK,IAAA,EAAE,CAAA,CAAE,OAAQ,CAAA,CAAC,MAAW,KAAA;AAClE,MAAM,MAAA,KAAA,GAAQ,cAAc,IAAI,CAAA,CAAA;AAIhC,MAAA,GAAA,CAAI,MAAM,CAAA,CAAE,KAAO,EAAA,CAAC,CAAe,KAAA;AAEjC,QAAA,IAAI,SAAS,SAAW,EAAA;AACtB,UAAA,OAAA,CAAQ,SAAU,CAAA;AAAA,YAChB,OAAS,EAAA,CAAA;AAAA;AAAA,YAET,WAAW,MAAO,CAAA,MAAA,CAAO,KAAM,CAAA,IAAI,EAAE,MAAM,CAAA;AAAA,WAC5C,CAAA,CAAA;AAAA,SACH;AAIA,QAAA,MAAM,YAAY,MAAO,CAAA,MAAA,EAAQ,KAAQ,GAAA,IAAI,IAAI,MAAM,CAAA,CAAA;AAEvD,QAAA,MAAM,4BACJ,SAAU,CAAA,SAAA,GAAY,KAAK,CAAA,EAAG,QAAQ,kBAAkB,CAAA,CAAA;AAE1D,QAAM,MAAA,QAAA,GAAW,2BAA2B,OACxC,GAAA,yBAAA,CAA0B,UAC1B,yBAA2B,EAAA,MAAA,GACzB,oBAAqB,CAAA,yBAAA,CAA0B,MAAQ,EAAA;AAAA,UACrD,WAAa,EAAA,QAAA;AAAA,SACd,CACD,GAAA,IAAA,CAAA;AAEN,QAAO,OAAA,CAAA,CAAE,KAAK,QAAQ,CAAA,CAAA;AAAA,OACvB,CAAA,CAAA;AAAA,KACF,CAAA,CAAA;AAAA,GACF,CAAA,CAAA;AAED,EAAO,OAAA,GAAA,CAAA;AACT;;;;"}
package/package.json CHANGED
@@ -16,42 +16,41 @@
16
16
  "swagger",
17
17
  "cli"
18
18
  ],
19
- "version": "0.1.61",
19
+ "version": "0.1.63",
20
20
  "engines": {
21
21
  "node": ">=18"
22
22
  },
23
23
  "type": "module",
24
- "main": "./dist/index.js",
25
- "types": "dist/src/index.d.ts",
24
+ "main": "dist/index.js",
25
+ "types": "dist/index.d.ts",
26
26
  "exports": {
27
27
  "import": "./dist/index.js"
28
28
  },
29
29
  "files": [
30
- "./dist"
30
+ "./dist",
31
+ "CHANGELOG.md"
31
32
  ],
33
+ "module": "dist/index.js",
32
34
  "dependencies": {
33
35
  "@hono/node-server": "^1.11.0",
34
36
  "@scalar/openapi-parser": "^0.3.2",
35
37
  "hono": "^4.2.7",
36
- "vite-node": "^1.3.1",
37
- "@scalar/api-reference": "1.22.24",
38
- "@scalar/oas-utils": "0.1.10",
39
- "@scalar/galaxy": "0.1.2"
38
+ "@scalar/oas-utils": "0.1.10"
40
39
  },
41
40
  "devDependencies": {
42
- "@rollup/plugin-json": "^6.1.0",
43
- "@rollup/plugin-typescript": "^11.1.6",
44
41
  "@types/node": "^20.8.4",
45
- "execa": "^8.0.1",
42
+ "esbuild": "^0.21.1",
46
43
  "rollup": "^4.16.4",
47
- "strip-ansi": "^7.1.0",
44
+ "rollup-plugin-delete": "^2.0.0",
45
+ "rollup-plugin-dts": "^6.1.0",
46
+ "rollup-plugin-esbuild": "^6.1.1",
48
47
  "tslib": "^2.6.2",
49
48
  "typescript": "^5.4.3",
49
+ "vite-node": "^1.3.1",
50
50
  "@scalar/galaxy": "0.1.2"
51
51
  },
52
52
  "scripts": {
53
- "build": "rm -Rf dist/ && tsc && rollup -c",
54
- "cli:link": "pnpm run build && npm unlink -g && npm link",
53
+ "build": "rm -Rf dist/ && rollup -c",
55
54
  "test": "vitest",
56
55
  "types:check": "tsc --noEmit --skipLibCheck"
57
56
  }
package/dist/package.json DELETED
@@ -1,58 +0,0 @@
1
- {
2
- "name": "@scalar/mock-server",
3
- "description": "OpenAPI mock server server",
4
- "license": "MIT",
5
- "author": "Scalar (https://github.com/scalar)",
6
- "homepage": "https://github.com/scalar/scalar",
7
- "bugs": "https://github.com/scalar/scalar/issues/new/choose",
8
- "repository": {
9
- "type": "git",
10
- "url": "https://github.com/scalar/scalar.git",
11
- "directory": "packages/mock-server"
12
- },
13
- "keywords": [
14
- "scalar",
15
- "openapi",
16
- "swagger",
17
- "cli"
18
- ],
19
- "version": "0.1.61",
20
- "engines": {
21
- "node": ">=18"
22
- },
23
- "scripts": {
24
- "build": "rm -Rf dist/ && tsc && rollup -c",
25
- "cli:link": "pnpm run build && npm unlink -g && npm link",
26
- "test": "vitest",
27
- "types:check": "tsc --noEmit --skipLibCheck"
28
- },
29
- "type": "module",
30
- "main": "./dist/index.js",
31
- "types": "dist/src/index.d.ts",
32
- "exports": {
33
- "import": "./dist/index.js"
34
- },
35
- "files": [
36
- "./dist"
37
- ],
38
- "dependencies": {
39
- "@hono/node-server": "^1.11.0",
40
- "@scalar/api-reference": "workspace:*",
41
- "@scalar/galaxy": "workspace:*",
42
- "@scalar/oas-utils": "workspace:*",
43
- "@scalar/openapi-parser": "^0.3.2",
44
- "hono": "^4.2.7",
45
- "vite-node": "^1.3.1"
46
- },
47
- "devDependencies": {
48
- "@rollup/plugin-json": "^6.1.0",
49
- "@rollup/plugin-typescript": "^11.1.6",
50
- "@scalar/galaxy": "workspace:*",
51
- "@types/node": "^20.8.4",
52
- "execa": "^8.0.1",
53
- "rollup": "^4.16.4",
54
- "strip-ansi": "^7.1.0",
55
- "tslib": "^2.6.2",
56
- "typescript": "^5.4.3"
57
- }
58
- }
@@ -1,12 +0,0 @@
1
- import { type Operation } from '@scalar/oas-utils';
2
- import { type Context, Hono } from 'hono';
3
- /**
4
- * Create a mock server instance
5
- */
6
- export declare function createMockServer(options?: {
7
- specification: string | Record<string, any>;
8
- onRequest?: (data: {
9
- context: Context;
10
- operation: Operation;
11
- }) => void;
12
- }): Promise<Hono<import("hono").Env, import("hono/types").BlankSchema, "/">>;
@@ -1,97 +0,0 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- var __generator = (this && this.__generator) || function (thisArg, body) {
11
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
- function verb(n) { return function (v) { return step([n, v]); }; }
14
- function step(op) {
15
- if (f) throw new TypeError("Generator is already executing.");
16
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
17
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
- if (y = 0, t) op = [op[0] & 2, t.value];
19
- switch (op[0]) {
20
- case 0: case 1: t = op; break;
21
- case 4: _.label++; return { value: op[1], done: false };
22
- case 5: _.label++; y = op[1]; op = [0]; continue;
23
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
- default:
25
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
- if (t[2]) _.ops.pop();
30
- _.trys.pop(); continue;
31
- }
32
- op = body.call(thisArg, _);
33
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
- }
36
- };
37
- import { getExampleFromSchema } from '@scalar/oas-utils';
38
- import { openapi } from '@scalar/openapi-parser';
39
- import { Hono } from 'hono';
40
- import { routeFromPath } from './utils';
41
- /**
42
- * Create a mock server instance
43
- */
44
- export function createMockServer(options) {
45
- return __awaiter(this, void 0, void 0, function () {
46
- var app, result;
47
- var _a;
48
- return __generator(this, function (_b) {
49
- switch (_b.label) {
50
- case 0:
51
- app = new Hono();
52
- return [4 /*yield*/, openapi().load(options.specification).resolve()
53
- // OpenAPI file
54
- ];
55
- case 1:
56
- result = _b.sent();
57
- // OpenAPI file
58
- app.get('/openapi.json', function (c) {
59
- if (!(options === null || options === void 0 ? void 0 : options.specification)) {
60
- return c.text('Not found', 404);
61
- }
62
- return c.json(options.specification);
63
- });
64
- // Paths
65
- Object.keys((_a = result.schema.paths) !== null && _a !== void 0 ? _a : {}).forEach(function (path) {
66
- // Request methods
67
- Object.keys(result.schema.paths[path]).forEach(function (method) {
68
- var route = routeFromPath(path);
69
- // Route
70
- app[method](route, function (c) {
71
- var _a, _b;
72
- // Call onRequest callback
73
- if (options === null || options === void 0 ? void 0 : options.onRequest) {
74
- options.onRequest({
75
- context: c,
76
- operation: result.schema.paths[path][method],
77
- });
78
- }
79
- // Response
80
- var operation = result.schema.paths[path][method];
81
- var jsonResponseConfiguration = (_b = (_a = operation.responses) === null || _a === void 0 ? void 0 : _a['200']) === null || _b === void 0 ? void 0 : _b.content['application/json'];
82
- var response = (jsonResponseConfiguration === null || jsonResponseConfiguration === void 0 ? void 0 : jsonResponseConfiguration.example)
83
- ? jsonResponseConfiguration.example
84
- : (jsonResponseConfiguration === null || jsonResponseConfiguration === void 0 ? void 0 : jsonResponseConfiguration.schema)
85
- ? getExampleFromSchema(jsonResponseConfiguration.schema, {
86
- emptyString: '…',
87
- })
88
- : null;
89
- return c.json(response);
90
- });
91
- });
92
- });
93
- return [2 /*return*/, app];
94
- }
95
- });
96
- });
97
- }
@@ -1 +0,0 @@
1
- export {};