@lidofinance/rpc 0.0.3

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/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # @lidofinance/rpc
2
+
3
+ Fetch extension to use it with RPC endpoints.
4
+
5
+ ## Installation
6
+
7
+ `yarn add @lidofinance/rpc`
8
+
9
+ ## Getting started
10
+
11
+ You can just use it as normal `fetch`, there are just a few types restrictions, and it will set right HTTP method for
12
+ you.
13
+
14
+ ```ts
15
+ fetchRPC(url, { body: req.body })
16
+ ```
17
+
18
+ ## Examples
19
+
20
+ ### Caching
21
+
22
+ Here is an example how you can extend `fetchRpc` by adding caching
23
+
24
+ ```ts
25
+ import { FetchRpc, fetchRpc } from '@lidofinance/rpc'
26
+ import { Cache } from 'memory-cache'
27
+
28
+ export type CachedFetchRPC = FetchRpc<{ cacheKey: string }>
29
+
30
+ // Just a sample how to use extend fetchRPC
31
+ export const cachedFetchRpcFactory = (timeout: number): CachedFetchRPC => {
32
+ const cache = new Cache<string, Response>()
33
+
34
+ return async (url, init, { cacheKey }) => {
35
+ const cachedValue = cache.get(cacheKey)
36
+ if (cachedValue != null) {
37
+ return cachedValue
38
+ }
39
+ const response = await fetchRpc(url, init)
40
+ cache.put(cacheKey, response, timeout)
41
+ return response
42
+ }
43
+ }
44
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1,65 @@
1
+ function $parcel$exportWildcard(dest, source) {
2
+ Object.keys(source).forEach(function(key) {
3
+ if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
4
+ return;
5
+ }
6
+
7
+ Object.defineProperty(dest, key, {
8
+ enumerable: true,
9
+ get: function get() {
10
+ return source[key];
11
+ }
12
+ });
13
+ });
14
+
15
+ return dest;
16
+ }
17
+ function $parcel$export(e, n, v, s) {
18
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
19
+ }
20
+ var $3d6d7d97961b05c0$exports = {};
21
+
22
+ $parcel$export($3d6d7d97961b05c0$exports, "fetchRpc", function () { return $3d6d7d97961b05c0$export$51ee03a558564558; });
23
+ const $3d6d7d97961b05c0$export$51ee03a558564558 = (url, init)=>fetch(url, {
24
+ ...init,
25
+ method: "POST",
26
+ body: JSON.stringify(init.body)
27
+ });
28
+
29
+
30
+ var $f5c96785547d6405$exports = {};
31
+
32
+ $parcel$export($f5c96785547d6405$exports, "ExhaustedIterationError", function () { return $f5c96785547d6405$export$544290ca59c30f2b; });
33
+ $parcel$export($f5c96785547d6405$exports, "iterateUrls", function () { return $f5c96785547d6405$export$874e79e4fbe19379; });
34
+ class $f5c96785547d6405$export$544290ca59c30f2b extends Error {
35
+ constructor(message){
36
+ super(message !== null && message !== void 0 ? message : "Iteration ended without success");
37
+ }
38
+ }
39
+ const $f5c96785547d6405$export$874e79e4fbe19379 = async (urls, callback, onError)=>{
40
+ let lastResponse;
41
+ let lastError;
42
+ for (const url of urls)try {
43
+ const response = await callback(url);
44
+ lastResponse = response;
45
+ // We want to return first succeeded response
46
+ if (response instanceof Response && !response.ok) continue;
47
+ return response;
48
+ } catch (error) {
49
+ onError === null || onError === void 0 ? void 0 : onError(error);
50
+ lastError = error;
51
+ }
52
+ // If there are no succeeded responses, return last not thrown
53
+ if (lastResponse != null) return lastResponse;
54
+ // If there are no responses at all, throw last error
55
+ if (lastError != null) throw lastError;
56
+ // This should not be reachable
57
+ throw new $f5c96785547d6405$export$544290ca59c30f2b();
58
+ };
59
+
60
+
61
+ $parcel$exportWildcard(module.exports, $3d6d7d97961b05c0$exports);
62
+ $parcel$exportWildcard(module.exports, $f5c96785547d6405$exports);
63
+
64
+
65
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"mappings":";;;;;;;;;;;;;;;;;;;;;;ACoBO,MAAM,yCAAQ,GAAa,CAAC,GAAG,EAAE,IAAI,GAC1C,KAAK,CAAC,GAAG,EAAE;QACT,GAAG,IAAI;QACP,MAAM,EAAE,MAAM;QACd,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC;KAChC,CAAC;;ADzBJ;;;;;AEAO,MAAM,yCAAuB,SAAS,KAAK;IAChD,YAAY,OAAgB,CAAE;QAC5B,KAAK,CAAC,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,iCAAiC,CAAC;KACpD;CACF;AAEM,MAAM,yCAAW,GAAG,OACzB,IAA2B,EAC3B,QAA4B,EAC5B,OAAqC,GACtB;IACf,IAAI,YAAY,AAAe;IAC/B,IAAI,SAAS,AAAqB;IAElC,KAAK,MAAM,GAAG,IAAI,IAAI,CACpB,IAAI;QACF,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAAC,GAAG,CAAC;QACpC,YAAY,GAAG,QAAQ;QAEvB,6CAA6C;QAC7C,IAAI,QAAQ,YAAY,QAAQ,IAAI,CAAC,QAAQ,CAAC,EAAE,EAC9C,SAAQ;QAGV,OAAO,QAAQ,CAAA;KAChB,CAAC,OAAO,KAAK,EAAE;QACd,OAAO,aAAP,OAAO,WAAS,GAAhB,KAAA,CAAgB,GAAhB,OAAO,CAAG,KAAK,CAAC,CAAA;QAChB,SAAS,GAAG,KAAK;KAClB;IAGH,8DAA8D;IAC9D,IAAI,YAAY,IAAI,IAAI,EACtB,OAAO,YAAY,CAAA;IAErB,qDAAqD;IACrD,IAAI,SAAS,IAAI,IAAI,EACnB,MAAM,SAAS,CAAA;IAEjB,+BAA+B;IAC/B,MAAM,IAAI,yCAAuB,EAAE,CAAA;CACpC;","sources":["packages/core/rpc/src/index.ts","packages/core/rpc/src/fetchRpc.ts","packages/core/rpc/src/iterateUrls.ts"],"sourcesContent":["export * from './fetchRpc'\nexport * from './iterateUrls'\n","export type FetchRpcInitBody = {\n jsonrpc: '1.0' | '2.0' | string\n method: string\n params?: unknown\n id?: string | number | null\n}\n\n// eslint-disable-next-line no-undef\nexport type FetchRpcInit = Omit<RequestInit, 'body' | 'method'> & {\n method?: 'POST'\n body: FetchRpcInitBody | FetchRpcInitBody[]\n}\n\n// Need Extension type so we can extend fetchRPC with metrics tracking or caching\nexport type FetchRpc<Extension extends Record<string | number, unknown> | void = void> = (\n url: string,\n init: FetchRpcInit,\n extension: Extension,\n) => Promise<Response>\n\nexport const fetchRpc: FetchRpc = (url, init) =>\n fetch(url, {\n ...init,\n method: 'POST',\n body: JSON.stringify(init.body),\n })\n","export class ExhaustedIterationError extends Error {\n constructor(message?: string) {\n super(message ?? 'Iteration ended without success')\n }\n}\n\nexport const iterateUrls = async <T>(\n urls: [string, ...string[]],\n callback: (url: string) => T,\n onError?: (error: unknown) => unknown,\n): Promise<T> => {\n let lastResponse: T | undefined\n let lastError: unknown | undefined\n\n for (const url of urls) {\n try {\n const response = await callback(url)\n lastResponse = response\n\n // We want to return first succeeded response\n if (response instanceof Response && !response.ok) {\n continue\n }\n\n return response\n } catch (error) {\n onError?.(error)\n lastError = error\n }\n }\n\n // If there are no succeeded responses, return last not thrown\n if (lastResponse != null) {\n return lastResponse\n }\n // If there are no responses at all, throw last error\n if (lastError != null) {\n throw lastError\n }\n // This should not be reachable\n throw new ExhaustedIterationError()\n}\n"],"names":[],"version":3,"file":"index.cjs.map"}
@@ -0,0 +1,18 @@
1
+ export type FetchRpcInitBody = {
2
+ jsonrpc: '1.0' | '2.0' | string;
3
+ method: string;
4
+ params?: unknown;
5
+ id?: string | number | null;
6
+ };
7
+ export type FetchRpcInit = Omit<RequestInit, 'body' | 'method'> & {
8
+ method?: 'POST';
9
+ body: FetchRpcInitBody | FetchRpcInitBody[];
10
+ };
11
+ export type FetchRpc<Extension extends Record<string | number, unknown> | void = void> = (url: string, init: FetchRpcInit, extension: Extension) => Promise<Response>;
12
+ export const fetchRpc: FetchRpc;
13
+ export class ExhaustedIterationError extends Error {
14
+ constructor(message?: string);
15
+ }
16
+ export const iterateUrls: <T>(urls: [string, ...string[]], callback: (url: string) => T, onError?: ((error: unknown) => unknown) | undefined) => Promise<T>;
17
+
18
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"mappings":"AAAA,+BAA+B;IAC7B,OAAO,EAAE,KAAK,GAAG,KAAK,GAAG,MAAM,CAAA;IAC/B,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAA;CAC5B,CAAA;AAGD,2BAA2B,IAAI,CAAC,WAAW,EAAE,MAAM,GAAG,QAAQ,CAAC,GAAG;IAChE,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,IAAI,EAAE,gBAAgB,GAAG,gBAAgB,EAAE,CAAA;CAC5C,CAAA;AAGD,qBAAqB,SAAS,SAAS,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,GAAG,IAAI,IAAI,CACvF,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,YAAY,EAClB,SAAS,EAAE,SAAS,KACjB,OAAO,CAAC,QAAQ,CAAC,CAAA;AAEtB,OAAO,MAAM,UAAU,QAKnB,CAAA;ACzBJ,oCAAqC,SAAQ,KAAK;gBACpC,OAAO,CAAC,EAAE,MAAM;CAG7B;AAED,OAAO,MAAM,uBACL,CAAC,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC,kBACX,MAAM,2BACJ,OAAO,KAAK,OAAO,4BAgCtC,CAAA","sources":["packages/core/rpc/src/src/fetchRpc.ts","packages/core/rpc/src/src/iterateUrls.ts","packages/core/rpc/src/src/index.ts","packages/core/rpc/src/index.ts"],"sourcesContent":[null,null,null,"export * from './fetchRpc'\nexport * from './iterateUrls'\n"],"names":[],"version":3,"file":"index.d.ts.map"}
package/dist/index.mjs ADDED
@@ -0,0 +1,48 @@
1
+ function $parcel$export(e, n, v, s) {
2
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
3
+ }
4
+ var $c4a16dc5cb85fe9d$exports = {};
5
+
6
+ $parcel$export($c4a16dc5cb85fe9d$exports, "fetchRpc", function () { return $c4a16dc5cb85fe9d$export$51ee03a558564558; });
7
+ const $c4a16dc5cb85fe9d$export$51ee03a558564558 = (url, init)=>fetch(url, {
8
+ ...init,
9
+ method: "POST",
10
+ body: JSON.stringify(init.body)
11
+ });
12
+
13
+
14
+ var $83a6df06ccf0d0e9$exports = {};
15
+
16
+ $parcel$export($83a6df06ccf0d0e9$exports, "ExhaustedIterationError", function () { return $83a6df06ccf0d0e9$export$544290ca59c30f2b; });
17
+ $parcel$export($83a6df06ccf0d0e9$exports, "iterateUrls", function () { return $83a6df06ccf0d0e9$export$874e79e4fbe19379; });
18
+ class $83a6df06ccf0d0e9$export$544290ca59c30f2b extends Error {
19
+ constructor(message){
20
+ super(message !== null && message !== void 0 ? message : "Iteration ended without success");
21
+ }
22
+ }
23
+ const $83a6df06ccf0d0e9$export$874e79e4fbe19379 = async (urls, callback, onError)=>{
24
+ let lastResponse;
25
+ let lastError;
26
+ for (const url of urls)try {
27
+ const response = await callback(url);
28
+ lastResponse = response;
29
+ // We want to return first succeeded response
30
+ if (response instanceof Response && !response.ok) continue;
31
+ return response;
32
+ } catch (error) {
33
+ onError === null || onError === void 0 ? void 0 : onError(error);
34
+ lastError = error;
35
+ }
36
+ // If there are no succeeded responses, return last not thrown
37
+ if (lastResponse != null) return lastResponse;
38
+ // If there are no responses at all, throw last error
39
+ if (lastError != null) throw lastError;
40
+ // This should not be reachable
41
+ throw new $83a6df06ccf0d0e9$export$544290ca59c30f2b();
42
+ };
43
+
44
+
45
+
46
+
47
+ export {$c4a16dc5cb85fe9d$export$51ee03a558564558 as fetchRpc, $83a6df06ccf0d0e9$export$544290ca59c30f2b as ExhaustedIterationError, $83a6df06ccf0d0e9$export$874e79e4fbe19379 as iterateUrls};
48
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"mappings":";;;;;;ACoBO,MAAM,yCAAQ,GAAa,CAAC,GAAG,EAAE,IAAI,GAC1C,KAAK,CAAC,GAAG,EAAE;QACT,GAAG,IAAI;QACP,MAAM,EAAE,MAAM;QACd,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC;KAChC,CAAC;;ADzBJ;;;;;AEAO,MAAM,yCAAuB,SAAS,KAAK;IAChD,YAAY,OAAgB,CAAE;QAC5B,KAAK,CAAC,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,iCAAiC,CAAC;KACpD;CACF;AAEM,MAAM,yCAAW,GAAG,OACzB,IAA2B,EAC3B,QAA4B,EAC5B,OAAqC,GACtB;IACf,IAAI,YAAY,AAAe;IAC/B,IAAI,SAAS,AAAqB;IAElC,KAAK,MAAM,GAAG,IAAI,IAAI,CACpB,IAAI;QACF,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAAC,GAAG,CAAC;QACpC,YAAY,GAAG,QAAQ;QAEvB,6CAA6C;QAC7C,IAAI,QAAQ,YAAY,QAAQ,IAAI,CAAC,QAAQ,CAAC,EAAE,EAC9C,SAAQ;QAGV,OAAO,QAAQ,CAAA;KAChB,CAAC,OAAO,KAAK,EAAE;QACd,OAAO,aAAP,OAAO,WAAS,GAAhB,KAAA,CAAgB,GAAhB,OAAO,CAAG,KAAK,CAAC,CAAA;QAChB,SAAS,GAAG,KAAK;KAClB;IAGH,8DAA8D;IAC9D,IAAI,YAAY,IAAI,IAAI,EACtB,OAAO,YAAY,CAAA;IAErB,qDAAqD;IACrD,IAAI,SAAS,IAAI,IAAI,EACnB,MAAM,SAAS,CAAA;IAEjB,+BAA+B;IAC/B,MAAM,IAAI,yCAAuB,EAAE,CAAA;CACpC;","sources":["packages/core/rpc/src/index.ts","packages/core/rpc/src/fetchRpc.ts","packages/core/rpc/src/iterateUrls.ts"],"sourcesContent":["export * from './fetchRpc'\nexport * from './iterateUrls'\n","export type FetchRpcInitBody = {\n jsonrpc: '1.0' | '2.0' | string\n method: string\n params?: unknown\n id?: string | number | null\n}\n\n// eslint-disable-next-line no-undef\nexport type FetchRpcInit = Omit<RequestInit, 'body' | 'method'> & {\n method?: 'POST'\n body: FetchRpcInitBody | FetchRpcInitBody[]\n}\n\n// Need Extension type so we can extend fetchRPC with metrics tracking or caching\nexport type FetchRpc<Extension extends Record<string | number, unknown> | void = void> = (\n url: string,\n init: FetchRpcInit,\n extension: Extension,\n) => Promise<Response>\n\nexport const fetchRpc: FetchRpc = (url, init) =>\n fetch(url, {\n ...init,\n method: 'POST',\n body: JSON.stringify(init.body),\n })\n","export class ExhaustedIterationError extends Error {\n constructor(message?: string) {\n super(message ?? 'Iteration ended without success')\n }\n}\n\nexport const iterateUrls = async <T>(\n urls: [string, ...string[]],\n callback: (url: string) => T,\n onError?: (error: unknown) => unknown,\n): Promise<T> => {\n let lastResponse: T | undefined\n let lastError: unknown | undefined\n\n for (const url of urls) {\n try {\n const response = await callback(url)\n lastResponse = response\n\n // We want to return first succeeded response\n if (response instanceof Response && !response.ok) {\n continue\n }\n\n return response\n } catch (error) {\n onError?.(error)\n lastError = error\n }\n }\n\n // If there are no succeeded responses, return last not thrown\n if (lastResponse != null) {\n return lastResponse\n }\n // If there are no responses at all, throw last error\n if (lastError != null) {\n throw lastError\n }\n // This should not be reachable\n throw new ExhaustedIterationError()\n}\n"],"names":[],"version":3,"file":"index.mjs.map"}
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@lidofinance/rpc",
3
+ "description": "Fetch extension to use it with RPC endpoints",
4
+ "repository": "git@github.com:lidofinance/lido-ui-blocks.git",
5
+ "license": "MIT",
6
+ "version": "0.0.3",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "engines": {
11
+ "node": ">= 16",
12
+ "browsers": "> 0.5%, last 2 versions, not dead"
13
+ },
14
+ "source": "./src/index.ts",
15
+ "main": "dist/index.cjs",
16
+ "module": "dist/index.mjs",
17
+ "types": "dist/index.d.ts",
18
+ "scripts": {
19
+ "build": "parcel build",
20
+ "format": "prettier --check src",
21
+ "format:fix": "yarn format --write",
22
+ "types": "tsc --noEmit"
23
+ },
24
+ "devDependencies": {
25
+ "@lidofinance/config-prettier": "0.0.1"
26
+ }
27
+ }