@byyuurin/nitro-openapi 0.0.1
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 +21 -0
- package/README.md +1 -0
- package/dist/index.d.mts +64 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.mjs +151 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Yuurin<https://github.com/byyuurin>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @byyuurin/nitro-openapi
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import * as openapi_typescript from 'openapi-typescript';
|
|
2
|
+
import { ReferenceObject, PathItemObject, OperationObject, SecurityRequirementObject, OpenAPI3, SchemaObject } from 'openapi-typescript';
|
|
3
|
+
|
|
4
|
+
type ReferenceSchema<T> = Omit<ReferenceObject, '$ref'> & {
|
|
5
|
+
$ref: T;
|
|
6
|
+
};
|
|
7
|
+
type MaybeReference<T, R = string> = T | ReferenceSchema<R>;
|
|
8
|
+
type PathOperations = Omit<PathItemObject, 'servers' | 'parameters' | `x-${string}`>;
|
|
9
|
+
type PathOperationMethod = keyof PathOperations;
|
|
10
|
+
type PathOperationItem<T extends OpenApiRegisterConfig = Required<OpenApiRegisterConfig>> = Omit<OperationObject, 'security' | 'tags'> & {
|
|
11
|
+
tags?: T extends {
|
|
12
|
+
tags: infer Tags;
|
|
13
|
+
} ? Tags extends ({
|
|
14
|
+
name: infer Tag;
|
|
15
|
+
})[] ? Tag[] | string[] : string[] : string[];
|
|
16
|
+
security?: T extends {
|
|
17
|
+
components: infer C;
|
|
18
|
+
} ? C extends {
|
|
19
|
+
securitySchemes: infer S;
|
|
20
|
+
} ? {
|
|
21
|
+
[SecurityName in keyof S]?: string[];
|
|
22
|
+
}[] : SecurityRequirementObject[] : SecurityRequirementObject[];
|
|
23
|
+
};
|
|
24
|
+
type OpenApiRegisterConfig = Pick<Partial<OpenAPI3>, 'paths' | 'components' | 'security' | 'servers' | 'info' | 'tags'>;
|
|
25
|
+
|
|
26
|
+
declare function createOpenApiRegister<T extends OpenApiRegisterConfig = OpenApiRegisterConfig>(defaults?: T): {
|
|
27
|
+
configExtends: T;
|
|
28
|
+
register: (route: string, routeOperation: MaybeReference<PathOperationItem<T>>, method?: keyof PathOperations) => void;
|
|
29
|
+
merge: (config: Partial<OpenAPI3>) => {
|
|
30
|
+
info: openapi_typescript.InfoObject | undefined;
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
type SchemaObjectOptions = SchemaObject & {
|
|
35
|
+
allowExample?: boolean;
|
|
36
|
+
};
|
|
37
|
+
declare function resolveSchemaObject(value: any, options?: SchemaObjectOptions): SchemaObject;
|
|
38
|
+
type ExampleDescription<ExampleT> = ExampleT extends number | string | boolean ? string : ExampleT extends (infer ArrayT)[] ? string | ExampleDescription<ArrayT> : ExampleT extends Record<infer PropertyT, unknown> ? {
|
|
39
|
+
[key in PropertyT]?: string;
|
|
40
|
+
} | string : string;
|
|
41
|
+
declare function toExampleSchema<T = any>(example: T, description?: ExampleDescription<T>): {
|
|
42
|
+
[key: `x-${string}`]: any;
|
|
43
|
+
discriminator?: openapi_typescript.DiscriminatorObject | undefined;
|
|
44
|
+
xml?: openapi_typescript.XMLObject | undefined;
|
|
45
|
+
externalDocs?: openapi_typescript.ExternalDocumentationObject | undefined;
|
|
46
|
+
example?: any;
|
|
47
|
+
title?: string | undefined;
|
|
48
|
+
description?: string | undefined;
|
|
49
|
+
$comment?: string | undefined;
|
|
50
|
+
deprecated?: boolean | undefined;
|
|
51
|
+
readOnly?: boolean | undefined;
|
|
52
|
+
writeOnly?: boolean | undefined;
|
|
53
|
+
enum?: unknown[] | undefined;
|
|
54
|
+
const?: unknown;
|
|
55
|
+
default?: unknown;
|
|
56
|
+
format?: string | undefined;
|
|
57
|
+
nullable?: boolean | undefined;
|
|
58
|
+
oneOf?: (openapi_typescript.ReferenceObject | SchemaObject)[] | undefined;
|
|
59
|
+
allOf?: (openapi_typescript.ReferenceObject | SchemaObject)[] | undefined;
|
|
60
|
+
anyOf?: (openapi_typescript.ReferenceObject | SchemaObject)[] | undefined;
|
|
61
|
+
required?: string[] | undefined;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export { type MaybeReference, type OpenApiRegisterConfig, type PathOperationItem, type PathOperationMethod, type PathOperations, type ReferenceSchema, createOpenApiRegister, resolveSchemaObject, toExampleSchema };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import * as openapi_typescript from 'openapi-typescript';
|
|
2
|
+
import { ReferenceObject, PathItemObject, OperationObject, SecurityRequirementObject, OpenAPI3, SchemaObject } from 'openapi-typescript';
|
|
3
|
+
|
|
4
|
+
type ReferenceSchema<T> = Omit<ReferenceObject, '$ref'> & {
|
|
5
|
+
$ref: T;
|
|
6
|
+
};
|
|
7
|
+
type MaybeReference<T, R = string> = T | ReferenceSchema<R>;
|
|
8
|
+
type PathOperations = Omit<PathItemObject, 'servers' | 'parameters' | `x-${string}`>;
|
|
9
|
+
type PathOperationMethod = keyof PathOperations;
|
|
10
|
+
type PathOperationItem<T extends OpenApiRegisterConfig = Required<OpenApiRegisterConfig>> = Omit<OperationObject, 'security' | 'tags'> & {
|
|
11
|
+
tags?: T extends {
|
|
12
|
+
tags: infer Tags;
|
|
13
|
+
} ? Tags extends ({
|
|
14
|
+
name: infer Tag;
|
|
15
|
+
})[] ? Tag[] | string[] : string[] : string[];
|
|
16
|
+
security?: T extends {
|
|
17
|
+
components: infer C;
|
|
18
|
+
} ? C extends {
|
|
19
|
+
securitySchemes: infer S;
|
|
20
|
+
} ? {
|
|
21
|
+
[SecurityName in keyof S]?: string[];
|
|
22
|
+
}[] : SecurityRequirementObject[] : SecurityRequirementObject[];
|
|
23
|
+
};
|
|
24
|
+
type OpenApiRegisterConfig = Pick<Partial<OpenAPI3>, 'paths' | 'components' | 'security' | 'servers' | 'info' | 'tags'>;
|
|
25
|
+
|
|
26
|
+
declare function createOpenApiRegister<T extends OpenApiRegisterConfig = OpenApiRegisterConfig>(defaults?: T): {
|
|
27
|
+
configExtends: T;
|
|
28
|
+
register: (route: string, routeOperation: MaybeReference<PathOperationItem<T>>, method?: keyof PathOperations) => void;
|
|
29
|
+
merge: (config: Partial<OpenAPI3>) => {
|
|
30
|
+
info: openapi_typescript.InfoObject | undefined;
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
type SchemaObjectOptions = SchemaObject & {
|
|
35
|
+
allowExample?: boolean;
|
|
36
|
+
};
|
|
37
|
+
declare function resolveSchemaObject(value: any, options?: SchemaObjectOptions): SchemaObject;
|
|
38
|
+
type ExampleDescription<ExampleT> = ExampleT extends number | string | boolean ? string : ExampleT extends (infer ArrayT)[] ? string | ExampleDescription<ArrayT> : ExampleT extends Record<infer PropertyT, unknown> ? {
|
|
39
|
+
[key in PropertyT]?: string;
|
|
40
|
+
} | string : string;
|
|
41
|
+
declare function toExampleSchema<T = any>(example: T, description?: ExampleDescription<T>): {
|
|
42
|
+
[key: `x-${string}`]: any;
|
|
43
|
+
discriminator?: openapi_typescript.DiscriminatorObject | undefined;
|
|
44
|
+
xml?: openapi_typescript.XMLObject | undefined;
|
|
45
|
+
externalDocs?: openapi_typescript.ExternalDocumentationObject | undefined;
|
|
46
|
+
example?: any;
|
|
47
|
+
title?: string | undefined;
|
|
48
|
+
description?: string | undefined;
|
|
49
|
+
$comment?: string | undefined;
|
|
50
|
+
deprecated?: boolean | undefined;
|
|
51
|
+
readOnly?: boolean | undefined;
|
|
52
|
+
writeOnly?: boolean | undefined;
|
|
53
|
+
enum?: unknown[] | undefined;
|
|
54
|
+
const?: unknown;
|
|
55
|
+
default?: unknown;
|
|
56
|
+
format?: string | undefined;
|
|
57
|
+
nullable?: boolean | undefined;
|
|
58
|
+
oneOf?: (openapi_typescript.ReferenceObject | SchemaObject)[] | undefined;
|
|
59
|
+
allOf?: (openapi_typescript.ReferenceObject | SchemaObject)[] | undefined;
|
|
60
|
+
anyOf?: (openapi_typescript.ReferenceObject | SchemaObject)[] | undefined;
|
|
61
|
+
required?: string[] | undefined;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export { type MaybeReference, type OpenApiRegisterConfig, type PathOperationItem, type PathOperationMethod, type PathOperations, type ReferenceSchema, createOpenApiRegister, resolveSchemaObject, toExampleSchema };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import defu from 'defu';
|
|
2
|
+
|
|
3
|
+
function createOpenApiRegister(defaults = {}) {
|
|
4
|
+
const { paths = {}, components = {}, security = [], servers = [], info, tags = [] } = defaults;
|
|
5
|
+
function register(route, routeOperation, method = "get") {
|
|
6
|
+
const _route = normalizeRoute(route);
|
|
7
|
+
paths[_route] = defu(
|
|
8
|
+
{ [method]: routeOperation },
|
|
9
|
+
paths[_route]
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
function merge(config) {
|
|
13
|
+
return mergeConfig(
|
|
14
|
+
config,
|
|
15
|
+
{
|
|
16
|
+
paths,
|
|
17
|
+
components,
|
|
18
|
+
security,
|
|
19
|
+
servers,
|
|
20
|
+
info,
|
|
21
|
+
tags
|
|
22
|
+
}
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
return {
|
|
26
|
+
configExtends: {
|
|
27
|
+
paths,
|
|
28
|
+
components,
|
|
29
|
+
security,
|
|
30
|
+
servers,
|
|
31
|
+
info,
|
|
32
|
+
tags
|
|
33
|
+
},
|
|
34
|
+
register,
|
|
35
|
+
merge
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function normalizeRoute(_route) {
|
|
39
|
+
let anonymousCtr = 0;
|
|
40
|
+
const route = _route.replace(/:(\w+)/g, (_, name) => `{${name}}`).replace(/\/(\*)\//g, () => `/{param${++anonymousCtr}}/`).replace(/\*\*{/, "{").replace(/\/(\*\*)$/g, () => `/{*param${++anonymousCtr}}`);
|
|
41
|
+
return route;
|
|
42
|
+
}
|
|
43
|
+
function mergeConfig(defaults, appends) {
|
|
44
|
+
const methods = /* @__PURE__ */ new Set(["delete", "get", "head", "options", "patch", "post", "put", "trace"]);
|
|
45
|
+
const { info } = appends;
|
|
46
|
+
const { paths = {} } = defaults;
|
|
47
|
+
for (const path in paths) {
|
|
48
|
+
const operations = paths[path];
|
|
49
|
+
if ("$ref" in operations)
|
|
50
|
+
continue;
|
|
51
|
+
paths[path] = Object.fromEntries(Object.entries(operations).map(([key, obj]) => {
|
|
52
|
+
if (methods.has(key))
|
|
53
|
+
return [key, normalizeSchema(obj)];
|
|
54
|
+
return [key, obj];
|
|
55
|
+
}));
|
|
56
|
+
}
|
|
57
|
+
return defu({ info }, defaults, appends);
|
|
58
|
+
}
|
|
59
|
+
function normalizeSchema(obj) {
|
|
60
|
+
if ("$ref" in obj)
|
|
61
|
+
return obj;
|
|
62
|
+
const { parameters = [] } = obj;
|
|
63
|
+
obj.parameters = parameters.map((p) => {
|
|
64
|
+
if ("$ref" in p)
|
|
65
|
+
return p;
|
|
66
|
+
return defu(p, {
|
|
67
|
+
schema: { type: "string" }
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
return obj;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function resolveSchemaObject(value, options = {}) {
|
|
74
|
+
const { allowExample = true, ...defaults } = options;
|
|
75
|
+
const { description: _, ...inheritOptions } = options;
|
|
76
|
+
const resolveResult = (obj, example) => {
|
|
77
|
+
if (allowExample && example != null)
|
|
78
|
+
obj.example = example;
|
|
79
|
+
return { ...defaults, ...obj };
|
|
80
|
+
};
|
|
81
|
+
if (Array.isArray(value)) {
|
|
82
|
+
return resolveResult(
|
|
83
|
+
{
|
|
84
|
+
type: "array",
|
|
85
|
+
items: resolveSchemaObject(value[0], { ...inheritOptions, allowExample: false })
|
|
86
|
+
},
|
|
87
|
+
value
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
const _type = typeof value;
|
|
91
|
+
switch (_type) {
|
|
92
|
+
case "function":
|
|
93
|
+
case "symbol":
|
|
94
|
+
case "undefined":
|
|
95
|
+
return { type: "null" };
|
|
96
|
+
case "object":
|
|
97
|
+
return resolveResult(
|
|
98
|
+
{
|
|
99
|
+
type: "object",
|
|
100
|
+
properties: Object.fromEntries(Object.entries(value).map(([k, v]) => [
|
|
101
|
+
k,
|
|
102
|
+
resolveSchemaObject(v, {
|
|
103
|
+
...inheritOptions,
|
|
104
|
+
allowExample: false
|
|
105
|
+
})
|
|
106
|
+
]))
|
|
107
|
+
},
|
|
108
|
+
value
|
|
109
|
+
);
|
|
110
|
+
case "bigint":
|
|
111
|
+
return resolveResult(
|
|
112
|
+
{ type: "integer" },
|
|
113
|
+
value
|
|
114
|
+
);
|
|
115
|
+
case "string":
|
|
116
|
+
return resolveResult(
|
|
117
|
+
{ type: "string" },
|
|
118
|
+
value || null
|
|
119
|
+
);
|
|
120
|
+
default:
|
|
121
|
+
return resolveResult(
|
|
122
|
+
{ type: _type },
|
|
123
|
+
value
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function toExampleSchema(example, description) {
|
|
128
|
+
if (typeof example !== "object") {
|
|
129
|
+
return resolveSchemaObject(
|
|
130
|
+
example,
|
|
131
|
+
typeof description === "string" ? { description } : {}
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
if (Array.isArray(example)) {
|
|
135
|
+
if (typeof description === "string")
|
|
136
|
+
return resolveSchemaObject(example, { description });
|
|
137
|
+
const schema2 = resolveSchemaObject(example, { allowExample: false });
|
|
138
|
+
schema2.items = toExampleSchema(example[0], description);
|
|
139
|
+
return schema2;
|
|
140
|
+
}
|
|
141
|
+
if (typeof description === "string")
|
|
142
|
+
return resolveSchemaObject(example, { description });
|
|
143
|
+
const schema = resolveSchemaObject(example);
|
|
144
|
+
schema.properties = Object.fromEntries(Object.entries(schema.properties).map(([p, item]) => [p, {
|
|
145
|
+
...item,
|
|
146
|
+
...typeof description === "object" ? { description: description?.[p] } : {}
|
|
147
|
+
}]));
|
|
148
|
+
return schema;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export { createOpenApiRegister, resolveSchemaObject, toExampleSchema };
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@byyuurin/nitro-openapi",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"private": false,
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://github.com/byyuurin/nitro-openapi",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/byyuurin/nitro-openapi.git"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.mjs"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"main": "./dist/index.mjs",
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"files": [
|
|
21
|
+
"*.d.ts",
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "unbuild",
|
|
26
|
+
"prepare": "nitropack prepare",
|
|
27
|
+
"dev": "nitropack dev playground",
|
|
28
|
+
"dev:build": "nitropack build playground",
|
|
29
|
+
"dev:preview": "node playground/.output/server/index.mjs",
|
|
30
|
+
"lint": "eslint .",
|
|
31
|
+
"test": "vitest",
|
|
32
|
+
"release": "bumpp && pnpm publish",
|
|
33
|
+
"stub": "unbuild --stub"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"defu": "^6.1.4",
|
|
37
|
+
"openapi-typescript": "^6.7.4"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@byyuurin/eslint-config": "^1.0.1",
|
|
41
|
+
"bumpp": "^9.3.0",
|
|
42
|
+
"nitropack": "latest",
|
|
43
|
+
"unbuild": "^2.0.0",
|
|
44
|
+
"vitest": "^1.3.0"
|
|
45
|
+
},
|
|
46
|
+
"resolutions": {
|
|
47
|
+
"@byyuurin/nitro-openapi": "link:."
|
|
48
|
+
}
|
|
49
|
+
}
|