@innet/server 1.5.1 → 1.5.3

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 { access, AccessProps, 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,9 +20,11 @@ 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;
27
+ access: typeof access;
26
28
  };
27
29
  export declare const fnPlugins: (typeof serverFn)[];
28
30
  export declare const objectPlugins: ((handler: any) => import("innet").PluginHandler)[];
@@ -43,9 +45,11 @@ declare global {
43
45
  switch: SwitchProps;
44
46
  proxy: ProxyProps;
45
47
  validation: ValidationProps<any>;
48
+ formatter: FormatterProps<any>;
46
49
  context: ContextProps;
47
50
  slot: SlotProps;
48
51
  slots: SlotsProps;
52
+ access: AccessProps;
49
53
  }
50
54
  }
51
55
  }
@@ -14,6 +14,8 @@ 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';
18
+ import { access } from '../plugins/access/access.es6.js';
17
19
  import { server } from '../server/server.es6.js';
18
20
 
19
21
  const arrayPlugins = [
@@ -35,9 +37,11 @@ const JSXPlugins = {
35
37
  proxy,
36
38
  redirect,
37
39
  validation,
40
+ formatter,
38
41
  context,
39
42
  slot,
40
43
  slots,
44
+ access,
41
45
  };
42
46
  const fnPlugins = [
43
47
  serverFn,
@@ -18,6 +18,8 @@ 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');
22
+ var access = require('../plugins/access/access.js');
21
23
  var server = require('../server/server.js');
22
24
 
23
25
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
@@ -43,9 +45,11 @@ var JSXPlugins = {
43
45
  proxy: proxy.proxy,
44
46
  redirect: redirect.redirect,
45
47
  validation: validation.validation,
48
+ formatter: formatter.formatter,
46
49
  context: jsx.context,
47
50
  slot: jsx.slot,
48
51
  slots: jsx.slots,
52
+ access: access.access,
49
53
  };
50
54
  var fnPlugins = [
51
55
  serverFn.serverFn,
package/index.es6.js CHANGED
@@ -10,6 +10,8 @@ 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';
14
+ export { access, accessContext } from './plugins/access/access.es6.js';
13
15
  export { ACTION, Action, URL_PARSER, useAction } from './action/Action/Action.es6.js';
14
16
  export { parseSearch } from './utils/parseSearch/parseSearch.es6.js';
15
17
  export { stringifySearch } from './utils/stringifySearch/stringifySearch.es6.js';
package/index.js CHANGED
@@ -14,6 +14,8 @@ 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');
18
+ var access = require('./plugins/access/access.js');
17
19
  var Action = require('./action/Action/Action.js');
18
20
  var parseSearch = require('./utils/parseSearch/parseSearch.js');
19
21
  var stringifySearch = require('./utils/stringifySearch/stringifySearch.js');
@@ -45,6 +47,9 @@ exports.redirect = redirect.redirect;
45
47
  exports.redirectStatuses = redirect.redirectStatuses;
46
48
  exports.validation = validation.validation;
47
49
  exports.validationContext = validation.validationContext;
50
+ exports.formatter = formatter.formatter;
51
+ exports.access = access.access;
52
+ exports.accessContext = access.accessContext;
48
53
  exports.ACTION = Action.ACTION;
49
54
  exports.Action = Action.Action;
50
55
  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.3",
4
4
  "description": "Create server-side application with innet",
5
5
  "main": "index.js",
6
6
  "module": "index.es6.js",
@@ -0,0 +1,14 @@
1
+ import { Handler } from 'innet';
2
+ import { Context } from '@innet/jsx';
3
+ export interface AccessProps {
4
+ role?: any;
5
+ }
6
+ export interface AccessJsxElement {
7
+ props: AccessProps;
8
+ children?: any;
9
+ }
10
+ export interface AccessContext {
11
+ handleRole?: (role: any, handler: Handler) => any;
12
+ }
13
+ export declare const accessContext: Context<AccessContext, AccessContext>;
14
+ export declare function access({ props, children }: AccessJsxElement, handler: any): any;
@@ -0,0 +1,23 @@
1
+ import innet from 'innet';
2
+ import { Context } from '@innet/jsx';
3
+ import { ACTION } from '../../action/Action/Action.es6.js';
4
+
5
+ const accessContext = new Context({});
6
+ function access({ props, children }, handler) {
7
+ const action = handler[ACTION];
8
+ if (!action) {
9
+ throw Error('`access` should be inside `server`');
10
+ }
11
+ const { handleRole } = accessContext.get(handler);
12
+ const role = props === null || props === void 0 ? void 0 : props.role;
13
+ if (!handleRole) {
14
+ return innet(children, handler);
15
+ }
16
+ const error = handleRole(role, handler);
17
+ if (error) {
18
+ return innet(error, handler);
19
+ }
20
+ return innet(children, handler);
21
+ }
22
+
23
+ export { access, accessContext };
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var innet = require('innet');
6
+ var jsx = require('@innet/jsx');
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
+ var accessContext = new jsx.Context({});
14
+ function access(_a, handler) {
15
+ var props = _a.props, children = _a.children;
16
+ var action = handler[Action.ACTION];
17
+ if (!action) {
18
+ throw Error('`access` should be inside `server`');
19
+ }
20
+ var handleRole = accessContext.get(handler).handleRole;
21
+ var role = props === null || props === void 0 ? void 0 : props.role;
22
+ if (!handleRole) {
23
+ return innet__default["default"](children, handler);
24
+ }
25
+ var error = handleRole(role, handler);
26
+ if (error) {
27
+ return innet__default["default"](error, handler);
28
+ }
29
+ return innet__default["default"](children, handler);
30
+ }
31
+
32
+ exports.access = access;
33
+ exports.accessContext = accessContext;
@@ -0,0 +1 @@
1
+ export * from './access';
@@ -0,0 +1 @@
1
+ export { access, accessContext } from './access.es6.js';
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var access = require('./access.js');
6
+
7
+
8
+
9
+ exports.access = access.access;
10
+ exports.accessContext = access.accessContext;
@@ -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('`formatter` 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('`formatter` 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,5 @@ export * from './file';
8
8
  export * from './proxy';
9
9
  export * from './redirect';
10
10
  export * from './validation';
11
+ export * from './formatter';
12
+ export * from './access';
@@ -8,3 +8,5 @@ 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';
12
+ export { access, accessContext } from './access/access.es6.js';
package/plugins/index.js CHANGED
@@ -12,6 +12,8 @@ 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');
16
+ var access = require('./access/access.js');
15
17
 
16
18
 
17
19
 
@@ -32,3 +34,6 @@ exports.redirect = redirect.redirect;
32
34
  exports.redirectStatuses = redirect.redirectStatuses;
33
35
  exports.validation = validation.validation;
34
36
  exports.validationContext = validation.validationContext;
37
+ exports.formatter = formatter.formatter;
38
+ exports.access = access.access;
39
+ exports.accessContext = access.accessContext;
@@ -10,7 +10,7 @@ export interface ValidationJsxElement<T> {
10
10
  children?: any;
11
11
  }
12
12
  export interface ValidationContext {
13
- handleError: (e: ValidationResponse<any>) => any;
13
+ handleError?: (e: ValidationResponse<any>) => any;
14
14
  }
15
15
  export declare const validationContext: Context<ValidationContext, ValidationContext>;
16
16
  export declare function validation<T extends object, E extends object>({ props, children }: ValidationJsxElement<T>, handler: any): Promise<any>;
@@ -1,10 +1,9 @@
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';
4
5
 
5
- const validationContext = new Context({
6
- handleError: () => { },
7
- });
6
+ const validationContext = new Context({});
8
7
  function validation({ props, children }, handler) {
9
8
  const action = handler[ACTION];
10
9
  if (!action) {
@@ -17,9 +16,10 @@ function validation({ props, children }, handler) {
17
16
  throw Error(`cannot find ${resource} in action`);
18
17
  return validation$1(map, data).then(e => {
19
18
  if (e) {
20
- return validationContext.get(handler).handleError(e);
19
+ const { handleError } = validationContext.get(handler);
20
+ return handleError ? innet(handleError(e), handler) : undefined;
21
21
  }
22
- return children;
22
+ return innet(children, handler);
23
23
  });
24
24
  };
25
25
  if (resource === 'body' || resource === 'files') {
@@ -2,13 +2,16 @@
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
 
9
- var validationContext = new jsx.Context({
10
- handleError: function () { },
11
- });
10
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
+
12
+ var innet__default = /*#__PURE__*/_interopDefaultLegacy(innet);
13
+
14
+ var validationContext = new jsx.Context({});
12
15
  function validation(_a, handler) {
13
16
  var props = _a.props, children = _a.children;
14
17
  var action = handler[Action.ACTION];
@@ -22,9 +25,10 @@ function validation(_a, handler) {
22
25
  throw Error("cannot find ".concat(resource, " in action"));
23
26
  return utils.validation(map, data).then(function (e) {
24
27
  if (e) {
25
- return validationContext.get(handler).handleError(e);
28
+ var handleError = validationContext.get(handler).handleError;
29
+ return handleError ? innet__default["default"](handleError(e), handler) : undefined;
26
30
  }
27
- return children;
31
+ return innet__default["default"](children, handler);
28
32
  });
29
33
  };
30
34
  if (resource === 'body' || resource === 'files') {