@innet/server 1.0.4 → 1.1.0

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.
package/README.md CHANGED
@@ -414,6 +414,47 @@ export default (
414
414
  In this case, if you have a file in cms folder then the file will return
415
415
  else makes request to the `site.com`.
416
416
 
417
+ ## redirect
418
+ You can redirect users to another resource.
419
+
420
+ ```typescript jsx
421
+ export default (
422
+ <server>
423
+ <switch>
424
+ <cms dir='cms' />
425
+ <redirect to='https://site.com' />
426
+ </switch>
427
+ </server>
428
+ )
429
+ ```
430
+
431
+ ### status
432
+ By default, status is `301`, you can change it with `status` prop.
433
+
434
+ ```typescript jsx
435
+ export default (
436
+ <server>
437
+ <switch>
438
+ <cms dir='cms' />
439
+ <redirect to='https://site.com' status={302} />
440
+ </switch>
441
+ </server>
442
+ )
443
+ ```
444
+
445
+ Also, you can use string key of status.
446
+
447
+ ```typescript jsx
448
+ export default (
449
+ <server>
450
+ <switch>
451
+ <cms dir='cms' />
452
+ <redirect to='https://site.com' status='found'/>
453
+ </switch>
454
+ </server>
455
+ )
456
+ ```
457
+
417
458
  ## Templates
418
459
 
419
460
  Any template is just a function which returns content that should be run.
@@ -23,7 +23,7 @@ export interface File {
23
23
  size: number;
24
24
  }
25
25
  export declare const URL_PARSER: RegExp;
26
- export default class Action<O extends ActionOptions = ActionOptions> {
26
+ export declare class Action<O extends ActionOptions = ActionOptions> {
27
27
  readonly req: Request;
28
28
  readonly res: Response;
29
29
  constructor(req: Request, res: Response);
@@ -94,4 +94,4 @@ __decorate([
94
94
  once
95
95
  ], Action.prototype, "parsedUrl", null);
96
96
 
97
- export { ACTION, URL_PARSER, Action as default };
97
+ export { ACTION, Action, URL_PARSER };
@@ -133,5 +133,5 @@ var Action = /** @class */ (function () {
133
133
  }());
134
134
 
135
135
  exports.ACTION = ACTION;
136
+ exports.Action = Action;
136
137
  exports.URL_PARSER = URL_PARSER;
137
- exports["default"] = Action;
@@ -1,2 +1 @@
1
- export { default } from './Action';
2
1
  export * from './Action';
@@ -1 +1 @@
1
- export { ACTION, URL_PARSER, default } from './Action.es6.js';
1
+ export { ACTION, Action, URL_PARSER } from './Action.es6.js';
@@ -7,5 +7,5 @@ var Action = require('./Action.js');
7
7
 
8
8
 
9
9
  exports.ACTION = Action.ACTION;
10
+ exports.Action = Action.Action;
10
11
  exports.URL_PARSER = Action.URL_PARSER;
11
- exports["default"] = Action["default"];
package/action/index.d.ts CHANGED
@@ -1,4 +1,2 @@
1
- export { default } from './Action';
2
1
  export * from './Action';
3
- export { default as withAction } from './withAction';
4
2
  export * from './withAction';
@@ -1,2 +1,2 @@
1
- export { ACTION, URL_PARSER, default } from './Action/Action.es6.js';
2
- export { default as withAction } from './withAction/withAction.es6.js';
1
+ export { ACTION, Action, URL_PARSER } from './Action/Action.es6.js';
2
+ export { withAction } from './withAction/withAction.es6.js';
package/action/index.js CHANGED
@@ -8,6 +8,6 @@ var withAction = require('./withAction/withAction.js');
8
8
 
9
9
 
10
10
  exports.ACTION = Action.ACTION;
11
+ exports.Action = Action.Action;
11
12
  exports.URL_PARSER = Action.URL_PARSER;
12
- exports["default"] = Action["default"];
13
- exports.withAction = withAction;
13
+ exports.withAction = withAction.withAction;
@@ -1,2 +1 @@
1
- export { default } from './withAction';
2
1
  export * from './withAction';
@@ -1 +1 @@
1
- export { default } from './withAction.es6.js';
1
+ export { withAction } from './withAction.es6.js';
@@ -1,7 +1,9 @@
1
1
  'use strict';
2
2
 
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
3
5
  var withAction = require('./withAction.js');
4
6
 
5
7
 
6
8
 
7
- module.exports = withAction;
9
+ exports.withAction = withAction.withAction;
@@ -1,6 +1,6 @@
1
1
  import { Children, Component, Props } from '@innet/jsx';
2
2
  import { Handler } from 'innet';
3
- import Action, { Body } from '../Action';
3
+ import { Action, Body } from '../Action';
4
4
  export interface ActionComponent<B extends Body> extends Component {
5
5
  action: Action<B>;
6
6
  }
@@ -8,4 +8,4 @@ export interface RequestComponentConstructor<B extends Body> {
8
8
  new (props?: Props, children?: Children, handler?: Handler): ActionComponent<B>;
9
9
  [key: string]: any;
10
10
  }
11
- export default function withAction<B extends Body, T extends RequestComponentConstructor<B>>(target: T): T;
11
+ export declare function withAction<B extends Body, T extends RequestComponentConstructor<B>>(target: T): T;
@@ -9,4 +9,4 @@ function withAction(target) {
9
9
  return target;
10
10
  }
11
11
 
12
- export { withAction as default };
12
+ export { withAction };
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
3
5
  var Action = require('../Action/Action.js');
4
6
 
5
7
  function withAction(target) {
@@ -15,4 +17,4 @@ function withAction(target) {
15
17
  return target;
16
18
  }
17
19
 
18
- module.exports = withAction;
20
+ exports.withAction = withAction;
@@ -1,7 +1,7 @@
1
1
  import html from '@innet/html';
2
2
  import { switchAsync, SwitchProps } from '@innet/switch';
3
3
  import { arrayAsync } from '@innet/utils';
4
- import { cms, CmsProps, cookie, CookieProps, error, ErrorProps, file, FileProps, header, HeaderProps, proxy, ProxyProps, router, RouterProps, success, SuccessProps } from '../plugins';
4
+ import { cms, CmsProps, cookie, CookieProps, error, ErrorProps, file, FileProps, header, HeaderProps, proxy, ProxyProps, router, RouterProps, success, SuccessProps, redirect, RedirectProps } from '../plugins';
5
5
  import { server, ServerProps } from '../server';
6
6
  export declare const arrayPlugins: (typeof arrayAsync)[];
7
7
  export declare const JSXPlugins: {
@@ -16,6 +16,7 @@ export declare const JSXPlugins: {
16
16
  cms: typeof cms;
17
17
  file: typeof file;
18
18
  proxy: typeof proxy;
19
+ redirect: typeof redirect;
19
20
  };
20
21
  export declare const objectPlugins: ((handler: any) => import("innet").PluginHandler)[];
21
22
  declare const _default: import("innet").Handler;
@@ -25,6 +26,7 @@ declare global {
25
26
  interface IntrinsicElements {
26
27
  server: ServerProps;
27
28
  router: RouterProps;
29
+ redirect: RedirectProps;
28
30
  cookie: CookieProps;
29
31
  success: SuccessProps;
30
32
  error: ErrorProps;
@@ -11,6 +11,7 @@ import { error } from '../plugins/error/error.es6.js';
11
11
  import { cms } from '../plugins/cms/cms.es6.js';
12
12
  import { file } from '../plugins/file/file.es6.js';
13
13
  import { proxy } from '../plugins/proxy/proxy.es6.js';
14
+ import { redirect } from '../plugins/redirect/redirect.es6.js';
14
15
  import { server } from '../server/server.es6.js';
15
16
 
16
17
  const arrayPlugins = [
@@ -30,6 +31,7 @@ const JSXPlugins = {
30
31
  cms,
31
32
  file,
32
33
  proxy,
34
+ redirect,
33
35
  };
34
36
  const objectPlugins = [
35
37
  jsxPlugins(JSXPlugins),
@@ -15,6 +15,7 @@ var error = require('../plugins/error/error.js');
15
15
  var cms = require('../plugins/cms/cms.js');
16
16
  var file = require('../plugins/file/file.js');
17
17
  var proxy = require('../plugins/proxy/proxy.js');
18
+ var redirect = require('../plugins/redirect/redirect.js');
18
19
  var server = require('../server/server.js');
19
20
 
20
21
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
@@ -38,6 +39,7 @@ var JSXPlugins = {
38
39
  cms: cms.cms,
39
40
  file: file.file,
40
41
  proxy: proxy.proxy,
42
+ redirect: redirect.redirect,
41
43
  };
42
44
  var objectPlugins = [
43
45
  jsx.jsxPlugins(JSXPlugins),
package/index.d.ts CHANGED
@@ -2,5 +2,4 @@ export { default } from './handler';
2
2
  export * from './server';
3
3
  export * from './plugins';
4
4
  export * from './handler';
5
- export { default as Action } from './action';
6
5
  export * from './action';
package/index.es6.js CHANGED
@@ -8,5 +8,6 @@ export { error, errorStatuses } from './plugins/error/error.es6.js';
8
8
  export { cms } from './plugins/cms/cms.es6.js';
9
9
  export { file } from './plugins/file/file.es6.js';
10
10
  export { proxy } from './plugins/proxy/proxy.es6.js';
11
- export { ACTION, default as Action, URL_PARSER } from './action/Action/Action.es6.js';
12
- export { default as withAction } from './action/withAction/withAction.es6.js';
11
+ export { redirect, redirectStatuses } from './plugins/redirect/redirect.es6.js';
12
+ export { ACTION, Action, URL_PARSER } from './action/Action/Action.es6.js';
13
+ export { withAction } from './action/withAction/withAction.es6.js';
package/index.js CHANGED
@@ -12,6 +12,7 @@ var error = require('./plugins/error/error.js');
12
12
  var cms = require('./plugins/cms/cms.js');
13
13
  var file = require('./plugins/file/file.js');
14
14
  var proxy = require('./plugins/proxy/proxy.js');
15
+ var redirect = require('./plugins/redirect/redirect.js');
15
16
  var Action = require('./action/Action/Action.js');
16
17
  var withAction = require('./action/withAction/withAction.js');
17
18
 
@@ -35,7 +36,9 @@ exports.errorStatuses = error.errorStatuses;
35
36
  exports.cms = cms.cms;
36
37
  exports.file = file.file;
37
38
  exports.proxy = proxy.proxy;
39
+ exports.redirect = redirect.redirect;
40
+ exports.redirectStatuses = redirect.redirectStatuses;
38
41
  exports.ACTION = Action.ACTION;
39
- exports.Action = Action["default"];
42
+ exports.Action = Action.Action;
40
43
  exports.URL_PARSER = Action.URL_PARSER;
41
- exports.withAction = withAction;
44
+ exports.withAction = withAction.withAction;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@innet/server",
3
- "version": "1.0.4",
3
+ "version": "1.1.0",
4
4
  "description": "Create server-side application with innet",
5
5
  "main": "index.js",
6
6
  "module": "index.es6.js",
@@ -6,3 +6,4 @@ export * from './error';
6
6
  export * from './cms';
7
7
  export * from './file';
8
8
  export * from './proxy';
9
+ export * from './redirect';
@@ -6,3 +6,4 @@ export { error, errorStatuses } from './error/error.es6.js';
6
6
  export { cms } from './cms/cms.es6.js';
7
7
  export { file } from './file/file.es6.js';
8
8
  export { proxy } from './proxy/proxy.es6.js';
9
+ export { redirect, redirectStatuses } from './redirect/redirect.es6.js';
package/plugins/index.js CHANGED
@@ -10,6 +10,7 @@ var error = require('./error/error.js');
10
10
  var cms = require('./cms/cms.js');
11
11
  var file = require('./file/file.js');
12
12
  var proxy = require('./proxy/proxy.js');
13
+ var redirect = require('./redirect/redirect.js');
13
14
 
14
15
 
15
16
 
@@ -26,3 +27,5 @@ exports.errorStatuses = error.errorStatuses;
26
27
  exports.cms = cms.cms;
27
28
  exports.file = file.file;
28
29
  exports.proxy = proxy.proxy;
30
+ exports.redirect = redirect.redirect;
31
+ exports.redirectStatuses = redirect.redirectStatuses;
@@ -0,0 +1 @@
1
+ export * from './redirect';
@@ -0,0 +1 @@
1
+ export { redirect, redirectStatuses } from './redirect.es6.js';
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var redirect = require('./redirect.js');
6
+
7
+
8
+
9
+ exports.redirect = redirect.redirect;
10
+ exports.redirectStatuses = redirect.redirectStatuses;
@@ -0,0 +1,18 @@
1
+ export interface RedirectProps {
2
+ to: string;
3
+ status?: number | keyof typeof redirectStatuses;
4
+ }
5
+ export declare const redirectStatuses: {
6
+ multipleChoices: number;
7
+ movedPermanently: number;
8
+ found: number;
9
+ seeOther: number;
10
+ notModified: number;
11
+ useProxy: number;
12
+ temporaryRedirect: number;
13
+ permanentRedirect: number;
14
+ };
15
+ export declare function redirect({ props, children }: {
16
+ props: any;
17
+ children: any;
18
+ }, handler: any): any;
@@ -0,0 +1,27 @@
1
+ import { ACTION } from '../../action/Action/Action.es6.js';
2
+
3
+ const redirectStatuses = {
4
+ multipleChoices: 300,
5
+ movedPermanently: 301,
6
+ found: 302,
7
+ seeOther: 303,
8
+ notModified: 304,
9
+ useProxy: 305,
10
+ temporaryRedirect: 307,
11
+ permanentRedirect: 308,
12
+ };
13
+ function getStatus(status) {
14
+ if (typeof status === 'number')
15
+ return status;
16
+ return redirectStatuses[status] || 301;
17
+ }
18
+ function redirect({ props, children }, handler) {
19
+ const { res } = handler[ACTION];
20
+ const { to, status = 301 } = props;
21
+ res.writeHead(getStatus(status), {
22
+ location: to,
23
+ });
24
+ return null;
25
+ }
26
+
27
+ export { redirect, redirectStatuses };
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var Action = require('../../action/Action/Action.js');
6
+
7
+ var redirectStatuses = {
8
+ multipleChoices: 300,
9
+ movedPermanently: 301,
10
+ found: 302,
11
+ seeOther: 303,
12
+ notModified: 304,
13
+ useProxy: 305,
14
+ temporaryRedirect: 307,
15
+ permanentRedirect: 308,
16
+ };
17
+ function getStatus(status) {
18
+ if (typeof status === 'number')
19
+ return status;
20
+ return redirectStatuses[status] || 301;
21
+ }
22
+ function redirect(_a, handler) {
23
+ var props = _a.props; _a.children;
24
+ var res = handler[Action.ACTION].res;
25
+ var to = props.to, _b = props.status, status = _b === void 0 ? 301 : _b;
26
+ res.writeHead(getStatus(status), {
27
+ location: to,
28
+ });
29
+ return null;
30
+ }
31
+
32
+ exports.redirect = redirect;
33
+ exports.redirectStatuses = redirectStatuses;
@@ -1,6 +1,6 @@
1
1
  import { Children, Component, Props } from '@innet/jsx';
2
2
  import { Handler } from 'innet';
3
- import Action from '../../action';
3
+ import { Action } from '../../action';
4
4
  export declare type Methods = 'GET' | 'HEAD' | 'POST' | 'DELETE' | 'PUT' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH';
5
5
  export interface RouterProps {
6
6
  method?: Methods;
package/server/index.d.ts CHANGED
@@ -1,2 +1 @@
1
- export { default } from './server';
2
1
  export * from './server';
@@ -1 +1 @@
1
- export { server as default, server } from './server.es6.js';
1
+ export { server } from './server.es6.js';
package/server/index.js CHANGED
@@ -6,5 +6,4 @@ var server = require('./server.js');
6
6
 
7
7
 
8
8
 
9
- exports["default"] = server.server;
10
9
  exports.server = server.server;
@@ -1,5 +1,5 @@
1
1
  import { Handler } from 'innet';
2
- import Action from '../action';
2
+ import { Action } from '../action';
3
3
  export interface SSL {
4
4
  cert: string;
5
5
  key: string;
@@ -16,4 +16,3 @@ export declare function server({ props, children }: {
16
16
  props?: ServerProps;
17
17
  children: any;
18
18
  }, handler: Handler): Promise<unknown>;
19
- export default server;
@@ -3,7 +3,7 @@ import fs from 'fs';
3
3
  import http from 'http';
4
4
  import http2 from 'https';
5
5
  import innet from 'innet';
6
- import Action, { ACTION } from '../action/Action/Action.es6.js';
6
+ import { ACTION, Action } from '../action/Action/Action.es6.js';
7
7
  import { CONTINUE } from '../constants.es6.js';
8
8
 
9
9
  const isInvalidPath = require('is-invalid-path');
@@ -60,4 +60,4 @@ function server({ props = {}, children }, handler) {
60
60
  return promise;
61
61
  }
62
62
 
63
- export { server as default, server };
63
+ export { server };
package/server/server.js CHANGED
@@ -38,7 +38,7 @@ function server(_a, handler) {
38
38
  switch (_a.label) {
39
39
  case 0:
40
40
  childHandler = Object.create(handler);
41
- childHandler[Action.ACTION] = new Action["default"](req, res);
41
+ childHandler[Action.ACTION] = new Action.Action(req, res);
42
42
  if (!onRequest) return [3 /*break*/, 2];
43
43
  return [4 /*yield*/, onRequest(childHandler[Action.ACTION])];
44
44
  case 1:
@@ -88,5 +88,4 @@ function server(_a, handler) {
88
88
  return promise;
89
89
  }
90
90
 
91
- exports["default"] = server;
92
91
  exports.server = server;