@byyuurin/nitro-openapi 0.0.7 → 0.1.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/LICENSE CHANGED
@@ -1,21 +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.
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 CHANGED
@@ -25,8 +25,8 @@ Create nitor plugin:
25
25
 
26
26
  ```ts
27
27
  // plugins/swagger.ts
28
+ import type { OpenAPI3 } from '@byyuurin/nitro-openapi'
28
29
  import { createOpenApiRegister } from '@byyuurin/nitro-openapi'
29
- import type { OpenAPI3 } from 'openapi-typescript'
30
30
 
31
31
  export const { register, merge, configExtends } = createOpenApiRegister({
32
32
  // ...
@@ -64,7 +64,7 @@ type RouteMeta = OperationType<typeof configExtends> & {
64
64
  method?: keyof PathOperation
65
65
  }
66
66
 
67
- export function defineApiRouteMeta(
67
+ export function defineApiResponse(
68
68
  route: keyof InternalApi,
69
69
  meta: RouteMeta,
70
70
  ) {
@@ -78,7 +78,7 @@ Then use the register
78
78
  ```ts
79
79
  // route/**/*.ts
80
80
 
81
- defineApiRouteMeta('/api/...', {
81
+ defineApiResponse('/api/...', {
82
82
  /* ... */
83
83
  })
84
84
 
package/dist/index.cjs CHANGED
@@ -10,11 +10,11 @@ function createOpenApiRegister(options) {
10
10
  const { paths = {}, components = {}, security = [], servers = [], info, tags = [] } = options;
11
11
  const defineOperation = (operation) => operation;
12
12
  function register(route, routeOperation, method = "get") {
13
- const _route = normalizeRoute(route);
14
- paths[_route] = defu__default(
15
- { [method]: routeOperation },
16
- paths[_route]
17
- );
13
+ const path = normalizeRoute(route);
14
+ paths[path] = {
15
+ ...paths[path],
16
+ [method]: routeOperation
17
+ };
18
18
  }
19
19
  function merge(config) {
20
20
  return mergeConfig(
@@ -49,33 +49,31 @@ function normalizeRoute(_route) {
49
49
  return route;
50
50
  }
51
51
  function mergeConfig(defaults, appends) {
52
- const methods = /* @__PURE__ */ new Set(["delete", "get", "head", "options", "patch", "post", "put", "trace"]);
53
52
  const { info } = appends;
54
- const { paths = {} } = defaults;
53
+ const { openapi, servers, security, tags, paths = {} } = defaults;
55
54
  for (const path in paths) {
56
55
  const operations = paths[path];
56
+ const operationsAppend = appends.paths?.[path] ?? {};
57
+ if ("$ref" in operationsAppend) {
58
+ paths[path] = operationsAppend;
59
+ continue;
60
+ }
57
61
  if ("$ref" in operations)
58
62
  continue;
59
- paths[path] = Object.fromEntries(Object.entries(operations).map(([key, obj]) => {
60
- if (methods.has(key))
61
- return [key, normalizeSchema(obj)];
62
- return [key, obj];
63
+ paths[path] = Object.fromEntries(Object.entries(operations).map(([method, operation]) => {
64
+ const extendOperation = operationsAppend[method] ?? {};
65
+ return [method, defu__default(operation, extendOperation)];
63
66
  }));
64
67
  }
65
- return defu__default({ info }, defaults, appends);
66
- }
67
- function normalizeSchema(obj) {
68
- if ("$ref" in obj)
69
- return obj;
70
- const { parameters = [] } = obj;
71
- obj.parameters = parameters.map((p) => {
72
- if ("$ref" in p)
73
- return p;
74
- return defu__default(p, {
75
- schema: { type: "string" }
76
- });
77
- });
78
- return obj;
68
+ return {
69
+ openapi,
70
+ info,
71
+ servers,
72
+ security,
73
+ tags,
74
+ ...defu__default(defaults, appends),
75
+ paths
76
+ };
79
77
  }
80
78
 
81
79
  function toExampleSchema(example, description, options = {}) {