nsp-server-pages 0.1.1 → 0.1.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/cjs/src/app.js CHANGED
@@ -15,7 +15,7 @@ class App {
15
15
  this.loaders = [];
16
16
  this.tagMap = new Map();
17
17
  this.fnMap = new Map();
18
- this.listeners = new Map;
18
+ this.hooks = new Map;
19
19
  this.options = options = Object.create(options || null);
20
20
  if (!options.vName)
21
21
  options.vName = "v";
@@ -25,12 +25,12 @@ class App {
25
25
  options.storeKey = "#nsp";
26
26
  }
27
27
  hook(type, fn) {
28
- this.listeners.set(type, fn);
28
+ this.hooks.set(type, fn);
29
29
  }
30
- process(type, arg) {
31
- const fn = this.listeners.get(type);
30
+ process(type, ...args) {
31
+ const fn = this.hooks.get(type);
32
32
  if (fn)
33
- return fn(arg);
33
+ return fn.apply(null, args);
34
34
  }
35
35
  log(message) {
36
36
  const logger = this.options.logger || console;
package/cjs/src/catch.js CHANGED
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.catchFn = void 0;
4
- const to_xml_1 = require("to-xml");
5
4
  const isPromise = (v) => v && (typeof v.catch === "function");
6
- const escapeError = (e) => (0, to_xml_1.toXML)({ "#": (e?.message || String(e)) });
7
5
  const catchFn = (app, fn) => {
8
6
  return context => {
9
7
  try {
@@ -26,11 +24,13 @@ const catchFn = (app, fn) => {
26
24
  throw e;
27
25
  data.error = e;
28
26
  }
29
- // call the error handler
27
+ // call the error hook
30
28
  const result = app.process("error", e, context);
31
- if (result != null)
32
- return result;
33
- return `<!--\n[ERR] ${escapeError(e)}\n-->`;
29
+ // if the hook returns nothing, throw the error
30
+ if (result == null)
31
+ throw e;
32
+ // if the hook returns a string, show it
33
+ return result;
34
34
  }
35
35
  };
36
36
  };
package/index.d.ts CHANGED
@@ -110,13 +110,7 @@ declare namespace NSP {
110
110
  /**
111
111
  * retrieve a result from hook function
112
112
  */
113
- process<T>(type: "error", e: Error, context: T): string;
114
-
115
- process<T>(type: "directive", src: string, context: T): string;
116
-
117
- process<T>(type: "declaration", src: string, context: T): string;
118
-
119
- process<T>(type: "scriptlet", src: string, context: T): string;
113
+ process<R>(type: string, ...args: any[]): R;
120
114
 
121
115
  /**
122
116
  * pickup the taglib function
@@ -150,13 +144,15 @@ declare namespace NSP {
150
144
  /**
151
145
  * register a hook function
152
146
  */
153
- hook(type: "error", fn: <T>(e: Error, context?: T) => string | void): void;
147
+ hook(type: "error", fn: (e: Error, context?: any) => string | void): void;
148
+
149
+ hook(type: "directive", fn: (src: string, context?: any) => string | void): void;
154
150
 
155
- hook(type: "directive", fn: <T>(src: string, context?: T) => string | void): void;
151
+ hook(type: "declaration", fn: (src: string, context?: any) => string | void): void;
156
152
 
157
- hook(type: "declaration", fn: <T>(src: string, context?: T) => string | void): void;
153
+ hook(type: "scriptlet", fn: (src: string, context?: any) => string | void): void;
158
154
 
159
- hook(type: "scriptlet", fn: <T>(src: string, context?: T) => string | void): void;
155
+ hook(type: string, fn: (...args: any[]) => any): void;
160
156
 
161
157
  /**
162
158
  * parse a JSP document
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nsp-server-pages",
3
3
  "description": "NSP JavaScript Server Pages for Node.js",
4
- "version": "0.1.1",
4
+ "version": "0.1.2",
5
5
  "author": "@kawanet",
6
6
  "bugs": {
7
7
  "url": "https://github.com/kawanet/nsp-server-pages/issues"
package/src/app.js CHANGED
@@ -11,7 +11,7 @@ class App {
11
11
  this.loaders = [];
12
12
  this.tagMap = new Map();
13
13
  this.fnMap = new Map();
14
- this.listeners = new Map;
14
+ this.hooks = new Map;
15
15
  this.options = options = Object.create(options || null);
16
16
  if (!options.vName)
17
17
  options.vName = "v";
@@ -21,12 +21,12 @@ class App {
21
21
  options.storeKey = "#nsp";
22
22
  }
23
23
  hook(type, fn) {
24
- this.listeners.set(type, fn);
24
+ this.hooks.set(type, fn);
25
25
  }
26
- process(type, arg) {
27
- const fn = this.listeners.get(type);
26
+ process(type, ...args) {
27
+ const fn = this.hooks.get(type);
28
28
  if (fn)
29
- return fn(arg);
29
+ return fn.apply(null, args);
30
30
  }
31
31
  log(message) {
32
32
  const logger = this.options.logger || console;
package/src/catch.js CHANGED
@@ -1,6 +1,4 @@
1
- import { toXML } from "to-xml";
2
1
  const isPromise = (v) => v && (typeof v.catch === "function");
3
- const escapeError = (e) => toXML({ "#": (e?.message || String(e)) });
4
2
  export const catchFn = (app, fn) => {
5
3
  return context => {
6
4
  try {
@@ -23,11 +21,13 @@ export const catchFn = (app, fn) => {
23
21
  throw e;
24
22
  data.error = e;
25
23
  }
26
- // call the error handler
24
+ // call the error hook
27
25
  const result = app.process("error", e, context);
28
- if (result != null)
29
- return result;
30
- return `<!--\n[ERR] ${escapeError(e)}\n-->`;
26
+ // if the hook returns nothing, throw the error
27
+ if (result == null)
28
+ throw e;
29
+ // if the hook returns a string, show it
30
+ return result;
31
31
  }
32
32
  };
33
33
  };