@polkadot-api/raw-client 0.1.0 → 0.2.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/LICENSE +21 -0
- package/dist/esm/createClient.mjs +31 -38
- package/dist/esm/createClient.mjs.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +32 -38
- package/dist/index.js.map +1 -1
- package/package.json +6 -7
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Josep M Sobrepere
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -1,54 +1,47 @@
|
|
|
1
|
+
import { isResponse } from '@polkadot-api/json-rpc-provider';
|
|
1
2
|
import { RpcError } from './RpcError.mjs';
|
|
2
3
|
import { getSubscriptionsManager } from './subscriptions-manager.mjs';
|
|
3
4
|
import { DestroyedError } from './DestroyedError.mjs';
|
|
4
5
|
|
|
5
6
|
let nextClientId = 1;
|
|
6
|
-
const createClient = (gProvider) => {
|
|
7
|
+
const createClient = (gProvider, onNotification) => {
|
|
7
8
|
let clientId = nextClientId++;
|
|
8
9
|
const responses = /* @__PURE__ */ new Map();
|
|
9
10
|
const subscriptions = getSubscriptionsManager();
|
|
10
11
|
let connection = null;
|
|
11
12
|
const send = (id, method, params) => {
|
|
12
|
-
connection.send(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
})
|
|
19
|
-
);
|
|
13
|
+
connection.send({
|
|
14
|
+
jsonrpc: "2.0",
|
|
15
|
+
id,
|
|
16
|
+
method,
|
|
17
|
+
params
|
|
18
|
+
});
|
|
20
19
|
};
|
|
21
|
-
function onMessage(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
(
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
if (!subscription || !error && !Object.hasOwn(params, "result")) throw 0;
|
|
42
|
-
const subscriptionId = subscription;
|
|
43
|
-
if (error) {
|
|
44
|
-
subscriptions.error(subscriptionId, new RpcError(error));
|
|
20
|
+
function onMessage(parsed) {
|
|
21
|
+
if (isResponse(parsed)) {
|
|
22
|
+
const { id } = parsed;
|
|
23
|
+
const cb = responses.get(id);
|
|
24
|
+
if (!cb) return;
|
|
25
|
+
responses.delete(id);
|
|
26
|
+
return "error" in parsed ? cb.onError(new RpcError(parsed.error)) : cb.onSuccess(parsed.result, (opaqueId, subscriber) => {
|
|
27
|
+
const subscriptionId = opaqueId;
|
|
28
|
+
subscriptions.subscribe(subscriptionId, subscriber);
|
|
29
|
+
return () => {
|
|
30
|
+
subscriptions.unsubscribe(subscriptionId);
|
|
31
|
+
};
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
if (parsed.id === void 0) {
|
|
35
|
+
const { params } = parsed;
|
|
36
|
+
const { subscription: subscriptionId, result, error } = params;
|
|
37
|
+
if (subscriptionId && subscriptions.has(subscriptionId) && ("result" in params || error)) {
|
|
38
|
+
if (error) subscriptions.error(subscriptionId, new RpcError(error));
|
|
39
|
+
else subscriptions.next(subscriptionId, result);
|
|
45
40
|
} else {
|
|
46
|
-
|
|
41
|
+
onNotification?.(parsed);
|
|
47
42
|
}
|
|
48
|
-
}
|
|
49
|
-
console.warn("Error parsing incomming message: " +
|
|
50
|
-
console.error(e);
|
|
51
|
-
}
|
|
43
|
+
} else
|
|
44
|
+
console.warn("Error parsing incomming message: " + JSON.stringify(parsed));
|
|
52
45
|
}
|
|
53
46
|
connection = gProvider(onMessage);
|
|
54
47
|
const disconnect = () => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createClient.mjs","sources":["../../src/createClient.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"file":"createClient.mjs","sources":["../../src/createClient.ts"],"sourcesContent":["import {\n isResponse,\n JsonRpcRequest,\n type JsonRpcConnection,\n type JsonRpcId,\n type JsonRpcMessage,\n type JsonRpcProvider,\n} from \"@polkadot-api/json-rpc-provider\"\nimport { RpcError } from \"./RpcError\"\nimport { getSubscriptionsManager, Subscriber } from \"./subscriptions-manager\"\nimport { DestroyedError } from \"./DestroyedError\"\n\ntype UnsubscribeFn = () => void\nexport type FollowSubscriptionCb<T> = (\n subscriptionId: string,\n cb: Subscriber<T>,\n) => UnsubscribeFn\n\nexport type ClientRequestCb<T, TT> = {\n onSuccess: (result: T, followSubscription: FollowSubscriptionCb<TT>) => void\n onError: (e: Error) => void\n}\n\nexport type ClientRequest<T, TT> = (\n method: string,\n params: Array<any>,\n cb?: ClientRequestCb<T, TT>,\n) => UnsubscribeFn\n\nexport interface Client {\n disconnect: () => void\n request: ClientRequest<any, any>\n}\n\nlet nextClientId = 1\nexport const createClient = (\n gProvider: JsonRpcProvider,\n onNotification?: (req: JsonRpcRequest) => void,\n): Client => {\n let clientId = nextClientId++\n const responses = new Map<JsonRpcId, ClientRequestCb<any, any>>()\n const subscriptions = getSubscriptionsManager()\n\n let connection: JsonRpcConnection | null = null\n const send = (\n id: string,\n method: string,\n params: Array<boolean | string | number | null>,\n ) => {\n connection!.send({\n jsonrpc: \"2.0\",\n id,\n method,\n params,\n })\n }\n\n function onMessage(parsed: JsonRpcMessage): void {\n if (isResponse(parsed)) {\n const { id } = parsed\n const cb = responses.get(id)\n if (!cb) return\n\n responses.delete(id)\n\n return \"error\" in parsed\n ? cb.onError(new RpcError(parsed.error))\n : cb.onSuccess(parsed.result, (opaqueId, subscriber) => {\n const subscriptionId = opaqueId\n subscriptions.subscribe(subscriptionId, subscriber)\n return () => {\n subscriptions.unsubscribe(subscriptionId)\n }\n })\n }\n\n if (parsed.id === undefined) {\n // at this point, it means that it should be a notification\n const { params } = parsed\n const { subscription: subscriptionId, result, error } = params\n if (\n subscriptionId &&\n subscriptions.has(subscriptionId) &&\n (\"result\" in params || error)\n ) {\n if (error) subscriptions.error(subscriptionId, new RpcError(error!))\n else subscriptions.next(subscriptionId, result)\n } else {\n onNotification?.(parsed)\n }\n } else\n console.warn(\"Error parsing incomming message: \" + JSON.stringify(parsed))\n }\n\n connection = gProvider(onMessage)\n\n const disconnect = () => {\n connection?.disconnect()\n connection = null\n subscriptions.errorAll(new DestroyedError())\n responses.forEach((r) => r.onError(new DestroyedError()))\n responses.clear()\n }\n\n let nextId = 1\n const request = <T, TT>(\n method: string,\n params: Array<any>,\n cb?: ClientRequestCb<T, TT>,\n ): UnsubscribeFn => {\n if (!connection) throw new Error(\"Not connected\")\n const id = `${clientId}-${nextId++}`\n\n if (cb) responses.set(id, cb)\n send(id, method, params)\n\n return (): void => {\n responses.delete(id)\n }\n }\n\n return {\n request,\n disconnect,\n }\n}\n"],"names":[],"mappings":";;;;;AAkCA,IAAI,YAAA,GAAe,CAAA;AACZ,MAAM,YAAA,GAAe,CAC1B,SAAA,EACA,cAAA,KACW;AACX,EAAA,IAAI,QAAA,GAAW,YAAA,EAAA;AACf,EAAA,MAAM,SAAA,uBAAgB,GAAA,EAA0C;AAChE,EAAA,MAAM,gBAAgB,uBAAA,EAAwB;AAE9C,EAAA,IAAI,UAAA,GAAuC,IAAA;AAC3C,EAAA,MAAM,IAAA,GAAO,CACX,EAAA,EACA,MAAA,EACA,MAAA,KACG;AACH,IAAA,UAAA,CAAY,IAAA,CAAK;AAAA,MACf,OAAA,EAAS,KAAA;AAAA,MACT,EAAA;AAAA,MACA,MAAA;AAAA,MACA;AAAA,KACD,CAAA;AAAA,EACH,CAAA;AAEA,EAAA,SAAS,UAAU,MAAA,EAA8B;AAC/C,IAAA,IAAI,UAAA,CAAW,MAAM,CAAA,EAAG;AACtB,MAAA,MAAM,EAAE,IAAG,GAAI,MAAA;AACf,MAAA,MAAM,EAAA,GAAK,SAAA,CAAU,GAAA,CAAI,EAAE,CAAA;AAC3B,MAAA,IAAI,CAAC,EAAA,EAAI;AAET,MAAA,SAAA,CAAU,OAAO,EAAE,CAAA;AAEnB,MAAA,OAAO,WAAW,MAAA,GACd,EAAA,CAAG,OAAA,CAAQ,IAAI,SAAS,MAAA,CAAO,KAAK,CAAC,CAAA,GACrC,GAAG,SAAA,CAAU,MAAA,CAAO,MAAA,EAAQ,CAAC,UAAU,UAAA,KAAe;AACpD,QAAA,MAAM,cAAA,GAAiB,QAAA;AACvB,QAAA,aAAA,CAAc,SAAA,CAAU,gBAAgB,UAAU,CAAA;AAClD,QAAA,OAAO,MAAM;AACX,UAAA,aAAA,CAAc,YAAY,cAAc,CAAA;AAAA,QAC1C,CAAA;AAAA,MACF,CAAC,C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AAI,MAAA,EAAQ,CAAA,CAAA;AAElC,IAAA,IAAI,EAAA,EAAI,SAAA,CAAU,GAAA,CAAI,EAAA,EAAI,EAAE,CAAA;AAC5B,IAAA,IAAA,CAAK,EAAA,EAAI,QAAQ,MAAM,CAAA;AAEvB,IAAA,OAAO,MAAY;AACjB,MAAA,SAAA,CAAU,OAAO,EAAE,CAAA;AAAA,IACrB,CAAA;AAAA,EACF,CAAA;AAEA,EAAA,OAAO;AAAA,IACL,OAAA;AAAA,IACA;AAAA,GACF;AACF;;;;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { JsonRpcProvider } from '@polkadot-api/json-rpc-provider';
|
|
1
|
+
import { JsonRpcProvider, JsonRpcRequest } from '@polkadot-api/json-rpc-provider';
|
|
2
2
|
|
|
3
3
|
interface IRpcError {
|
|
4
4
|
code: number;
|
|
@@ -36,7 +36,7 @@ interface Client {
|
|
|
36
36
|
disconnect: () => void;
|
|
37
37
|
request: ClientRequest<any, any>;
|
|
38
38
|
}
|
|
39
|
-
declare const createClient: (gProvider: JsonRpcProvider) => Client;
|
|
39
|
+
declare const createClient: (gProvider: JsonRpcProvider, onNotification?: (req: JsonRpcRequest) => void) => Client;
|
|
40
40
|
|
|
41
41
|
declare class DestroyedError extends Error {
|
|
42
42
|
constructor();
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var jsonRpcProvider = require('@polkadot-api/json-rpc-provider');
|
|
4
|
+
|
|
3
5
|
var __defProp = Object.defineProperty;
|
|
4
6
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
5
7
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
@@ -52,52 +54,44 @@ class DestroyedError extends Error {
|
|
|
52
54
|
}
|
|
53
55
|
|
|
54
56
|
let nextClientId = 1;
|
|
55
|
-
const createClient = (gProvider) => {
|
|
57
|
+
const createClient = (gProvider, onNotification) => {
|
|
56
58
|
let clientId = nextClientId++;
|
|
57
59
|
const responses = /* @__PURE__ */ new Map();
|
|
58
60
|
const subscriptions = getSubscriptionsManager();
|
|
59
61
|
let connection = null;
|
|
60
62
|
const send = (id, method, params) => {
|
|
61
|
-
connection.send(
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
})
|
|
68
|
-
);
|
|
63
|
+
connection.send({
|
|
64
|
+
jsonrpc: "2.0",
|
|
65
|
+
id,
|
|
66
|
+
method,
|
|
67
|
+
params
|
|
68
|
+
});
|
|
69
69
|
};
|
|
70
|
-
function onMessage(
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
const
|
|
74
|
-
(
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
if (!subscription || !error && !Object.hasOwn(params, "result")) throw 0;
|
|
91
|
-
const subscriptionId = subscription;
|
|
92
|
-
if (error) {
|
|
93
|
-
subscriptions.error(subscriptionId, new RpcError(error));
|
|
70
|
+
function onMessage(parsed) {
|
|
71
|
+
if (jsonRpcProvider.isResponse(parsed)) {
|
|
72
|
+
const { id } = parsed;
|
|
73
|
+
const cb = responses.get(id);
|
|
74
|
+
if (!cb) return;
|
|
75
|
+
responses.delete(id);
|
|
76
|
+
return "error" in parsed ? cb.onError(new RpcError(parsed.error)) : cb.onSuccess(parsed.result, (opaqueId, subscriber) => {
|
|
77
|
+
const subscriptionId = opaqueId;
|
|
78
|
+
subscriptions.subscribe(subscriptionId, subscriber);
|
|
79
|
+
return () => {
|
|
80
|
+
subscriptions.unsubscribe(subscriptionId);
|
|
81
|
+
};
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
if (parsed.id === void 0) {
|
|
85
|
+
const { params } = parsed;
|
|
86
|
+
const { subscription: subscriptionId, result, error } = params;
|
|
87
|
+
if (subscriptionId && subscriptions.has(subscriptionId) && ("result" in params || error)) {
|
|
88
|
+
if (error) subscriptions.error(subscriptionId, new RpcError(error));
|
|
89
|
+
else subscriptions.next(subscriptionId, result);
|
|
94
90
|
} else {
|
|
95
|
-
|
|
91
|
+
onNotification?.(parsed);
|
|
96
92
|
}
|
|
97
|
-
}
|
|
98
|
-
console.warn("Error parsing incomming message: " +
|
|
99
|
-
console.error(e);
|
|
100
|
-
}
|
|
93
|
+
} else
|
|
94
|
+
console.warn("Error parsing incomming message: " + JSON.stringify(parsed));
|
|
101
95
|
}
|
|
102
96
|
connection = gProvider(onMessage);
|
|
103
97
|
const disconnect = () => {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/RpcError.ts","../src/subscriptions-manager.ts","../src/DestroyedError.ts","../src/createClient.ts"],"sourcesContent":["export interface IRpcError {\n code: number\n message: string\n data?: any\n}\n\nexport class RpcError extends Error implements IRpcError {\n code\n data\n constructor(e: IRpcError) {\n super(e.message)\n this.code = e.code\n this.data = e.data\n this.name = \"RpcError\"\n }\n}\n","export interface Subscriber<T> {\n next: (data: T) => void\n error: (e: Error) => void\n}\n\nexport const getSubscriptionsManager = <T>() => {\n const subscriptions = new Map<string, Subscriber<T>>()\n\n return {\n has: subscriptions.has.bind(subscriptions),\n subscribe(id: string, subscriber: Subscriber<T>) {\n subscriptions.set(id, subscriber)\n },\n unsubscribe(id: string) {\n subscriptions.delete(id)\n },\n next(id: string, data: T) {\n subscriptions.get(id)?.next(data)\n },\n error(id: string, e: Error) {\n const subscriber = subscriptions.get(id)\n if (subscriber) {\n subscriptions.delete(id)\n subscriber.error(e)\n }\n },\n errorAll(e: Error) {\n const subscribers = [...subscriptions.values()]\n subscriptions.clear()\n subscribers.forEach((s) => {\n s.error(e)\n })\n },\n }\n}\n\nexport type SubscriptionManager<T> = ReturnType<\n typeof getSubscriptionsManager<T>\n>\n","export class DestroyedError extends Error {\n constructor() {\n super(\"Client destroyed\")\n this.name = \"DestroyedError\"\n }\n}\n","import
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/RpcError.ts","../src/subscriptions-manager.ts","../src/DestroyedError.ts","../src/createClient.ts"],"sourcesContent":["export interface IRpcError {\n code: number\n message: string\n data?: any\n}\n\nexport class RpcError extends Error implements IRpcError {\n code\n data\n constructor(e: IRpcError) {\n super(e.message)\n this.code = e.code\n this.data = e.data\n this.name = \"RpcError\"\n }\n}\n","export interface Subscriber<T> {\n next: (data: T) => void\n error: (e: Error) => void\n}\n\nexport const getSubscriptionsManager = <T>() => {\n const subscriptions = new Map<string, Subscriber<T>>()\n\n return {\n has: subscriptions.has.bind(subscriptions),\n subscribe(id: string, subscriber: Subscriber<T>) {\n subscriptions.set(id, subscriber)\n },\n unsubscribe(id: string) {\n subscriptions.delete(id)\n },\n next(id: string, data: T) {\n subscriptions.get(id)?.next(data)\n },\n error(id: string, e: Error) {\n const subscriber = subscriptions.get(id)\n if (subscriber) {\n subscriptions.delete(id)\n subscriber.error(e)\n }\n },\n errorAll(e: Error) {\n const subscribers = [...subscriptions.values()]\n subscriptions.clear()\n subscribers.forEach((s) => {\n s.error(e)\n })\n },\n }\n}\n\nexport type SubscriptionManager<T> = ReturnType<\n typeof getSubscriptionsManager<T>\n>\n","export class DestroyedError extends Error {\n constructor() {\n super(\"Client destroyed\")\n this.name = \"DestroyedError\"\n }\n}\n","import {\n isResponse,\n JsonRpcRequest,\n type JsonRpcConnection,\n type JsonRpcId,\n type JsonRpcMessage,\n type JsonRpcProvider,\n} from \"@polkadot-api/json-rpc-provider\"\nimport { RpcError } from \"./RpcError\"\nimport { getSubscriptionsManager, Subscriber } from \"./subscriptions-manager\"\nimport { DestroyedError } from \"./DestroyedError\"\n\ntype UnsubscribeFn = () => void\nexport type FollowSubscriptionCb<T> = (\n subscriptionId: string,\n cb: Subscriber<T>,\n) => UnsubscribeFn\n\nexport type ClientRequestCb<T, TT> = {\n onSuccess: (result: T, followSubscription: FollowSubscriptionCb<TT>) => void\n onError: (e: Error) => void\n}\n\nexport type ClientRequest<T, TT> = (\n method: string,\n params: Array<any>,\n cb?: ClientRequestCb<T, TT>,\n) => UnsubscribeFn\n\nexport interface Client {\n disconnect: () => void\n request: ClientRequest<any, any>\n}\n\nlet nextClientId = 1\nexport const createClient = (\n gProvider: JsonRpcProvider,\n onNotification?: (req: JsonRpcRequest) => void,\n): Client => {\n let clientId = nextClientId++\n const responses = new Map<JsonRpcId, ClientRequestCb<any, any>>()\n const subscriptions = getSubscriptionsManager()\n\n let connection: JsonRpcConnection | null = null\n const send = (\n id: string,\n method: string,\n params: Array<boolean | string | number | null>,\n ) => {\n connection!.send({\n jsonrpc: \"2.0\",\n id,\n method,\n params,\n })\n }\n\n function onMessage(parsed: JsonRpcMessage): void {\n if (isResponse(parsed)) {\n const { id } = parsed\n const cb = responses.get(id)\n if (!cb) return\n\n responses.delete(id)\n\n return \"error\" in parsed\n ? cb.onError(new RpcError(parsed.error))\n : cb.onSuccess(parsed.result, (opaqueId, subscriber) => {\n const subscriptionId = opaqueId\n subscriptions.subscribe(subscriptionId, subscriber)\n return () => {\n subscriptions.unsubscribe(subscriptionId)\n }\n })\n }\n\n if (parsed.id === undefined) {\n // at this point, it means that it should be a notification\n const { params } = parsed\n const { subscription: subscriptionId, result, error } = params\n if (\n subscriptionId &&\n subscriptions.has(subscriptionId) &&\n (\"result\" in params || error)\n ) {\n if (error) subscriptions.error(subscriptionId, new RpcError(error!))\n else subscriptions.next(subscriptionId, result)\n } else {\n onNotification?.(parsed)\n }\n } else\n console.warn(\"Error parsing incomming message: \" + JSON.stringify(parsed))\n }\n\n connection = gProvider(onMessage)\n\n const disconnect = () => {\n connection?.disconnect()\n connection = null\n subscriptions.errorAll(new DestroyedError())\n responses.forEach((r) => r.onError(new DestroyedError()))\n responses.clear()\n }\n\n let nextId = 1\n const request = <T, TT>(\n method: string,\n params: Array<any>,\n cb?: ClientRequestCb<T, TT>,\n ): UnsubscribeFn => {\n if (!connection) throw new Error(\"Not connected\")\n const id = `${clientId}-${nextId++}`\n\n if (cb) responses.set(id, cb)\n send(id, method, params)\n\n return (): void => {\n responses.delete(id)\n }\n }\n\n return {\n request,\n disconnect,\n }\n}\n"],"names":["isResponse"],"mappings":";;;;;;;AAMO,MAAM,iBAAiB,KAAA,CAA2B;AAAA,EAGvD,YAAY,CAAA,EAAc;AACxB,IAAA,KAAA,CAAM,EAAE,OAAO,CAAA;AAHjB,IAAA,aAAA,CAAA,IAAA,EAAA,MAAA,CAAA;AACA,IAAA,aAAA,CAAA,IAAA,EAAA,MAAA,CAAA;AAGE,IAAA,IAAA,CAAK,OAAO,CAAA,CAAE,IAAA;AACd,IAAA,IAAA,CAAK,OAAO,CAAA,CAAE,IAAA;AACd,IAAA,IAAA,CAAK,IAAA,GAAO,UAAA;AAAA,EACd;AACF;;ACVO,MAAM,0BAA0B,MAAS;AAC9C,EAAA,MAAM,aAAA,uBAAoB,GAAA,EAA2B;AAErD,EAAA,OAAO;AAAA,IACL,GAAA,EAAK,aAAA,CAAc,GAAA,CAAI,IAAA,CAAK,aAAa,CAAA;AAAA,IACzC,SAAA,CAAU,IAAY,UAAA,EAA2B;AAC/C,MAAA,aAAA,CAAc,GAAA,CAAI,IAAI,UAAU,CAAA;AAAA,IAClC,CAAA;AAAA,IACA,YAAY,EAAA,EAAY;AACtB,MAAA,aAAA,CAAc,OAAO,EAAE,CAAA;AAAA,IACzB,CAAA;AAAA,IACA,IAAA,CAAK,IAAY,IAAA,EAAS;AACxB,MAAA,aAAA,CAAc,GAAA,CAAI,EAAE,CAAA,EAAG,IAAA,CAAK,IAAI,CAAA;AAAA,IAClC,CAAA;AAAA,IACA,KAAA,CAAM,IAAY,CAAA,EAAU;AAC1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B,IAAA,UAAA,EAAY,UAAA,EAAW;AACvB,IAAA,UAAA,GAAa,IAAA;AACb,IAAA,aAAA,CAAc,QAAA,CAAS,IAAI,cAAA,EAAgB,CAAA;AAC3C,IAAA,SAAA,CAAU,OAAA,CAAQ,CAAC,CAAA,KAAM,CAAA,CAAE,QAAQ,IAAI,cAAA,EAAgB,CAAC,CAAA;AACxD,IAAA,SAAA,CAAU,KAAA,EAAM;AAAA,EAClB,CAAA;AAEA,EAAA,IAAI,MAAA,GAAS,CAAA;AACb,EAAA,MAAM,OAAA,GAAU,CACd,MAAA,EACA,MAAA,EACA,EAAA,KACkB;AAClB,IAAA,IAAI,CAAC,UAAA,EAAY,MAAM,IAAI,MAAM,eAAe,CAAA;AAChD,IAAA,MAAM,EAAA,GAAK,CAAA,EAAG,QAAQ,CAAA,CAAA,EAAI,MAAA,EAAQ,CAAA,CAAA;AAElC,IAAA,IAAI,EAAA,EAAI,SAAA,CAAU,GAAA,CAAI,EAAA,EAAI,EAAE,CAAA;AAC5B,IAAA,IAAA,CAAK,EAAA,EAAI,QAAQ,MAAM,CAAA;AAEvB,IAAA,OAAO,MAAY;AACjB,MAAA,SAAA,CAAU,OAAO,EAAE,CAAA;AAAA,IACrB,CAAA;AAAA,EACF,CAAA;AAEA,EAAA,OAAO;AAAA,IACL,OAAA;AAAA,IACA;AAAA,GACF;AACF;;;;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@polkadot-api/raw-client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"author": "Josep M Sobrepere (https://github.com/josepot)",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -34,16 +34,15 @@
|
|
|
34
34
|
"files": [
|
|
35
35
|
"dist"
|
|
36
36
|
],
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@polkadot-api/json-rpc-provider": "0.1.0"
|
|
39
|
+
},
|
|
37
40
|
"scripts": {
|
|
38
41
|
"build-core": "tsc --noEmit && rollup -c ../../rollup.config.js",
|
|
39
42
|
"build": "pnpm build-core",
|
|
40
43
|
"test": "echo 'no tests'",
|
|
41
44
|
"coverage": "vitest run --coverage",
|
|
42
45
|
"lint": "prettier --check README.md \"src/**/*.{js,jsx,ts,tsx,json,md}\"",
|
|
43
|
-
"format": "prettier --write README.md \"src/**/*.{js,jsx,ts,tsx,json,md}\""
|
|
44
|
-
"prepack": "pnpm run build"
|
|
45
|
-
},
|
|
46
|
-
"dependencies": {
|
|
47
|
-
"@polkadot-api/json-rpc-provider": "workspace:*"
|
|
46
|
+
"format": "prettier --write README.md \"src/**/*.{js,jsx,ts,tsx,json,md}\""
|
|
48
47
|
}
|
|
49
|
-
}
|
|
48
|
+
}
|