@innet/server 2.0.0-beta.13 → 2.0.0-beta.14

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.
@@ -6,7 +6,7 @@ const actionContext = new Context();
6
6
  function useAction() {
7
7
  const action = useContext(actionContext);
8
8
  if (!action) {
9
- useThrow('<{type}> MUST be in <request>, <preset> or <fallback>');
9
+ useThrow('<{type}> MUST be in <return> or <preset>');
10
10
  }
11
11
  return action;
12
12
  }
@@ -10,7 +10,7 @@ const actionContext = new jsx.Context();
10
10
  function useAction() {
11
11
  const action = jsx.useContext(actionContext);
12
12
  if (!action) {
13
- useThrow.useThrow('<{type}> MUST be in <request>, <preset> or <fallback>');
13
+ useThrow.useThrow('<{type}> MUST be in <return> or <preset>');
14
14
  }
15
15
  return action;
16
16
  }
@@ -1,5 +1,5 @@
1
1
  import { type ObjectType, type SchemaObject, type ValuesSchemaProps } from '../../types';
2
2
  export type SchemaType = 'any' | ObjectType;
3
3
  type TypeMap<T extends SchemaType> = T extends 'integer' | 'number' ? number : T extends 'string' ? string : T extends 'object' ? object : T extends 'array' ? any[] : T extends 'boolean' ? boolean : T extends 'null' ? null : T extends 'any' ? any : unknown;
4
- export declare function useSchemaType<T extends SchemaType>(type: T, { example, examples, ref, values, ...options }?: ValuesSchemaProps<TypeMap<T>> | undefined): SchemaObject | undefined;
4
+ export declare function useSchemaType<T extends SchemaType>(type: T, { example, examples, nullable, ref, value, values, ...options }?: ValuesSchemaProps<TypeMap<T>> | undefined): SchemaObject | undefined;
5
5
  export {};
@@ -3,7 +3,7 @@ import '../useNewSchema/index.es6.js';
3
3
  import { useApi } from '../useApi/useApi.es6.js';
4
4
  import { useNewSchema } from '../useNewSchema/useNewSchema.es6.js';
5
5
 
