@static-pages/core 1.0.2 → 2.0.1
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 +10 -4
- package/{lib/cjs → cjs}/index.d.ts +2 -4
- package/{lib/cjs → cjs}/index.js +7 -5
- package/cjs/package.json +3 -0
- package/{lib/esm → esm}/index.d.ts +2 -4
- package/{lib/esm → esm}/index.js +7 -4
- package/package.json +18 -9
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
[](https://app.travis-ci.com/staticpagesjs/core)
|
|
4
4
|
[](https://coveralls.io/github/staticpagesjs/core?branch=master)
|
|
5
5
|

|
|
6
|
-

|
|
7
7
|
|
|
8
8
|
This package contains only the core; this means it does not provide CLI support or readers and writers.
|
|
9
9
|
You can import this library to your JS project then add your own controllers, readers and writers.
|
|
@@ -75,14 +75,18 @@ type Options = {
|
|
|
75
75
|
from: Iterable<Data> | AsyncIterable<Data>;
|
|
76
76
|
to: (data: Data) => void | Promise<void>;
|
|
77
77
|
controller?: (data: Data) => undefined | Data | Data[] | Promise<undefined | Data | Data[]>;
|
|
78
|
+
variables?: Record<string, unknown>;
|
|
78
79
|
}[];
|
|
79
80
|
|
|
81
|
+
// Where `Data` is:
|
|
82
|
+
type Data = Record<string, unknown>;
|
|
83
|
+
// Or the same definition in a more known form:
|
|
80
84
|
type Data = { [k: string]: unknown };
|
|
81
85
|
```
|
|
82
86
|
|
|
83
87
|
## Custom options for the controller
|
|
84
|
-
You can
|
|
85
|
-
These
|
|
88
|
+
You can pass additional configuration options to your controller under the `variables` property.
|
|
89
|
+
These variables are accessible through the `this` context variable of the controller.
|
|
86
90
|
|
|
87
91
|
```js
|
|
88
92
|
require("@static-pages/core").default([{
|
|
@@ -91,7 +95,9 @@ require("@static-pages/core").default([{
|
|
|
91
95
|
controller: function(data) {
|
|
92
96
|
this.myProp; // <-- 123
|
|
93
97
|
},
|
|
94
|
-
|
|
98
|
+
variables: {
|
|
99
|
+
myProp: 123,
|
|
100
|
+
},
|
|
95
101
|
}]);
|
|
96
102
|
```
|
|
97
103
|
|
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
export declare type Data =
|
|
2
|
-
[key: string]: unknown;
|
|
3
|
-
};
|
|
1
|
+
export declare type Data = Record<string, unknown>;
|
|
4
2
|
export declare type Controller = (data: Data) => undefined | Data | Data[] | Promise<undefined | Data | Data[]>;
|
|
5
3
|
export declare type Route = {
|
|
6
4
|
from: Iterable<Data> | AsyncIterable<Data>;
|
|
7
5
|
to: (data: Data) => void | Promise<void>;
|
|
8
6
|
controller?: Controller;
|
|
9
|
-
|
|
7
|
+
variables?: Record<string, unknown>;
|
|
10
8
|
};
|
|
11
9
|
declare const _default: (routes: Route | Route[]) => Promise<void>;
|
|
12
10
|
export default _default;
|
package/{lib/cjs → cjs}/index.js
RENAMED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const getType = (x) => typeof x === 'object' ? (x ? 'object' : 'null') : typeof x;
|
|
4
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
5
|
+
const isIterable = (x) => typeof (x === null || x === void 0 ? void 0 : x[Symbol.iterator]) === 'function';
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
7
|
+
const isAsyncIterable = (x) => typeof (x === null || x === void 0 ? void 0 : x[Symbol.asyncIterator]) === 'function';
|
|
4
8
|
exports.default = async (routes) => {
|
|
5
|
-
var _a, _b;
|
|
6
9
|
for (const route of Array.isArray(routes) ? routes : [routes]) {
|
|
7
10
|
if (typeof route !== 'object' || !route)
|
|
8
11
|
throw new Error(`Route type mismatch, expected 'object', got '${getType(route)}'.`);
|
|
9
|
-
const { from, to, controller,
|
|
10
|
-
|
|
11
|
-
if (typeof ((_a = from) === null || _a === void 0 ? void 0 : _a[Symbol.iterator]) !== 'function' && typeof ((_b = from) === null || _b === void 0 ? void 0 : _b[Symbol.asyncIterator]) !== 'function')
|
|
12
|
+
const { from, to, controller, variables } = route;
|
|
13
|
+
if (!isIterable(from) && !isAsyncIterable(from))
|
|
12
14
|
throw new Error('Route \'from\' is not an \'iterable\' or an \'asyncIterable\'.');
|
|
13
15
|
if (typeof to !== 'function')
|
|
14
16
|
throw new Error(`Route 'to' type mismatch, expected 'function', got '${getType(to)}'.`);
|
|
@@ -16,7 +18,7 @@ exports.default = async (routes) => {
|
|
|
16
18
|
throw new Error(`Route 'controller' type mismatch, expected 'function', got '${getType(controller)}'.`);
|
|
17
19
|
const isController = typeof controller === 'function';
|
|
18
20
|
for await (const data of from) {
|
|
19
|
-
const results = isController ? await controller.call(
|
|
21
|
+
const results = isController ? await controller.call(variables, data) : data;
|
|
20
22
|
if (typeof results === 'object' && results) {
|
|
21
23
|
if (Array.isArray(results)) {
|
|
22
24
|
for (const result of results) {
|
package/cjs/package.json
ADDED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
export declare type Data =
|
|
2
|
-
[key: string]: unknown;
|
|
3
|
-
};
|
|
1
|
+
export declare type Data = Record<string, unknown>;
|
|
4
2
|
export declare type Controller = (data: Data) => undefined | Data | Data[] | Promise<undefined | Data | Data[]>;
|
|
5
3
|
export declare type Route = {
|
|
6
4
|
from: Iterable<Data> | AsyncIterable<Data>;
|
|
7
5
|
to: (data: Data) => void | Promise<void>;
|
|
8
6
|
controller?: Controller;
|
|
9
|
-
|
|
7
|
+
variables?: Record<string, unknown>;
|
|
10
8
|
};
|
|
11
9
|
declare const _default: (routes: Route | Route[]) => Promise<void>;
|
|
12
10
|
export default _default;
|
package/{lib/esm → esm}/index.js
RENAMED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
const getType = (x) => typeof x === 'object' ? (x ? 'object' : 'null') : typeof x;
|
|
2
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
3
|
+
const isIterable = (x) => typeof (x === null || x === void 0 ? void 0 : x[Symbol.iterator]) === 'function';
|
|
4
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
5
|
+
const isAsyncIterable = (x) => typeof (x === null || x === void 0 ? void 0 : x[Symbol.asyncIterator]) === 'function';
|
|
2
6
|
export default async (routes) => {
|
|
3
7
|
for (const route of Array.isArray(routes) ? routes : [routes]) {
|
|
4
8
|
if (typeof route !== 'object' || !route)
|
|
5
9
|
throw new Error(`Route type mismatch, expected 'object', got '${getType(route)}'.`);
|
|
6
|
-
const { from, to, controller,
|
|
7
|
-
|
|
8
|
-
if (typeof from?.[Symbol.iterator] !== 'function' && typeof from?.[Symbol.asyncIterator] !== 'function')
|
|
10
|
+
const { from, to, controller, variables } = route;
|
|
11
|
+
if (!isIterable(from) && !isAsyncIterable(from))
|
|
9
12
|
throw new Error('Route \'from\' is not an \'iterable\' or an \'asyncIterable\'.');
|
|
10
13
|
if (typeof to !== 'function')
|
|
11
14
|
throw new Error(`Route 'to' type mismatch, expected 'function', got '${getType(to)}'.`);
|
|
@@ -13,7 +16,7 @@ export default async (routes) => {
|
|
|
13
16
|
throw new Error(`Route 'controller' type mismatch, expected 'function', got '${getType(controller)}'.`);
|
|
14
17
|
const isController = typeof controller === 'function';
|
|
15
18
|
for await (const data of from) {
|
|
16
|
-
const results = isController ? await controller.call(
|
|
19
|
+
const results = isController ? await controller.call(variables, data) : data;
|
|
17
20
|
if (typeof results === 'object' && results) {
|
|
18
21
|
if (Array.isArray(results)) {
|
|
19
22
|
for (const result of results) {
|
package/package.json
CHANGED
|
@@ -1,19 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@static-pages/core",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "General purpose static pages renderer.",
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./cjs/index.js",
|
|
7
|
+
"module": "./esm/index.js",
|
|
8
|
+
"browser": "./esm/index.js",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"require": "./cjs/index.js",
|
|
12
|
+
"default": "./esm/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
8
15
|
"scripts": {
|
|
9
16
|
"prepack": "npm run build",
|
|
10
17
|
"build": "npm run build:esm && npm run build:cjs",
|
|
11
|
-
"build:esm": "tsc
|
|
12
|
-
"build:cjs": "tsc --outDir
|
|
13
|
-
"watch:esm": "tsc --watch
|
|
14
|
-
"watch:cjs": "tsc --watch --outDir
|
|
18
|
+
"build:esm": "tsc",
|
|
19
|
+
"build:cjs": "tsc --outDir cjs --module commonjs",
|
|
20
|
+
"watch:esm": "tsc --watch",
|
|
21
|
+
"watch:cjs": "tsc --watch --outDir cjs --module commonjs",
|
|
15
22
|
"test": "eslint src && npm run build && jest",
|
|
16
|
-
"coverage": "jest --coverage && cat ./coverage/lcov.info | coveralls"
|
|
23
|
+
"coverage": "jest --coverage && cat ./coverage/lcov.info | coveralls",
|
|
24
|
+
"clean": "rimraf esm cjs/*.?s"
|
|
17
25
|
},
|
|
18
26
|
"repository": {
|
|
19
27
|
"type": "git",
|
|
@@ -43,6 +51,7 @@
|
|
|
43
51
|
"coveralls": "^3.1.1",
|
|
44
52
|
"eslint": "^8.1.0",
|
|
45
53
|
"jest": "^27.2.5",
|
|
54
|
+
"rimraf": "^3.0.2",
|
|
46
55
|
"typescript": "^4.4.4"
|
|
47
56
|
}
|
|
48
57
|
}
|