@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 +41 -0
- package/action/Action/Action.d.ts +1 -1
- package/action/Action/Action.es6.js +1 -1
- package/action/Action/Action.js +1 -1
- package/action/Action/index.d.ts +0 -1
- package/action/Action/index.es6.js +1 -1
- package/action/Action/index.js +1 -1
- package/action/index.d.ts +0 -2
- package/action/index.es6.js +2 -2
- package/action/index.js +2 -2
- package/action/withAction/index.d.ts +0 -1
- package/action/withAction/index.es6.js +1 -1
- package/action/withAction/index.js +3 -1
- package/action/withAction/withAction.d.ts +2 -2
- package/action/withAction/withAction.es6.js +1 -1
- package/action/withAction/withAction.js +3 -1
- package/handler/handler.d.ts +3 -1
- package/handler/handler.es6.js +2 -0
- package/handler/handler.js +2 -0
- package/index.d.ts +0 -1
- package/index.es6.js +3 -2
- package/index.js +5 -2
- package/package.json +1 -1
- package/plugins/index.d.ts +1 -0
- package/plugins/index.es6.js +1 -0
- package/plugins/index.js +3 -0
- package/plugins/redirect/index.d.ts +1 -0
- package/plugins/redirect/index.es6.js +1 -0
- package/plugins/redirect/index.js +10 -0
- package/plugins/redirect/redirect.d.ts +18 -0
- package/plugins/redirect/redirect.es6.js +27 -0
- package/plugins/redirect/redirect.js +33 -0
- package/plugins/router/router.d.ts +1 -1
- package/server/index.d.ts +0 -1
- package/server/index.es6.js +1 -1
- package/server/index.js +0 -1
- package/server/server.d.ts +1 -2
- package/server/server.es6.js +2 -2
- package/server/server.js +1 -2
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
|
|
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);
|
package/action/Action/Action.js
CHANGED
package/action/Action/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { ACTION,
|
|
1
|
+
export { ACTION, Action, URL_PARSER } from './Action.es6.js';
|
package/action/Action/index.js
CHANGED
package/action/index.d.ts
CHANGED
package/action/index.es6.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { ACTION,
|
|
2
|
-
export {
|
|
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
|
|
13
|
-
exports.withAction = withAction;
|
|
13
|
+
exports.withAction = withAction.withAction;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { withAction } from './withAction.es6.js';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Children, Component, Props } from '@innet/jsx';
|
|
2
2
|
import { Handler } from 'innet';
|
|
3
|
-
import 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
|
|
11
|
+
export declare function withAction<B extends Body, T extends RequestComponentConstructor<B>>(target: T): T;
|
|
@@ -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
|
-
|
|
20
|
+
exports.withAction = withAction;
|
package/handler/handler.d.ts
CHANGED
|
@@ -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;
|
package/handler/handler.es6.js
CHANGED
|
@@ -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),
|
package/handler/handler.js
CHANGED
|
@@ -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
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 {
|
|
12
|
-
export {
|
|
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
|
|
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
package/plugins/index.d.ts
CHANGED
package/plugins/index.es6.js
CHANGED
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,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
package/server/index.es6.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { server
|
|
1
|
+
export { server } from './server.es6.js';
|
package/server/index.js
CHANGED
package/server/server.d.ts
CHANGED
|
@@ -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;
|
package/server/server.es6.js
CHANGED
|
@@ -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
|
|
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
|
|
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
|
|
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;
|