@eik/node-client 1.1.64 → 1.2.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/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## [1.2.1](https://github.com/eik-lib/node-client/compare/v1.2.0...v1.2.1) (2024-10-16)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * slugify image type ([#207](https://github.com/eik-lib/node-client/issues/207)) ([c0ed5e7](https://github.com/eik-lib/node-client/commit/c0ed5e787119657fa83647256a5f6179ccd24389))
7
+
8
+ # [1.2.0](https://github.com/eik-lib/node-client/compare/v1.1.64...v1.2.0) (2024-10-15)
9
+
10
+
11
+ ### Features
12
+
13
+ * add toHTML function ([4576ddd](https://github.com/eik-lib/node-client/commit/4576dddc88afb5722b80574438b03dc41956af72))
14
+
1
15
  ## [1.1.64](https://github.com/eik-lib/node-client/compare/v1.1.63...v1.1.64) (2024-08-30)
2
16
 
3
17
 
package/README.md CHANGED
@@ -200,13 +200,6 @@ const client = new Eik({
200
200
  await client.load();
201
201
 
202
202
  const maps = client.maps();
203
- const combined = maps.reduce((map, acc) => ({ ...acc, ...map }), {});
204
-
205
- const html = `
206
- <script type="importmap">
207
- ${JSON.stringify(combined, null, 2)}
208
- </script>
209
- `;
210
203
  ```
211
204
 
212
205
  #### returns
@@ -257,6 +250,33 @@ await client.load();
257
250
  client.base(); // http://localhost:8080/assets
258
251
  ```
259
252
 
253
+ ### .toHTML()
254
+
255
+ Constructs an HTML import map script tag for use in the document head when doing import mapping.
256
+
257
+ ```js
258
+ const client = new Eik({
259
+ loadMaps: true,
260
+ ...
261
+ });
262
+ await client.load();
263
+
264
+ const html = `
265
+ <html>
266
+ <head>
267
+ ...
268
+ ${client.toHTML()}
269
+ ...
270
+ </head>
271
+ <body>
272
+ ...
273
+ </body>
274
+ </html>
275
+ `;
276
+ ```
277
+
278
+ Due to browsers being restricted to a single import map, all import maps registered in eik.json or package.json will be merged down into a single import map with last in winning in case of duplicate keys.
279
+
260
280
  ## License
261
281
 
262
282
  Copyright (c) 2021 FINN.no
package/dist/index.cjs CHANGED
@@ -238,7 +238,12 @@ class Eik {
238
238
  */
239
239
  get pathname() {
240
240
  if (this.#config.type && this.#config.name && this.#config.version)
241
- return path.join("/", this.type, this.name, this.version).replace(/\\/g, "/");
241
+ return path.join(
242
+ "/",
243
+ common.helpers.typeSlug(this.type),
244
+ this.name,
245
+ this.version,
246
+ ).replace(/\\/g, "/");
242
247
  throw new Error("Eik config was not loaded before calling .pathname");
243
248
  }
244
249
 
@@ -321,23 +326,12 @@ class Eik {
321
326
  *
322
327
  * @example
323
328
  * ```js
324
- * // generate a <script type="importmap">
325
- * // for import mapping in the browser
326
329
  * const client = new Eik({
327
330
  * loadMaps: true,
328
331
  * });
329
332
  * await client.load();
330
333
  *
331
334
  * const maps = client.maps();
332
- * const combined = maps
333
- * .map((map) => map.imports)
334
- * .reduce((map, acc) => ({ ...acc, ...map }), {});
335
- *
336
- * const html = `
337
- * <script type="importmap">
338
- * ${JSON.stringify(combined, null, 2)}
339
- * </script>
340
- * `;
341
335
  * ```
342
336
  */
343
337
  maps() {
@@ -346,6 +340,38 @@ class Eik {
346
340
  'Eik config was not loaded or "loadMaps" is "false" when calling .maps()',
347
341
  );
348
342
  }
343
+
344
+ /**
345
+ * Function that generates and returns an import map script tag for use in an document head.
346
+ *
347
+ * Only a single import map is allowed per HTML document.
348
+ * A key (ex. `react`) can only be defined once.
349
+ * If multiple import maps defined in `eik.json` use the same key, the last key wins.
350
+ *
351
+ * @example
352
+ * ```
353
+ * const importMap = eik.toHTML();
354
+ *
355
+ * <head>
356
+ * ...
357
+ * ${importMap}
358
+ * ...
359
+ * </head>
360
+ * ```
361
+ *
362
+ * @returns {string}
363
+ */
364
+ toHTML() {
365
+ const allImportMapKeyValuePairs = this.maps().flatMap((map) =>
366
+ Object.entries(map.imports),
367
+ );
368
+ const mergedAndDedupedImportMapObject = Object.fromEntries(
369
+ new Map(allImportMapKeyValuePairs).entries(),
370
+ );
371
+ return `<script type="importmap">${JSON.stringify({
372
+ imports: mergedAndDedupedImportMapObject,
373
+ })}</script>`;
374
+ }
349
375
  }
350
376
 
351
377
  module.exports = Eik;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eik/node-client",
3
- "version": "1.1.64",
3
+ "version": "1.2.1",
4
4
  "description": "Utilities for working with assets and import maps on an Eik server",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -52,13 +52,13 @@
52
52
  "@eik/prettier-config": "1.0.1",
53
53
  "@eik/semantic-release-config": "1.0.0",
54
54
  "@eik/typescript-config": "1.0.0",
55
- "eslint": "9.8.0",
56
- "npm-run-all": "4.1.5",
55
+ "eslint": "9.11.1",
56
+ "npm-run-all2": "5.0.2",
57
57
  "prettier": "3.3.3",
58
58
  "rimraf": "6.0.1",
59
- "rollup": "4.20.0",
59
+ "rollup": "4.22.5",
60
60
  "semantic-release": "24.0.0",
61
- "tap": "21.0.0",
61
+ "tap": "21.0.1",
62
62
  "typescript": "5.5.4"
63
63
  }
64
64
  }
package/src/index.js CHANGED
@@ -186,7 +186,12 @@ export default class Eik {
186
186
  */
187
187
  get pathname() {
188
188
  if (this.#config.type && this.#config.name && this.#config.version)
189
- return join("/", this.type, this.name, this.version).replace(/\\/g, "/");
189
+ return join(
190
+ "/",
191
+ helpers.typeSlug(this.type),
192
+ this.name,
193
+ this.version,
194
+ ).replace(/\\/g, "/");
190
195
  throw new Error("Eik config was not loaded before calling .pathname");
191
196
  }
192
197
 
@@ -269,23 +274,12 @@ export default class Eik {
269
274
  *
270
275
  * @example
271
276
  * ```js
272
- * // generate a <script type="importmap">
273
- * // for import mapping in the browser
274
277
  * const client = new Eik({
275
278
  * loadMaps: true,
276
279
  * });
277
280
  * await client.load();
278
281
  *
279
282
  * const maps = client.maps();
280
- * const combined = maps
281
- * .map((map) => map.imports)
282
- * .reduce((map, acc) => ({ ...acc, ...map }), {});
283
- *
284
- * const html = `
285
- * <script type="importmap">
286
- * ${JSON.stringify(combined, null, 2)}
287
- * </script>
288
- * `;
289
283
  * ```
290
284
  */
291
285
  maps() {
@@ -294,4 +288,36 @@ export default class Eik {
294
288
  'Eik config was not loaded or "loadMaps" is "false" when calling .maps()',
295
289
  );
296
290
  }
291
+
292
+ /**
293
+ * Function that generates and returns an import map script tag for use in an document head.
294
+ *
295
+ * Only a single import map is allowed per HTML document.
296
+ * A key (ex. `react`) can only be defined once.
297
+ * If multiple import maps defined in `eik.json` use the same key, the last key wins.
298
+ *
299
+ * @example
300
+ * ```
301
+ * const importMap = eik.toHTML();
302
+ *
303
+ * <head>
304
+ * ...
305
+ * ${importMap}
306
+ * ...
307
+ * </head>
308
+ * ```
309
+ *
310
+ * @returns {string}
311
+ */
312
+ toHTML() {
313
+ const allImportMapKeyValuePairs = this.maps().flatMap((map) =>
314
+ Object.entries(map.imports),
315
+ );
316
+ const mergedAndDedupedImportMapObject = Object.fromEntries(
317
+ new Map(allImportMapKeyValuePairs).entries(),
318
+ );
319
+ return `<script type="importmap">${JSON.stringify({
320
+ imports: mergedAndDedupedImportMapObject,
321
+ })}</script>`;
322
+ }
297
323
  }
package/types/index.d.ts CHANGED
@@ -175,26 +175,36 @@ export default class Eik {
175
175
  *
176
176
  * @example
177
177
  * ```js
178
- * // generate a <script type="importmap">
179
- * // for import mapping in the browser
180
178
  * const client = new Eik({
181
179
  * loadMaps: true,
182
180
  * });
183
181
  * await client.load();
184
182
  *
185
183
  * const maps = client.maps();
186
- * const combined = maps
187
- * .map((map) => map.imports)
188
- * .reduce((map, acc) => ({ ...acc, ...map }), {});
189
- *
190
- * const html = `
191
- * <script type="importmap">
192
- * ${JSON.stringify(combined, null, 2)}
193
- * </script>
194
- * `;
195
184
  * ```
196
185
  */
197
186
  maps(): ImportMap[];
187
+ /**
188
+ * Function that generates and returns an import map script tag for use in an document head.
189
+ *
190
+ * Only a single import map is allowed per HTML document.
191
+ * A key (ex. `react`) can only be defined once.
192
+ * If multiple import maps defined in `eik.json` use the same key, the last key wins.
193
+ *
194
+ * @example
195
+ * ```
196
+ * const importMap = eik.toHTML();
197
+ *
198
+ * <head>
199
+ * ...
200
+ * ${importMap}
201
+ * ...
202
+ * </head>
203
+ * ```
204
+ *
205
+ * @returns {string}
206
+ */
207
+ toHTML(): string;
198
208
  #private;
199
209
  }
200
210
  export type Options = {