@static-pages/core 1.0.3 → 2.0.2
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 +11 -5
- package/{lib/cjs → cjs}/index.d.ts +2 -4
- package/{lib/cjs → cjs}/index.js +2 -2
- package/cjs/package.json +3 -0
- package/{lib/esm → esm}/index.d.ts +2 -4
- package/{lib/esm → esm}/index.js +4 -4
- package/package.json +19 -10
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.
|
|
@@ -15,7 +15,7 @@ Yes! Because I browsed the whole jamstack scene, but could not find one which
|
|
|
15
15
|
3. can render with any template engine (Twig, ejs, Pug, Mustache etc.)
|
|
16
16
|
4. supports incremental builds
|
|
17
17
|
5. has a flexible CLI tool (see [@static-pages/cli](https://www.npmjs.com/package/@static-pages/cli) on npm)
|
|
18
|
-
6. has a Docker image (see [
|
|
18
|
+
6. has a Docker image (see [staticpages/cli](https://hub.docker.com/repository/docker/staticpages/cli) on dockerhub)
|
|
19
19
|
7. written in JS (preferably TypeScript)
|
|
20
20
|
8. easy to extend with JS code
|
|
21
21
|
9. learning and using is easy (Gatsby, Hugo, Jekyll, Eleventy etc. are so cool but harder to learn and configure)
|
|
@@ -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
|
@@ -9,7 +9,7 @@ exports.default = async (routes) => {
|
|
|
9
9
|
for (const route of Array.isArray(routes) ? routes : [routes]) {
|
|
10
10
|
if (typeof route !== 'object' || !route)
|
|
11
11
|
throw new Error(`Route type mismatch, expected 'object', got '${getType(route)}'.`);
|
|
12
|
-
const { from, to, controller,
|
|
12
|
+
const { from, to, controller, variables } = route;
|
|
13
13
|
if (!isIterable(from) && !isAsyncIterable(from))
|
|
14
14
|
throw new Error('Route \'from\' is not an \'iterable\' or an \'asyncIterable\'.');
|
|
15
15
|
if (typeof to !== 'function')
|
|
@@ -18,7 +18,7 @@ exports.default = async (routes) => {
|
|
|
18
18
|
throw new Error(`Route 'controller' type mismatch, expected 'function', got '${getType(controller)}'.`);
|
|
19
19
|
const isController = typeof controller === 'function';
|
|
20
20
|
for await (const data of from) {
|
|
21
|
-
const results = isController ? await controller.call(
|
|
21
|
+
const results = isController ? await controller.call(variables, data) : data;
|
|
22
22
|
if (typeof results === 'object' && results) {
|
|
23
23
|
if (Array.isArray(results)) {
|
|
24
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,13 +1,13 @@
|
|
|
1
1
|
const getType = (x) => typeof x === 'object' ? (x ? 'object' : 'null') : typeof x;
|
|
2
2
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
3
|
-
const isIterable = (x) => typeof x
|
|
3
|
+
const isIterable = (x) => typeof (x === null || x === void 0 ? void 0 : x[Symbol.iterator]) === 'function';
|
|
4
4
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
5
|
-
const isAsyncIterable = (x) => typeof x
|
|
5
|
+
const isAsyncIterable = (x) => typeof (x === null || x === void 0 ? void 0 : x[Symbol.asyncIterator]) === 'function';
|
|
6
6
|
export default async (routes) => {
|
|
7
7
|
for (const route of Array.isArray(routes) ? routes : [routes]) {
|
|
8
8
|
if (typeof route !== 'object' || !route)
|
|
9
9
|
throw new Error(`Route type mismatch, expected 'object', got '${getType(route)}'.`);
|
|
10
|
-
const { from, to, controller,
|
|
10
|
+
const { from, to, controller, variables } = route;
|
|
11
11
|
if (!isIterable(from) && !isAsyncIterable(from))
|
|
12
12
|
throw new Error('Route \'from\' is not an \'iterable\' or an \'asyncIterable\'.');
|
|
13
13
|
if (typeof to !== 'function')
|
|
@@ -16,7 +16,7 @@ export default async (routes) => {
|
|
|
16
16
|
throw new Error(`Route 'controller' type mismatch, expected 'function', got '${getType(controller)}'.`);
|
|
17
17
|
const isController = typeof controller === 'function';
|
|
18
18
|
for await (const data of from) {
|
|
19
|
-
const results = isController ? await controller.call(
|
|
19
|
+
const results = isController ? await controller.call(variables, data) : data;
|
|
20
20
|
if (typeof results === 'object' && results) {
|
|
21
21
|
if (Array.isArray(results)) {
|
|
22
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.2",
|
|
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",
|
|
@@ -37,12 +45,13 @@
|
|
|
37
45
|
},
|
|
38
46
|
"homepage": "https://staticpagesjs.github.io/",
|
|
39
47
|
"devDependencies": {
|
|
40
|
-
"@types/node": "^
|
|
48
|
+
"@types/node": "^14.18.9",
|
|
41
49
|
"@typescript-eslint/eslint-plugin": "^5.3.0",
|
|
42
50
|
"@typescript-eslint/parser": "^5.3.0",
|
|
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
|
}
|