@elysiajs/openapi 1.3.2
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/LICENSE +7 -0
- package/README.md +101 -0
- package/bun.lock +537 -0
- package/bunfig.toml +2 -0
- package/dist/cjs/gen/index.js +127 -0
- package/dist/cjs/index.d.ts +35 -0
- package/dist/cjs/index.js +639 -0
- package/dist/cjs/openapi.d.ts +25 -0
- package/dist/cjs/openapi.js +285 -0
- package/dist/cjs/scalar/index.js +183 -0
- package/dist/cjs/swagger/index.js +129 -0
- package/dist/cjs/swagger/types.js +18 -0
- package/dist/cjs/types.d.ts +137 -0
- package/dist/cjs/types.js +18 -0
- package/dist/gen/index.d.ts +36 -0
- package/dist/gen/index.mjs +108 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.mjs +612 -0
- package/dist/openapi.d.ts +25 -0
- package/dist/openapi.mjs +257 -0
- package/dist/scalar/index.d.ts +3 -0
- package/dist/scalar/index.mjs +158 -0
- package/dist/swagger/index.d.ts +7 -0
- package/dist/swagger/index.mjs +103 -0
- package/dist/swagger/types.d.ts +274 -0
- package/dist/swagger/types.mjs +0 -0
- package/dist/types.d.ts +137 -0
- package/dist/types.mjs +0 -0
- package/package.json +92 -0
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/gen/index.ts
|
|
21
|
+
var gen_exports = {};
|
|
22
|
+
__export(gen_exports, {
|
|
23
|
+
fromTypes: () => fromTypes
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(gen_exports);
|
|
26
|
+
var import_fs = require("fs");
|
|
27
|
+
var import_typemap = require("@sinclair/typemap");
|
|
28
|
+
var import_os = require("os");
|
|
29
|
+
var import_path = require("path");
|
|
30
|
+
var import_child_process = require("child_process");
|
|
31
|
+
var matchRoute = /: Elysia<(.*)>/gs;
|
|
32
|
+
var matchStatus = /(\d{3}):/gs;
|
|
33
|
+
var wrapStatusInQuote = (value) => value.replace(matchStatus, '"$1":');
|
|
34
|
+
var exec = (command, cwd) => (0, import_child_process.spawnSync)(command, {
|
|
35
|
+
shell: true,
|
|
36
|
+
cwd,
|
|
37
|
+
stdio: "inherit"
|
|
38
|
+
});
|
|
39
|
+
var fromTypes = (targetFilePath, {
|
|
40
|
+
tsconfigPath = "tsconfig.json",
|
|
41
|
+
instanceName,
|
|
42
|
+
projectRoot = process.cwd()
|
|
43
|
+
} = {}) => () => {
|
|
44
|
+
if (!targetFilePath.endsWith(".ts") && !targetFilePath.endsWith(".tsx"))
|
|
45
|
+
throw new Error("Only .ts files are supported");
|
|
46
|
+
const tmpRoot = (0, import_path.join)((0, import_os.tmpdir)(), ".ElysiaAutoOpenAPI");
|
|
47
|
+
if ((0, import_fs.existsSync)(tmpRoot))
|
|
48
|
+
(0, import_fs.rmSync)(tmpRoot, { recursive: true, force: true });
|
|
49
|
+
(0, import_fs.mkdirSync)(tmpRoot, { recursive: true });
|
|
50
|
+
const extendsRef = (0, import_fs.existsSync)((0, import_path.join)(projectRoot, "tsconfig.json")) ? `"extends": "${(0, import_path.join)(projectRoot, "tsconfig.json")}",` : "";
|
|
51
|
+
if (!(0, import_path.join)(projectRoot, targetFilePath))
|
|
52
|
+
throw new Error("Target file does not exist");
|
|
53
|
+
(0, import_fs.writeFileSync)(
|
|
54
|
+
(0, import_path.join)(tmpRoot, tsconfigPath),
|
|
55
|
+
`{
|
|
56
|
+
${extendsRef}
|
|
57
|
+
"compilerOptions": {
|
|
58
|
+
"lib": ["ESNext"],
|
|
59
|
+
"module": "ESNext",
|
|
60
|
+
"noEmit": false,
|
|
61
|
+
"moduleResolution": "bundler",
|
|
62
|
+
"skipLibCheck": true,
|
|
63
|
+
"skipDefaultLibCheck": true,
|
|
64
|
+
"emitDeclarationOnly": true,
|
|
65
|
+
"outDir": "./dist"
|
|
66
|
+
},
|
|
67
|
+
"include": ["${(0, import_path.join)(projectRoot, targetFilePath)}"]
|
|
68
|
+
}`
|
|
69
|
+
);
|
|
70
|
+
exec(`tsc`, tmpRoot);
|
|
71
|
+
try {
|
|
72
|
+
const declaration = (0, import_fs.readFileSync)(
|
|
73
|
+
(0, import_path.join)(
|
|
74
|
+
tmpRoot,
|
|
75
|
+
"dist",
|
|
76
|
+
targetFilePath.replace(/.tsx$/, ".ts").replace(/.ts$/, ".d.ts")
|
|
77
|
+
),
|
|
78
|
+
"utf8"
|
|
79
|
+
);
|
|
80
|
+
if ((0, import_fs.existsSync)(tmpRoot))
|
|
81
|
+
(0, import_fs.rmSync)(tmpRoot, { recursive: true, force: true });
|
|
82
|
+
let instance = declaration.match(
|
|
83
|
+
instanceName ? new RegExp(`${instanceName}: Elysia<(.*)`, "gs") : matchRoute
|
|
84
|
+
)?.[0];
|
|
85
|
+
if (!instance) return;
|
|
86
|
+
for (let i = 0; i < 3; i++)
|
|
87
|
+
instance = instance.slice(instance.indexOf("}, {", 3));
|
|
88
|
+
const routesString = wrapStatusInQuote(instance).slice(
|
|
89
|
+
3,
|
|
90
|
+
instance.indexOf("}, {", 3)
|
|
91
|
+
) + "}\n}\n";
|
|
92
|
+
const routes = {};
|
|
93
|
+
for (let route of routesString.slice(1).split("} & {")) {
|
|
94
|
+
route = "{" + route + "}";
|
|
95
|
+
let schema = (0, import_typemap.TypeBox)(route);
|
|
96
|
+
if (schema.type !== "object") continue;
|
|
97
|
+
const paths = [];
|
|
98
|
+
while (true) {
|
|
99
|
+
const keys = Object.keys(schema.properties);
|
|
100
|
+
if (!keys.length || keys.length > 1) break;
|
|
101
|
+
paths.push(keys[0]);
|
|
102
|
+
schema = schema.properties[keys[0]];
|
|
103
|
+
if (!schema?.properties) break;
|
|
104
|
+
}
|
|
105
|
+
const method = paths.pop();
|
|
106
|
+
const path = "/" + paths.join("/");
|
|
107
|
+
schema = schema.properties;
|
|
108
|
+
if (schema?.response?.type === "object") {
|
|
109
|
+
const responseSchema = {};
|
|
110
|
+
for (const key in schema.response.properties)
|
|
111
|
+
responseSchema[key] = schema.response.properties[key];
|
|
112
|
+
schema.response = responseSchema;
|
|
113
|
+
}
|
|
114
|
+
if (!routes[path]) routes[path] = {};
|
|
115
|
+
routes[path][method.toLowerCase()] = schema;
|
|
116
|
+
}
|
|
117
|
+
return routes;
|
|
118
|
+
} catch (error) {
|
|
119
|
+
console.warn("Failed to generate OpenAPI schema");
|
|
120
|
+
console.warn(error);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
125
|
+
0 && (module.exports = {
|
|
126
|
+
fromTypes
|
|
127
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Elysia } from 'elysia';
|
|
2
|
+
import type { ElysiaOpenAPIConfig, OpenAPIProvider } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Plugin for [elysia](https://github.com/elysiajs/elysia) that auto-generate OpenAPI documentation page.
|
|
5
|
+
*
|
|
6
|
+
* @see https://github.com/elysiajs/elysia-swagger
|
|
7
|
+
*/
|
|
8
|
+
export declare const openapi: <const Enabled extends boolean = true, const Path extends string = "/openapi", const Provider extends OpenAPIProvider = "scalar">({ enabled, path, provider, specPath, documentation, exclude, swagger, scalar, references }?: ElysiaOpenAPIConfig<Enabled, Path, Provider>) => Elysia<"", {
|
|
9
|
+
decorator: {};
|
|
10
|
+
store: {};
|
|
11
|
+
derive: {};
|
|
12
|
+
resolve: {};
|
|
13
|
+
}, {
|
|
14
|
+
typebox: {};
|
|
15
|
+
error: {};
|
|
16
|
+
}, {
|
|
17
|
+
schema: {};
|
|
18
|
+
standaloneSchema: {};
|
|
19
|
+
macro: {};
|
|
20
|
+
macroFn: {};
|
|
21
|
+
parser: {};
|
|
22
|
+
}, {}, {
|
|
23
|
+
derive: {};
|
|
24
|
+
resolve: {};
|
|
25
|
+
schema: {};
|
|
26
|
+
standaloneSchema: {};
|
|
27
|
+
}, {
|
|
28
|
+
derive: {};
|
|
29
|
+
resolve: {};
|
|
30
|
+
schema: {};
|
|
31
|
+
standaloneSchema: {};
|
|
32
|
+
}>;
|
|
33
|
+
export { toOpenAPISchema, withHeaders } from './openapi';
|
|
34
|
+
export type { ElysiaOpenAPIConfig };
|
|
35
|
+
export default openapi;
|