6
- function useSchemaType(type, { example, examples, ref, values, ...options } = {}) {
6
+ function useSchemaType(type, { example, examples, nullable, ref, value, values, ...options } = {}) {
7
7
  var _a;
8
8
  if (ref) {
9
9
  const { docs } = useApi();
@@ -21,10 +21,11 @@ function useSchemaType(type, { example, examples, ref, values, ...options } = {}
21
21
  }
22
22
  return (docs.components.schemas[ref] = {
23
23
  ...options,
24
+ const: value,
24
25
  enum: values,
25
26
  example,
26
27
  examples,
27
- type: type === 'any' ? undefined : type,
28
+ type: type === 'any' ? undefined : nullable ? [type, 'null'] : type,
28
29
  });
29
30
  }
30
31
  const arrayValues = values ? Array.isArray(values) ? values : Object.keys(values) : values;
@@ -36,10 +37,11 @@ function useSchemaType(type, { example, examples, ref, values, ...options } = {}
36
37
  return useNewSchema({
37
38
  ...options,
38
39
  ...enumDescription,
40
+ const: value,
39
41
  enum: arrayValues,
40
42
  example,
41
43
  examples,
42
- type: type === 'any' ? undefined : type,
44
+ type: type === 'any' ? undefined : nullable ? [type, 'null'] : type,
43
45
  });
44
46
  }
45
47
 
@@ -7,7 +7,7 @@ require('../useNewSchema/index.js');
7
7
  var useApi = require('../useApi/useApi.js');
8
8
  var useNewSchema = require('../useNewSchema/useNewSchema.js');
9
9
 
10
- function useSchemaType(type, { example, examples, ref, values, ...options } = {}) {
10
+ function useSchemaType(type, { example, examples, nullable, ref, value, values, ...options } = {}) {
11
11
  var _a;
12
12
  if (ref) {
13
13
  const { docs } = useApi.useApi();
@@ -25,10 +25,11 @@ function useSchemaType(type, { example, examples, ref, values, ...options } = {}
25
25
  }
26
26
  return (docs.components.schemas[ref] = {
27
27
  ...options,
28
+ const: value,
28
29
  enum: values,
29
30
  example,
30
31
  examples,
31
- type: type === 'any' ? undefined : type,
32
+ type: type === 'any' ? undefined : nullable ? [type, 'null'] : type,
32
33
  });
33
34
  }
34
35
  const arrayValues = values ? Array.isArray(values) ? values : Object.keys(values) : values;
@@ -40,10 +41,11 @@ function useSchemaType(type, { example, examples, ref, values, ...options } = {}
40
41
  return useNewSchema.useNewSchema({
41
42
  ...options,
42
43
  ...enumDescription,
44
+ const: value,
43
45
  enum: arrayValues,
44
46
  example,
45
47
  examples,
46
- type: type === 'any' ? undefined : type,
48
+ type: type === 'any' ? undefined : nullable ? [type, 'null'] : type,
47
49
  });
48
50
  }
49
51
 
@@ -6,7 +6,7 @@ import { useThrow } from '../useThrow/useThrow.es6.js';
6
6
  function useSearch() {
7
7
  const action = useAction();
8
8
  if (!action) {
9
- useThrow('<{type}> MUST be in <request> or <fallback>');
9
+ useThrow('<{type}> MUST be in <return> or <preset>');
10
10
  }
11
11
  return action.search;
12
12
  }
@@ -10,7 +10,7 @@ var useThrow = require('../useThrow/useThrow.js');
10
10
  function useSearch() {
11
11
  const action = useAction.useAction();
12
12
  if (!action) {
13
- useThrow.useThrow('<{type}> MUST be in <request> or <fallback>');
13
+ useThrow.useThrow('<{type}> MUST be in <return> or <preset>');
14
14
  }
15
15
  return action.search;
16
16
  }
@@ -1,9 +1,11 @@
1
1
  import { Context } from '@innet/jsx';
2
2
  import { type Server as HttpServer } from 'http';
3
3
  import { type Server as HttpsServer } from 'https';
4
+ import { type ServerProps } from '../../plugins';
4
5
  export type ServerPlugin = () => any;
5
6
  export interface ServerContext {
6
7
  port: number;
8
+ props: ServerProps;
7
9
  server: HttpServer | HttpsServer;
8
10
  }
9
11
  export declare const serverContext: Context<ServerContext, ServerContext | undefined>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@innet/server",
3
- "version": "2.0.0-beta.13",
3
+ "version": "2.0.0-beta.14",
4
4
  "description": "Create server-side application with innet",
5
5
  "main": "index.js",
6
6
  "module": "index.es6.js",
@@ -3,6 +3,10 @@ import { type IncomingMessage, type ServerResponse } from 'http';
3
3
  import { type ServerStartParams, type SSL } from '../../../types';
4
4
  export interface ServerProps {
5
5
  children?: any;
6
+ formatError?: (target: {
7
+ data: any;
8
+ error: string;
9
+ }) => string;
6
10
  onClose?: () => any;
7
11
  onError?: (e: Error) => any;
8
12
  onRequest?: (req: IncomingMessage, res: ServerResponse) => any;
@@ -36,7 +36,7 @@ const server = () => {
36
36
  const { onClose, onError, onRequest, onStart, port = Number((_c = env.INNET_PORT) !== null && _c !== void 0 ? _c : (https ? 443 : 80)), } = props;
37
37
  const plugins = new Set();
38
38
  const server = https ? http2.createServer({ cert, key }) : http.createServer();
39
- serverContext.set(handler, { port, server });
39
+ serverContext.set(handler, { port, props, server });
40
40
  serverPlugins.set(handler, plugins);
41
41
  serverPortContext.set(handler, port);
42
42
  serverHttpsContext.set(handler, https);
@@ -46,7 +46,7 @@ const server = () => {
46
46
  const { onClose, onError, onRequest, onStart, port = Number((_c = env.INNET_PORT) !== null && _c !== void 0 ? _c : (https ? 443 : 80)), } = props;
47
47
  const plugins = new Set();
48
48
  const server = https ? http2__default["default"].createServer({ cert, key }) : http__default["default"].createServer();
49
- useServer.serverContext.set(handler, { port, server });
49
+ useServer.serverContext.set(handler, { port, props, server });
50
50
  useServerPlugins.serverPlugins.set(handler, plugins);
51
51
  useServerPort.serverPortContext.set(handler, port);
52
52
  useIsServerHttps.serverHttpsContext.set(handler, https);
@@ -1,6 +1,7 @@
1
1
  import { useProps } from '@innet/jsx';
2
2
  import '../../../hooks/index.es6.js';
3
3
  import '../../../utils/index.es6.js';
4
+ import { useServer } from '../../../hooks/useServer/useServer.es6.js';
4
5
  import { useResponse } from '../../../hooks/useResponse/useResponse.es6.js';
5
6
  import { JSONString } from '../../../utils/JSONString/JSONString.es6.js';
6
7
 
@@ -58,14 +59,17 @@ const errorStatuses = {
58
59
  webServerIsDown: 521,
59
60
  };
60
61
  const error = () => {
61
- const { children, ...props } = useProps();
62
+ var _a;
63
+ const server = useServer();
62
64
  const res = useResponse();
63
65
  if (!res) {
64
- throw Error('<error> MUST be in <request>');
66
+ throw Error('<error> MUST be in <return>');
65
67
  }
68
+ const { children, ...props } = useProps();
66
69
  const { code = 'undefined', status = 520, } = props;
67
70
  res.statusCode = typeof status === 'string' ? errorStatuses[status] : status;
68
- const content = JSONString({ data: children, error: code });
71
+ const format = (_a = server.props.formatError) !== null && _a !== void 0 ? _a : JSONString;
72
+ const content = format({ data: children, error: code });
69
73
  res.setHeader('Content-Type', 'application/json');
70
74
  res.setHeader('Content-Length', content.length);
71
75
  res.write(content);
@@ -5,6 +5,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var jsx = require('@innet/jsx');
6
6
  require('../../../hooks/index.js');
7
7
  require('../../../utils/index.js');
8
+ var useServer = require('../../../hooks/useServer/useServer.js');
8
9
  var useResponse = require('../../../hooks/useResponse/useResponse.js');
9
10
  var JSONString = require('../../../utils/JSONString/JSONString.js');
10
11
 
@@ -62,14 +63,17 @@ const errorStatuses = {
62
63
  webServerIsDown: 521,
63
64
  };
64
65
  const error = () => {
65
- const { children, ...props } = jsx.useProps();
66
+ var _a;
67
+ const server = useServer.useServer();
66
68
  const res = useResponse.useResponse();
67
69
  if (!res) {
68
- throw Error('<error> MUST be in <request>');
70
+ throw Error('<error> MUST be in <return>');
69
71
  }
72
+ const { children, ...props } = jsx.useProps();
70
73
  const { code = 'undefined', status = 520, } = props;
71
74
  res.statusCode = typeof status === 'string' ? errorStatuses[status] : status;
72
- const content = JSONString.JSONString({ data: children, error: code });
75
+ const format = (_a = server.props.formatError) !== null && _a !== void 0 ? _a : JSONString.JSONString;
76
+ const content = format({ data: children, error: code });
73
77
  res.setHeader('Content-Type', 'application/json');
74
78
  res.setHeader('Content-Length', content.length);
75
79
  res.write(content);
@@ -11,7 +11,7 @@ function file() {
11
11
  const { children, ...props } = useProps();
12
12
  const res = useResponse();
13
13
  if (!res) {
14
- useThrow('<{type}> MUST be in <request> or <fallback>');
14
+ useThrow('<{type}> MUST be in <return> or <preset>');
15
15
  }
16
16
  const { path } = props;
17
17
  if (fs.existsSync(path)) {
@@ -20,7 +20,7 @@ function file() {
20
20
  const { children, ...props } = jsx.useProps();
21
21
  const res = useResponse.useResponse();
22
22
  if (!res) {
23
- useThrow.useThrow('<{type}> MUST be in <request> or <fallback>');
23
+ useThrow.useThrow('<{type}> MUST be in <return> or <preset>');
24
24
  }
25
25
  const { path } = props;
26
26
  if (fs__default["default"].existsSync(path)) {
@@ -6,7 +6,7 @@ import { useThrow } from '../../../hooks/useThrow/useThrow.es6.js';
6
6
  const header = () => {
7
7
  const res = useResponse();
8
8
  if (!res) {
9
- useThrow('<{type}> MUST be in <request> or <fallback>');
9
+ useThrow('<{type}> MUST be in <return> or <preset>');
10
10
  }
11
11
  const { key, value, } = useProps();
12
12
  res.setHeader(key, value);
@@ -10,7 +10,7 @@ var useThrow = require('../../../hooks/useThrow/useThrow.js');
10
10
  const header = () => {
11
11
  const res = useResponse.useResponse();
12
12
  if (!res) {
13
- useThrow.useThrow('<{type}> MUST be in <request> or <fallback>');
13
+ useThrow.useThrow('<{type}> MUST be in <return> or <preset>');
14
14
  }
15
15
  const { key, value, } = jsx.useProps();
16
16
  res.setHeader(key, value);
@@ -1,7 +1,7 @@
1
1
  import { type HandlerPlugin } from 'innet';
2
2
  import { type ValuesSchemaProps } from '../../../types';
3
3
  import { type DateFormat } from '../../../utils';
4
- export interface DateProps extends Omit<ValuesSchemaProps<DateFormat>, 'const'> {
4
+ export interface DateProps extends Omit<ValuesSchemaProps<DateFormat>, 'value'> {
5
5
  const?: string;
6
6
  max?: DateFormat;
7
7
  min?: DateFormat;
@@ -15,10 +15,10 @@ const integer = () => {
15
15
  const { default: defaultValue, example, examples, exclusiveMaximum, exclusiveMinimum, format = 'int32', max: max$1, min: min$1, multipleOf, values: values$1, ...props } = useProps() || {};
16
16
  const schema = useSchemaType('integer', {
17
17
  ...props,
18
- const: props.const !== undefined ? Number(props.const) : undefined,
19
18
  default: defaultValue !== undefined ? Number(defaultValue) : undefined,
20
19
  example: example !== undefined ? Number(example) : undefined,
21
20
  examples: examples === null || examples === void 0 ? void 0 : examples.map(Number),
21
+ value: props.value !== undefined ? Number(props.value) : undefined,
22
22
  values: values$1 && getArrayValues(values$1, Number),
23
23
  });
24
24
  if (schema) {
@@ -19,10 +19,10 @@ const integer = () => {
19
19
  const { default: defaultValue, example, examples, exclusiveMaximum, exclusiveMinimum, format = 'int32', max: max$1, min: min$1, multipleOf, values: values$1, ...props } = jsx.useProps() || {};
20
20
  const schema = useSchemaType.useSchemaType('integer', {
21
21
  ...props,
22
- const: props.const !== undefined ? Number(props.const) : undefined,
23
22
  default: defaultValue !== undefined ? Number(defaultValue) : undefined,
24
23
  example: example !== undefined ? Number(example) : undefined,
25
24
  examples: examples === null || examples === void 0 ? void 0 : examples.map(Number),
25
+ value: props.value !== undefined ? Number(props.value) : undefined,
26
26
  values: values$1 && values.getArrayValues(values$1, Number),
27
27
  });
28
28
  if (schema) {
package/types.d.ts CHANGED
@@ -63,6 +63,7 @@ export interface BaseSchemaProps<T> {
63
63
  description?: string;
64
64
  example?: T;
65
65
  examples?: T[];
66
+ nullable?: boolean;
66
67
  readOnly?: boolean;
67
68
  ref?: string;
68
69
  title?: string;
@@ -70,6 +71,6 @@ export interface BaseSchemaProps<T> {
70
71
  }
71
72
  export type SchemaValues<T> = (T extends (number | string) ? Record<T, string> : never) | T[];
72
73
  export interface ValuesSchemaProps<T> extends BaseSchemaProps<T> {
73
- const?: T;
74
+ value?: T;
74
75
  values?: SchemaValues<T>;
75
76
  }
@@ -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
  }
@@ -112,7 +121,7 @@ function generateTypes(docs, namespace = 'Api') {
112
121
  query: '',
113
122
  };
114
123
  for (const param of parameters) {
115
- const splitter = param.in === 'path' || hasDefault(param.schema) || param.required ? ':' : '?:';
124
+ const splitter = param.in === 'path' || hasDefault(getElement(docs, param.schema)) || param.required ? ':' : '?:';
116
125
  params[param.in] += ` ${param.name}${splitter} ${generateSchemaTypes(param.schema)}`;
117
126
  }
118
127
  if (params.path) {
@@ -2,6 +2,15 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ function getElement(docs, target) {
6
+ var _a, _b, _c;
7
+ if (!target)
8
+ return target;
9
+ if ('$ref' in target) {
10
+ 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;
11
+ }
12
+ return target;
13
+ }
5
14
  function hasDefault(target) {
6
15
  return Boolean(target && ('default' in target || 'x-default' in target));
7
16
  }
@@ -116,7 +125,7 @@ function generateTypes(docs, namespace = 'Api') {
116
125
  query: '',
117
126
  };
118
127
  for (const param of parameters) {
119
- const splitter = param.in === 'path' || hasDefault(param.schema) || param.required ? ':' : '?:';
128
+ const splitter = param.in === 'path' || hasDefault(getElement(docs, param.schema)) || param.required ? ':' : '?:';
120
129
  params[param.in] += ` ${param.name}${splitter} ${generateSchemaTypes(param.schema)}`;
121
130
  }
122
131
  if (params.path) {