@novasamatech/host-api 0.9.4 → 0.10.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/dist/hostApi.js +87 -3
- package/dist/index.d.ts +5 -1
- package/dist/index.js +4 -1
- package/dist/protocol/callError.d.ts +63 -0
- package/dist/protocol/callError.js +57 -0
- package/dist/protocol/impl.d.ts +349 -54
- package/dist/protocol/impl.js +70 -10
- package/dist/protocol/messageCodec.d.ts +820 -109
- package/dist/protocol/v1/accounts.d.ts +10 -10
- package/dist/protocol/v1/accounts.js +12 -11
- package/dist/protocol/v1/chainInteraction.d.ts +44 -12
- package/dist/protocol/v1/chainInteraction.js +25 -15
- package/dist/protocol/v1/chat.d.ts +3 -3
- package/dist/protocol/v1/chat.js +5 -4
- package/dist/protocol/v1/coinPayment.d.ts +345 -0
- package/dist/protocol/v1/coinPayment.js +105 -0
- package/dist/protocol/v1/createTransaction.d.ts +9 -9
- package/dist/protocol/v1/createTransaction.js +4 -3
- package/dist/protocol/v1/deriveEntropy.d.ts +1 -1
- package/dist/protocol/v1/deriveEntropy.js +2 -2
- package/dist/protocol/v1/devicePermission.d.ts +1 -1
- package/dist/protocol/v1/devicePermission.js +3 -2
- package/dist/protocol/v1/feature.d.ts +1 -1
- package/dist/protocol/v1/feature.js +3 -2
- package/dist/protocol/v1/handshake.d.ts +1 -1
- package/dist/protocol/v1/handshake.js +3 -2
- package/dist/protocol/v1/localStorage.d.ts +10 -3
- package/dist/protocol/v1/localStorage.js +10 -4
- package/dist/protocol/v1/navigation.d.ts +1 -1
- package/dist/protocol/v1/navigation.js +3 -2
- package/dist/protocol/v1/notification.d.ts +2 -2
- package/dist/protocol/v1/notification.js +4 -3
- package/dist/protocol/v1/payments.d.ts +8 -8
- package/dist/protocol/v1/payments.js +4 -3
- package/dist/protocol/v1/preimage.d.ts +1 -1
- package/dist/protocol/v1/preimage.js +3 -2
- package/dist/protocol/v1/remotePermission.d.ts +1 -1
- package/dist/protocol/v1/remotePermission.js +3 -2
- package/dist/protocol/v1/resourceAllocation.d.ts +1 -1
- package/dist/protocol/v1/resourceAllocation.js +3 -2
- package/dist/protocol/v1/sign.d.ts +4 -4
- package/dist/protocol/v1/sign.js +6 -5
- package/dist/protocol/v1/statementStore.d.ts +3 -3
- package/dist/protocol/v1/statementStore.js +5 -4
- package/dist/protocol/v1/worker.d.ts +37 -0
- package/dist/protocol/v1/worker.js +20 -0
- package/dist/transport.js +16 -2
- package/dist/transport.spec.js +18 -0
- package/package.json +2 -2
package/dist/hostApi.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { errAsync, fromPromise, okAsync } from 'neverthrow';
|
|
2
2
|
import { extractErrorMessage } from './helpers.js';
|
|
3
|
+
import { CALL_ERROR_FAILURE } from './protocol/callError.js';
|
|
3
4
|
import { GenericError } from './protocol/commonCodecs.js';
|
|
4
5
|
import { CreateProofErr, GetAliasErr, GetUserIdErr, ListRingVrfKeysErr, LoginErr, RegisterRingVrfKeyErr, RequestCredentialsErr, RingVrfSignErr, SignVrfErr, } from './protocol/v1/accounts.js';
|
|
6
|
+
import { ChainInfoErr } from './protocol/v1/chainInteraction.js';
|
|
5
7
|
import { ChatBotRegistrationErr, ChatMessagePostingErr, ChatRoomRegistrationErr } from './protocol/v1/chat.js';
|
|
8
|
+
import { CoinPaymentErr } from './protocol/v1/coinPayment.js';
|
|
6
9
|
import { CreateTransactionErr } from './protocol/v1/createTransaction.js';
|
|
7
10
|
import { DeriveEntropyErr } from './protocol/v1/deriveEntropy.js';
|
|
8
11
|
import { HandshakeErr } from './protocol/v1/handshake.js';
|
|
@@ -14,6 +17,7 @@ import { PreimageSubmitErr } from './protocol/v1/preimage.js';
|
|
|
14
17
|
import { ResourceAllocationErr } from './protocol/v1/resourceAllocation.js';
|
|
15
18
|
import { SigningErr } from './protocol/v1/sign.js';
|
|
16
19
|
import { StatementProofErr } from './protocol/v1/statementStore.js';
|
|
20
|
+
import { WorkerErr } from './protocol/v1/worker.js';
|
|
17
21
|
export function createHostApi(transport) {
|
|
18
22
|
return {
|
|
19
23
|
handshake(payload) {
|
|
@@ -85,6 +89,21 @@ export function createHostApi(transport) {
|
|
|
85
89
|
value: new StorageErr.Unknown({ reason }),
|
|
86
90
|
}));
|
|
87
91
|
},
|
|
92
|
+
localStorageSubscribe(args, callback) {
|
|
93
|
+
return transport.subscribe('host_local_storage_subscribe', args, callback);
|
|
94
|
+
},
|
|
95
|
+
workerBeginOperation(payload) {
|
|
96
|
+
return makeRequest(transport.request('host_worker_begin_operation', payload), reason => ({
|
|
97
|
+
tag: payload.tag,
|
|
98
|
+
value: new WorkerErr.Unknown({ reason }),
|
|
99
|
+
}));
|
|
100
|
+
},
|
|
101
|
+
workerEndOperation(payload) {
|
|
102
|
+
return makeRequest(transport.request('host_worker_end_operation', payload), reason => ({
|
|
103
|
+
tag: payload.tag,
|
|
104
|
+
value: new WorkerErr.Unknown({ reason }),
|
|
105
|
+
}));
|
|
106
|
+
},
|
|
88
107
|
accountConnectionStatusSubscribe(args, callback) {
|
|
89
108
|
return transport.subscribe('host_account_connection_status_subscribe', args, callback);
|
|
90
109
|
},
|
|
@@ -265,6 +284,46 @@ export function createHostApi(transport) {
|
|
|
265
284
|
value: new ResourceAllocationErr.Unknown({ reason }),
|
|
266
285
|
}));
|
|
267
286
|
},
|
|
287
|
+
// coin payment (RFC 0017)
|
|
288
|
+
coinPaymentCreatePurse(payload) {
|
|
289
|
+
return makeRequest(transport.request('host_coin_payment_create_purse', payload), () => ({
|
|
290
|
+
tag: payload.tag,
|
|
291
|
+
value: new CoinPaymentErr.Internal(),
|
|
292
|
+
}));
|
|
293
|
+
},
|
|
294
|
+
coinPaymentQueryPurse(payload) {
|
|
295
|
+
return makeRequest(transport.request('host_coin_payment_query_purse', payload), () => ({
|
|
296
|
+
tag: payload.tag,
|
|
297
|
+
value: new CoinPaymentErr.Internal(),
|
|
298
|
+
}));
|
|
299
|
+
},
|
|
300
|
+
coinPaymentRebalancePurse(args, callback) {
|
|
301
|
+
return transport.subscribe('host_coin_payment_rebalance_purse', args, callback);
|
|
302
|
+
},
|
|
303
|
+
coinPaymentDeletePurse(args, callback) {
|
|
304
|
+
return transport.subscribe('host_coin_payment_delete_purse', args, callback);
|
|
305
|
+
},
|
|
306
|
+
coinPaymentCreateReceivable(payload) {
|
|
307
|
+
return makeRequest(transport.request('host_coin_payment_create_receivable', payload), () => ({
|
|
308
|
+
tag: payload.tag,
|
|
309
|
+
value: new CoinPaymentErr.Internal(),
|
|
310
|
+
}));
|
|
311
|
+
},
|
|
312
|
+
coinPaymentCreateCheque(payload) {
|
|
313
|
+
return makeRequest(transport.request('host_coin_payment_create_cheque', payload), () => ({
|
|
314
|
+
tag: payload.tag,
|
|
315
|
+
value: new CoinPaymentErr.Internal(),
|
|
316
|
+
}));
|
|
317
|
+
},
|
|
318
|
+
coinPaymentDeposit(args, callback) {
|
|
319
|
+
return transport.subscribe('host_coin_payment_deposit', args, callback);
|
|
320
|
+
},
|
|
321
|
+
coinPaymentRefund(args, callback) {
|
|
322
|
+
return transport.subscribe('host_coin_payment_refund', args, callback);
|
|
323
|
+
},
|
|
324
|
+
coinPaymentListenForPayment(args, callback) {
|
|
325
|
+
return transport.subscribe('host_coin_payment_listen_for_payment', args, callback);
|
|
326
|
+
},
|
|
268
327
|
// chain interaction
|
|
269
328
|
chainHeadFollowSubscribe(args, callback) {
|
|
270
329
|
return transport.subscribe('remote_chain_head_follow_subscribe', args, callback);
|
|
@@ -311,6 +370,12 @@ export function createHostApi(transport) {
|
|
|
311
370
|
value: new GenericError({ reason }),
|
|
312
371
|
}));
|
|
313
372
|
},
|
|
373
|
+
chainGetChainInfo(payload) {
|
|
374
|
+
return makeRequest(transport.request('remote_chain_get_chain_info', payload), reason => ({
|
|
375
|
+
tag: payload.tag,
|
|
376
|
+
value: new ChainInfoErr.Unknown({ reason }),
|
|
377
|
+
}));
|
|
378
|
+
},
|
|
314
379
|
chainSpecGenesisHash(payload) {
|
|
315
380
|
return makeRequest(transport.request('remote_chain_spec_genesis_hash', payload), reason => ({
|
|
316
381
|
tag: payload.tag,
|
|
@@ -343,10 +408,29 @@ export function createHostApi(transport) {
|
|
|
343
408
|
},
|
|
344
409
|
};
|
|
345
410
|
}
|
|
411
|
+
/** Human-readable reason for a transport-level `CallError`, for the domain fallback. */
|
|
412
|
+
function describeCallErrorFailure(failure) {
|
|
413
|
+
switch (failure.tag) {
|
|
414
|
+
case 'Denied':
|
|
415
|
+
return 'call denied by host';
|
|
416
|
+
case 'Unsupported':
|
|
417
|
+
return 'method unsupported by host';
|
|
418
|
+
case 'MalformedFrame':
|
|
419
|
+
return `malformed frame: ${failure.value.reason}`;
|
|
420
|
+
case 'HostFailure':
|
|
421
|
+
return `host failure: ${failure.value.reason}`;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
346
424
|
function makeRequest(promise, mapErr) {
|
|
347
425
|
return fromPromise(promise, e => mapErr(extractErrorMessage(e))).andThen(r => {
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
426
|
+
const value = r.value;
|
|
427
|
+
// A transport-level CallError carries no domain answer; fold it into the
|
|
428
|
+
// method's domain error so products keep a single error type.
|
|
429
|
+
if (CALL_ERROR_FAILURE in value) {
|
|
430
|
+
return errAsync(mapErr(describeCallErrorFailure(value[CALL_ERROR_FAILURE])));
|
|
431
|
+
}
|
|
432
|
+
if (value.success)
|
|
433
|
+
return okAsync({ tag: r.tag, value: value.value });
|
|
434
|
+
return errAsync({ tag: r.tag, value: value.value });
|
|
351
435
|
});
|
|
352
436
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,8 @@ export { createTransport } from './transport.js';
|
|
|
8
8
|
export { createDefaultLogger } from './logger.js';
|
|
9
9
|
export type { HostApiProtocol, VersionedProtocolRequest, VersionedProtocolSubscription } from './protocol/impl.js';
|
|
10
10
|
export { hostApiProtocol } from './protocol/impl.js';
|
|
11
|
+
export { CALL_ERROR_FAILURE, isCallErrorFailure } from './protocol/callError.js';
|
|
12
|
+
export type { CallErrorTransportFailure } from './protocol/callError.js';
|
|
11
13
|
export type { Codec, CodecType } from 'scale-ts';
|
|
12
14
|
export type { HexString } from '@novasamatech/scale';
|
|
13
15
|
export { assertEnumVariant, enumValue, fromHex, isEnumVariant, resultErr, resultOk, toHex, unwrapResultOrThrow, } from '@novasamatech/scale';
|
|
@@ -22,6 +24,7 @@ export { HandshakeErr } from './protocol/v1/handshake.js';
|
|
|
22
24
|
export { SigningErr, SigningPayload, SigningPayloadWithoutAccount, SigningRawPayload, SigningRawPayloadWithoutAccount, SigningResult, } from './protocol/v1/sign.js';
|
|
23
25
|
export { SignedStatement, SignedStatementsPage, Statement, StatementProofErr, Topic, TopicFilter, } from './protocol/v1/statementStore.js';
|
|
24
26
|
export { StorageErr } from './protocol/v1/localStorage.js';
|
|
27
|
+
export { OperationId, WorkerErr } from './protocol/v1/worker.js';
|
|
25
28
|
export { DevicePermission } from './protocol/v1/devicePermission.js';
|
|
26
29
|
export { RemotePermission } from './protocol/v1/remotePermission.js';
|
|
27
30
|
export { NotificationId, PushNotification, PushNotificationError } from './protocol/v1/notification.js';
|
|
@@ -30,5 +33,6 @@ export { PreimageKey, PreimageSubmitErr, PreimageValue } from './protocol/v1/pre
|
|
|
30
33
|
export { AllocatableResource, AllocationOutcome, ResourceAllocationErr } from './protocol/v1/resourceAllocation.js';
|
|
31
34
|
export { PaymentBalance, PaymentBalanceErr, PaymentId, PaymentReceipt, PaymentRequestErr, PaymentStatus, PaymentStatusErr, PaymentTopUpErr, PaymentTopUpSource, } from './protocol/v1/payments.js';
|
|
32
35
|
export { Arrangement, BorderStyle, ButtonVariant, ColorToken, ContentAlignment, CustomRendererNode, Dimensions, HorizontalAlignment, Modifier, Shape, Size, TypographyStyle, VerticalAlignment, } from './protocol/v1/customRenderer.js';
|
|
33
|
-
export { ChainHeadEvent, ChainHeadFollowV1_start, OperationStartedResult, RuntimeType, StorageQueryItem, StorageQueryType, StorageResultItem, TransactionBroadcastV1_request, TransactionBroadcastV1_response, TransactionStopV1_request, TransactionStopV1_response, } from './protocol/v1/chainInteraction.js';
|
|
36
|
+
export { ChainHeadEvent, ChainHeadFollowV1_start, ChainIdentifier, ChainInfoErr, OperationStartedResult, RuntimeType, StorageQueryItem, StorageQueryType, StorageResultItem, TransactionBroadcastV1_request, TransactionBroadcastV1_response, TransactionStopV1_request, TransactionStopV1_response, } from './protocol/v1/chainInteraction.js';
|
|
37
|
+
export { CoinPaymentBalance, CoinPaymentCheque, CoinPaymentErr, CoinPaymentListenForItem, CoinPaymentPurseId, CoinPaymentPurseInfo, CoinPaymentReceivable, CoinPaymentStatus, CoinPaymentTransmissionChannel, } from './protocol/v1/coinPayment.js';
|
|
34
38
|
export { Theme, ThemeName, ThemeVariant } from './protocol/v1/theme.js';
|
package/dist/index.js
CHANGED
|
@@ -3,6 +3,7 @@ export { createHostApi } from './hostApi.js';
|
|
|
3
3
|
export { createTransport } from './transport.js';
|
|
4
4
|
export { createDefaultLogger } from './logger.js';
|
|
5
5
|
export { hostApiProtocol } from './protocol/impl.js';
|
|
6
|
+
export { CALL_ERROR_FAILURE, isCallErrorFailure } from './protocol/callError.js';
|
|
6
7
|
export { assertEnumVariant, enumValue, fromHex, isEnumVariant, resultErr, resultOk, toHex, unwrapResultOrThrow, } from '@novasamatech/scale';
|
|
7
8
|
// Codecs
|
|
8
9
|
export { GenericError } from './protocol/commonCodecs.js';
|
|
@@ -15,6 +16,7 @@ export { HandshakeErr } from './protocol/v1/handshake.js';
|
|
|
15
16
|
export { SigningErr, SigningPayload, SigningPayloadWithoutAccount, SigningRawPayload, SigningRawPayloadWithoutAccount, SigningResult, } from './protocol/v1/sign.js';
|
|
16
17
|
export { SignedStatement, SignedStatementsPage, Statement, StatementProofErr, Topic, TopicFilter, } from './protocol/v1/statementStore.js';
|
|
17
18
|
export { StorageErr } from './protocol/v1/localStorage.js';
|
|
19
|
+
export { OperationId, WorkerErr } from './protocol/v1/worker.js';
|
|
18
20
|
export { DevicePermission } from './protocol/v1/devicePermission.js';
|
|
19
21
|
export { RemotePermission } from './protocol/v1/remotePermission.js';
|
|
20
22
|
export { NotificationId, PushNotification, PushNotificationError } from './protocol/v1/notification.js';
|
|
@@ -23,5 +25,6 @@ export { PreimageKey, PreimageSubmitErr, PreimageValue } from './protocol/v1/pre
|
|
|
23
25
|
export { AllocatableResource, AllocationOutcome, ResourceAllocationErr } from './protocol/v1/resourceAllocation.js';
|
|
24
26
|
export { PaymentBalance, PaymentBalanceErr, PaymentId, PaymentReceipt, PaymentRequestErr, PaymentStatus, PaymentStatusErr, PaymentTopUpErr, PaymentTopUpSource, } from './protocol/v1/payments.js';
|
|
25
27
|
export { Arrangement, BorderStyle, ButtonVariant, ColorToken, ContentAlignment, CustomRendererNode, Dimensions, HorizontalAlignment, Modifier, Shape, Size, TypographyStyle, VerticalAlignment, } from './protocol/v1/customRenderer.js';
|
|
26
|
-
export { ChainHeadEvent, ChainHeadFollowV1_start, OperationStartedResult, RuntimeType, StorageQueryItem, StorageQueryType, StorageResultItem, TransactionBroadcastV1_request, TransactionBroadcastV1_response, TransactionStopV1_request, TransactionStopV1_response, } from './protocol/v1/chainInteraction.js';
|
|
28
|
+
export { ChainHeadEvent, ChainHeadFollowV1_start, ChainIdentifier, ChainInfoErr, OperationStartedResult, RuntimeType, StorageQueryItem, StorageQueryType, StorageResultItem, TransactionBroadcastV1_request, TransactionBroadcastV1_response, TransactionStopV1_request, TransactionStopV1_response, } from './protocol/v1/chainInteraction.js';
|
|
29
|
+
export { CoinPaymentBalance, CoinPaymentCheque, CoinPaymentErr, CoinPaymentListenForItem, CoinPaymentPurseId, CoinPaymentPurseInfo, CoinPaymentReceivable, CoinPaymentStatus, CoinPaymentTransmissionChannel, } from './protocol/v1/coinPayment.js';
|
|
27
30
|
export { Theme, ThemeName, ThemeVariant } from './protocol/v1/theme.js';
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { Codec, ResultPayload } from 'scale-ts';
|
|
2
|
+
/** The transport-level failure variants (everything except `Domain`). */
|
|
3
|
+
export type CallErrorTransportFailure = {
|
|
4
|
+
tag: 'Denied';
|
|
5
|
+
} | {
|
|
6
|
+
tag: 'Unsupported';
|
|
7
|
+
} | {
|
|
8
|
+
tag: 'MalformedFrame';
|
|
9
|
+
value: {
|
|
10
|
+
reason: string;
|
|
11
|
+
};
|
|
12
|
+
} | {
|
|
13
|
+
tag: 'HostFailure';
|
|
14
|
+
value: {
|
|
15
|
+
reason: string;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
/** SCALE codec for `CallError<D>`, discriminants pinned to the truapi order. */
|
|
19
|
+
export declare const CallError: <D>(domain: Codec<D>) => Codec<{
|
|
20
|
+
tag: "Denied";
|
|
21
|
+
value: undefined;
|
|
22
|
+
} | {
|
|
23
|
+
tag: "Unsupported";
|
|
24
|
+
value: undefined;
|
|
25
|
+
} | {
|
|
26
|
+
tag: "MalformedFrame";
|
|
27
|
+
value: {
|
|
28
|
+
reason: string;
|
|
29
|
+
};
|
|
30
|
+
} | {
|
|
31
|
+
tag: "HostFailure";
|
|
32
|
+
value: {
|
|
33
|
+
reason: string;
|
|
34
|
+
};
|
|
35
|
+
} | {
|
|
36
|
+
tag: "Domain";
|
|
37
|
+
value: D;
|
|
38
|
+
}>;
|
|
39
|
+
/** A decoded transport failure carries this brand so the transport can spot it. */
|
|
40
|
+
export declare const CALL_ERROR_FAILURE: unique symbol;
|
|
41
|
+
/** Business (present) shape of a response: a plain scale-ts `Result`. */
|
|
42
|
+
type Business<OK, ERR> = ResultPayload<OK, ERR>;
|
|
43
|
+
/** True when a decoded response is a transport-level `CallError`, not a domain answer. */
|
|
44
|
+
export declare const isCallErrorFailure: <OK, ERR>(value: CallResultValue<OK, ERR>) => value is {
|
|
45
|
+
[CALL_ERROR_FAILURE]: CallErrorTransportFailure;
|
|
46
|
+
};
|
|
47
|
+
export type CallResultValue<OK, ERR> = Business<OK, ERR> | {
|
|
48
|
+
[CALL_ERROR_FAILURE]: CallErrorTransportFailure;
|
|
49
|
+
};
|
|
50
|
+
export declare const CallResult: <OK, ERR>(ok: Codec<OK>, domainErr: Codec<ERR>) => Codec<CallResultValue<OK, ERR>>;
|
|
51
|
+
/** A decoded subscription interrupt: the domain reason, or a transport failure. */
|
|
52
|
+
export type CallInterruptValue<ERR> = ERR | {
|
|
53
|
+
[CALL_ERROR_FAILURE]: CallErrorTransportFailure;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Interrupt codec counterpart to `Result`. A subscription's interrupt reason is
|
|
57
|
+
* `CallError(V1(err))` on the wire (byte-identical to truapi); its value form is
|
|
58
|
+
* the plain domain reason for the `Domain` case, and a `CALL_ERROR_FAILURE`
|
|
59
|
+
* marker for a transport-level interrupt. The transport assembly applies this to
|
|
60
|
+
* typed interrupts, so interrupt codec definitions stay the plain domain error.
|
|
61
|
+
*/
|
|
62
|
+
export declare const interruptError: <ERR>(domainErr: Codec<ERR>) => Codec<CallInterruptValue<ERR>>;
|
|
63
|
+
export {};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { Enum } from '@novasamatech/scale';
|
|
2
|
+
import { Result as ScaleResult, Struct, _void, enhanceCodec, str } from 'scale-ts';
|
|
3
|
+
/** Wraps a domain error codec into a `V1`-versioned enum, as truapi does. */
|
|
4
|
+
const versioned = (domain) => Enum({ v1: domain }, [0]);
|
|
5
|
+
/** SCALE codec for `CallError<D>`, discriminants pinned to the truapi order. */
|
|
6
|
+
export const CallError = (domain) => Enum({
|
|
7
|
+
Domain: domain,
|
|
8
|
+
Denied: _void,
|
|
9
|
+
Unsupported: _void,
|
|
10
|
+
MalformedFrame: Struct({ reason: str }),
|
|
11
|
+
HostFailure: Struct({ reason: str }),
|
|
12
|
+
}, [0, 1, 2, 3, 4]);
|
|
13
|
+
/** A decoded transport failure carries this brand so the transport can spot it. */
|
|
14
|
+
export const CALL_ERROR_FAILURE = Symbol('callErrorFailure');
|
|
15
|
+
/** True when a decoded response is a transport-level `CallError`, not a domain answer. */
|
|
16
|
+
export const isCallErrorFailure = (value) => CALL_ERROR_FAILURE in value;
|
|
17
|
+
export const CallResult = (ok, domainErr) => {
|
|
18
|
+
const wire = ScaleResult(ok, CallError(versioned(domainErr)));
|
|
19
|
+
return enhanceCodec(wire, value => {
|
|
20
|
+
if (CALL_ERROR_FAILURE in value) {
|
|
21
|
+
// Re-encoding a decoded transport failure (rare): pass it straight back.
|
|
22
|
+
return { success: false, value: value[CALL_ERROR_FAILURE] };
|
|
23
|
+
}
|
|
24
|
+
if (value.success)
|
|
25
|
+
return { success: true, value: value.value };
|
|
26
|
+
return { success: false, value: { tag: 'Domain', value: { tag: 'v1', value: value.value } } };
|
|
27
|
+
}, decoded => {
|
|
28
|
+
if (decoded.success)
|
|
29
|
+
return { success: true, value: decoded.value };
|
|
30
|
+
const callError = decoded.value;
|
|
31
|
+
if (callError.tag === 'Domain') {
|
|
32
|
+
return { success: false, value: callError.value.value };
|
|
33
|
+
}
|
|
34
|
+
return { [CALL_ERROR_FAILURE]: callError };
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Interrupt codec counterpart to `Result`. A subscription's interrupt reason is
|
|
39
|
+
* `CallError(V1(err))` on the wire (byte-identical to truapi); its value form is
|
|
40
|
+
* the plain domain reason for the `Domain` case, and a `CALL_ERROR_FAILURE`
|
|
41
|
+
* marker for a transport-level interrupt. The transport assembly applies this to
|
|
42
|
+
* typed interrupts, so interrupt codec definitions stay the plain domain error.
|
|
43
|
+
*/
|
|
44
|
+
export const interruptError = (domainErr) => {
|
|
45
|
+
const wire = CallError(versioned(domainErr));
|
|
46
|
+
return enhanceCodec(wire, value => {
|
|
47
|
+
if (typeof value === 'object' && value !== null && CALL_ERROR_FAILURE in value) {
|
|
48
|
+
const failure = value[CALL_ERROR_FAILURE];
|
|
49
|
+
return { tag: failure.tag, value: 'value' in failure ? failure.value : undefined };
|
|
50
|
+
}
|
|
51
|
+
return { tag: 'Domain', value: { tag: 'v1', value } };
|
|
52
|
+
}, decoded => {
|
|
53
|
+
if (decoded.tag === 'Domain')
|
|
54
|
+
return decoded.value.value;
|
|
55
|
+
return { [CALL_ERROR_FAILURE]: decoded };
|
|
56
|
+
});
|
|
57
|
+
};
|