@vdegenne/cmc 0.1.35 → 0.1.37
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/data/all.json +1 -1
- package/data/mini.json +1 -1
- package/lib/CMCManager.d.ts +4 -26
- package/lib/CMCManager.d.ts.map +1 -1
- package/lib/CMCManager.js +20 -23
- package/lib/CMCManager.js.map +1 -1
- package/package.json +1 -1
package/lib/CMCManager.d.ts
CHANGED
|
@@ -1,46 +1,24 @@
|
|
|
1
1
|
type UrlBuilderFunction = (getLastVersion: () => Promise<string>) => Promise<string>;
|
|
2
2
|
interface ManagerOptions {
|
|
3
3
|
/**
|
|
4
|
-
* Data to use to hydrate the object when
|
|
4
|
+
* Data to use to hydrate the object when instantiated.
|
|
5
5
|
* Providing this will automatically turn prefetch to false.
|
|
6
6
|
*
|
|
7
7
|
* @default undefined
|
|
8
8
|
*/
|
|
9
9
|
initData: CMC.MiniCurrency[] | undefined;
|
|
10
10
|
/**
|
|
11
|
-
*
|
|
11
|
+
* Whether to fetch data remotely automatically when object is created.
|
|
12
12
|
*
|
|
13
13
|
* @default true
|
|
14
14
|
*/
|
|
15
15
|
prefetch: boolean;
|
|
16
16
|
/**
|
|
17
|
-
* Where to load the data from when using
|
|
18
|
-
*
|
|
19
|
-
* By default it creates the url using the follow UrlBuilderFunction
|
|
20
|
-
* ```ts
|
|
21
|
-
* async function (getLastVersion) {
|
|
22
|
-
* return `https://cdn.jsdelivr.net/npm/@vdegenne/cmc@${await getLastVersion()}/data/mini.json`;
|
|
23
|
-
* }
|
|
24
|
-
* ```
|
|
25
|
-
*
|
|
26
|
-
* You can also use your own function:
|
|
27
|
-
* ```ts
|
|
28
|
-
* new CMCManager({
|
|
29
|
-
* async remoteUrl(getLastVersion) { return `https://unpkg.com/@vdegenne/cmc@${await getLastVersion()}/data/all.json` }
|
|
30
|
-
* })
|
|
31
|
-
* ```
|
|
32
|
-
*
|
|
33
|
-
* Or just a simple string:
|
|
34
|
-
* ```ts
|
|
35
|
-
* new CMCManager({
|
|
36
|
-
* remoteUrl: 'https://unpkg.com/@vdegenne/cmc@0.1.17/mini.json'
|
|
37
|
-
* })
|
|
38
|
-
* ```
|
|
17
|
+
* Where to load the data from when using loadRemote().
|
|
39
18
|
*/
|
|
40
19
|
remoteUrl: string | UrlBuilderFunction;
|
|
41
20
|
/**
|
|
42
|
-
* Trying to encourage cache if possible when using
|
|
43
|
-
* That doesn't prevent the endpoint to provide updated data when it changes.
|
|
21
|
+
* Trying to encourage cache if possible when using loadRemote().
|
|
44
22
|
*
|
|
45
23
|
* @default undefined
|
|
46
24
|
*/
|
package/lib/CMCManager.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CMCManager.d.ts","sourceRoot":"","sources":["../src/CMCManager.ts"],"names":[],"mappings":"AAAA,KAAK,kBAAkB,GAAG,CACzB,cAAc,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,KACjC,OAAO,CAAC,MAAM,CAAC,CAAA;AAEpB,UAAU,cAAc;IACvB;;;;;OAKG;IACH,QAAQ,EAAE,GAAG,CAAC,YAAY,EAAE,GAAG,SAAS,CAAA;IAExC;;;;OAIG;IACH,QAAQ,EAAE,OAAO,CAAA;IAEjB
|
|
1
|
+
{"version":3,"file":"CMCManager.d.ts","sourceRoot":"","sources":["../src/CMCManager.ts"],"names":[],"mappings":"AAAA,KAAK,kBAAkB,GAAG,CACzB,cAAc,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,KACjC,OAAO,CAAC,MAAM,CAAC,CAAA;AAEpB,UAAU,cAAc;IACvB;;;;;OAKG;IACH,QAAQ,EAAE,GAAG,CAAC,YAAY,EAAE,GAAG,SAAS,CAAA;IAExC;;;;OAIG;IACH,QAAQ,EAAE,OAAO,CAAA;IAEjB;;OAEG;IACH,SAAS,EAAE,MAAM,GAAG,kBAAkB,CAAA;IAEtC;;;;OAIG;IACH,kBAAkB,EACf,SAAS,GACT,UAAU,GACV,aAAa,GACb,UAAU,GACV,gBAAgB,GAChB,QAAQ,GACR,SAAS,CAAA;IAEZ,KAAK,EAAE,OAAO,CAAA;CACd;AAeD,qBAAa,UAAU;;IAYtB,YAAY,OAAO,CAAC,EAAE,OAAO,CAAC,cAAc,CAAC,EAoB5C;IAEK,UAAU,CACf,aAAa,gGAAmC,GAC9C,OAAO,CAAC,IAAI,CAAC,CAyBf;IAED,IAAI,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAEzB;IAED,OAAO,CAAC,UAAU;IAQlB,qBAAqB,CACpB,MAAM,EAAE,MAAM,EACd,QAAQ,UAAQ,GACd,GAAG,CAAC,YAAY,GAAG,SAAS,CAY9B;IAED,MAAM,IAAI,aAAa,CAAC,GAAG,CAAC,YAAY,CAAC,CAGxC;CACD"}
|
package/lib/CMCManager.js
CHANGED
|
@@ -1,24 +1,27 @@
|
|
|
1
1
|
async function getLastVersion() {
|
|
2
2
|
const res = await fetch('https://registry.npmjs.org/@vdegenne/cmc/latest');
|
|
3
|
+
if (!res.ok) {
|
|
4
|
+
throw new Error(`Failed to fetch latest version: ${res.status} ${res.statusText}`);
|
|
5
|
+
}
|
|
3
6
|
const data = await res.json();
|
|
4
7
|
return data.version;
|
|
5
8
|
}
|
|
6
9
|
export class CMCManager {
|
|
7
10
|
#options;
|
|
8
|
-
#data
|
|
11
|
+
#data;
|
|
9
12
|
#ready;
|
|
13
|
+
/**
|
|
14
|
+
* @deprecated
|
|
15
|
+
*/
|
|
10
16
|
#log(...message) {
|
|
11
|
-
if (this.#options.debug)
|
|
17
|
+
if (this.#options.debug)
|
|
12
18
|
console.log(...message);
|
|
13
|
-
}
|
|
14
19
|
}
|
|
15
20
|
constructor(options) {
|
|
16
21
|
this.#options = {
|
|
17
22
|
prefetch: true,
|
|
18
23
|
initData: undefined,
|
|
19
|
-
async
|
|
20
|
-
return `https://cdn.jsdelivr.net/npm/@vdegenne/cmc@${await getLastVersion()}/data/mini.json`;
|
|
21
|
-
},
|
|
24
|
+
remoteUrl: async (getLastVersion) => `https://cdn.jsdelivr.net/npm/@vdegenne/cmc@${await getLastVersion()}/data/mini.json`,
|
|
22
25
|
fetchCacheStrategy: undefined,
|
|
23
26
|
debug: false,
|
|
24
27
|
...options,
|
|
@@ -36,21 +39,12 @@ export class CMCManager {
|
|
|
36
39
|
async loadRemote(cacheStrategy = this.#options.fetchCacheStrategy) {
|
|
37
40
|
this.#ready = (async () => {
|
|
38
41
|
this.#log('Fetching remote data...');
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
break;
|
|
44
|
-
case 'function':
|
|
45
|
-
url = await this.#options.remoteUrl(getLastVersion);
|
|
46
|
-
break;
|
|
47
|
-
default:
|
|
48
|
-
// exhaustive check: if we ever get here, TS will error
|
|
49
|
-
const _exhaustive = this.#options.remoteUrl;
|
|
50
|
-
throw new Error(`Unsupported type: ${_exhaustive}`);
|
|
51
|
-
}
|
|
42
|
+
const remoteUrl = this.#options.remoteUrl;
|
|
43
|
+
const url = typeof remoteUrl === 'string'
|
|
44
|
+
? remoteUrl
|
|
45
|
+
: await remoteUrl(getLastVersion);
|
|
52
46
|
const res = await fetch(url, {
|
|
53
|
-
...(cacheStrategy !== undefined
|
|
47
|
+
...(cacheStrategy !== undefined && { cache: cacheStrategy }),
|
|
54
48
|
});
|
|
55
49
|
if (!res.ok) {
|
|
56
50
|
throw new Error(`Failed to fetch crypto data: ${res.status} ${res.statusText}`);
|
|
@@ -70,9 +64,12 @@ export class CMCManager {
|
|
|
70
64
|
}
|
|
71
65
|
getCurrencyFromSymbol(symbol, fromLast = false) {
|
|
72
66
|
this.ensureData();
|
|
73
|
-
const
|
|
74
|
-
const
|
|
75
|
-
|
|
67
|
+
const normalizedSymbol = symbol.toUpperCase();
|
|
68
|
+
const data = this.#data;
|
|
69
|
+
const currency = fromLast
|
|
70
|
+
? data.findLast((c) => c.symbol.toUpperCase() === normalizedSymbol)
|
|
71
|
+
: data.find((c) => c.symbol.toUpperCase() === normalizedSymbol);
|
|
72
|
+
// this.#log('getCurrencyFromSymbol found: ', currency)
|
|
76
73
|
return currency;
|
|
77
74
|
}
|
|
78
75
|
getAll() {
|
package/lib/CMCManager.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CMCManager.js","sourceRoot":"","sources":["../src/CMCManager.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"CMCManager.js","sourceRoot":"","sources":["../src/CMCManager.ts"],"names":[],"mappings":"AA0CA,KAAK,UAAU,cAAc;IAC5B,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,iDAAiD,CAAC,CAAA;IAE1E,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CACd,mCAAmC,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,UAAU,EAAE,CACjE,CAAA;IACF,CAAC;IAED,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,IAAI,EAAE,CAAA;IAC7B,OAAO,IAAI,CAAC,OAAiB,CAAA;AAC9B,CAAC;AAED,MAAM,OAAO,UAAU;IACtB,QAAQ,CAAgB;IACxB,KAAK,CAAiD;IACtD,MAAM,CAA2B;IAEjC;;OAEG;IACH,IAAI,CAAC,GAAG,OAAc;QACrB,IAAI,IAAI,CAAC,QAAQ,CAAC,KAAK;YAAE,OAAO,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,CAAA;IACjD,CAAC;IAED,YAAY,OAAiC;QAC5C,IAAI,CAAC,QAAQ,GAAG;YACf,QAAQ,EAAE,IAAI;YACd,QAAQ,EAAE,SAAS;YACnB,SAAS,EAAE,KAAK,EAAE,cAAc,EAAE,EAAE,CACnC,8CAA8C,MAAM,cAAc,EAAE,iBAAiB;YACtF,kBAAkB,EAAE,SAAS;YAC7B,KAAK,EAAE,KAAK;YACZ,GAAG,OAAO;SACV,CAAA;QAED,IAAI,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,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