@innet/server 2.0.0-beta.2 → 2.0.0-beta.21

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.
Files changed (154) hide show
  1. package/README.md +1869 -3651
  2. package/handler/handler.d.ts +5 -2
  3. package/handler/handler.es6.js +2 -0
  4. package/handler/handler.js +2 -0
  5. package/hooks/index.d.ts +25 -23
  6. package/hooks/index.es6.js +25 -23
  7. package/hooks/index.js +25 -23
  8. package/hooks/useAction/useAction.es6.js +1 -1
  9. package/hooks/useAction/useAction.js +1 -1
  10. package/hooks/useApi/useApi.d.ts +2 -0
  11. package/hooks/useData/index.d.ts +1 -0
  12. package/hooks/useData/index.es6.js +1 -0
  13. package/hooks/useData/index.js +9 -0
  14. package/hooks/useData/useData.d.ts +6 -0
  15. package/hooks/useData/useData.es6.js +29 -0
  16. package/hooks/useData/useData.js +33 -0
  17. package/hooks/useEffect/index.d.ts +1 -0
  18. package/hooks/useEffect/index.es6.js +1 -0
  19. package/hooks/useEffect/index.js +9 -0
  20. package/hooks/useEffect/useEffect.d.ts +2 -0
  21. package/hooks/useEffect/useEffect.es6.js +8 -0
  22. package/hooks/useEffect/useEffect.js +12 -0
  23. package/hooks/useSchemaType/useSchemaType.d.ts +2 -2
  24. package/hooks/useSchemaType/useSchemaType.es6.js +13 -4
  25. package/hooks/useSchemaType/useSchemaType.js +13 -4
  26. package/hooks/useSearch/useSearch.es6.js +1 -1
  27. package/hooks/useSearch/useSearch.js +1 -1
  28. package/hooks/useServer/useServer.d.ts +2 -0
  29. package/index.d.ts +2 -2
  30. package/index.es6.js +90 -86
  31. package/index.js +204 -193
  32. package/package.json +5 -4
  33. package/plugins/handler/serverFn/serverFn.es6.js +1 -1
  34. package/plugins/handler/serverFn/serverFn.js +1 -5
  35. package/plugins/index.d.ts +2 -2
  36. package/plugins/index.es6.js +2 -2
  37. package/plugins/index.js +2 -2
  38. package/plugins/main/api/api.es6.js +6 -5
  39. package/plugins/main/api/api.js +8 -11
  40. package/plugins/main/body/body.es6.js +4 -4
  41. package/plugins/main/body/body.js +4 -8
  42. package/plugins/main/endpoint/endpoint.es6.js +4 -4
  43. package/plugins/main/endpoint/endpoint.js +4 -8
  44. package/plugins/main/host/host.es6.js +1 -1
  45. package/plugins/main/host/host.js +1 -5
  46. package/plugins/main/index.d.ts +8 -8
  47. package/plugins/main/index.es6.js +8 -8
  48. package/plugins/main/index.js +8 -8
  49. package/plugins/main/param/param.es6.js +1 -1
  50. package/plugins/main/param/param.js +1 -5
  51. package/plugins/main/preset/preset.es6.js +1 -1
  52. package/plugins/main/preset/preset.js +1 -5
  53. package/plugins/main/response/response.d.ts +2 -1
  54. package/plugins/main/response/response.es6.js +1 -1
  55. package/plugins/main/response/response.js +1 -5
  56. package/plugins/main/server/server.d.ts +4 -0
  57. package/plugins/main/server/server.es6.js +5 -3
  58. package/plugins/main/server/server.js +6 -5
  59. package/plugins/main/tag/index.es6.js +1 -1
  60. package/plugins/main/tag/index.js +1 -0
  61. package/plugins/main/tag/tag.d.ts +8 -1
  62. package/plugins/main/tag/tag.es6.js +20 -3
  63. package/plugins/main/tag/tag.js +20 -6
  64. package/plugins/request/cms/cms.es6.js +1 -1
  65. package/plugins/request/cms/cms.js +2 -3
  66. package/plugins/request/error/error.es6.js +7 -3
  67. package/plugins/request/error/error.js +7 -3
  68. package/plugins/request/file/file.es6.js +2 -2
  69. package/plugins/request/file/file.js +2 -3
  70. package/plugins/request/header/header.es6.js +1 -1
  71. package/plugins/request/header/header.js +1 -1
  72. package/plugins/request/index.d.ts +5 -5
  73. package/plugins/request/index.es6.js +5 -5
  74. package/plugins/request/index.js +5 -5
  75. package/plugins/request/success/success.es6.js +6 -4
  76. package/plugins/request/success/success.js +6 -4
  77. package/plugins/schema/any/any.d.ts +2 -2
  78. package/plugins/schema/array/array.d.ts +5 -2
  79. package/plugins/schema/array/array.es6.js +14 -5
  80. package/plugins/schema/array/array.js +15 -10
  81. package/plugins/schema/binary/binary.d.ts +4 -0
  82. package/plugins/schema/boolean/boolean.d.ts +3 -2
  83. package/plugins/schema/date/date.d.ts +3 -2
  84. package/plugins/schema/date/date.es6.js +2 -2
  85. package/plugins/schema/date/date.js +2 -2
  86. package/plugins/schema/field/field.d.ts +4 -0
  87. package/plugins/schema/field/field.es6.js +16 -3
  88. package/plugins/schema/field/field.js +16 -7
  89. package/plugins/schema/index.d.ts +8 -8
  90. package/plugins/schema/index.es6.js +8 -8
  91. package/plugins/schema/index.js +8 -8
  92. package/plugins/schema/integer/integer.d.ts +39 -2
  93. package/plugins/schema/integer/integer.es6.js +25 -10
  94. package/plugins/schema/integer/integer.js +25 -10
  95. package/plugins/schema/null/null.d.ts +2 -2
  96. package/plugins/schema/number/number.d.ts +34 -2
  97. package/plugins/schema/number/number.es6.js +23 -7
  98. package/plugins/schema/number/number.js +22 -6
  99. package/plugins/schema/object/object.d.ts +2 -2
  100. package/plugins/schema/object/object.es6.js +8 -6
  101. package/plugins/schema/object/object.js +8 -10
  102. package/plugins/schema/string/string.d.ts +29 -2
  103. package/plugins/schema/string/string.es6.js +11 -5
  104. package/plugins/schema/string/string.js +10 -4
  105. package/plugins/schema/tuple/tuple.d.ts +2 -2
  106. package/plugins/schema/tuple/tuple.es6.js +4 -4
  107. package/plugins/schema/tuple/tuple.js +4 -8
  108. package/plugins/schema/uuid/uuid.d.ts +2 -2
  109. package/plugins/schema/uuid/uuid.es6.js +2 -2
  110. package/plugins/schema/uuid/uuid.js +1 -1
  111. package/plugins/utils/dts/dts.es6.js +5 -2
  112. package/plugins/utils/dts/dts.js +5 -2
  113. package/plugins/utils/env/env.es6.js +1 -1
  114. package/plugins/utils/env/env.js +1 -5
  115. package/plugins/utils/index.d.ts +3 -2
  116. package/plugins/utils/index.es6.js +3 -2
  117. package/plugins/utils/index.js +3 -2
  118. package/plugins/utils/swagger/swagger.d.ts +1 -0
  119. package/plugins/utils/swagger/swagger.es6.js +1 -0
  120. package/plugins/utils/swagger/swagger.js +1 -0
  121. package/plugins/utils/ui/index.d.ts +1 -0
  122. package/plugins/utils/ui/index.es6.js +1 -0
  123. package/plugins/utils/ui/index.js +10 -0
  124. package/plugins/utils/ui/rapidoc.html.es6.js +3 -0
  125. package/plugins/utils/ui/rapidoc.html.js +7 -0
  126. package/plugins/utils/ui/redoc.html.es6.js +3 -0
  127. package/plugins/utils/ui/redoc.html.js +7 -0
  128. package/plugins/utils/ui/scalar.html.es6.js +3 -0
  129. package/plugins/utils/ui/scalar.html.js +7 -0
  130. package/plugins/utils/ui/swagger.html.es6.js +3 -0
  131. package/plugins/utils/ui/swagger.html.js +7 -0
  132. package/plugins/utils/ui/ui.d.ts +13 -0
  133. package/plugins/utils/ui/ui.es6.js +45 -0
  134. package/plugins/utils/ui/ui.js +50 -0
  135. package/types.d.ts +23 -3
  136. package/utils/generateTypes/generateTypes.es6.js +105 -76
  137. package/utils/generateTypes/generateTypes.js +105 -76
  138. package/utils/getSafeSchema/getSafeSchema.d.ts +2 -0
  139. package/utils/getSafeSchema/getSafeSchema.es6.js +5 -0
  140. package/utils/getSafeSchema/getSafeSchema.js +9 -0
  141. package/utils/getSafeSchema/index.d.ts +1 -0
  142. package/utils/getSafeSchema/index.es6.js +1 -0
  143. package/utils/getSafeSchema/index.js +9 -0
  144. package/utils/index.d.ts +9 -8
  145. package/utils/index.es6.js +9 -8
  146. package/utils/index.js +9 -8
  147. package/utils/rules/index.d.ts +17 -17
  148. package/utils/rules/index.es6.js +17 -17
  149. package/utils/rules/index.js +18 -18
  150. package/utils/rules/values/index.es6.js +1 -1
  151. package/utils/rules/values/index.js +1 -0
  152. package/utils/rules/values/values.d.ts +3 -1
  153. package/utils/rules/values/values.es6.js +4 -1
  154. package/utils/rules/values/values.js +4 -0
