@eik/common 5.0.4 → 5.1.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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [5.1.0](https://github.com/eik-lib/common/compare/v5.0.4...v5.1.0) (2025-05-08)
2
+
3
+
4
+ ### Features
5
+
6
+ * new helper function fetchImportMaps ([#358](https://github.com/eik-lib/common/issues/358)) ([0f89645](https://github.com/eik-lib/common/commit/0f89645937a5c786ea54cd4ff873c37de3efee01))
7
+
1
8
  ## [5.0.4](https://github.com/eik-lib/common/compare/v5.0.3...v5.0.4) (2025-04-23)
2
9
 
3
10
 
package/README.md CHANGED
@@ -9,12 +9,12 @@ can be found here in this repo. Here is how you can use it in your `eik.json`.
9
9
 
10
10
  ```json
11
11
  {
12
- "$schema": "https://raw.githubusercontent.com/eik-lib/common/main/lib/schemas/eikjson.schema.json",
13
- "name": "my-app",
14
- "version": "1.0.0",
15
- "server": "https://eik.store.com",
16
- "files": "./public",
17
- "import-map": ["https://eik.store.com/map/store/v1"]
12
+ "$schema": "https://raw.githubusercontent.com/eik-lib/common/main/lib/schemas/eikjson.schema.json",
13
+ "name": "my-app",
14
+ "version": "1.0.0",
15
+ "server": "https://eik.store.com",
16
+ "files": "./public",
17
+ "import-map": ["https://eik.store.com/map/store/v1"]
18
18
  }
19
19
  ```
20
20
 
@@ -27,7 +27,7 @@ can be found here in this repo. Here is how you can use it in your `eik.json`.
27
27
  `helpers` has utility functions used by several other Eik modules.
28
28
 
29
29
  ```js
30
- import { helpers } from '@eik/common';
30
+ import { helpers } from "@eik/common";
31
31
 
32
32
  let config = helpers.getDefaults();
33
33
  ```
@@ -46,6 +46,7 @@ These are the available functions on `helpers`.
46
46
  | `removeLeadingSlash` | |
47
47
  | `resolveFiles` | Uses an Eik JSON "files" definition to resolve files on disk into a data structure. Returns a list of [ResolvedFile](https://github.com/eik-lib/common/blob/main/lib/classes/resolved-files.js). |
48
48
  | `configStore` | Collection of helper methods for reading and writing Eik configuration files. |
49
+ | `fetchImportMaps` | Helper to get import maps (array of URLs) with some common error handling. |
49
50
 
50
51
  #### localAssets
51
52
 
@@ -54,8 +55,8 @@ Sets up asset routes for local development. Mounted paths match those on Eik ser
54
55
  Given this server and `eik.json`, the following routes would be added to your app.
55
56
 
56
57
  ```js
57
- import { helpers } from '@eik/common';
58
- import express from 'express';
58
+ import { helpers } from "@eik/common";
59
+ import express from "express";
59
60
 
60
61
  let app = express();
61
62
 
@@ -64,14 +65,14 @@ await helpers.localAssets(app);
64
65
 
65
66
  ```json
66
67
  {
67
- "name": "my-app",
68
- "version": "1.0.0",
69
- "server": "https://eik.store.com",
70
- "files": {
71
- "esm.js": "./assets/esm.js",
72
- "esm.css": "./assets/esm.css",
73
- "/": "./assets/**/*.map"
74
- }
68
+ "name": "my-app",
69
+ "version": "1.0.0",
70
+ "server": "https://eik.store.com",
71
+ "files": {
72
+ "esm.js": "./assets/esm.js",
73
+ "esm.css": "./assets/esm.css",
74
+ "/": "./assets/**/*.map"
75
+ }
75
76
  }
76
77
  ```
77
78
 
@@ -89,23 +90,23 @@ You can check a value against the schema for `eik.json` as a whole, or for indiv
89
90
  values in the schema.
90
91
 
91
92
  ```js
92
- import { schemas } from '@eik/common';
93
+ import { schemas } from "@eik/common";
93
94
 
94
95
  let { error, value } = schemas.validate.eikJSON(eikConfig);
95
96
  if (error) {
96
- // fallback
97
+ // fallback
97
98
  }
98
99
  ```
99
100
 
100
101
  If you prefer, you can use the `assert` API which throws on error.
101
102
 
102
103
  ```js
103
- import { schemas } from '@eik/common';
104
+ import { schemas } from "@eik/common";
104
105
 
105
106
  try {
106
- schemas.assert.eikJSON(eikConfig);
107
+ schemas.assert.eikJSON(eikConfig);
107
108
  } catch {
108
- // fallback
109
+ // fallback
109
110
  }
110
111
  ```
111
112
 
@@ -127,14 +128,14 @@ These are the available functions on `schemas.validate` and `schemas.assert`.
127
128
  `stream` has functions to check that a value is a Stream.
128
129
 
129
130
  ```js
130
- import { stream } from '@eik/common';
131
+ import { stream } from "@eik/common";
131
132
 
132
133
  if (stream.isStream(maybeStream)) {
133
- // yup, it's a Stream
134
+ // yup, it's a Stream
134
135
  }
135
136
 
136
137
  if (stream.isReadableStream(maybeReadableStream)) {
137
- // yup, it's a ReadableStream
138
+ // yup, it's a ReadableStream
138
139
  }
139
140
  ```
140
141
 
@@ -144,9 +145,9 @@ if (stream.isReadableStream(maybeReadableStream)) {
144
145
  Where possible, prefer using the [`schemas` API](#schemas).
145
146
 
146
147
  ```js
147
- import { validators } from '@eik/common';
148
+ import { validators } from "@eik/common";
148
149
 
149
- let alias = validators.alias('1');
150
+ let alias = validators.alias("1");
150
151
  ```
151
152
 
152
153
  These are the available functions on `validators`.
@@ -0,0 +1,45 @@
1
+ /**
2
+ * @typedef {object} ImportMap
3
+ * @property {Record<string, string>} imports
4
+ */
5
+
6
+ /**
7
+ * @param {string[]} urls
8
+ * @returns {Promise<ImportMap[]>}
9
+ */
10
+ export async function fetchImportMaps(urls = []) {
11
+ try {
12
+ const maps = urls.map(async (map) => {
13
+ const response = await fetch(map);
14
+
15
+ if (response.status === 404) {
16
+ throw new Error("Import map could not be found on server");
17
+ } else if (response.status >= 400 && response.status < 500) {
18
+ throw new Error("Server rejected client request");
19
+ } else if (response.status >= 500) {
20
+ throw new Error("Server error");
21
+ }
22
+
23
+ let contentType = response.headers.get("content-type");
24
+ if (!contentType.startsWith("application/json")) {
25
+ const content = await response.text();
26
+ if (content.length === 0) {
27
+ throw new Error(
28
+ `${map} did not return JSON, got an empty response. HTTP status: ${response.status}`,
29
+ );
30
+ }
31
+ throw new Error(
32
+ `${map} did not return JSON, got: ${content}. HTTP status: ${response.status}`,
33
+ );
34
+ }
35
+
36
+ const json = await response.json();
37
+ return /** @type {ImportMap}*/ (json);
38
+ });
39
+ return await Promise.all(maps);
40
+ } catch (err) {
41
+ throw new Error(
42
+ `Unable to load import map file from server: ${err.message}`,
43
+ );
44
+ }
45
+ }
@@ -1,6 +1,7 @@
1
1
  import localAssets from "./local-assets.js";
2
2
  import getDefaults from "./get-defaults.js";
3
3
  import configStore from "./config-store.js";
4
+ import { fetchImportMaps } from "./fetch-import-maps.js";
4
5
  import typeSlug from "./type-slug.js";
5
6
  import typeTitle from "./type-title.js";
6
7
  import resolveFiles from "./resolve-files.js";
@@ -15,6 +16,7 @@ export default {
15
16
  localAssets,
16
17
  getDefaults,
17
18
  configStore,
19
+ fetchImportMaps,
18
20
  typeSlug,
19
21
  typeTitle,
20
22
  addTrailingSlash,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eik/common",
3
- "version": "5.0.4",
3
+ "version": "5.1.0",
4
4
  "description": "Common utilities for Eik modules",
5
5
  "main": "lib/index.js",
6
6
  "types": "types/index.d.ts",
@@ -44,10 +44,10 @@
44
44
  "validate-npm-package-name": "6.0.0"
45
45
  },
46
46
  "devDependencies": {
47
- "@babel/plugin-syntax-import-assertions": "7.26.0",
48
- "@eik/eslint-config": "1.0.10",
47
+ "@babel/plugin-syntax-import-assertions": "7.27.1",
48
+ "@eik/eslint-config": "1.0.13",
49
49
  "@eik/prettier-config": "1.0.1",
50
- "@eik/semantic-release-config": "1.0.0",
50
+ "@eik/semantic-release-config": "1.0.2",
51
51
  "@eik/typescript-config": "1.0.0",
52
52
  "@hapi/hapi": "21.3.12",
53
53
  "@semantic-release/changelog": "6.0.3",
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @typedef {object} ImportMap
3
+ * @property {Record<string, string>} imports
4
+ */
5
+ /**
6
+ * @param {string[]} urls
7
+ * @returns {Promise<ImportMap[]>}
8
+ */
9
+ export function fetchImportMaps(urls?: string[]): Promise<ImportMap[]>;
10
+ export type ImportMap = {
11
+ imports: Record<string, string>;
12
+ };
@@ -2,6 +2,7 @@ declare namespace _default {
2
2
  export { localAssets };
3
3
  export { getDefaults };
4
4
  export { configStore };
5
+ export { fetchImportMaps };
5
6
  export { typeSlug };
6
7
  export { typeTitle };
7
8
  export { addTrailingSlash };
@@ -14,6 +15,7 @@ export default _default;
14
15
  import localAssets from "./local-assets.js";
15
16
  import getDefaults from "./get-defaults.js";
16
17
  import configStore from "./config-store.js";
18
+ import { fetchImportMaps } from "./fetch-import-maps.js";
17
19
  import typeSlug from "./type-slug.js";
18
20
  import typeTitle from "./type-title.js";
19
21
  import { addTrailingSlash } from "./path-slashes.js";