@static-pages/core 2.0.2 → 3.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 CHANGED
@@ -32,10 +32,10 @@ This project targets small and medium sized projects. The rendering process trie
32
32
  __Readers__ provides an iterable list of page data. __Controllers__ can manipulate and extend each data object. __Writers__ render the final output for you.
33
33
 
34
34
  ```js
35
- const staticPages = require("@static-pages/core").default;
36
- const markdownReader = require("@static-pages/markdown-reader").default;
37
- const yamlReader = require("@static-pages/yaml-reader").default;
38
- const twigWriter = require("@static-pages/twig-writer").default;
35
+ import staticPages from '@static-pages/core';
36
+ import markdownReader from '@static-pages/markdown-reader';
37
+ import yamlReader from '@static-pages/yaml-reader';
38
+ import twigWriter from '@static-pages/twig-writer';
39
39
 
40
40
  staticPages([{
41
41
  from: markdownReader({
@@ -66,40 +66,20 @@ staticPages([{
66
66
  }]).catch(console.error);
67
67
  ```
68
68
 
69
- ## Options of `staticPages()`
69
+ ## `staticPages(options: Options)`
70
70
  The `staticPages()` function expects one parameter which must be an array.
71
71
  Each item should contain `from`, `to` and optionally a `controller` property matching the definition below.
72
72
 
73
73
  ```ts
74
+ type Data = Record<string, unknown>;
74
75
  type Options = {
75
76
  from: Iterable<Data> | AsyncIterable<Data>;
76
77
  to: (data: Data) => void | Promise<void>;
77
78
  controller?: (data: Data) => undefined | Data | Data[] | Promise<undefined | Data | Data[]>;
78
- variables?: Record<string, unknown>;
79
79
  }[];
80
-
81
- // Where `Data` is:
82
- type Data = Record<string, unknown>;
83
- // Or the same definition in a more known form:
84
- type Data = { [k: string]: unknown };
85
80
  ```
86
81
 
87
- ## Custom options for 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.
90
-
91
- ```js
92
- require("@static-pages/core").default([{
93
- from: ...,
94
- to: ...,
95
- controller: function(data) {
96
- this.myProp; // <-- 123
97
- },
98
- variables: {
99
- myProp: 123,
100
- },
101
- }]);
102
- ```
82
+ > Additionally, when `Options` is not passed as an array, it is wrapped into an array.
103
83
 
104
84
  ## Missing a feature?
105
85
  Create an issue describing your needs. If it fits the scope of the project I will implement it or you can implement it your own and submit a pull request.
package/cjs/index.d.ts CHANGED
@@ -4,7 +4,6 @@ export declare type Route = {
4
4
  from: Iterable<Data> | AsyncIterable<Data>;
5
5
  to: (data: Data) => void | Promise<void>;
6
6
  controller?: Controller;
7
- variables?: Record<string, unknown>;
8
7
  };
9
- declare const _default: (routes: Route | Route[]) => Promise<void>;
10
- export default _default;
8
+ export declare const staticPages: (routes: Route | Route[]) => Promise<void>;
9
+ export default staticPages;
package/cjs/index.js CHANGED
@@ -1,15 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.staticPages = void 0;
3
4
  const getType = (x) => typeof x === 'object' ? (x ? 'object' : 'null') : typeof x;
4
5
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
5
6
  const isIterable = (x) => typeof (x === null || x === void 0 ? void 0 : x[Symbol.iterator]) === 'function';
6
7
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
7
8
  const isAsyncIterable = (x) => typeof (x === null || x === void 0 ? void 0 : x[Symbol.asyncIterator]) === 'function';
8
- exports.default = async (routes) => {
9
+ const staticPages = async (routes) => {
9
10
  for (const route of Array.isArray(routes) ? routes : [routes]) {
10
11
  if (typeof route !== 'object' || !route)
11
12
  throw new Error(`Route type mismatch, expected 'object', got '${getType(route)}'.`);
12
- const { from, to, controller, variables } = route;
13
+ const { from, to, controller } = route;
13
14
  if (!isIterable(from) && !isAsyncIterable(from))
14
15
  throw new Error('Route \'from\' is not an \'iterable\' or an \'asyncIterable\'.');
15
16
  if (typeof to !== 'function')
@@ -18,7 +19,7 @@ exports.default = async (routes) => {
18
19
  throw new Error(`Route 'controller' type mismatch, expected 'function', got '${getType(controller)}'.`);
19
20
  const isController = typeof controller === 'function';
20
21
  for await (const data of from) {
21
- const results = isController ? await controller.call(variables, data) : data;
22
+ const results = isController ? await controller(data) : data;
22
23
  if (typeof results === 'object' && results) {
23
24
  if (Array.isArray(results)) {
24
25
  for (const result of results) {
@@ -32,3 +33,5 @@ exports.default = async (routes) => {
32
33
  }
33
34
  }
34
35
  };
36
+ exports.staticPages = staticPages;
37
+ exports.default = exports.staticPages;
package/esm/index.d.ts CHANGED
@@ -4,7 +4,6 @@ export declare type Route = {
4
4
  from: Iterable<Data> | AsyncIterable<Data>;
5
5
  to: (data: Data) => void | Promise<void>;
6
6
  controller?: Controller;
7
- variables?: Record<string, unknown>;
8
7
  };
9
- declare const _default: (routes: Route | Route[]) => Promise<void>;
10
- export default _default;
8
+ export declare const staticPages: (routes: Route | Route[]) => Promise<void>;
9
+ export default staticPages;
package/esm/index.js CHANGED
@@ -3,11 +3,11 @@ const getType = (x) => typeof x === 'object' ? (x ? 'object' : 'null') : typeof
3
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
5
  const isAsyncIterable = (x) => typeof (x === null || x === void 0 ? void 0 : x[Symbol.asyncIterator]) === 'function';
6
- export default async (routes) => {
6
+ export const staticPages = 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, variables } = route;
10
+ const { from, to, controller } = 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(variables, data) : data;
19
+ const results = isController ? await controller(data) : data;
20
20
  if (typeof results === 'object' && results) {
21
21
  if (Array.isArray(results)) {
22
22
  for (const result of results) {
@@ -30,3 +30,4 @@ export default async (routes) => {
30
30
  }
31
31
  }
32
32
  };
33
+ export default staticPages;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@static-pages/core",
3
- "version": "2.0.2",
3
+ "version": "3.0.1",
4
4
  "description": "General purpose static pages renderer.",
5
5
  "type": "module",
6
6
  "main": "./cjs/index.js",