@@ -5,6 +5,7 @@ import { useApi } from '../../../hooks/useApi/useApi.es6.js';
5
5
  import { useServerPlugin } from '../../../hooks/useServerPlugin/useServerPlugin.es6.js';
6
6
  import { useAction } from '../../../hooks/useAction/useAction.es6.js';
7
7
 
8
+ /** @deprecated Use <ui> */
8
9
  const swagger = () => {
9
10
  const { path = process.env.INNET_SWAGGER_PATH || '/swagger-ui', } = useProps() || {};
10
11
  const { docs, prefix, } = useApi();
@@ -9,6 +9,7 @@ var useApi = require('../../../hooks/useApi/useApi.js');
9
9
  var useServerPlugin = require('../../../hooks/useServerPlugin/useServerPlugin.js');
10
10
  var useAction = require('../../../hooks/useAction/useAction.js');
11
11
 
12
+ /** @deprecated Use <ui> */
12
13
  const swagger = () => {
13
14
  const { path = process.env.INNET_SWAGGER_PATH || '/swagger-ui', } = jsx.useProps() || {};
14
15
  const { docs, prefix, } = useApi.useApi();
@@ -0,0 +1 @@
1
+ export * from './ui';
@@ -0,0 +1 @@
1
+ export { ui, uiPresets } from './ui.es6.js';
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var ui = require('./ui.js');
6
+
7
+
8
+
9
+ exports.ui = ui.ui;
10
+ exports.uiPresets = ui.uiPresets;
@@ -0,0 +1,3 @@
1
+ var rapidoc = "<!doctype html>\n<html>\n<head>\n <script type=\"module\" src=\"https://unpkg.com/rapidoc/dist/rapidoc-min.js\"></script>\n</head>\n<body>\n<rapi-doc spec-url=\"{apiUrl}\" {attributes}></rapi-doc>\n</body>\n</html>\n";
2
+
3
+ export { rapidoc as default };
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var rapidoc = "<!doctype html>\n<html>\n<head>\n <script type=\"module\" src=\"https://unpkg.com/rapidoc/dist/rapidoc-min.js\"></script>\n</head>\n<body>\n<rapi-doc spec-url=\"{apiUrl}\" {attributes}></rapi-doc>\n</body>\n</html>\n";
6
+
7
+ exports["default"] = rapidoc;
@@ -0,0 +1,3 @@
1
+ var redoc = "<!DOCTYPE html>\n<html>\n<head>\n <title>Redoc CE</title>\n <meta charset=\"utf-8\"/>\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n <link href=\"https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700\" rel=\"stylesheet\">\n <style>\n body {\n margin: 0;\n padding: 0;\n }\n </style>\n</head>\n<body>\n<redoc spec-url='{apiUrl}' {attributes}></redoc>\n<script src=\"https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js\"> </script>\n</body>\n</html>\n";
2
+
3
+ export { redoc as default };
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var redoc = "<!DOCTYPE html>\n<html>\n<head>\n <title>Redoc CE</title>\n <meta charset=\"utf-8\"/>\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n <link href=\"https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700\" rel=\"stylesheet\">\n <style>\n body {\n margin: 0;\n padding: 0;\n }\n </style>\n</head>\n<body>\n<redoc spec-url='{apiUrl}' {attributes}></redoc>\n<script src=\"https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js\"> </script>\n</body>\n</html>\n";
6
+
7
+ exports["default"] = redoc;
@@ -0,0 +1,3 @@
1
+ var scalar = "<!doctype html>\n<html>\n<head>\n <title>API Reference</title>\n <meta charset=\"utf-8\" />\n <meta\n name=\"viewport\"\n content=\"width=device-width, initial-scale=1\" />\n</head>\n<body>\n<div id=\"app\"></div>\n<script src=\"https://cdn.jsdelivr.net/npm/@scalar/api-reference\"></script>\n<script>\n Scalar.createApiReference('#app', {\n url: '{apiUrl}',\n ...{params}\n })\n</script>\n</body>\n</html>\n";
2
+
3
+ export { scalar as default };
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var scalar = "<!doctype html>\n<html>\n<head>\n <title>API Reference</title>\n <meta charset=\"utf-8\" />\n <meta\n name=\"viewport\"\n content=\"width=device-width, initial-scale=1\" />\n</head>\n<body>\n<div id=\"app\"></div>\n<script src=\"https://cdn.jsdelivr.net/npm/@scalar/api-reference\"></script>\n<script>\n Scalar.createApiReference('#app', {\n url: '{apiUrl}',\n ...{params}\n })\n</script>\n</body>\n</html>\n";
6
+
7
+ exports["default"] = scalar;
@@ -0,0 +1,3 @@
1
+ var swagger = "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n <meta\n name=\"description\"\n content=\"SwaggerUI\"\n />\n <title>SwaggerUI</title>\n <link rel=\"stylesheet\" href=\"https://unpkg.com/swagger-ui-dist/swagger-ui.css\" />\n</head>\n<body>\n<div id=\"swagger-ui\"></div>\n<script src=\"https://unpkg.com/swagger-ui-dist/swagger-ui-bundle.js\" crossorigin></script>\n<script>\n window.onload = () => {\n window.ui = SwaggerUIBundle({\n url: \"{apiUrl}\",\n dom_id: '#swagger-ui',\n ...{params}\n });\n };\n</script>\n</body>\n</html>\n";
2
+
3
+ export { swagger as default };
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var swagger = "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n <meta\n name=\"description\"\n content=\"SwaggerUI\"\n />\n <title>SwaggerUI</title>\n <link rel=\"stylesheet\" href=\"https://unpkg.com/swagger-ui-dist/swagger-ui.css\" />\n</head>\n<body>\n<div id=\"swagger-ui\"></div>\n<script src=\"https://unpkg.com/swagger-ui-dist/swagger-ui-bundle.js\" crossorigin></script>\n<script>\n window.onload = () => {\n window.ui = SwaggerUIBundle({\n url: \"{apiUrl}\",\n dom_id: '#swagger-ui',\n ...{params}\n });\n };\n</script>\n</body>\n</html>\n";
6
+
7
+ exports["default"] = swagger;
@@ -0,0 +1,13 @@
1
+ import { type HandlerPlugin } from 'innet';
2
+ export declare const uiPresets: {
3
+ rapidoc: string;
4
+ redoc: string;
5
+ scalar: string;
6
+ swagger: string;
7
+ };
8
+ export interface UiProps {
9
+ html?: string;
10
+ params?: Record<string, any>;
11
+ path?: string;
12
+ }
13
+ export declare const ui: HandlerPlugin;
@@ -0,0 +1,45 @@
1
+ import { placeholder } from '@cantinc/utils';
2
+ import { useProps } from '@innet/jsx';
3
+ import '../../../hooks/index.es6.js';
4
+ import rapidoc from './rapidoc.html.es6.js';
5
+ import redoc from './redoc.html.es6.js';
6
+ import scalar from './scalar.html.es6.js';
7
+ import swagger from './swagger.html.es6.js';
8
+ import { useApi } from '../../../hooks/useApi/useApi.es6.js';
9
+ import { useServerPlugin } from '../../../hooks/useServerPlugin/useServerPlugin.es6.js';
10
+ import { useAction } from '../../../hooks/useAction/useAction.es6.js';
11
+
12
+ function camelToDash(str) {
13
+ return str.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);
14
+ }
15
+ const uiPresets = { rapidoc, redoc, scalar, swagger };
16
+ const ui = () => {
17
+ const { html = uiPresets.swagger, params = {}, path = process.env.INNET_UI_PATH || '/ui', } = useProps() || {};
18
+ const { docs, prefix, } = useApi();
19
+ let cache = '';
20
+ useServerPlugin(() => {
21
+ const action = useAction();
22
+ if (action.path === prefix + path) {
23
+ if (!cache) {
24
+ const attributes = Object
25
+ .keys(params)
26
+ .reduce((res, key) => {
27
+ return `${res} ${camelToDash(key)}='${String(params[key])}'`;
28
+ }, '');
29
+ cache = placeholder(html, {
30
+ apiUrl: prefix,
31
+ attributes,
32
+ docs: JSON.stringify(docs),
33
+ params: JSON.stringify(params),
34
+ ...params,
35
+ });
36
+ }
37
+ action.res.statusCode = 200;
38
+ action.res.write(cache);
39
+ action.res.end();
40
+ return true;
41
+ }
42
+ });
43
+ };
44
+
45
+ export { ui, uiPresets };
@@ -0,0 +1,50 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var utils = require('@cantinc/utils');
6
+ var jsx = require('@innet/jsx');
7
+ require('../../../hooks/index.js');
8
+ var rapidoc = require('./rapidoc.html.js');
9
+ var redoc = require('./redoc.html.js');
10
+ var scalar = require('./scalar.html.js');
11
+ var swagger = require('./swagger.html.js');
12
+ var useApi = require('../../../hooks/useApi/useApi.js');
13
+ var useServerPlugin = require('../../../hooks/useServerPlugin/useServerPlugin.js');
14
+ var useAction = require('../../../hooks/useAction/useAction.js');
15
+
16
+ function camelToDash(str) {
17
+ return str.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);
18
+ }
19
+ const uiPresets = { rapidoc: rapidoc["default"], redoc: redoc["default"], scalar: scalar["default"], swagger: swagger["default"] };
20
+ const ui = () => {
21
+ const { html = uiPresets.swagger, params = {}, path = process.env.INNET_UI_PATH || '/ui', } = jsx.useProps() || {};
22
+ const { docs, prefix, } = useApi.useApi();
23
+ let cache = '';
24
+ useServerPlugin.useServerPlugin(() => {
25
+ const action = useAction.useAction();
26
+ if (action.path === prefix + path) {
27
+ if (!cache) {
28
+ const attributes = Object
29
+ .keys(params)
30
+ .reduce((res, key) => {
31
+ return `${res} ${camelToDash(key)}='${String(params[key])}'`;
32
+ }, '');
33
+ cache = utils.placeholder(html, {
34
+ apiUrl: prefix,
35
+ attributes,
36
+ docs: JSON.stringify(docs),
37
+ params: JSON.stringify(params),
38
+ ...params,
39
+ });
40
+ }
41
+ action.res.statusCode = 200;
42
+ action.res.write(cache);
43
+ action.res.end();
44
+ return true;
45
+ }
46
+ });
47
+ };
48
+
49
+ exports.ui = ui;
50
+ exports.uiPresets = uiPresets;
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;
@@ -16,6 +17,7 @@ export type ParameterObject = API.ParameterObject;
16
17
  export type RequestBodyObject = API.RequestBodyObject;
