@olenbetong/appframe-core 2.3.2 → 2.4.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/es/expose.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { has } from "./has.js";
2
+ import { invariant } from "./invariant.js";
2
3
  import { parseFunc } from "./parseFunc.js";
3
4
  import { requireNamespace } from "./requireNamespace.js";
4
5
  /**
@@ -7,18 +8,12 @@ import { requireNamespace } from "./requireNamespace.js";
7
8
  * @param value {object | Function | Function[]} The value(s) to expose
8
9
  */
9
10
  export function expose(name, value) {
10
- if (arguments.length !== 2) {
11
- throw new Error("Incorrect number of arguments passed to the expose function!");
12
- }
11
+ invariant(arguments.length === 2, "Incorrect number of arguments passed to the expose function!");
13
12
  if (Array.isArray(value)) {
14
13
  for (let func of value) {
15
14
  const { name: funcName } = parseFunc(func);
16
- if (funcName) {
17
- expose(name, func); // Expose func on namespace found in pName
18
- }
19
- else {
20
- throw new Error("Unable to detect name of function! Name it, or only supply one function and include name in first parameter.");
21
- }
15
+ invariant(funcName, "Unable to detect name of function! Name it, or only supply one function and include name in first parameter.");
16
+ expose(name, func); // Expose func on namespace found in pName
22
17
  }
23
18
  }
24
19
  else {
@@ -39,7 +34,7 @@ export function expose(name, value) {
39
34
  namespace = name;
40
35
  }
41
36
  else {
42
- throw new TypeError("Invalid typeof parameter 'name', or unable to parse pValue!");
37
+ invariant(false, "Invalid typeof parameter 'name', or unable to parse pValue!");
43
38
  }
44
39
  if (has(namespace, keyName)) {
45
40
  // If class is already defined, warn or show an alert
@@ -1,4 +1,4 @@
1
- import { has } from "./has";
1
+ import { has } from "./has.js";
2
2
  async function fetchLocalizedString(text) {
3
3
  try {
4
4
  let response = await fetch(`/api/user/localize/new/${af.article.id}`, {
package/es/index.d.ts CHANGED
@@ -7,8 +7,9 @@ export * from "./downloadRequest.js";
7
7
  export { expose } from "./expose.js";
8
8
  export * from "./getUserImage.js";
9
9
  export { getLocalizedString } from "./getLocalizedString.js";
10
- export { localize } from "./localize.js";
11
10
  export * from "./has.js";
11
+ export { invariant } from "./invariant.js";
12
+ export { localize } from "./localize.js";
12
13
  export { default as localStorage } from "./localStorage.js";
13
14
  export { default as SessionHandler } from "./SessionHandler.js";
14
15
  export { UserSession };
package/es/index.js CHANGED
@@ -7,10 +7,11 @@ export * from "./downloadRequest.js";
7
7
  export { expose } from "./expose.js";
8
8
  export * from "./getUserImage.js";
9
9
  export { getLocalizedString } from "./getLocalizedString.js";
10
- export { localize } from "./localize.js";
11
10
  export * from "./has.js";
11
+ export { invariant } from "./invariant.js";
12
+ export { localize } from "./localize.js";
12
13
  export { default as localStorage } from "./localStorage.js";
13
14
  export { default as SessionHandler } from "./SessionHandler.js";
14
15
  export { UserSession };
15
16
  export * from "./settings.js";
16
- export const version = "2.3.2";
17
+ export const version = "2.4.0";
@@ -0,0 +1 @@
1
+ export declare function invariant(condition: any, message?: string | (() => string)): asserts condition;
@@ -0,0 +1,7 @@
1
+ export function invariant(condition, message) {
2
+ if (condition) {
3
+ return;
4
+ }
5
+ const provided = typeof message === "function" ? message() : message;
6
+ throw new Error(provided ?? "Invariant failed");
7
+ }
package/es/localize.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { getLocalizedString } from "./getLocalizedString.js";
2
+ import { invariant } from "./invariant.js";
2
3
  /**
3
4
  * Localization function for tagged templates. Replaces the arguments
4
5
  * with $n, where n is the index, before asking Appframe to translate
@@ -13,9 +14,7 @@ import { getLocalizedString } from "./getLocalizedString.js";
13
14
  * @returns Translated string
14
15
  */
15
16
  export function localize(stringParts, ...args) {
16
- if (!Array.isArray(stringParts) || stringParts.length !== args.length + 1) {
17
- throw TypeError("Method 'localize' must be used as a tag function");
18
- }
17
+ invariant(!Array.isArray(stringParts) || stringParts.length !== args.length + 1, "Method 'localize' must be used as a tag function");
19
18
  let stringToLocalize = stringParts.map((s, i) => (i === stringParts.length - 1 ? s : `${s}\$${i}`)).join("");
20
19
  let localized = getLocalizedString(stringToLocalize);
21
20
  args.forEach((arg, index) => {
package/es/parseFunc.js CHANGED
@@ -1,8 +1,7 @@
1
+ import { invariant } from "./invariant.js";
1
2
  const funcRegex = /^function(?:\s+([a-zA-Z$_][a-zA-Z0-9$_]*))?\s*\(((?:(?:\s*[a-zA-Z$_][a-zA-Z0-9$_]*)(?:\s*,\s*[a-zA-Z$_][a-zA-Z0-9$_]*)*)?)\s*\)\s*\{((?:.|\n|\r)*)\}$/;
2
3
  export function parseFunc(func) {
3
4
  var match = func.toString().match(funcRegex);
4
- if (match === null) {
5
- throw new TypeError("Unable to parse function!");
6
- }
5
+ invariant(match, "Unable to parse function!");
7
6
  return { name: match[1], args: match[2].split(","), body: match[3] };
8
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@olenbetong/appframe-core",
3
- "version": "2.3.2",
3
+ "version": "2.4.0",
4
4
  "description": "Javascript libraries to utilize the Appframe framework",
5
5
  "type": "module",
6
6
  "sideEffects": [
@@ -48,5 +48,5 @@
48
48
  "devDependencies": {
49
49
  "chalk": "^5.1.2"
50
50
  },
51
- "gitHead": "cd1250e1e7607255c038c12fc086f4c9529b1262"
51
+ "gitHead": "d484fdccc2273b06f14dc4bf7f690a0952c4a5c6"
52
52
  }
@@ -1,4 +1,5 @@
1
1
  import { downloadBlob } from "./downloadBlob.js";
2
+ import { invariant } from "./invariant.js";
2
3
 
3
4
  export type DownloadItem = {
4
5
  status: "cancelled" | "error" | "requested" | "downloading" | "downloaded" | "completed";
@@ -29,9 +30,7 @@ export async function downloadRequest(request: Request, { fileName, signal, onPr
29
30
  if (!response.ok || !response.body) {
30
31
  if (response.headers.get("content-type")?.includes("application/json")) {
31
32
  let data = await response.json();
32
- if (data.error) {
33
- throw new Error(data.error);
34
- }
33
+ invariant(!data.error, data.error);
35
34
  }
36
35
  throw new Error(`${response.status} ${response.statusText}`);
37
36
  }
@@ -1,24 +1,26 @@
1
- window.addEventListener("error", (event) => {
2
- try {
3
- // Try to avoid error-logging-loop
4
- const data = {
5
- error: event.error?.message ?? event.message ?? null,
6
- file: event.filename,
7
- line: event.lineno ?? 0,
8
- column: event.colno ?? 0,
9
- stack: event.error?.stack ?? null,
10
- };
1
+ if (process.env.NODE_ENV === "production") {
2
+ globalThis.window?.addEventListener("error", (event) => {
3
+ try {
4
+ // Try to avoid error-logging-loop
5
+ const data = {
6
+ error: event.error?.message ?? event.message ?? null,
7
+ file: event.filename,
8
+ line: event.lineno ?? 0,
9
+ column: event.colno ?? 0,
10
+ stack: event.error?.stack ?? null,
11
+ };
11
12
 
12
- fetch("/data/Logger/LogError", {
13
- body: JSON.stringify(data),
14
- headers: {
15
- Accept: "application/json",
16
- "Content-Type": "application/json; charset=utf-8",
17
- "X-Requested-With": "XMLHttpRequest",
18
- },
19
- method: "POST",
20
- });
21
- } catch (ex) {
22
- // ignore
23
- }
24
- });
13
+ fetch("/data/Logger/LogError", {
14
+ body: JSON.stringify(data),
15
+ headers: {
16
+ Accept: "application/json",
17
+ "Content-Type": "application/json; charset=utf-8",
18
+ "X-Requested-With": "XMLHttpRequest",
19
+ },
20
+ method: "POST",
21
+ });
22
+ } catch (ex) {
23
+ // ignore
24
+ }
25
+ });
26
+ }
package/src/expose.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { has } from "./has.js";
2
+ import { invariant } from "./invariant.js";
2
3
  import { parseFunc } from "./parseFunc.js";
3
4
  import { requireNamespace } from "./requireNamespace.js";
4
5
 
@@ -8,21 +9,17 @@ import { requireNamespace } from "./requireNamespace.js";
8
9
  * @param value {object | Function | Function[]} The value(s) to expose
9
10
  */
10
11
  export function expose(name: object | string, value: object | Function | Function[]): void {
11
- if (arguments.length !== 2) {
12
- throw new Error("Incorrect number of arguments passed to the expose function!");
13
- }
12
+ invariant(arguments.length === 2, "Incorrect number of arguments passed to the expose function!");
14
13
 
15
14
  if (Array.isArray(value)) {
16
15
  for (let func of value) {
17
16
  const { name: funcName } = parseFunc(func);
18
17
 
19
- if (funcName) {
20
- expose(name, func); // Expose func on namespace found in pName
21
- } else {
22
- throw new Error(
23
- "Unable to detect name of function! Name it, or only supply one function and include name in first parameter."
24
- );
25
- }
18
+ invariant(
19
+ funcName,
20
+ "Unable to detect name of function! Name it, or only supply one function and include name in first parameter."
21
+ );
22
+ expose(name, func); // Expose func on namespace found in pName
26
23
  }
27
24
  } else {
28
25
  let keyName: string | undefined;
@@ -44,7 +41,7 @@ export function expose(name: object | string, value: object | Function | Functio
44
41
  } else if (keyName && (typeof name === "object" || typeof name === "function")) {
45
42
  namespace = name;
46
43
  } else {
47
- throw new TypeError("Invalid typeof parameter 'name', or unable to parse pValue!");
44
+ invariant(false, "Invalid typeof parameter 'name', or unable to parse pValue!");
48
45
  }
49
46
 
50
47
  if (has(namespace, keyName)) {
@@ -1,4 +1,4 @@
1
- import { has } from "./has";
1
+ import { has } from "./has.js";
2
2
 
3
3
  async function fetchLocalizedString(text: string) {
4
4
  try {
package/src/index.ts CHANGED
@@ -9,8 +9,9 @@ export * from "./downloadRequest.js";
9
9
  export { expose } from "./expose.js";
10
10
  export * from "./getUserImage.js";
11
11
  export { getLocalizedString } from "./getLocalizedString.js";
12
- export { localize } from "./localize.js";
13
12
  export * from "./has.js";
13
+ export { invariant } from "./invariant.js";
14
+ export { localize } from "./localize.js";
14
15
  export { default as localStorage } from "./localStorage.js";
15
16
  export { default as SessionHandler } from "./SessionHandler.js";
16
17
  export { UserSession };
@@ -0,0 +1,8 @@
1
+ export function invariant(condition: any, message?: string | (() => string)): asserts condition {
2
+ if (condition) {
3
+ return;
4
+ }
5
+ const provided: string | undefined = typeof message === "function" ? message() : message;
6
+
7
+ throw new Error(provided ?? "Invariant failed");
8
+ }
package/src/localize.ts CHANGED
@@ -1,29 +1,31 @@
1
1
  import { getLocalizedString } from "./getLocalizedString.js";
2
+ import { invariant } from "./invariant.js";
2
3
 
3
4
  /**
4
5
  * Localization function for tagged templates. Replaces the arguments
5
6
  * with $n, where n is the index, before asking Appframe to translate
6
7
  * the string.
7
- *
8
+ *
8
9
  * For example, if you call localize`Page ${2} or ${5}`, the string
9
10
  * "Page $0 of $1" will be sent to Appframe, and for norwegian,
10
11
  * "Side 2 av 5" will be returned.
11
- *
12
- * @param stringParts
13
- * @param args
12
+ *
13
+ * @param stringParts
14
+ * @param args
14
15
  * @returns Translated string
15
16
  */
16
17
  export function localize(stringParts: TemplateStringsArray, ...args: any[]): string {
17
- if (!Array.isArray(stringParts) || stringParts.length !== args.length + 1) {
18
- throw TypeError("Method 'localize' must be used as a tag function");
19
- }
18
+ invariant(
19
+ !Array.isArray(stringParts) || stringParts.length !== args.length + 1,
20
+ "Method 'localize' must be used as a tag function"
21
+ );
20
22
 
21
23
  let stringToLocalize = stringParts.map((s, i) => (i === stringParts.length - 1 ? s : `${s}\$${i}`)).join("");
22
24
  let localized = getLocalizedString(stringToLocalize);
23
25
 
24
26
  args.forEach((arg, index) => {
25
27
  localized = localized.replace("$" + index, arg);
26
- })
28
+ });
27
29
 
28
30
  return localized;
29
- }
31
+ }
package/src/parseFunc.ts CHANGED
@@ -1,9 +1,10 @@
1
- const funcRegex = /^function(?:\s+([a-zA-Z$_][a-zA-Z0-9$_]*))?\s*\(((?:(?:\s*[a-zA-Z$_][a-zA-Z0-9$_]*)(?:\s*,\s*[a-zA-Z$_][a-zA-Z0-9$_]*)*)?)\s*\)\s*\{((?:.|\n|\r)*)\}$/;
1
+ import { invariant } from "./invariant.js";
2
+
3
+ const funcRegex =
4
+ /^function(?:\s+([a-zA-Z$_][a-zA-Z0-9$_]*))?\s*\(((?:(?:\s*[a-zA-Z$_][a-zA-Z0-9$_]*)(?:\s*,\s*[a-zA-Z$_][a-zA-Z0-9$_]*)*)?)\s*\)\s*\{((?:.|\n|\r)*)\}$/;
2
5
 
3
6
  export function parseFunc(func: Function) {
4
7
  var match = func.toString().match(funcRegex);
5
- if (match === null) {
6
- throw new TypeError("Unable to parse function!");
7
- }
8
+ invariant(match, "Unable to parse function!");
8
9
  return { name: match[1], args: match[2].split(","), body: match[3] };
9
10
  }