@static-pages/core 1.0.1 → 2.0.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
@@ -3,7 +3,7 @@
3
3
  [![Build Status](https://app.travis-ci.com/staticpagesjs/core.svg?branch=master)](https://app.travis-ci.com/staticpagesjs/core)
4
4
  [![Coverage Status](https://coveralls.io/repos/github/staticpagesjs/core/badge.svg?branch=master)](https://coveralls.io/github/staticpagesjs/core?branch=master)
5
5
  ![npms.io (quality)](https://img.shields.io/npms-io/quality-score/@static-pages/core?label=quality)
6
- ![Maintenance](https://img.shields.io/maintenance/yes/2021)
6
+ ![Maintenance](https://img.shields.io/maintenance/yes/2022)
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 define additional properties at the same level as the `from`, `to` and `controller` keys.
85
- These user defined properties are accessible through the `this` context variable of the controller.
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
- myProp: 123,
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
- [key: string]: unknown;
7
+ variables?: Record<string, unknown>;
10
8
  };
11
9
  declare const _default: (routes: Route | Route[]) => Promise<void>;
12
10
  export default _default;
@@ -1,20 +1,24 @@
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
9
  for (const route of Array.isArray(routes) ? routes : [routes]) {
6
10
  if (typeof route !== 'object' || !route)
7
11
  throw new Error(`Route type mismatch, expected 'object', got '${getType(route)}'.`);
8
- const { from, to, controller, ...userOptions } = route;
9
- if (typeof from[Symbol.iterator] !== 'function' && typeof from[Symbol.asyncIterator] !== 'function')
10
- throw new Error(`Route 'from' is not an 'iterable' or an 'asyncIterable'.`);
12
+ const { from, to, controller, variables } = route;
13
+ if (!isIterable(from) && !isAsyncIterable(from))
14
+ throw new Error('Route \'from\' is not an \'iterable\' or an \'asyncIterable\'.');
11
15
  if (typeof to !== 'function')
12
16
  throw new Error(`Route 'to' type mismatch, expected 'function', got '${getType(to)}'.`);
13
17
  if (typeof controller !== 'undefined' && typeof controller !== 'function')
14
18
  throw new Error(`Route 'controller' type mismatch, expected 'function', got '${getType(controller)}'.`);
15
19
  const isController = typeof controller === 'function';
16
- for await (let data of from) {
17
- const results = isController ? await controller.call(userOptions, data) : data;
20
+ for await (const data of from) {
21
+ const results = isController ? await controller.call(variables, data) : data;
18
22
  if (typeof results === 'object' && results) {
19
23
  if (Array.isArray(results)) {
20
24
  for (const result of results) {
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -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
- [key: string]: unknown;
7
+ variables?: Record<string, unknown>;
10
8
  };
11
9
  declare const _default: (routes: Route | Route[]) => Promise<void>;
12
10
  export default _default;
@@ -1,18 +1,22 @@
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, ...userOptions } = route;
7
- if (typeof from[Symbol.iterator] !== 'function' && typeof from[Symbol.asyncIterator] !== 'function')
8
- throw new Error(`Route 'from' is not an 'iterable' or an 'asyncIterable'.`);
10
+ const { from, to, controller, variables } = route;
11
+ if (!isIterable(from) && !isAsyncIterable(from))
12
+ throw new Error('Route \'from\' is not an \'iterable\' or an \'asyncIterable\'.');
9
13
  if (typeof to !== 'function')
10
14
  throw new Error(`Route 'to' type mismatch, expected 'function', got '${getType(to)}'.`);
11
15
  if (typeof controller !== 'undefined' && typeof controller !== 'function')
12
16
  throw new Error(`Route 'controller' type mismatch, expected 'function', got '${getType(controller)}'.`);
13
17
  const isController = typeof controller === 'function';
14
- for await (let data of from) {
15
- const results = isController ? await controller.call(userOptions, data) : data;
18
+ for await (const data of from) {
19
+ const results = isController ? await controller.call(variables, data) : data;
16
20
  if (typeof results === 'object' && results) {
17
21
  if (Array.isArray(results)) {
18
22
  for (const result of results) {
package/package.json CHANGED
@@ -1,19 +1,27 @@
1
1
  {
2
2
  "name": "@static-pages/core",
3
- "version": "1.0.1",
3
+ "version": "2.0.0",
4
4
  "description": "General purpose static pages renderer.",
5
- "main": "lib/cjs/index.js",
6
- "module": "lib/esm/index.js",
7
- "browser": "lib/esm/index.js",
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 --outDir lib/esm",
12
- "build:cjs": "tsc --outDir lib/cjs --module commonjs --target ES2019",
13
- "watch:esm": "tsc --watch --outDir lib/esm",
14
- "watch:cjs": "tsc --watch --outDir lib/cjs --module commonjs --target ES2019",
15
- "test": "jest",
16
- "coverage": "jest --coverage && cat ./coverage/lcov.info | coveralls"
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",
22
+ "test": "eslint src && npm run build && jest",
23
+ "coverage": "jest --coverage && cat ./coverage/lcov.info | coveralls",
24
+ "clean": "rimraf esm cjs/*.?s"
17
25
  },
18
26
  "repository": {
19
27
  "type": "git",
@@ -38,8 +46,12 @@
38
46
  "homepage": "https://staticpagesjs.github.io/",
39
47
  "devDependencies": {
40
48
  "@types/node": "^12.20.33",
49
+ "@typescript-eslint/eslint-plugin": "^5.3.0",
50
+ "@typescript-eslint/parser": "^5.3.0",
41
51
  "coveralls": "^3.1.1",
52
+ "eslint": "^8.1.0",
42
53
  "jest": "^27.2.5",
54
+ "rimraf": "^3.0.2",
43
55
  "typescript": "^4.4.4"
44
56
  }
45
57
  }