@eik/node-client 2.0.0-next.4 → 2.0.0-next.5

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
+ # [2.0.0-next.5](https://github.com/eik-lib/node-client/compare/v2.0.0-next.4...v2.0.0-next.5) (2022-09-23)
2
+
3
+
4
+ ### Features
5
+
6
+ * add .mapping(dep) shorthand method to resolve mapped bare imports directly ([a557cfd](https://github.com/eik-lib/node-client/commit/a557cfdc56f4553f721d1a67a7670eafc284b565))
7
+
1
8
  # [2.0.0-next.4](https://github.com/eik-lib/node-client/compare/v2.0.0-next.3...v2.0.0-next.4) (2022-08-22)
2
9
 
3
10
 
package/README.md CHANGED
@@ -187,6 +187,37 @@ If `integrity` of the file is not available, the value for `integrity` will be `
187
187
 
188
188
  Returns the import maps defined in Eik config from the Eik server. For the maps to be returned they need to be loaded from the Eik server. This is done by setting the `loadMaps` option on the constructor to `true`.
189
189
 
190
+ ### .mapping(identifier)
191
+
192
+ Returns the last mapping entry for a given bare import `identifier`.
193
+ `identifier` is a `string` key from an import map and the returned `string` is a the matching value from the same import map entry.
194
+
195
+ #### arguments
196
+
197
+ | option | default | type | required | details |
198
+ | ---------- | ------- | -------- | -------- | ------------------- |
199
+ | identifier | | `string` | `true` | Bare import map key |
200
+
201
+ **Example:**
202
+
203
+ If an import map being used looks like:
204
+
205
+ ```json
206
+ {
207
+ "imports": {
208
+ "react": "https://myserver.com/react/18.0.0/react.min.js"
209
+ }
210
+ }
211
+ ```
212
+
213
+ When the mapping method is called:
214
+
215
+ ```js
216
+ const absoluteURL = client.mapping('react');
217
+ ```
218
+
219
+ `absoluteURL` will be `https://myserver.com/react/18.0.0/react.min.js`
220
+
190
221
  ## License
191
222
 
192
223
  Copyright (c) 2021 FINN.no
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eik/node-client",
3
- "version": "2.0.0-next.4",
3
+ "version": "2.0.0-next.5",
4
4
  "description": "A node.js client for interacting with a Eik server.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
package/src/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ /* eslint-disable no-restricted-syntax */
1
2
  import { request } from 'undici';
2
3
  import { join } from 'path';
3
4
  import loader from '@eik/common-config-loader';
@@ -6,16 +7,15 @@ import Asset from './asset.js';
6
7
  const trimSlash = (value = '') => {
7
8
  if (value.endsWith('/')) return value.substring(0, value.length - 1);
8
9
  return value;
9
- }
10
+ };
10
11
 
11
12
  const fetchImportMaps = async (urls = []) => {
12
- try{
13
+ try {
13
14
  const maps = urls.map(async (map) => {
14
- const {
15
- statusCode,
16
- body
17
- } = await request(map, { maxRedirections: 2 });
18
-
15
+ const { statusCode, body } = await request(map, {
16
+ maxRedirections: 2,
17
+ });
18
+
19
19
  if (statusCode === 404) {
20
20
  throw new Error('Import map could not be found on server');
21
21
  } else if (statusCode >= 400 && statusCode < 500) {
@@ -31,7 +31,7 @@ const fetchImportMaps = async (urls = []) => {
31
31
  `Unable to load import map file from server: ${err.message}`,
32
32
  );
33
33
  }
34
- }
34
+ };
35
35
 
36
36
  export default class NodeClient {
37
37
  #development;
@@ -83,7 +83,8 @@ export default class NodeClient {
83
83
  }
84
84
 
85
85
  get pathname() {
86
- if (this.#config.type && this.#config.name && this.#config.version) return join('/', this.type, this.name, this.version);
86
+ if (this.#config.type && this.#config.name && this.#config.version)
87
+ return join('/', this.type, this.name, this.version);
87
88
  throw new Error('Eik config was not loaded before calling .pathname');
88
89
  }
89
90
 
@@ -101,6 +102,16 @@ export default class NodeClient {
101
102
 
102
103
  maps() {
103
104
  if (this.#config.version && this.#loadMaps) return this.#maps;
104
- throw new Error('Eik config was not loaded or "loadMaps" is "false" when calling .maps()');
105
+ throw new Error(
106
+ 'Eik config was not loaded or "loadMaps" is "false" when calling .maps()',
107
+ );
108
+ }
109
+
110
+ mapping(dependency) {
111
+ let mapping = null;
112
+ for (const map of this.maps()) {
113
+ if (map?.imports[dependency]) mapping = map.imports[dependency];
114
+ }
115
+ return mapping;
105
116
  }
106
117
  }