@innet/server 2.0.0-beta.25 → 2.0.0-beta.27
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/constants.d.ts +3 -0
- package/constants.es6.js +28 -1
- package/constants.js +29 -0
- package/hooks/index.d.ts +1 -1
- package/hooks/index.es6.js +1 -1
- package/hooks/index.js +1 -1
- package/hooks/useBodyContext/index.d.ts +1 -0
- package/hooks/useBodyContext/index.es6.js +1 -0
- package/hooks/useBodyContext/index.js +10 -0
- package/hooks/useBodyContext/useBodyContext.d.ts +6 -0
- package/hooks/{useBodyFile/useBodyFile.es6.js → useBodyContext/useBodyContext.es6.js} +6 -6
- package/hooks/{useBodyFile/useBodyFile.js → useBodyContext/useBodyContext.js} +7 -7
- package/hooks/useData/useData.d.ts +1 -1
- package/hooks/useData/useData.es6.js +4 -4
- package/hooks/useData/useData.js +4 -4
- package/index.es6.js +2 -2
- package/index.js +5 -3
- package/package.json +1 -1
- package/plugins/main/api/api.d.ts +7 -0
- package/plugins/main/api/api.es6.js +2 -4
- package/plugins/main/api/api.js +2 -4
- package/plugins/main/body/body.es6.js +6 -4
- package/plugins/main/body/body.js +6 -4
- package/plugins/main/endpoint/endpoint.d.ts +2 -0
- package/plugins/main/endpoint/endpoint.es6.js +78 -2
- package/plugins/main/endpoint/endpoint.js +78 -2
- package/plugins/schema/any/any.es6.js +6 -1
- package/plugins/schema/any/any.js +5 -0
- package/plugins/schema/array/array.es6.js +4 -1
- package/plugins/schema/array/array.js +4 -1
- package/plugins/schema/binary/binary.es6.js +7 -3
- package/plugins/schema/binary/binary.js +6 -2
- package/plugins/schema/boolean/boolean.es6.js +6 -1
- package/plugins/schema/boolean/boolean.js +5 -0
- package/plugins/schema/date/date.es6.js +6 -1
- package/plugins/schema/date/date.js +5 -0
- package/plugins/schema/integer/integer.es6.js +6 -1
- package/plugins/schema/integer/integer.js +5 -0
- package/plugins/schema/null/null.es6.js +8 -2
- package/plugins/schema/null/null.js +7 -1
- package/plugins/schema/number/number.es6.js +6 -1
- package/plugins/schema/number/number.js +5 -0
- package/plugins/schema/object/object.es6.js +34 -29
- package/plugins/schema/object/object.js +33 -28
- package/plugins/schema/string/string.es6.js +7 -2
- package/plugins/schema/string/string.js +6 -1
- package/plugins/schema/tuple/tuple.es6.js +27 -22
- package/plugins/schema/tuple/tuple.js +26 -21
- package/plugins/schema/uuid/uuid.es6.js +6 -1
- package/plugins/schema/uuid/uuid.js +5 -0
- package/types/appTypes.d.ts +3 -1
- package/utils/rules/constants.d.ts +4 -0
- package/utils/rules/constants.es6.js +547 -1
- package/utils/rules/constants.js +548 -0
- package/utils/rules/index.es6.js +1 -1
- package/utils/rules/index.js +2 -0
- package/hooks/useBodyFile/index.d.ts +0 -1
- package/hooks/useBodyFile/index.es6.js +0 -1
- package/hooks/useBodyFile/index.js +0 -10
- package/hooks/useBodyFile/useBodyFile.d.ts +0 -3
package/constants.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
import { type OpenAPIV3_1 } from 'openapi-types';
|
|
1
2
|
import { type BodyType } from './types';
|
|
2
3
|
export declare const apiErrors: readonly ["requestValidation", "requestBodyContentType"];
|
|
3
4
|
export type ApiErrorValue = typeof apiErrors[number];
|
|
4
5
|
export declare const allBodyTypes: BodyType[];
|
|
6
|
+
export declare const defaultRequestBodyContentTypeSchema: OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.SchemaObject;
|
|
7
|
+
export declare const defaultRequestValidationSchema: OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.SchemaObject;
|
package/constants.es6.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { rulesErrorSchemas } from './utils/rules/constants.es6.js';
|
|
2
|
+
|
|
1
3
|
const apiErrors = [
|
|
2
4
|
'requestValidation',
|
|
3
5
|
'requestBodyContentType',
|
|
@@ -7,5 +9,30 @@ const allBodyTypes = [
|
|
|
7
9
|
'application/x-www-form-urlencoded',
|
|
8
10
|
'multipart/form-data',
|
|
9
11
|
];
|
|
12
|
+
const defaultRequestBodyContentTypeSchema = {
|
|
13
|
+
description: 'The request has no body or has unsupported content type format',
|
|
14
|
+
properties: {
|
|
15
|
+
error: {
|
|
16
|
+
const: 'requestBodyContentType',
|
|
17
|
+
title: 'The error code',
|
|
18
|
+
type: 'string',
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
type: 'object',
|
|
22
|
+
};
|
|
23
|
+
const defaultRequestValidationSchema = {
|
|
24
|
+
description: 'The request contains invalid data',
|
|
25
|
+
properties: {
|
|
26
|
+
data: {
|
|
27
|
+
oneOf: Object.values(rulesErrorSchemas),
|
|
28
|
+
},
|
|
29
|
+
error: {
|
|
30
|
+
const: 'requestValidation',
|
|
31
|
+
title: 'The error code',
|
|
32
|
+
type: 'string',
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
type: 'object',
|
|
36
|
+
};
|
|
10
37
|
|
|
11
|
-
export { allBodyTypes, apiErrors };
|
|
38
|
+
export { allBodyTypes, apiErrors, defaultRequestBodyContentTypeSchema, defaultRequestValidationSchema };
|
package/constants.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
+
var constants = require('./utils/rules/constants.js');
|
|
6
|
+
|
|
5
7
|
const apiErrors = [
|
|
6
8
|
'requestValidation',
|
|
7
9
|
'requestBodyContentType',
|
|
@@ -11,6 +13,33 @@ const allBodyTypes = [
|
|
|
11
13
|
'application/x-www-form-urlencoded',
|
|
12
14
|
'multipart/form-data',
|
|
13
15
|
];
|
|
16
|
+
const defaultRequestBodyContentTypeSchema = {
|
|
17
|
+
description: 'The request has no body or has unsupported content type format',
|
|
18
|
+
properties: {
|
|
19
|
+
error: {
|
|
20
|
+
const: 'requestBodyContentType',
|
|
21
|
+
title: 'The error code',
|
|
22
|
+
type: 'string',
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
type: 'object',
|
|
26
|
+
};
|
|
27
|
+
const defaultRequestValidationSchema = {
|
|
28
|
+
description: 'The request contains invalid data',
|
|
29
|
+
properties: {
|
|
30
|
+
data: {
|
|
31
|
+
oneOf: Object.values(constants.rulesErrorSchemas),
|
|
32
|
+
},
|
|
33
|
+
error: {
|
|
34
|
+
const: 'requestValidation',
|
|
35
|
+
title: 'The error code',
|
|
36
|
+
type: 'string',
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
type: 'object',
|
|
40
|
+
};
|
|
14
41
|
|
|
15
42
|
exports.allBodyTypes = allBodyTypes;
|
|
16
43
|
exports.apiErrors = apiErrors;
|
|
44
|
+
exports.defaultRequestBodyContentTypeSchema = defaultRequestBodyContentTypeSchema;
|
|
45
|
+
exports.defaultRequestValidationSchema = defaultRequestValidationSchema;
|
package/hooks/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ export * from './useAction';
|
|
|
2
2
|
export * from './useApi';
|
|
3
3
|
export * from './useBlock';
|
|
4
4
|
export * from './useBody';
|
|
5
|
-
export * from './
|
|
5
|
+
export * from './useBodyContext';
|
|
6
6
|
export * from './useClientIp';
|
|
7
7
|
export * from './useComponentName';
|
|
8
8
|
export * from './useCookies';
|
package/hooks/index.es6.js
CHANGED
|
@@ -2,7 +2,7 @@ import './useAction/index.es6.js';
|
|
|
2
2
|
import './useApi/index.es6.js';
|
|
3
3
|
import './useBlock/index.es6.js';
|
|
4
4
|
import './useBody/index.es6.js';
|
|
5
|
-
import './
|
|
5
|
+
import './useBodyContext/index.es6.js';
|
|
6
6
|
import './useClientIp/index.es6.js';
|
|
7
7
|
import './useComponentName/index.es6.js';
|
|
8
8
|
import './useCookies/index.es6.js';
|
package/hooks/index.js
CHANGED
|
@@ -4,7 +4,7 @@ require('./useAction/index.js');
|
|
|
4
4
|
require('./useApi/index.js');
|
|
5
5
|
require('./useBlock/index.js');
|
|
6
6
|
require('./useBody/index.js');
|
|
7
|
-
require('./
|
|
7
|
+
require('./useBodyContext/index.js');
|
|
8
8
|
require('./useClientIp/index.js');
|
|
9
9
|
require('./useComponentName/index.js');
|
|
10
10
|
require('./useCookies/index.js');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useBodyContext';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { bodyContext, useBodyContext } from './useBodyContext.es6.js';
|
|
@@ -2,13 +2,13 @@ import { Context, useContext } from '@innet/jsx';
|
|
|
2
2
|
import '../useThrow/index.es6.js';
|
|
3
3
|
import { useThrow } from '../useThrow/useThrow.es6.js';
|
|
4
4
|
|
|
5
|
-
const
|
|
6
|
-
function
|
|
7
|
-
const
|
|
8
|
-
if (!
|
|
5
|
+
const bodyContext = new Context();
|
|
6
|
+
function useBodyContext() {
|
|
7
|
+
const context = useContext(bodyContext);
|
|
8
|
+
if (!context) {
|
|
9
9
|
useThrow('<{type}> MUST be in <body>');
|
|
10
10
|
}
|
|
11
|
-
|
|
11
|
+
return context;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
export {
|
|
14
|
+
export { bodyContext, useBodyContext };
|
|
@@ -6,14 +6,14 @@ var jsx = require('@innet/jsx');
|
|
|
6
6
|
require('../useThrow/index.js');
|
|
7
7
|
var useThrow = require('../useThrow/useThrow.js');
|
|
8
8
|
|
|
9
|
-
const
|
|
10
|
-
function
|
|
11
|
-
const
|
|
12
|
-
if (!
|
|
9
|
+
const bodyContext = new jsx.Context();
|
|
10
|
+
function useBodyContext() {
|
|
11
|
+
const context = jsx.useContext(bodyContext);
|
|
12
|
+
if (!context) {
|
|
13
13
|
useThrow.useThrow('<{type}> MUST be in <body>');
|
|
14
14
|
}
|
|
15
|
-
|
|
15
|
+
return context;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
exports.
|
|
19
|
-
exports.
|
|
18
|
+
exports.bodyContext = bodyContext;
|
|
19
|
+
exports.useBodyContext = useBodyContext;
|
|
@@ -2,5 +2,5 @@ import { type ApiEndpoints, type TEndpoint } from '../../types';
|
|
|
2
2
|
type KeysWithField<F extends string> = {
|
|
3
3
|
[K in keyof ApiEndpoints]: F extends keyof ApiEndpoints[K] ? K : never;
|
|
4
4
|
}[keyof ApiEndpoints];
|
|
5
|
-
export declare function useData<F extends Exclude<keyof TEndpoint, 'response'>, K extends KeysWithField<F> = KeysWithField<F>, T extends boolean = false>(from: F, path?: K,
|
|
5
|
+
export declare function useData<F extends Exclude<keyof TEndpoint, 'response'>, K extends KeysWithField<F> = KeysWithField<F>, T extends boolean = false>(from: F, path?: K, preventThrow?: T): T extends true ? ApiEndpoints[K][F] | undefined : ApiEndpoints[K][F];
|
|
6
6
|
export {};
|
|
@@ -8,16 +8,16 @@ import { useThrow } from '../useThrow/useThrow.es6.js';
|
|
|
8
8
|
import { useAction } from '../useAction/useAction.es6.js';
|
|
9
9
|
import { paramsContext } from '../useParams/useParams.es6.js';
|
|
10
10
|
|
|
11
|
-
function useData(from, path,
|
|
11
|
+
function useData(from, path, preventThrow) {
|
|
12
12
|
if (path) {
|
|
13
13
|
const endpoint = useEndpoint();
|
|
14
14
|
const endpointKey = `${endpoint.props.method.toUpperCase()}:${endpoint.props.path}`;
|
|
15
15
|
if (endpointKey !== path) {
|
|
16
|
-
if (
|
|
17
|
-
|
|
16
|
+
if (preventThrow) {
|
|
17
|
+
return undefined;
|
|
18
18
|
}
|
|
19
19
|
else {
|
|
20
|
-
|
|
20
|
+
useThrow(`<{type}> MUST be in <endpoint> of ${path}`);
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
}
|
package/hooks/useData/useData.js
CHANGED
|
@@ -12,16 +12,16 @@ var useThrow = require('../useThrow/useThrow.js');
|
|
|
12
12
|
var useAction = require('../useAction/useAction.js');
|
|
13
13
|
var useParams = require('../useParams/useParams.js');
|
|
14
14
|
|
|
15
|
-
function useData(from, path,
|
|
15
|
+
function useData(from, path, preventThrow) {
|
|
16
16
|
if (path) {
|
|
17
17
|
const endpoint = useEndpoint.useEndpoint();
|
|
18
18
|
const endpointKey = `${endpoint.props.method.toUpperCase()}:${endpoint.props.path}`;
|
|
19
19
|
if (endpointKey !== path) {
|
|
20
|
-
if (
|
|
21
|
-
|
|
20
|
+
if (preventThrow) {
|
|
21
|
+
return undefined;
|
|
22
22
|
}
|
|
23
23
|
else {
|
|
24
|
-
|
|
24
|
+
useThrow.useThrow(`<{type}> MUST be in <endpoint> of ${path}`);
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
}
|
package/index.es6.js
CHANGED
|
@@ -8,7 +8,7 @@ export { actionContext, useAction } from './hooks/useAction/useAction.es6.js';
|
|
|
8
8
|
export { apiContext, useApi } from './hooks/useApi/useApi.es6.js';
|
|
9
9
|
export { useBlock } from './hooks/useBlock/useBlock.es6.js';
|
|
10
10
|
export { useBody } from './hooks/useBody/useBody.es6.js';
|
|
11
|
-
export {
|
|
11
|
+
export { bodyContext, useBodyContext } from './hooks/useBodyContext/useBodyContext.es6.js';
|
|
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';
|
|
@@ -95,7 +95,7 @@ export { EMPTY_SEARCH, parseSearch } from './utils/parseSearch/parseSearch.es6.j
|
|
|
95
95
|
export { arrayOf } from './utils/rules/arrayOf/arrayOf.es6.js';
|
|
96
96
|
export { bin } from './utils/rules/bin/bin.es6.js';
|
|
97
97
|
export { binaryAccept } from './utils/rules/binaryAccept/binaryAccept.es6.js';
|
|
98
|
-
export { rulesErrors } from './utils/rules/constants.es6.js';
|
|
98
|
+
export { inValidationValues, rulesErrorSchemas, rulesErrors } from './utils/rules/constants.es6.js';
|
|
99
99
|
export { dateTo } from './utils/rules/dateTo/dateTo.es6.js';
|
|
100
100
|
export { defaultTo } from './utils/rules/defaultTo/defaultTo.es6.js';
|
|
101
101
|
export { RulesError, addKey } from './utils/rules/helpers.es6.js';
|
package/index.js
CHANGED
|
@@ -12,7 +12,7 @@ var useAction = require('./hooks/useAction/useAction.js');
|
|
|
12
12
|
var useApi = require('./hooks/useApi/useApi.js');
|
|
13
13
|
var useBlock = require('./hooks/useBlock/useBlock.js');
|
|
14
14
|
var useBody = require('./hooks/useBody/useBody.js');
|
|
15
|
-
var
|
|
15
|
+
var useBodyContext = require('./hooks/useBodyContext/useBodyContext.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');
|
|
@@ -140,8 +140,8 @@ exports.apiContext = useApi.apiContext;
|
|
|
140
140
|
exports.useApi = useApi.useApi;
|
|
141
141
|
exports.useBlock = useBlock.useBlock;
|
|
142
142
|
exports.useBody = useBody.useBody;
|
|
143
|
-
exports.
|
|
144
|
-
exports.
|
|
143
|
+
exports.bodyContext = useBodyContext.bodyContext;
|
|
144
|
+
exports.useBodyContext = useBodyContext.useBodyContext;
|
|
145
145
|
exports.useClientIp = useClientIp.useClientIp;
|
|
146
146
|
exports.useComponentName = useComponentName.useComponentName;
|
|
147
147
|
exports.useCookies = useCookies.useCookies;
|
|
@@ -252,6 +252,8 @@ exports.parseSearch = parseSearch.parseSearch;
|
|
|
252
252
|
exports.arrayOf = arrayOf.arrayOf;
|
|
253
253
|
exports.bin = bin.bin;
|
|
254
254
|
exports.binaryAccept = binaryAccept.binaryAccept;
|
|
255
|
+
exports.inValidationValues = constants.inValidationValues;
|
|
256
|
+
exports.rulesErrorSchemas = constants.rulesErrorSchemas;
|
|
255
257
|
exports.rulesErrors = constants.rulesErrors;
|
|
256
258
|
exports.dateTo = dateTo.dateTo;
|
|
257
259
|
exports.defaultTo = defaultTo.defaultTo;
|
package/package.json
CHANGED
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
import { type HandlerPlugin } from 'innet';
|
|
2
|
+
import { type ApiErrorSchema, type ApiErrorSchemaRefs } from '../../../types';
|
|
2
3
|
export interface ApiProps {
|
|
3
4
|
children?: any;
|
|
4
5
|
/** A description of the API. [CommonMark syntax](https://spec.commonmark.or.org) MAY be used for rich text representation. */
|
|
5
6
|
description?: string;
|
|
7
|
+
/** URL path prefix scopes the API. */
|
|
8
|
+
errorShema?: Partial<ApiErrorSchema>;
|
|
9
|
+
/** Error schema reference names. */
|
|
10
|
+
errorShemaRefs?: Partial<ApiErrorSchemaRefs>;
|
|
6
11
|
exclude?: RegExp;
|
|
7
12
|
include?: RegExp;
|
|
8
13
|
/** URL path prefix scopes the API. */
|
|
9
14
|
prefix?: string;
|
|
15
|
+
/** It turns on auto-generation for schemas. */
|
|
16
|
+
schemaGeneration?: boolean;
|
|
10
17
|
/** A short summary of the API. */
|
|
11
18
|
summary?: string;
|
|
12
19
|
/** A URL to the Terms of Service for the API. This MUST be in the form of a URL. */
|
|
@@ -14,7 +14,7 @@ import { paramsContext } from '../../../hooks/useParams/useParams.es6.js';
|
|
|
14
14
|
const api = () => {
|
|
15
15
|
const handler = useNewHandler();
|
|
16
16
|
const props = useProps();
|
|
17
|
-
const { children, exclude, include, prefix = process.env.INNET_API_PREFIX || '', title = '', version = process.env.INNET_API_VERSION || '0.0.0', ...rest } = props;
|
|
17
|
+
const { children, errorShema, errorShemaRefs, exclude, include, prefix = process.env.INNET_API_PREFIX || '', schemaGeneration, title = '', version = process.env.INNET_API_VERSION || '0.0.0', ...rest } = props;
|
|
18
18
|
const info = { ...rest, title, version };
|
|
19
19
|
const endpoints = {};
|
|
20
20
|
const docs = {
|
|
@@ -120,9 +120,7 @@ const api = () => {
|
|
|
120
120
|
if (!action.body) {
|
|
121
121
|
res.statusCode = 400;
|
|
122
122
|
res.setHeader('Content-Type', 'application/json');
|
|
123
|
-
res.write(JSONString({
|
|
124
|
-
error: 'requestBodyContentType',
|
|
125
|
-
}));
|
|
123
|
+
res.write(JSONString({ error: 'requestBodyContentType' }));
|
|
126
124
|
res.end();
|
|
127
125
|
return true;
|
|
128
126
|
}
|
package/plugins/main/api/api.js
CHANGED
|
@@ -18,7 +18,7 @@ var useParams = require('../../../hooks/useParams/useParams.js');
|
|
|
18
18
|
const api = () => {
|
|
19
19
|
const handler = innet.useNewHandler();
|
|
20
20
|
const props = jsx.useProps();
|
|
21
|
-
const { children, exclude, include, prefix = process.env.INNET_API_PREFIX || '', title = '', version = process.env.INNET_API_VERSION || '0.0.0', ...rest } = props;
|
|
21
|
+
const { children, errorShema, errorShemaRefs, exclude, include, prefix = process.env.INNET_API_PREFIX || '', schemaGeneration, title = '', version = process.env.INNET_API_VERSION || '0.0.0', ...rest } = props;
|
|
22
22
|
const info = { ...rest, title, version };
|
|
23
23
|
const endpoints = {};
|
|
24
24
|
const docs = {
|
|
@@ -124,9 +124,7 @@ const api = () => {
|
|
|
124
124
|
if (!action.body) {
|
|
125
125
|
res.statusCode = 400;
|
|
126
126
|
res.setHeader('Content-Type', 'application/json');
|
|
127
|
-
res.write(JSONString.JSONString({
|
|
128
|
-
error: 'requestBodyContentType',
|
|
129
|
-
}));
|
|
127
|
+
res.write(JSONString.JSONString({ error: 'requestBodyContentType' }));
|
|
130
128
|
res.end();
|
|
131
129
|
return true;
|
|
132
130
|
}
|
|
@@ -6,7 +6,7 @@ import '../../../utils/index.es6.js';
|
|
|
6
6
|
import { endpointContext } from '../../../hooks/useEndpoint/useEndpoint.es6.js';
|
|
7
7
|
import { schemaContext } from '../../../hooks/useSchemaContext/useSchemaContext.es6.js';
|
|
8
8
|
import { getOrAdd } from '../../../utils/getOrAdd/getOrAdd.es6.js';
|
|
9
|
-
import {
|
|
9
|
+
import { bodyContext } from '../../../hooks/useBodyContext/useBodyContext.es6.js';
|
|
10
10
|
import { ruleContext } from '../../../hooks/useRule/useRule.es6.js';
|
|
11
11
|
import { useEffect } from '../../../hooks/useEffect/useEffect.es6.js';
|
|
12
12
|
|
|
@@ -33,13 +33,14 @@ const body = () => {
|
|
|
33
33
|
schemaContext.set(handler, schema);
|
|
34
34
|
const rules = getOrAdd(endpoint, 'endpoint.rules', [{}, {}]);
|
|
35
35
|
let fileUsed = false;
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
bodyContext.set(handler, {
|
|
37
|
+
useFile: () => {
|
|
38
|
+
fileUsed = true;
|
|
39
|
+
},
|
|
38
40
|
});
|
|
39
41
|
ruleContext.set(handler, rule => {
|
|
40
42
|
rules.body = rule;
|
|
41
43
|
});
|
|
42
|
-
innet(children, handler);
|
|
43
44
|
useEffect(() => {
|
|
44
45
|
if (fileUsed) {
|
|
45
46
|
requestBody.content['multipart/form-data'] = { schema };
|
|
@@ -50,6 +51,7 @@ const body = () => {
|
|
|
50
51
|
}
|
|
51
52
|
}
|
|
52
53
|
});
|
|
54
|
+
innet(children, handler);
|
|
53
55
|
};
|
|
54
56
|
|
|
55
57
|
export { body };
|
|
@@ -10,7 +10,7 @@ require('../../../utils/index.js');
|
|
|
10
10
|
var useEndpoint = require('../../../hooks/useEndpoint/useEndpoint.js');
|
|
11
11
|
var useSchemaContext = require('../../../hooks/useSchemaContext/useSchemaContext.js');
|
|
12
12
|
var getOrAdd = require('../../../utils/getOrAdd/getOrAdd.js');
|
|
13
|
-
var
|
|
13
|
+
var useBodyContext = require('../../../hooks/useBodyContext/useBodyContext.js');
|
|
14
14
|
var useRule = require('../../../hooks/useRule/useRule.js');
|
|
15
15
|
var useEffect = require('../../../hooks/useEffect/useEffect.js');
|
|
16
16
|
|
|
@@ -37,13 +37,14 @@ const body = () => {
|
|
|
37
37
|
useSchemaContext.schemaContext.set(handler, schema);
|
|
38
38
|
const rules = getOrAdd.getOrAdd(endpoint, 'endpoint.rules', [{}, {}]);
|
|
39
39
|
let fileUsed = false;
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
useBodyContext.bodyContext.set(handler, {
|
|
41
|
+
useFile: () => {
|
|
42
|
+
fileUsed = true;
|
|
43
|
+
},
|
|
42
44
|
});
|
|
43
45
|
useRule.ruleContext.set(handler, rule => {
|
|
44
46
|
rules.body = rule;
|
|
45
47
|
});
|
|
46
|
-
innet.innet(children, handler);
|
|
47
48
|
useEffect.useEffect(() => {
|
|
48
49
|
if (fileUsed) {
|
|
49
50
|
requestBody.content['multipart/form-data'] = { schema };
|
|
@@ -54,6 +55,7 @@ const body = () => {
|
|
|
54
55
|
}
|
|
55
56
|
}
|
|
56
57
|
});
|
|
58
|
+
innet.innet(children, handler);
|
|
57
59
|
};
|
|
58
60
|
|
|
59
61
|
exports.body = body;
|
|
@@ -35,6 +35,8 @@ export interface EndpointProps {
|
|
|
35
35
|
* That means the endpoint should not be described and will not be shown in the Open API documentation.
|
|
36
36
|
* */
|
|
37
37
|
private?: boolean;
|
|
38
|
+
/** It turns on auto-generation for schemas. */
|
|
39
|
+
schemaGeneration?: boolean;
|
|
38
40
|
/**
|
|
39
41
|
* An optional, string summary, intended to apply to all operations in this path.
|
|
40
42
|
* */
|
|
@@ -1,19 +1,55 @@
|
|
|
1
1
|
import { useNewHandler, innet } from 'innet';
|
|
2
2
|
import { useProps } from '@innet/jsx';
|
|
3
|
+
import { defaultRequestValidationSchema, defaultRequestBodyContentTypeSchema } from '../../../constants.es6.js';
|
|
3
4
|
import '../../../hooks/index.es6.js';
|
|
4
5
|
import '../../../utils/index.es6.js';
|
|
5
6
|
import { useTag } from '../../../hooks/useTag/useTag.es6.js';
|
|
6
7
|
import { useApi } from '../../../hooks/useApi/useApi.es6.js';
|
|
7
8
|
import { getEndpoint } from '../../../utils/getEndpoint/getEndpoint.es6.js';
|
|
9
|
+
import { useEffect } from '../../../hooks/useEffect/useEffect.es6.js';
|
|
8
10
|
import { endpointContext } from '../../../hooks/useEndpoint/useEndpoint.es6.js';
|
|
9
11
|
import { serverPlugins } from '../../../hooks/useServerPlugins/useServerPlugins.es6.js';
|
|
10
12
|
|
|
13
|
+
function addErrorRequest(errorSchema, response) {
|
|
14
|
+
if (!response) {
|
|
15
|
+
return {
|
|
16
|
+
content: {
|
|
17
|
+
'application/json': {
|
|
18
|
+
schema: errorSchema,
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
if (!('content' in response)) {
|
|
24
|
+
console.error('Cannot find content in response: ', response);
|
|
25
|
+
return response;
|
|
26
|
+
}
|
|
27
|
+
if (!('application/json' in response.content)) {
|
|
28
|
+
response.content['application/json'] = {
|
|
29
|
+
schema: errorSchema,
|
|
30
|
+
};
|
|
31
|
+
return response;
|
|
32
|
+
}
|
|
33
|
+
const schema = response.content['application/json'].schema;
|
|
34
|
+
if (!schema) {
|
|
35
|
+
response.content['application/json'].schema = errorSchema;
|
|
36
|
+
return response;
|
|
37
|
+
}
|
|
38
|
+
if ('oneOf' in schema) {
|
|
39
|
+
schema.oneOf.push(errorSchema);
|
|
40
|
+
return response;
|
|
41
|
+
}
|
|
42
|
+
response.content['application/json'].schema = {
|
|
43
|
+
oneOf: [schema, errorSchema],
|
|
44
|
+
};
|
|
45
|
+
return response;
|
|
46
|
+
}
|
|
11
47
|
const endpoint = () => {
|
|
12
48
|
const handler = useNewHandler();
|
|
13
49
|
const tag = useTag();
|
|
14
50
|
const props = useProps();
|
|
15
|
-
const { docs, endpoints, } = useApi();
|
|
16
|
-
const { children, deprecated, description, method, operationId, path, private: privateMode, summary, } = props;
|
|
51
|
+
const { docs, endpoints, props: apiProps, } = useApi();
|
|
52
|
+
const { children, deprecated, description, method, operationId, path, private: privateMode, schemaGeneration = apiProps.schemaGeneration, summary, } = props;
|
|
17
53
|
const { paths } = docs;
|
|
18
54
|
if (!paths)
|
|
19
55
|
throw Error('cannot find paths in docs');
|
|
@@ -46,6 +82,46 @@ const endpoint = () => {
|
|
|
46
82
|
endpoints[method] = { key: '', plugins: new Set() };
|
|
47
83
|
}
|
|
48
84
|
const endpoint = getEndpoint(path, endpoints[method]);
|
|
85
|
+
if (schemaGeneration) {
|
|
86
|
+
useEffect(() => {
|
|
87
|
+
var _a, _b, _c, _d, _e;
|
|
88
|
+
if (operation.requestBody || ((_a = operation.parameters) === null || _a === void 0 ? void 0 : _a.length)) {
|
|
89
|
+
if (!operation.responses) {
|
|
90
|
+
operation.responses = {};
|
|
91
|
+
}
|
|
92
|
+
if (!docs.components) {
|
|
93
|
+
docs.components = {};
|
|
94
|
+
}
|
|
95
|
+
if (!docs.components.schemas) {
|
|
96
|
+
docs.components.schemas = {};
|
|
97
|
+
}
|
|
98
|
+
const ref = (_c = (_b = apiProps.errorShemaRefs) === null || _b === void 0 ? void 0 : _b.requestValidation) !== null && _c !== void 0 ? _c : 'ApiValidationError';
|
|
99
|
+
if (!(ref in docs.components.schemas)) {
|
|
100
|
+
docs.components.schemas[ref] = (_e = (_d = apiProps.errorShema) === null || _d === void 0 ? void 0 : _d.requestValidation) !== null && _e !== void 0 ? _e : defaultRequestValidationSchema;
|
|
101
|
+
}
|
|
102
|
+
operation.responses[400] = addErrorRequest({ $ref: `#/components/schemas/${ref}` }, operation.responses[400]);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
useEffect(() => {
|
|
106
|
+
var _a, _b, _c, _d;
|
|
107
|
+
if (operation.requestBody) {
|
|
108
|
+
if (!operation.responses) {
|
|
109
|
+
operation.responses = {};
|
|
110
|
+
}
|
|
111
|
+
if (!docs.components) {
|
|
112
|
+
docs.components = {};
|
|
113
|
+
}
|
|
114
|
+
if (!docs.components.schemas) {
|
|
115
|
+
docs.components.schemas = {};
|
|
116
|
+
}
|
|
117
|
+
const ref = (_b = (_a = apiProps.errorShemaRefs) === null || _a === void 0 ? void 0 : _a.requestValidation) !== null && _b !== void 0 ? _b : 'ApiRequestBodyContentTypeError';
|
|
118
|
+
if (!(ref in docs.components.schemas)) {
|
|
119
|
+
docs.components.schemas[ref] = (_d = (_c = apiProps.errorShema) === null || _c === void 0 ? void 0 : _c.requestBodyContentType) !== null && _d !== void 0 ? _d : defaultRequestBodyContentTypeSchema;
|
|
120
|
+
}
|
|
121
|
+
operation.responses[400] = addErrorRequest({ $ref: `#/components/schemas/${ref}` }, operation.responses[400]);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
49
125
|
// @ts-expect-error: it's always an object
|
|
50
126
|
endpointContext.set(handler, { endpoint, operation, props });
|
|
51
127
|
// @ts-expect-error: it's always an object
|