@innet/server 2.0.0-beta.15 → 2.0.0-beta.17
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/hooks/index.d.ts +1 -0
- package/hooks/index.es6.js +1 -0
- package/hooks/index.js +1 -0
- package/hooks/useData/index.d.ts +1 -0
- package/hooks/useData/index.es6.js +1 -0
- package/hooks/useData/index.js +9 -0
- package/hooks/useData/useData.d.ts +2 -0
- package/hooks/useData/useData.es6.js +23 -0
- package/hooks/useData/useData.js +27 -0
- package/index.es6.js +1 -0
- package/index.js +2 -0
- package/package.json +1 -1
- package/plugins/main/response/response.d.ts +2 -1
- package/types.d.ts +12 -0
- package/utils/generateTypes/generateTypes.es6.js +88 -63
- package/utils/generateTypes/generateTypes.js +88 -63
package/hooks/index.d.ts
CHANGED
package/hooks/index.es6.js
CHANGED
|
@@ -6,6 +6,7 @@ import './useBodyFile/index.es6.js';
|
|
|
6
6
|
import './useClientIp/index.es6.js';
|
|
7
7
|
import './useComponentName/index.es6.js';
|
|
8
8
|
import './useCookies/index.es6.js';
|
|
9
|
+
import './useData/index.es6.js';
|
|
9
10
|
import './useEffect/index.es6.js';
|
|
10
11
|
import './useEndpoint/index.es6.js';
|
|
11
12
|
import './useHeaders/index.es6.js';
|
package/hooks/index.js
CHANGED
|
@@ -8,6 +8,7 @@ require('./useBodyFile/index.js');
|
|
|
8
8
|
require('./useClientIp/index.js');
|
|
9
9
|
require('./useComponentName/index.js');
|
|
10
10
|
require('./useCookies/index.js');
|
|
11
|
+
require('./useData/index.js');
|
|
11
12
|
require('./useEffect/index.js');
|
|
12
13
|
require('./useEndpoint/index.js');
|
|
13
14
|
require('./useHeaders/index.js');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useData';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useData } from './useData.es6.js';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import '../useAction/index.es6.js';
|
|
2
|
+
import '../useEndpoint/index.es6.js';
|
|
3
|
+
import '../useThrow/index.es6.js';
|
|
4
|
+
import { useEndpoint } from '../useEndpoint/useEndpoint.es6.js';
|
|
5
|
+
import { useThrow } from '../useThrow/useThrow.es6.js';
|
|
6
|
+
import { useAction } from '../useAction/useAction.es6.js';
|
|
7
|
+
|
|
8
|
+
function useData(from, path) {
|
|
9
|
+
if (path) {
|
|
10
|
+
const endpoint = useEndpoint();
|
|
11
|
+
const endpointKey = `${endpoint.props.method.toUpperCase()}:${endpoint.props.path}`;
|
|
12
|
+
if (endpointKey !== path) {
|
|
13
|
+
useThrow(`<{type}> MUST be in <endpoint> of ${path}`);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
const action = useAction();
|
|
17
|
+
if (!action) {
|
|
18
|
+
useThrow('<{type}> MUST be in <return> or <preset>');
|
|
19
|
+
}
|
|
20
|
+
return action[from];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { useData };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
require('../useAction/index.js');
|
|
6
|
+
require('../useEndpoint/index.js');
|
|
7
|
+
require('../useThrow/index.js');
|
|
8
|
+
var useEndpoint = require('../useEndpoint/useEndpoint.js');
|
|
9
|
+
var useThrow = require('../useThrow/useThrow.js');
|
|
10
|
+
var useAction = require('../useAction/useAction.js');
|
|
11
|
+
|
|
12
|
+
function useData(from, path) {
|
|
13
|
+
if (path) {
|
|
14
|
+
const endpoint = useEndpoint.useEndpoint();
|
|
15
|
+
const endpointKey = `${endpoint.props.method.toUpperCase()}:${endpoint.props.path}`;
|
|
16
|
+
if (endpointKey !== path) {
|
|
17
|
+
useThrow.useThrow(`<{type}> MUST be in <endpoint> of ${path}`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const action = useAction.useAction();
|
|
21
|
+
if (!action) {
|
|
22
|
+
useThrow.useThrow('<{type}> MUST be in <return> or <preset>');
|
|
23
|
+
}
|
|
24
|
+
return action[from];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
exports.useData = useData;
|
package/index.es6.js
CHANGED
|
@@ -12,6 +12,7 @@ export { bodyFileContext, useBodyFile } from './hooks/useBodyFile/useBodyFile.es
|
|
|
12
12
|
export { useClientIp } from './hooks/useClientIp/useClientIp.es6.js';
|
|
13
13
|
export { useComponentName } from './hooks/useComponentName/useComponentName.es6.js';
|
|
14
14
|
export { useCookies } from './hooks/useCookies/useCookies.es6.js';
|
|
15
|
+
export { useData } from './hooks/useData/useData.es6.js';
|
|
15
16
|
export { useEffect } from './hooks/useEffect/useEffect.es6.js';
|
|
16
17
|
export { endpointContext, useEndpoint } from './hooks/useEndpoint/useEndpoint.es6.js';
|
|
17
18
|
export { useHeaders } from './hooks/useHeaders/useHeaders.es6.js';
|
package/index.js
CHANGED
|
@@ -16,6 +16,7 @@ var useBodyFile = require('./hooks/useBodyFile/useBodyFile.js');
|
|
|
16
16
|
var useClientIp = require('./hooks/useClientIp/useClientIp.js');
|
|
17
17
|
var useComponentName = require('./hooks/useComponentName/useComponentName.js');
|
|
18
18
|
var useCookies = require('./hooks/useCookies/useCookies.js');
|
|
19
|
+
var useData = require('./hooks/useData/useData.js');
|
|
19
20
|
var useEffect = require('./hooks/useEffect/useEffect.js');
|
|
20
21
|
var useEndpoint = require('./hooks/useEndpoint/useEndpoint.js');
|
|
21
22
|
var useHeaders = require('./hooks/useHeaders/useHeaders.js');
|
|
@@ -144,6 +145,7 @@ exports.useBodyFile = useBodyFile.useBodyFile;
|
|
|
144
145
|
exports.useClientIp = useClientIp.useClientIp;
|
|
145
146
|
exports.useComponentName = useComponentName.useComponentName;
|
|
146
147
|
exports.useCookies = useCookies.useCookies;
|
|
148
|
+
exports.useData = useData.useData;
|
|
147
149
|
exports.useEffect = useEffect.useEffect;
|
|
148
150
|
exports.endpointContext = useEndpoint.endpointContext;
|
|
149
151
|
exports.useEndpoint = useEndpoint.useEndpoint;
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type HandlerPlugin } from 'innet';
|
|
2
2
|
import { type ErrorStatuses, type RedirectStatuses, type SuccessStatuses } from '../../request';
|
|
3
3
|
export type StatusKey = ErrorStatuses | RedirectStatuses | SuccessStatuses;
|
|
4
|
+
export type ResponseStatus = 'default' | `${1 | 2 | 3 | 4 | 5}XX` | StatusKey | number;
|
|
4
5
|
export interface ResponseProps {
|
|
5
6
|
children?: any;
|
|
6
7
|
/**
|
|
@@ -14,7 +15,7 @@ export interface ResponseProps {
|
|
|
14
15
|
* For example, 2XX represents all response codes between [200-299].
|
|
15
16
|
* Only the following range definitions are allowed: 1XX, 2XX, 3XX, 4XX, and 5XX.
|
|
16
17
|
* */
|
|
17
|
-
status?:
|
|
18
|
+
status?: ResponseStatus;
|
|
18
19
|
type?: string;
|
|
19
20
|
}
|
|
20
21
|
export declare const statuses: Record<StatusKey, number>;
|
package/types.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { OpenAPIV3_1 as API } from 'openapi-types';
|
|
2
2
|
import type { ApiErrorValue } from './constants';
|
|
3
3
|
import { type ServerPlugin } from './hooks';
|
|
4
|
+
import { type ResponseStatus } from './plugins';
|
|
4
5
|
import { type Rule, type RulesErrors } from './utils/rules';
|
|
5
6
|
export type TagObject = API.TagObject;
|
|
6
7
|
export type Document = API.Document;
|
|
@@ -74,3 +75,14 @@ export interface ValuesSchemaProps<T> extends BaseSchemaProps<T> {
|
|
|
74
75
|
value?: T;
|
|
75
76
|
values?: SchemaValues<T>;
|
|
76
77
|
}
|
|
78
|
+
export type TResponse = Record<ResponseStatus, unknown>;
|
|
79
|
+
export interface TEndpoint {
|
|
80
|
+
body?: unknown;
|
|
81
|
+
cookies?: Record<string, string>;
|
|
82
|
+
headers?: Record<string, string>;
|
|
83
|
+
params?: Record<string, unknown>;
|
|
84
|
+
response?: Record<string, TResponse>;
|
|
85
|
+
search?: Record<string, unknown>;
|
|
86
|
+
}
|
|
87
|
+
export type ApiEndpoints = Record<string, TEndpoint>;
|
|
88
|
+
export type ApiSchemas = Record<string, unknown>;
|
|
@@ -15,67 +15,85 @@ function generateSchemaTypes(schema, spaces = 2, lastChar = '\n') {
|
|
|
15
15
|
if ('$ref' in schema) {
|
|
16
16
|
return `Schemas.${schema.$ref.slice(21)}${lastChar}`;
|
|
17
17
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
const types = Array.isArray(schema.type) ? schema.type : [schema.type];
|
|
19
|
+
let scope = '';
|
|
20
|
+
for (let i = 0; i < types.length; i++) {
|
|
21
|
+
const type = types[i];
|
|
22
|
+
const operator = i ? ' | ' : '';
|
|
23
|
+
if (schema.oneOf) {
|
|
24
|
+
let result = '';
|
|
25
|
+
for (const item of schema.oneOf) {
|
|
26
|
+
if (result) {
|
|
27
|
+
result += ' | ';
|
|
28
|
+
}
|
|
29
|
+
result += generateSchemaTypes(item, spaces + 2, '');
|
|
30
|
+
}
|
|
31
|
+
scope += `${operator}${result}`;
|
|
32
|
+
continue;
|
|
24
33
|
}
|
|
25
|
-
if (
|
|
26
|
-
|
|
34
|
+
if (!type) {
|
|
35
|
+
scope += `${operator}any`;
|
|
36
|
+
continue;
|
|
27
37
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if (result) {
|
|
37
|
-
result += ' | ';
|
|
38
|
+
if (type === 'integer') {
|
|
39
|
+
scope += `${operator}${schema.format === 'int64' ? 'bigint' : 'number'}`;
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
if (type === 'string') {
|
|
43
|
+
if (schema.format === 'date-time') {
|
|
44
|
+
scope += `${operator}Date`;
|
|
45
|
+
continue;
|
|
38
46
|
}
|
|
39
|
-
|
|
47
|
+
if (schema.format === 'binary') {
|
|
48
|
+
scope += `${operator}Bin`;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
scope += `${operator}string`;
|
|
52
|
+
continue;
|
|
40
53
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
return `any${lastChar}`;
|
|
50
|
-
}
|
|
51
|
-
if (schema.type !== 'object') {
|
|
52
|
-
console.error('unknown type', schema);
|
|
53
|
-
return `any${lastChar}`;
|
|
54
|
-
}
|
|
55
|
-
let result = '{\n';
|
|
56
|
-
const required = schema.required || [];
|
|
57
|
-
const hasProps = Boolean(schema.properties && Object.keys(schema.properties).length);
|
|
58
|
-
const hasRestProps = Boolean(typeof schema.additionalProperties === 'object' &&
|
|
59
|
-
Object.keys(schema.additionalProperties).length);
|
|
60
|
-
if (hasProps) {
|
|
61
|
-
for (const key in schema.properties) {
|
|
62
|
-
const prop = schema.properties[key];
|
|
63
|
-
const splitter = required.includes(key) || hasDefault(prop)
|
|
64
|
-
? ':'
|
|
65
|
-
: '?:';
|
|
66
|
-
if ('deprecated' in prop && prop.deprecated) {
|
|
67
|
-
result += `${space}/** @deprecated */\n`;
|
|
54
|
+
if (['boolean', 'null', 'number'].includes(type)) {
|
|
55
|
+
scope += `${operator}${type}`;
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (type === 'array') {
|
|
59
|
+
if (schema.type !== 'array' || !schema.items) {
|
|
60
|
+
scope += `${operator}any[]`;
|
|
61
|
+
continue;
|
|
68
62
|
}
|
|
69
|
-
|
|
63
|
+
scope += `${operator}Array<${generateSchemaTypes(schema.items, spaces + 2, '')}>`;
|
|
64
|
+
continue;
|
|
70
65
|
}
|
|
66
|
+
if (type !== 'object') {
|
|
67
|
+
console.error('Error: Unknown Type', schema);
|
|
68
|
+
scope += `${operator}any`;
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
let result = '{\n';
|
|
72
|
+
const required = schema.required || [];
|
|
73
|
+
const hasProps = Boolean(schema.properties && Object.keys(schema.properties).length);
|
|
74
|
+
const hasRestProps = Boolean(typeof schema.additionalProperties === 'object' &&
|
|
75
|
+
Object.keys(schema.additionalProperties).length);
|
|
76
|
+
if (hasProps) {
|
|
77
|
+
for (const key in schema.properties) {
|
|
78
|
+
const prop = schema.properties[key];
|
|
79
|
+
const splitter = required.includes(key) || hasDefault(prop)
|
|
80
|
+
? ':'
|
|
81
|
+
: '?:';
|
|
82
|
+
if ('deprecated' in prop && prop.deprecated) {
|
|
83
|
+
result += `${space}/** @deprecated */\n`;
|
|
84
|
+
}
|
|
85
|
+
result += `${space}${key}${splitter} ${generateSchemaTypes(prop, spaces + 2)}`;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (hasRestProps) {
|
|
89
|
+
const value = hasProps
|
|
90
|
+
? 'any\n'
|
|
91
|
+
: generateSchemaTypes(schema.additionalProperties, spaces + 2);
|
|
92
|
+
result += `${space}[key: string]: ${value}`;
|
|
93
|
+
}
|
|
94
|
+
scope += `${operator}${result}${space.slice(0, -2)}}`;
|
|
71
95
|
}
|
|
72
|
-
|
|
73
|
-
const value = hasProps
|
|
74
|
-
? 'any\n'
|
|
75
|
-
: generateSchemaTypes(schema.additionalProperties, spaces + 2);
|
|
76
|
-
result += `${space}[key: string]: ${value}`;
|
|
77
|
-
}
|
|
78
|
-
return `${result}${space.slice(0, -2)}}${lastChar}`;
|
|
96
|
+
return `${scope}${lastChar}`;
|
|
79
97
|
}
|
|
80
98
|
function generateTypes(docs, namespace = 'Api') {
|
|
81
99
|
var _a;
|
|
@@ -94,9 +112,9 @@ function generateTypes(docs, namespace = 'Api') {
|
|
|
94
112
|
const schemas = (_a = docs.components) === null || _a === void 0 ? void 0 : _a.schemas;
|
|
95
113
|
const paths = docs.paths;
|
|
96
114
|
if (schemas) {
|
|
97
|
-
result += '
|
|
115
|
+
result += ' export interface Schemas {\n';
|
|
98
116
|
for (const name in schemas) {
|
|
99
|
-
result += `
|
|
117
|
+
result += ` ${name}: ${generateSchemaTypes(schemas[name], 6)}`;
|
|
100
118
|
}
|
|
101
119
|
result += ' }\n';
|
|
102
120
|
}
|
|
@@ -125,23 +143,23 @@ function generateTypes(docs, namespace = 'Api') {
|
|
|
125
143
|
params[param.in] += ` ${param.name}${splitter} ${generateSchemaTypes(param.schema)}`;
|
|
126
144
|
}
|
|
127
145
|
if (params.path) {
|
|
128
|
-
result += `
|
|
146
|
+
result += ` params: {\n${params.path} }\n`;
|
|
129
147
|
}
|
|
130
148
|
if (params.query) {
|
|
131
|
-
result += `
|
|
149
|
+
result += ` search: {\n${params.query} }\n`;
|
|
132
150
|
}
|
|
133
151
|
if (params.header) {
|
|
134
|
-
result += `
|
|
152
|
+
result += ` headers: {\n${params.header} }\n`;
|
|
135
153
|
}
|
|
136
154
|
if (params.cookie) {
|
|
137
|
-
result += `
|
|
155
|
+
result += ` cookies: {\n${params.cookie} }\n`;
|
|
138
156
|
}
|
|
139
157
|
}
|
|
140
158
|
if (requestBody) {
|
|
141
|
-
result += `
|
|
159
|
+
result += ` body: ${generateSchemaTypes(requestBody.content['multipart/form-data'].schema, 8)}`;
|
|
142
160
|
}
|
|
143
161
|
if (responses) {
|
|
144
|
-
result += '
|
|
162
|
+
result += ' response: {\n';
|
|
145
163
|
for (const key in responses) {
|
|
146
164
|
let multiple = false;
|
|
147
165
|
const response = responses[key];
|
|
@@ -165,7 +183,14 @@ function generateTypes(docs, namespace = 'Api') {
|
|
|
165
183
|
result += ' }\n';
|
|
166
184
|
}
|
|
167
185
|
}
|
|
168
|
-
|
|
186
|
+
const body = result + ' }\n}';
|
|
187
|
+
return `${body}
|
|
188
|
+
|
|
189
|
+
declare module '@innet/server' {
|
|
190
|
+
type ApiEndpoints = ${namespace}.Endpoints
|
|
191
|
+
type ApiSchemas = ${namespace}.Schemas
|
|
192
|
+
}
|
|
193
|
+
`;
|
|
169
194
|
}
|
|
170
195
|
|
|
171
196
|
export { generateSchemaTypes, generateTypes };
|
|
@@ -19,67 +19,85 @@ function generateSchemaTypes(schema, spaces = 2, lastChar = '\n') {
|
|
|
19
19
|
if ('$ref' in schema) {
|
|
20
20
|
return `Schemas.${schema.$ref.slice(21)}${lastChar}`;
|
|
21
21
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
const types = Array.isArray(schema.type) ? schema.type : [schema.type];
|
|
23
|
+
let scope = '';
|
|
24
|
+
for (let i = 0; i < types.length; i++) {
|
|
25
|
+
const type = types[i];
|
|
26
|
+
const operator = i ? ' | ' : '';
|
|
27
|
+
if (schema.oneOf) {
|
|
28
|
+
let result = '';
|
|
29
|
+
for (const item of schema.oneOf) {
|
|
30
|
+
if (result) {
|
|
31
|
+
result += ' | ';
|
|
32
|
+
}
|
|
33
|
+
result += generateSchemaTypes(item, spaces + 2, '');
|
|
34
|
+
}
|
|
35
|
+
scope += `${operator}${result}`;
|
|
36
|
+
continue;
|
|
28
37
|
}
|
|
29
|
-
if (
|
|
30
|
-
|
|
38
|
+
if (!type) {
|
|
39
|
+
scope += `${operator}any`;
|
|
40
|
+
continue;
|
|
31
41
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
if (result) {
|
|
41
|
-
result += ' | ';
|
|
42
|
+
if (type === 'integer') {
|
|
43
|
+
scope += `${operator}${schema.format === 'int64' ? 'bigint' : 'number'}`;
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (type === 'string') {
|
|
47
|
+
if (schema.format === 'date-time') {
|
|
48
|
+
scope += `${operator}Date`;
|
|
49
|
+
continue;
|
|
42
50
|
}
|
|
43
|
-
|
|
51
|
+
if (schema.format === 'binary') {
|
|
52
|
+
scope += `${operator}Bin`;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
scope += `${operator}string`;
|
|
56
|
+
continue;
|
|
44
57
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
return `any${lastChar}`;
|
|
54
|
-
}
|
|
55
|
-
if (schema.type !== 'object') {
|
|
56
|
-
console.error('unknown type', schema);
|
|
57
|
-
return `any${lastChar}`;
|
|
58
|
-
}
|
|
59
|
-
let result = '{\n';
|
|
60
|
-
const required = schema.required || [];
|
|
61
|
-
const hasProps = Boolean(schema.properties && Object.keys(schema.properties).length);
|
|
62
|
-
const hasRestProps = Boolean(typeof schema.additionalProperties === 'object' &&
|
|
63
|
-
Object.keys(schema.additionalProperties).length);
|
|
64
|
-
if (hasProps) {
|
|
65
|
-
for (const key in schema.properties) {
|
|
66
|
-
const prop = schema.properties[key];
|
|
67
|
-
const splitter = required.includes(key) || hasDefault(prop)
|
|
68
|
-
? ':'
|
|
69
|
-
: '?:';
|
|
70
|
-
if ('deprecated' in prop && prop.deprecated) {
|
|
71
|
-
result += `${space}/** @deprecated */\n`;
|
|
58
|
+
if (['boolean', 'null', 'number'].includes(type)) {
|
|
59
|
+
scope += `${operator}${type}`;
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (type === 'array') {
|
|
63
|
+
if (schema.type !== 'array' || !schema.items) {
|
|
64
|
+
scope += `${operator}any[]`;
|
|
65
|
+
continue;
|
|
72
66
|
}
|
|
73
|
-
|
|
67
|
+
scope += `${operator}Array<${generateSchemaTypes(schema.items, spaces + 2, '')}>`;
|
|
68
|
+
continue;
|
|
74
69
|
}
|
|
70
|
+
if (type !== 'object') {
|
|
71
|
+
console.error('Error: Unknown Type', schema);
|
|
72
|
+
scope += `${operator}any`;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
let result = '{\n';
|
|
76
|
+
const required = schema.required || [];
|
|
77
|
+
const hasProps = Boolean(schema.properties && Object.keys(schema.properties).length);
|
|
78
|
+
const hasRestProps = Boolean(typeof schema.additionalProperties === 'object' &&
|
|
79
|
+
Object.keys(schema.additionalProperties).length);
|
|
80
|
+
if (hasProps) {
|
|
81
|
+
for (const key in schema.properties) {
|
|
82
|
+
const prop = schema.properties[key];
|
|
83
|
+
const splitter = required.includes(key) || hasDefault(prop)
|
|
84
|
+
? ':'
|
|
85
|
+
: '?:';
|
|
86
|
+
if ('deprecated' in prop && prop.deprecated) {
|
|
87
|
+
result += `${space}/** @deprecated */\n`;
|
|
88
|
+
}
|
|
89
|
+
result += `${space}${key}${splitter} ${generateSchemaTypes(prop, spaces + 2)}`;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (hasRestProps) {
|
|
93
|
+
const value = hasProps
|
|
94
|
+
? 'any\n'
|
|
95
|
+
: generateSchemaTypes(schema.additionalProperties, spaces + 2);
|
|
96
|
+
result += `${space}[key: string]: ${value}`;
|
|
97
|
+
}
|
|
98
|
+
scope += `${operator}${result}${space.slice(0, -2)}}`;
|
|
75
99
|
}
|
|
76
|
-
|
|
77
|
-
const value = hasProps
|
|
78
|
-
? 'any\n'
|
|
79
|
-
: generateSchemaTypes(schema.additionalProperties, spaces + 2);
|
|
80
|
-
result += `${space}[key: string]: ${value}`;
|
|
81
|
-
}
|
|
82
|
-
return `${result}${space.slice(0, -2)}}${lastChar}`;
|
|
100
|
+
return `${scope}${lastChar}`;
|
|
83
101
|
}
|
|
84
102
|
function generateTypes(docs, namespace = 'Api') {
|
|
85
103
|
var _a;
|
|
@@ -98,9 +116,9 @@ function generateTypes(docs, namespace = 'Api') {
|
|
|
98
116
|
const schemas = (_a = docs.components) === null || _a === void 0 ? void 0 : _a.schemas;
|
|
99
117
|
const paths = docs.paths;
|
|
100
118
|
if (schemas) {
|
|
101
|
-
result += '
|
|
119
|
+
result += ' export interface Schemas {\n';
|
|
102
120
|
for (const name in schemas) {
|
|
103
|
-
result += `
|
|
121
|
+
result += ` ${name}: ${generateSchemaTypes(schemas[name], 6)}`;
|
|
104
122
|
}
|
|
105
123
|
result += ' }\n';
|
|
106
124
|
}
|
|
@@ -129,23 +147,23 @@ function generateTypes(docs, namespace = 'Api') {
|
|
|
129
147
|
params[param.in] += ` ${param.name}${splitter} ${generateSchemaTypes(param.schema)}`;
|
|
130
148
|
}
|
|
131
149
|
if (params.path) {
|
|
132
|
-
result += `
|
|
150
|
+
result += ` params: {\n${params.path} }\n`;
|
|
133
151
|
}
|
|
134
152
|
if (params.query) {
|
|
135
|
-
result += `
|
|
153
|
+
result += ` search: {\n${params.query} }\n`;
|
|
136
154
|
}
|
|
137
155
|
if (params.header) {
|
|
138
|
-
result += `
|
|
156
|
+
result += ` headers: {\n${params.header} }\n`;
|
|
139
157
|
}
|
|
140
158
|
if (params.cookie) {
|
|
141
|
-
result += `
|
|
159
|
+
result += ` cookies: {\n${params.cookie} }\n`;
|
|
142
160
|
}
|
|
143
161
|
}
|
|
144
162
|
if (requestBody) {
|
|
145
|
-
result += `
|
|
163
|
+
result += ` body: ${generateSchemaTypes(requestBody.content['multipart/form-data'].schema, 8)}`;
|
|
146
164
|
}
|
|
147
165
|
if (responses) {
|
|
148
|
-
result += '
|
|
166
|
+
result += ' response: {\n';
|
|
149
167
|
for (const key in responses) {
|
|
150
168
|
let multiple = false;
|
|
151
169
|
const response = responses[key];
|
|
@@ -169,7 +187,14 @@ function generateTypes(docs, namespace = 'Api') {
|
|
|
169
187
|
result += ' }\n';
|
|
170
188
|
}
|
|
171
189
|
}
|
|
172
|
-
|
|
190
|
+
const body = result + ' }\n}';
|
|
191
|
+
return `${body}
|
|
192
|
+
|
|
193
|
+
declare module '@innet/server' {
|
|
194
|
+
type ApiEndpoints = ${namespace}.Endpoints
|
|
195
|
+
type ApiSchemas = ${namespace}.Schemas
|
|
196
|
+
}
|
|
197
|
+
`;
|
|
173
198
|
}
|
|
174
199
|
|
|
175
200
|
exports.generateSchemaTypes = generateSchemaTypes;
|