@byyuurin/nitro-openapi 0.0.6 → 0.0.7

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 CHANGED
@@ -55,16 +55,16 @@ Or custom register
55
55
 
56
56
  ```ts
57
57
  // utils/swagger.ts
58
- import type { PathOperationItem, PathOperationMethod } from '@byyuurin/nitro-openapi'
58
+ import type { OperationType, PathOperation } from '@byyuurin/nitro-openapi'
59
59
  import type { InternalApi } from 'nitropack'
60
60
  import type { configExtends } from '../plugins/swagger'
61
61
  import { register } from '../plugins/swagger'
62
62
 
63
- type RouteMeta = PathOperationItem<typeof configExtends> & {
64
- method?: PathOperationMethod
63
+ type RouteMeta = OperationType<typeof configExtends> & {
64
+ method?: keyof PathOperation
65
65
  }
66
66
 
67
- export function defineRouteMeta(
67
+ export function defineApiRouteMeta(
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
- defineRouteMeta('/api/...', {
81
+ defineApiRouteMeta('/api/...', {
82
82
  /* ... */
83
83
  })
84
84
 
package/dist/index.cjs CHANGED
@@ -78,82 +78,54 @@ function normalizeSchema(obj) {
78
78
  return obj;
79
79
  }
80
80
 
81
- function resolveSchemaObject(value, options = {}) {
82
- const { allowExample = true, ...defaults } = options || {};
83
- const resolveResult = (obj, example) => {
84
- if (allowExample && example != null)
85
- obj.example = example;
86
- return { ...defaults, ...obj };
81
+ function toExampleSchema(example, description, options = {}) {
82
+ const { withExample = true, ...overrides } = options;
83
+ const createSchemaObject = (schema2) => {
84
+ const merged = {
85
+ ...schema2,
86
+ ...typeof description === "string" && description !== "" ? { description } : {},
87
+ ...withExample ? { example } : {},
88
+ ...overrides
89
+ };
90
+ return merged;
87
91
  };
88
- if (Array.isArray(value)) {
89
- return resolveResult(
90
- {
91
- type: "array",
92
- items: resolveSchemaObject(value[0], { allowExample: false })
93
- },
94
- value
95
- );
92
+ let schema = createSchemaObject({
93
+ type: "null"
94
+ });
95
+ if (typeof example === "number") {
96
+ schema = createSchemaObject({
97
+ type: "number"
98
+ });
96
99
  }
97
- const _type = typeof value;
98
- switch (_type) {
99
- case "function":
100
- case "symbol":
101
- case "undefined":
102
- return { type: "null" };
103
- case "object":
104
- return resolveResult(
105
- {
106
- type: "object",
107
- properties: Object.fromEntries(Object.entries(value).map(([k, v]) => [
108
- k,
109
- resolveSchemaObject(v, {
110
- allowExample: false
111
- })
112
- ]))
113
- },
114
- value
115
- );
116
- case "bigint":
117
- return resolveResult(
118
- { type: "integer" },
119
- value
120
- );
121
- case "string":
122
- return resolveResult(
123
- { type: "string" },
124
- value || null
125
- );
126
- default:
127
- return resolveResult(
128
- { type: _type },
129
- value
130
- );
100
+ if (typeof example === "string") {
101
+ schema = createSchemaObject({
102
+ type: "string"
103
+ });
131
104
  }
132
- }
133
- function toExampleSchema(example, description, options) {
134
- if (typeof example !== "object") {
135
- return resolveSchemaObject(
136
- example,
137
- typeof description === "string" ? { ...options, description } : {}
138
- );
105
+ if (typeof example === "boolean") {
106
+ schema = createSchemaObject({
107
+ type: "boolean"
108
+ });
139
109
  }
140
- if (Array.isArray(example)) {
141
- if (typeof description === "string")
142
- return resolveSchemaObject(example, { ...options, description });
143
- const schema2 = resolveSchemaObject(example, { allowExample: false });
144
- schema2.items = toExampleSchema(example[0], description, options);
145
- return schema2;
110
+ if (typeof example === "object" && example !== null) {
111
+ if (Array.isArray(example)) {
112
+ const value = typeof example[0] === "object" ? defu.defu({}, ...example) : example[0];
113
+ schema = createSchemaObject({
114
+ type: "array",
115
+ items: toExampleSchema(value, typeof description === "string" ? "" : description, { withExample: false })
116
+ });
117
+ } else {
118
+ schema = createSchemaObject({
119
+ type: "object",
120
+ properties: Object.fromEntries(Object.entries(example).map(([k, v]) => {
121
+ const _description = typeof description === "object" ? description[k] : description;
122
+ return [k, toExampleSchema(v, _description, { withExample: false })];
123
+ }))
124
+ });
125
+ }
146
126
  }
147
- if (typeof description === "string")
148
- return resolveSchemaObject(example, { ...options, description });
149
- const schema = resolveSchemaObject(example, options);
150
- schema.properties = Object.fromEntries(Object.entries(schema.properties).map(([p, item]) => [p, {
151
- ...item,
152
- ...typeof description === "object" ? { description: description?.[p] } : {}
153
- }]));
154
127
  return schema;
155
128
  }
156
129
 
157
130
  exports.createOpenApiRegister = createOpenApiRegister;
158
- exports.resolveSchemaObject = resolveSchemaObject;
159
131
  exports.toExampleSchema = toExampleSchema;
package/dist/index.d.cts CHANGED
@@ -1,6 +1,14 @@
1
- import * as openapi_typescript from 'openapi-typescript';
2
- import { ReferenceObject, SchemaObject, PathItemObject, OperationObject, SecurityRequirementObject, OpenAPI3 } from 'openapi-typescript';
1
+ import { NumberSubtype, StringSubtype, BooleanSubtype, ArraySubtype, ObjectSubtype, NullSubtype, SchemaObject, ReferenceObject, PathItemObject, OperationObject, SecurityRequirementObject, OpenAPI3 } from 'openapi-typescript';
3
2
 
3
+ type SchemaSubType<T> = T extends number ? NumberSubtype : T extends string ? StringSubtype : T extends boolean ? BooleanSubtype : T extends Array<any> ? ArraySubtype : T extends Record<string, any> ? ObjectSubtype : NullSubtype;
4
+ type SchemaObjectType<T> = Omit<SchemaObject, 'example'> & SchemaSubType<T> & {
5
+ example?: T;
6
+ };
7
+ type ExampleDescription<T> = T extends (infer V)[] ? SchemaObjectType<V> extends ObjectSubtype ? ({
8
+ [Key in keyof V]?: string;
9
+ } | string) : string : SchemaObjectType<T> extends ObjectSubtype ? {
10
+ [Key in keyof T]?: string;
11
+ } : string;
4
12
  type ReferenceType<T extends ApiRegisterOptions> = T extends {
5
13
  components: infer C;
6
14
  } ? {
@@ -41,32 +49,8 @@ declare function createOpenApiRegister<T extends ApiRegisterOptions = ApiRegiste
41
49
  merge: (config: Partial<OpenAPI3>) => Partial<OpenAPI3>;
42
50
  };
43
51
 
44
- type SchemaObjectOptions = SchemaObject & {
45
- allowExample?: boolean;
46
- };
47
- declare function resolveSchemaObject(value: any, options?: SchemaObjectOptions): SchemaObject;
48
- type ExampleDescription<ExampleT> = MaybeValueOrObject<ExampleT, string>;
49
- declare function toExampleSchema<T = any>(example: T, description?: ExampleDescription<T>, options?: SchemaObject): {
50
- [key: `x-${string}`]: any;
51
- discriminator?: openapi_typescript.DiscriminatorObject | undefined;
52
- xml?: openapi_typescript.XMLObject | undefined;
53
- externalDocs?: openapi_typescript.ExternalDocumentationObject | undefined;
54
- example?: any;
55
- title?: string | undefined;
56
- description?: string | undefined;
57
- $comment?: string | undefined;
58
- deprecated?: boolean | undefined;
59
- readOnly?: boolean | undefined;
60
- writeOnly?: boolean | undefined;
61
- enum?: unknown[] | undefined;
62
- const?: unknown;
63
- default?: unknown;
64
- format?: string | undefined;
65
- nullable?: boolean | undefined;
66
- oneOf?: (openapi_typescript.ReferenceObject | SchemaObject)[] | undefined;
67
- allOf?: (openapi_typescript.ReferenceObject | SchemaObject)[] | undefined;
68
- anyOf?: (openapi_typescript.ReferenceObject | SchemaObject)[] | undefined;
69
- required?: string[] | undefined;
70
- };
52
+ declare function toExampleSchema<T>(example: T, description: ExampleDescription<T> | undefined, options?: Partial<Omit<SchemaObjectType<T>, 'type' | 'example'> & {
53
+ withExample?: boolean;
54
+ }>): SchemaObjectType<T>;
71
55
 
72
- export { type ApiRegisterOptions, type MaybeReference, type MaybeValueOrObject, type OperationType, type PathOperation, type ReferenceType, type ReplaceRef, type SchemaType, type WithTypedRef, createOpenApiRegister, resolveSchemaObject, toExampleSchema };
56
+ export { type ApiRegisterOptions, type ExampleDescription, type MaybeReference, type MaybeValueOrObject, type OperationType, type PathOperation, type ReferenceType, type ReplaceRef, type SchemaObjectType, type SchemaSubType, type SchemaType, type WithTypedRef, createOpenApiRegister, toExampleSchema };
package/dist/index.d.mts CHANGED
@@ -1,6 +1,14 @@
1
- import * as openapi_typescript from 'openapi-typescript';
2
- import { ReferenceObject, SchemaObject, PathItemObject, OperationObject, SecurityRequirementObject, OpenAPI3 } from 'openapi-typescript';
1
+ import { NumberSubtype, StringSubtype, BooleanSubtype, ArraySubtype, ObjectSubtype, NullSubtype, SchemaObject, ReferenceObject, PathItemObject, OperationObject, SecurityRequirementObject, OpenAPI3 } from 'openapi-typescript';
3
2
 
3
+ type SchemaSubType<T> = T extends number ? NumberSubtype : T extends string ? StringSubtype : T extends boolean ? BooleanSubtype : T extends Array<any> ? ArraySubtype : T extends Record<string, any> ? ObjectSubtype : NullSubtype;
4
+ type SchemaObjectType<T> = Omit<SchemaObject, 'example'> & SchemaSubType<T> & {
5
+ example?: T;
6
+ };
7
+ type ExampleDescription<T> = T extends (infer V)[] ? SchemaObjectType<V> extends ObjectSubtype ? ({
8
+ [Key in keyof V]?: string;
9
+ } | string) : string : SchemaObjectType<T> extends ObjectSubtype ? {
10
+ [Key in keyof T]?: string;
11
+ } : string;
4
12
  type ReferenceType<T extends ApiRegisterOptions> = T extends {
5
13
  components: infer C;
6
14
  } ? {
@@ -41,32 +49,8 @@ declare function createOpenApiRegister<T extends ApiRegisterOptions = ApiRegiste
41
49
  merge: (config: Partial<OpenAPI3>) => Partial<OpenAPI3>;
42
50
  };
43
51
 
44
- type SchemaObjectOptions = SchemaObject & {
45
- allowExample?: boolean;
46
- };
47
- declare function resolveSchemaObject(value: any, options?: SchemaObjectOptions): SchemaObject;
48
- type ExampleDescription<ExampleT> = MaybeValueOrObject<ExampleT, string>;
49
- declare function toExampleSchema<T = any>(example: T, description?: ExampleDescription<T>, options?: SchemaObject): {
50
- [key: `x-${string}`]: any;
51
- discriminator?: openapi_typescript.DiscriminatorObject | undefined;
52
- xml?: openapi_typescript.XMLObject | undefined;
53
- externalDocs?: openapi_typescript.ExternalDocumentationObject | undefined;
54
- example?: any;
55
- title?: string | undefined;
56
- description?: string | undefined;
57
- $comment?: string | undefined;
58
- deprecated?: boolean | undefined;
59
- readOnly?: boolean | undefined;
60
- writeOnly?: boolean | undefined;
61
- enum?: unknown[] | undefined;
62
- const?: unknown;
63
- default?: unknown;
64
- format?: string | undefined;
65
- nullable?: boolean | undefined;
66
- oneOf?: (openapi_typescript.ReferenceObject | SchemaObject)[] | undefined;
67
- allOf?: (openapi_typescript.ReferenceObject | SchemaObject)[] | undefined;
68
- anyOf?: (openapi_typescript.ReferenceObject | SchemaObject)[] | undefined;
69
- required?: string[] | undefined;
70
- };
52
+ declare function toExampleSchema<T>(example: T, description: ExampleDescription<T> | undefined, options?: Partial<Omit<SchemaObjectType<T>, 'type' | 'example'> & {
53
+ withExample?: boolean;
54
+ }>): SchemaObjectType<T>;
71
55
 
72
- export { type ApiRegisterOptions, type MaybeReference, type MaybeValueOrObject, type OperationType, type PathOperation, type ReferenceType, type ReplaceRef, type SchemaType, type WithTypedRef, createOpenApiRegister, resolveSchemaObject, toExampleSchema };
56
+ export { type ApiRegisterOptions, type ExampleDescription, type MaybeReference, type MaybeValueOrObject, type OperationType, type PathOperation, type ReferenceType, type ReplaceRef, type SchemaObjectType, type SchemaSubType, type SchemaType, type WithTypedRef, createOpenApiRegister, toExampleSchema };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,14 @@
1
- import * as openapi_typescript from 'openapi-typescript';
2
- import { ReferenceObject, SchemaObject, PathItemObject, OperationObject, SecurityRequirementObject, OpenAPI3 } from 'openapi-typescript';
1
+ import { NumberSubtype, StringSubtype, BooleanSubtype, ArraySubtype, ObjectSubtype, NullSubtype, SchemaObject, ReferenceObject, PathItemObject, OperationObject, SecurityRequirementObject, OpenAPI3 } from 'openapi-typescript';
3
2
 
3
+ type SchemaSubType<T> = T extends number ? NumberSubtype : T extends string ? StringSubtype : T extends boolean ? BooleanSubtype : T extends Array<any> ? ArraySubtype : T extends Record<string, any> ? ObjectSubtype : NullSubtype;
4
+ type SchemaObjectType<T> = Omit<SchemaObject, 'example'> & SchemaSubType<T> & {
5
+ example?: T;
6
+ };
7
+ type ExampleDescription<T> = T extends (infer V)[] ? SchemaObjectType<V> extends ObjectSubtype ? ({
8
+ [Key in keyof V]?: string;
9
+ } | string) : string : SchemaObjectType<T> extends ObjectSubtype ? {
10
+ [Key in keyof T]?: string;
11
+ } : string;
4
12
  type ReferenceType<T extends ApiRegisterOptions> = T extends {
5
13
  components: infer C;
6
14
  } ? {
@@ -41,32 +49,8 @@ declare function createOpenApiRegister<T extends ApiRegisterOptions = ApiRegiste
41
49
  merge: (config: Partial<OpenAPI3>) => Partial<OpenAPI3>;
42
50
  };
43
51
 
44
- type SchemaObjectOptions = SchemaObject & {
45
- allowExample?: boolean;
46
- };
47
- declare function resolveSchemaObject(value: any, options?: SchemaObjectOptions): SchemaObject;
48
- type ExampleDescription<ExampleT> = MaybeValueOrObject<ExampleT, string>;
49
- declare function toExampleSchema<T = any>(example: T, description?: ExampleDescription<T>, options?: SchemaObject): {
50
- [key: `x-${string}`]: any;
51
- discriminator?: openapi_typescript.DiscriminatorObject | undefined;
52
- xml?: openapi_typescript.XMLObject | undefined;
53
- externalDocs?: openapi_typescript.ExternalDocumentationObject | undefined;
54
- example?: any;
55
- title?: string | undefined;
56
- description?: string | undefined;
57
- $comment?: string | undefined;
58
- deprecated?: boolean | undefined;
59
- readOnly?: boolean | undefined;
60
- writeOnly?: boolean | undefined;
61
- enum?: unknown[] | undefined;
62
- const?: unknown;
63
- default?: unknown;
64
- format?: string | undefined;
65
- nullable?: boolean | undefined;
66
- oneOf?: (openapi_typescript.ReferenceObject | SchemaObject)[] | undefined;
67
- allOf?: (openapi_typescript.ReferenceObject | SchemaObject)[] | undefined;
68
- anyOf?: (openapi_typescript.ReferenceObject | SchemaObject)[] | undefined;
69
- required?: string[] | undefined;
70
- };
52
+ declare function toExampleSchema<T>(example: T, description: ExampleDescription<T> | undefined, options?: Partial<Omit<SchemaObjectType<T>, 'type' | 'example'> & {
53
+ withExample?: boolean;
54
+ }>): SchemaObjectType<T>;
71
55
 
72
- export { type ApiRegisterOptions, type MaybeReference, type MaybeValueOrObject, type OperationType, type PathOperation, type ReferenceType, type ReplaceRef, type SchemaType, type WithTypedRef, createOpenApiRegister, resolveSchemaObject, toExampleSchema };
56
+ export { type ApiRegisterOptions, type ExampleDescription, type MaybeReference, type MaybeValueOrObject, type OperationType, type PathOperation, type ReferenceType, type ReplaceRef, type SchemaObjectType, type SchemaSubType, type SchemaType, type WithTypedRef, createOpenApiRegister, toExampleSchema };
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import defu from 'defu';
1
+ import defu, { defu as defu$1 } from 'defu';
2
2
 
3
3
  function createOpenApiRegister(options) {
4
4
  const { paths = {}, components = {}, security = [], servers = [], info, tags = [] } = options;
@@ -72,80 +72,53 @@ function normalizeSchema(obj) {
72
72
  return obj;
73
73
  }
74
74
 
75
- function resolveSchemaObject(value, options = {}) {
76
- const { allowExample = true, ...defaults } = options || {};
77
- const resolveResult = (obj, example) => {
78
- if (allowExample && example != null)
79
- obj.example = example;
80
- return { ...defaults, ...obj };
75
+ function toExampleSchema(example, description, options = {}) {
76
+ const { withExample = true, ...overrides } = options;
77
+ const createSchemaObject = (schema2) => {
78
+ const merged = {
79
+ ...schema2,
80
+ ...typeof description === "string" && description !== "" ? { description } : {},
81
+ ...withExample ? { example } : {},
82
+ ...overrides
83
+ };
84
+ return merged;
81
85
  };
82
- if (Array.isArray(value)) {
83
- return resolveResult(
84
- {
85
- type: "array",
86
- items: resolveSchemaObject(value[0], { allowExample: false })
87
- },
88
- value
89
- );
86
+ let schema = createSchemaObject({
87
+ type: "null"
88
+ });
89
+ if (typeof example === "number") {
90
+ schema = createSchemaObject({
91
+ type: "number"
92
+ });
90
93
  }
91
- const _type = typeof value;
92
- switch (_type) {
93
- case "function":
94
- case "symbol":
95
- case "undefined":
96
- return { type: "null" };
97
- case "object":
98
- return resolveResult(
99
- {
100
- type: "object",
101
- properties: Object.fromEntries(Object.entries(value).map(([k, v]) => [
102
- k,
103
- resolveSchemaObject(v, {
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
- );
94
+ if (typeof example === "string") {
95
+ schema = createSchemaObject({
96
+ type: "string"
97
+ });
125
98
  }
126
- }
127
- function toExampleSchema(example, description, options) {
128
- if (typeof example !== "object") {
129
- return resolveSchemaObject(
130
- example,
131
- typeof description === "string" ? { ...options, description } : {}
132
- );
99
+ if (typeof example === "boolean") {
100
+ schema = createSchemaObject({
101
+ type: "boolean"
102
+ });
133
103
  }
134
- if (Array.isArray(example)) {
135
- if (typeof description === "string")
136
- return resolveSchemaObject(example, { ...options, description });
137
- const schema2 = resolveSchemaObject(example, { allowExample: false });
138
- schema2.items = toExampleSchema(example[0], description, options);
139
- return schema2;
104
+ if (typeof example === "object" && example !== null) {
105
+ if (Array.isArray(example)) {
106
+ const value = typeof example[0] === "object" ? defu$1({}, ...example) : example[0];
107
+ schema = createSchemaObject({
108
+ type: "array",
109
+ items: toExampleSchema(value, typeof description === "string" ? "" : description, { withExample: false })
110
+ });
111
+ } else {
112
+ schema = createSchemaObject({
113
+ type: "object",
114
+ properties: Object.fromEntries(Object.entries(example).map(([k, v]) => {
115
+ const _description = typeof description === "object" ? description[k] : description;
116
+ return [k, toExampleSchema(v, _description, { withExample: false })];
117
+ }))
118
+ });
119
+ }
140
120
  }
141
- if (typeof description === "string")
142
- return resolveSchemaObject(example, { ...options, description });
143
- const schema = resolveSchemaObject(example, options);
144
- schema.properties = Object.fromEntries(Object.entries(schema.properties).map(([p, item]) => [p, {
145
- ...item,
146
- ...typeof description === "object" ? { description: description?.[p] } : {}
147
- }]));
148
121
  return schema;
149
122
  }
150
123
 
151
- export { createOpenApiRegister, resolveSchemaObject, toExampleSchema };
124
+ export { createOpenApiRegister, toExampleSchema };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@byyuurin/nitro-openapi",
3
3
  "type": "module",
4
- "version": "0.0.6",
4
+ "version": "0.0.7",
5
5
  "private": false,
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/byyuurin/nitro-openapi",
@@ -27,10 +27,12 @@
27
27
  "openapi-typescript": "^6.7.6"
28
28
  },
29
29
  "devDependencies": {
30
- "@byyuurin/eslint-config": "^1.2.0",
31
- "bumpp": "^9.4.1",
32
- "eslint-plugin-format": "^0.1.1",
33
- "nitropack": "^2.9.6",
30
+ "@byyuurin/eslint-config": "^1.6.3",
31
+ "bumpp": "^9.8.1",
32
+ "eslint": "^9.14.0",
33
+ "eslint-plugin-format": "^0.1.2",
34
+ "nitropack": "^2.10.4",
35
+ "typescript": "^5.6.3",
34
36
  "unbuild": "^2.0.0",
35
37
  "vitest": "^1.6.0"
36
38
  },