@innet/server 1.6.1 → 1.6.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.
@@ -1,6 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  import { CookieSerializeOptions } from 'cookie';
3
3
  import { IncomingMessage, ServerResponse } from 'http';
4
+ import { FormOptions } from 'multiparty';
4
5
  import { ParsedQs } from 'qs';
5
6
  export declare const ACTION: string;
6
7
  export declare type Body = Record<string, any>;
@@ -22,12 +23,16 @@ export interface ActionOptions {
22
23
  body?: Body;
23
24
  files?: Files;
24
25
  }
26
+ export interface ActionParams {
27
+ multipartyForm?: FormOptions;
28
+ }
25
29
  export declare type Resources = Exclude<keyof ActionOptions, undefined>;
26
30
  export declare const URL_PARSER: RegExp;
27
31
  export declare class Action<O extends ActionOptions = ActionOptions> {
28
32
  readonly req: Request;
29
33
  readonly res: Response;
30
- constructor(req: Request, res: Response);
34
+ params: ActionParams;
35
+ constructor(req: Request, res: Response, params?: ActionParams);
31
36
  get cookies(): O['cookies'];
32
37
  setCookie(key: string, value?: string, opt?: CookieSerializeOptions): void;
33
38
  parseBody(): Promise<unknown>;
@@ -9,9 +9,10 @@ import 'qs';
9
9
  const ACTION = Symbol('Action');
10
10
  const URL_PARSER = /^(?<path>[^?]+)(\?(?<search>.*))?/;
11
11
  class Action {
12
- constructor(req, res) {
12
+ constructor(req, res, params = {}) {
13
13
  this.req = req;
14
14
  this.res = res;
15
+ this.params = params;
15
16
  }
16
17
  get cookies() {
17
18
  return cookie.parse(this.req.headers.cookie || '');
@@ -32,7 +33,7 @@ class Action {
32
33
  }
33
34
  parseBody() {
34
35
  return new Promise((resolve, reject) => {
35
- new multiparty.Form().parse(this.req, (err, fields, files) => {
36
+ new multiparty.Form(this.params.multipartyForm).parse(this.req, (err, fields, files) => {
36
37
  if (err) {
37
38
  reject(err);
38
39
  }
@@ -18,9 +18,11 @@ var multiparty__default = /*#__PURE__*/_interopDefaultLegacy(multiparty);
18
18
  var ACTION = Symbol('Action');
19
19
  var URL_PARSER = /^(?<path>[^?]+)(\?(?<search>.*))?/;
20
20
  var Action = /** @class */ (function () {
21
- function Action(req, res) {
21
+ function Action(req, res, params) {
22
+ if (params === void 0) { params = {}; }
22
23
  this.req = req;
23
24
  this.res = res;
25
+ this.params = params;
24
26
  }
25
27
  Object.defineProperty(Action.prototype, "cookies", {
26
28
  get: function () {
@@ -46,7 +48,7 @@ var Action = /** @class */ (function () {
46
48
  Action.prototype.parseBody = function () {
47
49
  var _this = this;
48
50
  return new Promise(function (resolve, reject) {
49
- new multiparty__default["default"].Form().parse(_this.req, function (err, fields, files) {
51
+ new multiparty__default["default"].Form(_this.params.multipartyForm).parse(_this.req, function (err, fields, files) {
50
52
  if (err) {
51
53
  reject(err);
52
54
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@innet/server",
3
- "version": "1.6.1",
3
+ "version": "1.6.3",
4
4
  "description": "Create server-side application with innet",
5
5
  "main": "index.js",
6
6
  "module": "index.es6.js",
@@ -22,7 +22,8 @@
22
22
  },
23
23
  "homepage": "https://github.com/d8corp/innet-server",
24
24
  "dependencies": {
25
- "@cantinc/utils": "^1.2.4",
25
+ "@types/multiparty": "^0.0.33",
26
+ "@cantinc/utils": "^1.2.7",
26
27
  "@innet/html": "^1.0.1",
27
28
  "@innet/jsx": "^1.1.0",
28
29
  "@innet/switch": "^1.0.0",
@@ -13,4 +13,4 @@ export interface ValidationContext {
13
13
  handleError?: (e: ValidationResponse<any>) => any;
14
14
  }
15
15
  export declare const validationContext: Context<ValidationContext, ValidationContext>;
16
- export declare function validation<T extends object, E extends object>({ props, children }: ValidationJsxElement<T>, handler: any): Promise<any>;
16
+ export declare function validation<T extends object, E extends object>({ props, children }: ValidationJsxElement<T>, handler: any): any;
@@ -14,13 +14,12 @@ function validation({ props, children }, handler) {
14
14
  const data = action[resource];
15
15
  if (!data)
16
16
  throw Error(`cannot find ${resource} in action`);
17
- return validation$1(map, data).then(e => {
18
- if (e) {
19
- const { handleError } = validationContext.get(handler);
20
- return handleError ? innet(handleError(e), handler) : undefined;
21
- }
22
- return innet(children, handler);
23
- });
17
+ const result = validation$1(map, data);
18
+ if (result) {
19
+ const { handleError } = validationContext.get(handler);
20
+ return handleError ? innet(handleError(result), handler) : undefined;
21
+ }
22
+ return innet(children, handler);
24
23
  };
25
24
  if (resource === 'body' || resource === 'files') {
26
25
  return action.parseBody().then(run);
@@ -23,13 +23,12 @@ function validation(_a, handler) {
23
23
  var data = action[resource];
24
24
  if (!data)
25
25
  throw Error("cannot find ".concat(resource, " in action"));
26
- return utils.validation(map, data).then(function (e) {
27
- if (e) {
28
- var handleError = validationContext.get(handler).handleError;
29
- return handleError ? innet__default["default"](handleError(e), handler) : undefined;
30
- }
31
- return innet__default["default"](children, handler);
32
- });
26
+ var result = utils.validation(map, data);
27
+ if (result) {
28
+ var handleError = validationContext.get(handler).handleError;
29
+ return handleError ? innet__default["default"](handleError(result), handler) : undefined;
30
+ }
31
+ return innet__default["default"](children, handler);
33
32
  };
34
33
  if (resource === 'body' || resource === 'files') {
35
34
  return action.parseBody().then(run);
@@ -1,10 +1,11 @@
1
1
  import { Handler } from 'innet';
2
- import { Action } from '../action';
2
+ import { Action, ActionParams } from '../action';
3
3
  export interface SSL {
4
4
  cert: string;
5
5
  key: string;
6
6
  }
7
7
  export interface ServerProps {
8
+ actionParams?: ActionParams;
8
9
  port?: number;
9
10
  ssl?: SSL;
10
11
  unknownError?: string;
@@ -10,7 +10,7 @@ import { CONTINUE } from '../constants.es6.js';
10
10
  const isInvalidPath = require('is-invalid-path');
11
11
  function server({ props = {}, children }, handler) {
12
12
  const { env } = process;
13
- let { ssl: { key = env.SSL_KEY, cert = env.SSL_CRT } = {} } = props;
13
+ let { ssl: { key = env.SSL_KEY, cert = env.SSL_CRT } = {}, actionParams } = props;
14
14
  if (!isInvalidPath(key)) {
15
15
  key = fs.readFileSync(key).toString();
16
16
  }
@@ -27,7 +27,7 @@ function server({ props = {}, children }, handler) {
27
27
  });
28
28
  server.on('request', (req, res) => __awaiter(this, void 0, void 0, function* () {
29
29
  const childHandler = Object.create(handler);
30
- childHandler[ACTION] = new Action(req, res);
30
+ childHandler[ACTION] = new Action(req, res, actionParams);
31
31
  if (onRequest) {
32
32
  yield onRequest(childHandler[ACTION]);
33
33
  }
package/server/server.js CHANGED
@@ -23,7 +23,7 @@ function server(_a, handler) {
23
23
  var _this = this;
24
24
  var _b = _a.props, props = _b === void 0 ? {} : _b, children = _a.children;
25
25
  var env = process.env;
26
- var _c = props.ssl, _d = _c === void 0 ? {} : _c, _e = _d.key, key = _e === void 0 ? env.SSL_KEY : _e, _f = _d.cert, cert = _f === void 0 ? env.SSL_CRT : _f;
26
+ var _c = props.ssl, _d = _c === void 0 ? {} : _c, _e = _d.key, key = _e === void 0 ? env.SSL_KEY : _e, _f = _d.cert, cert = _f === void 0 ? env.SSL_CRT : _f, actionParams = props.actionParams;
27
27
  if (!isInvalidPath(key)) {
28
28
  key = fs__default["default"].readFileSync(key).toString();
29
29
  }
@@ -44,7 +44,7 @@ function server(_a, handler) {
44
44
  switch (_a.label) {
45
45
  case 0:
46
46
  childHandler = Object.create(handler);
47
- childHandler[Action.ACTION] = new Action.Action(req, res);
47
+ childHandler[Action.ACTION] = new Action.Action(req, res, actionParams);
48
48
  if (!onRequest) return [3 /*break*/, 2];
49
49
  return [4 /*yield*/, onRequest(childHandler[Action.ACTION])];
50
50
  case 1: