@innet/server 1.5.1 → 1.5.2

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.
@@ -3,7 +3,7 @@ import { context, ContextProps, slot, SlotProps, slots, SlotsProps } from '@inne
3
3
  import { switchAsync, SwitchProps } from '@innet/switch';
4
4
  import { arrayAsync, async } from '@innet/utils';
5
5
  import { serverFn } from '../experimental/serverFn';
6
- import { cms, CmsProps, cookie, CookieProps, error, ErrorProps, file, FileProps, header, HeaderProps, proxy, ProxyProps, redirect, RedirectProps, router, RouterProps, success, SuccessProps, validation, ValidationProps } from '../plugins';
6
+ import { cms, CmsProps, cookie, CookieProps, error, ErrorProps, file, FileProps, formatter, FormatterProps, header, HeaderProps, proxy, ProxyProps, redirect, RedirectProps, router, RouterProps, success, SuccessProps, validation, ValidationProps } from '../plugins';
7
7
  import { server, ServerProps } from '../server';
8
8
  export declare const arrayPlugins: (typeof arrayAsync)[];
9
9
  export declare const JSXPlugins: {
@@ -20,6 +20,7 @@ export declare const JSXPlugins: {
20
20
  proxy: typeof proxy;
21
21
  redirect: typeof redirect;
22
22
  validation: typeof validation;
23
+ formatter: typeof formatter;
23
24
  context: typeof context;
24
25
  slot: typeof slot;
25
26
  slots: typeof slots;
@@ -43,6 +44,7 @@ declare global {
43
44
  switch: SwitchProps;
44
45
  proxy: ProxyProps;
45
46
  validation: ValidationProps<any>;
47
+ formatter: FormatterProps<any>;
46
48
  context: ContextProps;
47
49
  slot: SlotProps;
48
50
  slots: SlotsProps;
@@ -14,6 +14,7 @@ import { file } from '../plugins/file/file.es6.js';
14
14
  import { proxy } from '../plugins/proxy/proxy.es6.js';
15
15
  import { redirect } from '../plugins/redirect/redirect.es6.js';
16
16
  import { validation } from '../plugins/validation/validation.es6.js';
17
+ import { formatter } from '../plugins/formatter/formatter.es6.js';
17
18
  import { server } from '../server/server.es6.js';
18
19
 
19
20
  const arrayPlugins = [
@@ -35,6 +36,7 @@ const JSXPlugins = {
35
36
  proxy,
36
37
  redirect,
37
38
  validation,
39
+ formatter,
38
40
  context,
39
41
  slot,
40
42
  slots,
@@ -18,6 +18,7 @@ var file = require('../plugins/file/file.js');
18
18
  var proxy = require('../plugins/proxy/proxy.js');
19
19
  var redirect = require('../plugins/redirect/redirect.js');
20
20
  var validation = require('../plugins/validation/validation.js');
21
+ var formatter = require('../plugins/formatter/formatter.js');
21
22
  var server = require('../server/server.js');
22
23
 
23
24
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
@@ -43,6 +44,7 @@ var JSXPlugins = {
43
44
  proxy: proxy.proxy,
44
45
  redirect: redirect.redirect,
45
46
  validation: validation.validation,
47
+ formatter: formatter.formatter,
46
48
  context: jsx.context,
47
49
  slot: jsx.slot,
48
50
  slots: jsx.slots,
package/index.es6.js CHANGED
@@ -10,6 +10,7 @@ export { file } from './plugins/file/file.es6.js';
10
10
  export { proxy } from './plugins/proxy/proxy.es6.js';
11
11
  export { redirect, redirectStatuses } from './plugins/redirect/redirect.es6.js';
12
12
  export { validation, validationContext } from './plugins/validation/validation.es6.js';
13
+ export { formatter } from './plugins/formatter/formatter.es6.js';
13
14
  export { ACTION, Action, URL_PARSER, useAction } from './action/Action/Action.es6.js';
14
15
  export { parseSearch } from './utils/parseSearch/parseSearch.es6.js';
15
16
  export { stringifySearch } from './utils/stringifySearch/stringifySearch.es6.js';
package/index.js CHANGED
@@ -14,6 +14,7 @@ var file = require('./plugins/file/file.js');
14
14
  var proxy = require('./plugins/proxy/proxy.js');
15
15
  var redirect = require('./plugins/redirect/redirect.js');
16
16
  var validation = require('./plugins/validation/validation.js');
17
+ var formatter = require('./plugins/formatter/formatter.js');
17
18
  var Action = require('./action/Action/Action.js');
18
19
  var parseSearch = require('./utils/parseSearch/parseSearch.js');
19
20
  var stringifySearch = require('./utils/stringifySearch/stringifySearch.js');
@@ -45,6 +46,7 @@ exports.redirect = redirect.redirect;
45
46
  exports.redirectStatuses = redirect.redirectStatuses;
46
47
  exports.validation = validation.validation;
47
48
  exports.validationContext = validation.validationContext;
49
+ exports.formatter = formatter.formatter;
48
50
  exports.ACTION = Action.ACTION;
49
51
  exports.Action = Action.Action;
50
52
  exports.URL_PARSER = Action.URL_PARSER;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@innet/server",
3
- "version": "1.5.1",
3
+ "version": "1.5.2",
4
4
  "description": "Create server-side application with innet",
5
5
  "main": "index.js",
6
6
  "module": "index.es6.js",
@@ -0,0 +1,16 @@
1
+ import { Resources } from '../../action';
2
+ export interface Formatter<V> {
3
+ (value?: any): V;
4
+ }
5
+ export declare type FormatterMap<B> = {
6
+ [K in keyof B]?: Formatter<B[K]>[];
7
+ };
8
+ export interface FormatterProps<T> {
9
+ map: FormatterMap<T>;
10
+ resource?: Resources;
11
+ }
12
+ export interface FormatterJsxElement<T> {
13
+ props: FormatterProps<T>;
14
+ children?: any;
15
+ }
16
+ export declare function formatter<T extends object, E extends object>({ props, children }: FormatterJsxElement<T>, handler: any): any;
@@ -0,0 +1,29 @@
1
+ import innet from 'innet';
2
+ import { ACTION } from '../../action/Action/Action.es6.js';
3
+
4
+ function formatter({ props, children }, handler) {
5
+ const action = handler[ACTION];
6
+ if (!action) {
7
+ throw Error('`validation` should be inside `server`');
8
+ }
9
+ const { map, resource = 'body' } = props;
10
+ const run = () => {
11
+ const data = action[resource];
12
+ if (!data)
13
+ throw Error(`cannot find ${resource} in action`);
14
+ for (const key in map) {
15
+ if (key in data) {
16
+ for (const format of map[key]) {
17
+ data[key] = format(data[key]);
18
+ }
19
+ }
20
+ }
21
+ return innet(children, handler);
22
+ };
23
+ if (resource === 'body' || resource === 'files') {
24
+ return action.parseBody().then(run);
25
+ }
26
+ return run();
27
+ }
28
+
29
+ export { formatter };
@@ -0,0 +1,50 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var tslib = require('tslib');
6
+ var innet = require('innet');
7
+ var Action = require('../../action/Action/Action.js');
8
+
9
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
10
+
11
+ var innet__default = /*#__PURE__*/_interopDefaultLegacy(innet);
12
+
13
+ function formatter(_a, handler) {
14
+ var props = _a.props, children = _a.children;
15
+ var action = handler[Action.ACTION];
16
+ if (!action) {
17
+ throw Error('`validation` should be inside `server`');
18
+ }
19
+ var map = props.map, _b = props.resource, resource = _b === void 0 ? 'body' : _b;
20
+ var run = function () {
21
+ var e_1, _a;
22
+ var data = action[resource];
23
+ if (!data)
24
+ throw Error("cannot find ".concat(resource, " in action"));
25
+ for (var key in map) {
26
+ if (key in data) {
27
+ try {
28
+ for (var _b = (e_1 = void 0, tslib.__values(map[key])), _c = _b.next(); !_c.done; _c = _b.next()) {
29
+ var format = _c.value;
30
+ data[key] = format(data[key]);
31
+ }
32
+ }
33
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
34
+ finally {
35
+ try {
36
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
37
+ }
38
+ finally { if (e_1) throw e_1.error; }
39
+ }
40
+ }
41
+ }
42
+ return innet__default["default"](children, handler);
43
+ };
44
+ if (resource === 'body' || resource === 'files') {
45
+ return action.parseBody().then(run);
46
+ }
47
+ return run();
48
+ }
49
+
50
+ exports.formatter = formatter;
@@ -0,0 +1 @@
1
+ export * from './formatter';
@@ -0,0 +1 @@
1
+ export { formatter } from './formatter.es6.js';
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var formatter = require('./formatter.js');
6
+
7
+
8
+
9
+ exports.formatter = formatter.formatter;
@@ -8,3 +8,4 @@ export * from './file';
8
8
  export * from './proxy';
9
9
  export * from './redirect';
10
10
  export * from './validation';
11
+ export * from './formatter';
@@ -8,3 +8,4 @@ export { file } from './file/file.es6.js';
8
8
  export { proxy } from './proxy/proxy.es6.js';
9
9
  export { redirect, redirectStatuses } from './redirect/redirect.es6.js';
10
10
  export { validation, validationContext } from './validation/validation.es6.js';
11
+ export { formatter } from './formatter/formatter.es6.js';
package/plugins/index.js CHANGED
@@ -12,6 +12,7 @@ var file = require('./file/file.js');
12
12
  var proxy = require('./proxy/proxy.js');
13
13
  var redirect = require('./redirect/redirect.js');
14
14
  var validation = require('./validation/validation.js');
15
+ var formatter = require('./formatter/formatter.js');
15
16
 
16
17
 
17
18
 
@@ -32,3 +33,4 @@ exports.redirect = redirect.redirect;
32
33
  exports.redirectStatuses = redirect.redirectStatuses;
33
34
  exports.validation = validation.validation;
34
35
  exports.validationContext = validation.validationContext;
36
+ exports.formatter = formatter.formatter;
@@ -1,3 +1,4 @@
1
+ import innet from 'innet';
1
2
  import { validation as validation$1 } from '@cantinc/utils';
2
3
  import { Context } from '@innet/jsx';
3
4
  import { ACTION } from '../../action/Action/Action.es6.js';
@@ -17,9 +18,9 @@ function validation({ props, children }, handler) {
17
18
  throw Error(`cannot find ${resource} in action`);
18
19
  return validation$1(map, data).then(e => {
19
20
  if (e) {
20
- return validationContext.get(handler).handleError(e);
21
+ return innet(validationContext.get(handler).handleError(e), handler);
21
22
  }
22
- return children;
23
+ return innet(children, handler);
23
24
  });
24
25
  };
25
26
  if (resource === 'body' || resource === 'files') {
@@ -2,10 +2,15 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ var innet = require('innet');
5
6
  var utils = require('@cantinc/utils');
6
7
  var jsx = require('@innet/jsx');
7
8
  var Action = require('../../action/Action/Action.js');
8
9
 
10
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
+
12
+ var innet__default = /*#__PURE__*/_interopDefaultLegacy(innet);
13
+
9
14
  var validationContext = new jsx.Context({
10
15
  handleError: function () { },
11
16
  });
@@ -22,9 +27,9 @@ function validation(_a, handler) {
22
27
  throw Error("cannot find ".concat(resource, " in action"));
23
28
  return utils.validation(map, data).then(function (e) {
24
29
  if (e) {
25
- return validationContext.get(handler).handleError(e);
30
+ return innet__default["default"](validationContext.get(handler).handleError(e), handler);
26
31
  }
27
- return children;
32
+ return innet__default["default"](children, handler);
28
33
  });
29
34
  };
30
35
  if (resource === 'body' || resource === 'files') {