17
18
  export type ObjectType = ArraySchemaObjectType | NonArraySchemaObjectType;
18
19
  export type RefSchemaObject = ReferenceObject | SchemaObject;
20
+ export type Effect = () => void;
19
21
  export interface IValidationErrorData extends Record<string, any> {
20
22
  error: RulesErrors;
21
23
  }
@@ -56,13 +58,31 @@ export interface ServerStartParams {
56
58
  https: boolean;
57
59
  port: number;
58
60
  }
59
- export interface BaseSchemaProps<T> {
61
+ export type SchemaValues<T> = (T extends (number | string) ? Record<T, string> : never) | T[];
62
+ export interface SchemaProps<T> {
60
63
  default?: T;
64
+ deprecated?: boolean;
61
65
  description?: string;
62
66
  example?: T;
63
67
  examples?: T[];
68
+ nullable?: boolean;
69
+ readOnly?: boolean;
64
70
  ref?: string;
71
+ title?: string;
72
+ value?: T;
73
+ values?: SchemaValues<T>;
74
+ writeOnly?: boolean;
65
75
  }
66
- export interface ValuesSchemaProps<T> extends BaseSchemaProps<T> {
67
- values?: T[];
76
+ export type TResponse = Record<ResponseStatus, unknown>;
77
+ export interface TEndpoint {
78
+ body?: unknown;
79
+ cookies?: Record<string, string>;
80
+ headers?: Record<string, string>;
81
+ params?: Record<string, unknown>;
82
+ response?: Record<string, TResponse>;
83
+ search?: Record<string, unknown>;
84
+ }
85
+ export interface ApiEndpoints {
86
+ }
87
+ export interface ApiSchemas {
68
88
  }
@@ -1,3 +1,12 @@
1
+ function getElement(docs, target) {
2
+ var _a, _b, _c;
3
+ if (!target)
4
+ return target;
5
+ if ('$ref' in target) {
6
+ return (_c = (_b = (_a = docs.components) === null || _a === void 0 ? void 0 : _a.schemas) === null || _b === void 0 ? void 0 : _b[String(target.$ref).replace('#/components/schemas/', '')]) !== null && _c !== void 0 ? _c : target;
7
+ }
8
+ return target;
9
+ }
1
10
  function hasDefault(target) {
2
11
  return Boolean(target && ('default' in target || 'x-default' in target));
3
12
  }
@@ -6,88 +15,95 @@ function generateSchemaTypes(schema, spaces = 2, lastChar = '\n') {
6
15
  if ('$ref' in schema) {
7
16
  return `Schemas.${schema.$ref.slice(21)}${lastChar}`;
8
17
  }
9
- if (schema.type === 'integer') {
10
- return `${schema.format === 'int64' ? 'bigint' : 'number'}${lastChar}`;
11
- }
12
- if (schema.type === 'string') {
13
- if (schema.format === 'date-time') {
14
- return `Date${lastChar}`;
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;
15
33
  }
16
- if (schema.format === 'binary') {
17
- return `Bin${lastChar}`;
34
+ if (!type) {
35
+ scope += `${operator}any`;
36
+ continue;
18
37
  }
19
- return `string${lastChar}`;
20
- }
21
- if (['boolean', 'null', 'number'].includes(schema.type)) {
22
- return `${schema.type}${lastChar}`;
23
- }
24
- if (schema.oneOf) {
25
- let result = '';
26
- for (const item of schema.oneOf) {
27
- if (result) {
28
- 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;
46
+ }
47
+ if (schema.format === 'binary') {
48
+ scope += `${operator}Bin`;
49
+ continue;
29
50
  }
30
- result += generateSchemaTypes(item, spaces + 2, '');
51
+ scope += `${operator}string`;
52
+ continue;
31
53
  }
32
- return result + lastChar;
33
- }
34
- if (schema.type === 'array') {
35
- if (!schema.items)
36
- return `any[]${lastChar}`;
37
- return `Array<${generateSchemaTypes(schema.items, spaces + 2, '')}>${lastChar}`;
38
- }
39
- if (!schema.type) {
40
- return `any${lastChar}`;
41
- }
42
- if (schema.type !== 'object') {
43
- console.error('unknown type', schema);
44
- return `any${lastChar}`;
45
- }
46
- let result = '{\n';
47
- const required = schema.required || [];
48
- const hasProps = Boolean(schema.properties && Object.keys(schema.properties).length);
49
- const hasRestProps = Boolean(typeof schema.additionalProperties === 'object' &&
50
- Object.keys(schema.additionalProperties).length);
51
- if (hasProps) {
52
- for (const key in schema.properties) {
53
- const prop = schema.properties[key];
54
- const splitter = required.includes(key) || hasDefault(prop)
55
- ? ':'
56
- : '?:';
57
- if ('deprecated' in prop && prop.deprecated) {
58
- 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;
59
62
  }
60
- result += `${space}${key}${splitter} ${generateSchemaTypes(prop, spaces + 2)}`;
63
+ scope += `${operator}Array<${generateSchemaTypes(schema.items, spaces + 2, '')}>`;
64
+ continue;
61
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)}}`;
62
95
  }
63
- if (hasRestProps) {
64
- const value = hasProps
65
- ? 'any\n'
66
- : generateSchemaTypes(schema.additionalProperties, spaces + 2);
67
- result += `${space}[key: string]: ${value}`;
68
- }
69
- return `${result}${space.slice(0, -2)}}${lastChar}`;
96
+ return `${scope}${lastChar}`;
70
97
  }
71
98
  function generateTypes(docs, namespace = 'Api') {
72
99
  var _a;
73
- let result = `declare namespace ${namespace} {
74
- export interface Bin {
75
- filename: string
76
- fieldName: string
77
- originalFilename: string
78
- path: string
79
- type: string
80
- disposition: string
81
- size: number
82
- extension?: string
83
- }
84
- `;
100
+ let result = '';
85
101
  const schemas = (_a = docs.components) === null || _a === void 0 ? void 0 : _a.schemas;
86
102
  const paths = docs.paths;
87
103
  if (schemas) {
88
- result += ' namespace Schemas {\n';
104
+ result += ' export interface Schemas {\n';
89
105
  for (const name in schemas) {
90
- result += ` export type ${name} = ${generateSchemaTypes(schemas[name], 6)}`;
106
+ result += ` ${name}: ${generateSchemaTypes(schemas[name], 6)}`;
91
107
  }
92
108
  result += ' }\n';
93
109
  }
@@ -112,27 +128,27 @@ function generateTypes(docs, namespace = 'Api') {
112
128
  query: '',
113
129
  };
114
130
  for (const param of parameters) {
115
- const splitter = param.in === 'path' || hasDefault(param.schema) || param.required ? ':' : '?:';
131
+ const splitter = param.in === 'path' || hasDefault(getElement(docs, param.schema)) || param.required ? ':' : '?:';
116
132
  params[param.in] += ` ${param.name}${splitter} ${generateSchemaTypes(param.schema)}`;
117
133
  }
118
134
  if (params.path) {
119
- result += ` Params: {\n${params.path} }\n`;
135
+ result += ` params: {\n${params.path} }\n`;
120
136
  }
121
137
  if (params.query) {
122
- result += ` Search: {\n${params.query} }\n`;
138
+ result += ` search: {\n${params.query} }\n`;
123
139
  }
124
140
  if (params.header) {
125
- result += ` Headers: {\n${params.header} }\n`;
141
+ result += ` headers: {\n${params.header} }\n`;
126
142
  }
127
143
  if (params.cookie) {
128
- result += ` Cookies: {\n${params.cookie} }\n`;
144
+ result += ` cookies: {\n${params.cookie} }\n`;
129
145
  }
130
146
  }
131
147
  if (requestBody) {
132
- result += ` Body: ${generateSchemaTypes(requestBody.content['multipart/form-data'].schema, 8)}`;
148
+ result += ` body: ${generateSchemaTypes(requestBody.content['multipart/form-data'].schema, 8)}`;
133
149
  }
134
150
  if (responses) {
135
- result += ' Response: {\n';
151
+ result += ' response: {\n';
136
152
  for (const key in responses) {
137
153
  let multiple = false;
138
154
  const response = responses[key];
@@ -156,7 +172,20 @@ function generateTypes(docs, namespace = 'Api') {
156
172
  result += ' }\n';
157
173
  }
158
174
  }
159
- return result + ' }\n}';
175
+ const body = result + ' }';
176
+ return `import '@innet/server'
177
+ ${body.includes('Bin') ? 'import { Bin } from \'@innet/server\'\n' : ''}
178
+ declare global {
179
+ namespace ${namespace} {
180
+ ${body}
181
+ }
182
+ }
183
+
184
+ declare module '@innet/server' {
185
+ interface ApiEndpoints extends ${namespace}.Endpoints {}
186
+ interface ApiSchemas extends ${namespace}.Schemas {}
187
+ }
188
+ `;
160
189
  }
161
190
 
162
191
  export { generateSchemaTypes, generateTypes };