@sprucelabs/mercury-client 42.0.556 → 42.0.558
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/build/esm/.spruce/errors/errors.types.d.ts +324 -0
- package/build/esm/.spruce/errors/errors.types.js +1 -0
- package/build/esm/.spruce/errors/mercuryClient/connectionFailed.schema.d.ts +3 -0
- package/build/esm/.spruce/errors/mercuryClient/connectionFailed.schema.js +25 -0
- package/build/esm/.spruce/errors/mercuryClient/invalidEventSignature.schema.d.ts +3 -0
- package/build/esm/.spruce/errors/mercuryClient/invalidEventSignature.schema.js +25 -0
- package/build/esm/.spruce/errors/mercuryClient/invalidPayload.schema.d.ts +3 -0
- package/build/esm/.spruce/errors/mercuryClient/invalidPayload.schema.js +19 -0
- package/build/esm/.spruce/errors/mercuryClient/invalidProtocol.schema.d.ts +3 -0
- package/build/esm/.spruce/errors/mercuryClient/invalidProtocol.schema.js +19 -0
- package/build/esm/.spruce/errors/mercuryClient/missingTestCacheDir.schema.d.ts +3 -0
- package/build/esm/.spruce/errors/mercuryClient/missingTestCacheDir.schema.js +12 -0
- package/build/esm/.spruce/errors/mercuryClient/mustCreateEvent.schema.d.ts +3 -0
- package/build/esm/.spruce/errors/mercuryClient/mustCreateEvent.schema.js +19 -0
- package/build/esm/.spruce/errors/mercuryClient/mustHandleLocally.schema.d.ts +3 -0
- package/build/esm/.spruce/errors/mercuryClient/mustHandleLocally.schema.js +19 -0
- package/build/esm/.spruce/errors/mercuryClient/notConnected.schema.d.ts +3 -0
- package/build/esm/.spruce/errors/mercuryClient/notConnected.schema.js +25 -0
- package/build/esm/.spruce/errors/mercuryClient/timeout.schema.d.ts +3 -0
- package/build/esm/.spruce/errors/mercuryClient/timeout.schema.js +35 -0
- package/build/esm/.spruce/errors/mercuryClient/unauthorizedAccess.schema.d.ts +3 -0
- package/build/esm/.spruce/errors/mercuryClient/unauthorizedAccess.schema.js +41 -0
- package/build/esm/.spruce/errors/mercuryClient/unauthorizedTarget.schema.d.ts +3 -0
- package/build/esm/.spruce/errors/mercuryClient/unauthorizedTarget.schema.js +38 -0
- package/build/esm/.spruce/errors/mercuryClient/unexpectedPayload.schema.d.ts +3 -0
- package/build/esm/.spruce/errors/mercuryClient/unexpectedPayload.schema.js +19 -0
- package/build/esm/.spruce/errors/mercuryClient/unknownError.schema.d.ts +3 -0
- package/build/esm/.spruce/errors/mercuryClient/unknownError.schema.js +12 -0
- package/build/esm/.spruce/errors/options.types.d.ts +40 -0
- package/build/esm/.spruce/errors/options.types.js +1 -0
- package/build/esm/.spruce/event-cache.json +1 -0
- package/build/esm/.spruce/schemas/fields/fieldClassMap.d.ts +2 -0
- package/build/esm/.spruce/schemas/fields/fieldClassMap.js +5 -0
- package/build/esm/.spruce/schemas/fields/fields.types.d.ts +1 -0
- package/build/esm/.spruce/schemas/fields/fields.types.js +3 -0
- package/build/esm/.spruce/schemas/schemas.types.d.ts +1 -0
- package/build/esm/.spruce/schemas/schemas.types.js +3 -0
- package/build/esm/.spruce/settings.json +26 -0
- package/build/esm/clients/MercuryClientFactory.d.ts +26 -0
- package/build/esm/clients/MercuryClientFactory.js +96 -0
- package/build/esm/clients/MercurySocketIoClient.d.ts +89 -0
- package/build/esm/clients/MercurySocketIoClient.js +605 -0
- package/build/esm/clients/MercuryTestClient.d.ts +57 -0
- package/build/esm/clients/MercuryTestClient.js +389 -0
- package/build/esm/clients/MutableContractClient.d.ts +10 -0
- package/build/esm/clients/MutableContractClient.js +43 -0
- package/build/esm/clients/statusChangePayloadSchema.d.ts +103 -0
- package/build/esm/clients/statusChangePayloadSchema.js +45 -0
- package/build/esm/constants.d.ts +1 -0
- package/build/esm/constants.js +1 -0
- package/build/esm/errors/SpruceError.d.ts +5 -0
- package/build/esm/errors/SpruceError.js +66 -0
- package/build/esm/errors/connectionFailed.builder.d.ts +16 -0
- package/build/esm/errors/connectionFailed.builder.js +16 -0
- package/build/esm/errors/invalidEventSignature.builder.d.ts +15 -0
- package/build/esm/errors/invalidEventSignature.builder.js +15 -0
- package/build/esm/errors/invalidPayload.builder.d.ts +12 -0
- package/build/esm/errors/invalidPayload.builder.js +12 -0
- package/build/esm/errors/invalidProtocol.builder.d.ts +12 -0
- package/build/esm/errors/invalidProtocol.builder.js +12 -0
- package/build/esm/errors/missingTestCacheDir.builder.d.ts +7 -0
- package/build/esm/errors/missingTestCacheDir.builder.js +7 -0
- package/build/esm/errors/mustCreateEvent.builder.d.ts +11 -0
- package/build/esm/errors/mustCreateEvent.builder.js +11 -0
- package/build/esm/errors/mustHandleLocally.builder.d.ts +11 -0
- package/build/esm/errors/mustHandleLocally.builder.js +11 -0
- package/build/esm/errors/notConnected.builder.d.ts +16 -0
- package/build/esm/errors/notConnected.builder.js +16 -0
- package/build/esm/errors/timeout.builder.d.ts +22 -0
- package/build/esm/errors/timeout.builder.js +22 -0
- package/build/esm/errors/unauthorizedAccess.builder.d.ts +57 -0
- package/build/esm/errors/unauthorizedAccess.builder.js +39 -0
- package/build/esm/errors/unexpectedPayload.builder.d.ts +12 -0
- package/build/esm/errors/unexpectedPayload.builder.js +12 -0
- package/build/esm/errors/unknownError.builder.d.ts +6 -0
- package/build/esm/errors/unknownError.builder.js +6 -0
- package/build/esm/index.d.ts +6 -0
- package/build/esm/index.js +6 -0
- package/build/esm/tests/AbstractClientTest.d.ts +31 -0
- package/build/esm/tests/AbstractClientTest.js +212 -0
- package/build/esm/tests/constants.d.ts +9 -0
- package/build/esm/tests/constants.js +11 -0
- package/build/esm/types/client.types.d.ts +30 -0
- package/build/esm/types/client.types.js +1 -0
- package/build/esm/utilities/socketIoEventUtil.utility.d.ts +5 -0
- package/build/esm/utilities/socketIoEventUtil.utility.js +9 -0
- package/package.json +12 -12
- package/build/__tests__/behavioral/AuthenticatingAndReauthenticating.test.d.ts +0 -21
- package/build/__tests__/behavioral/AuthenticatingAndReauthenticating.test.js +0 -287
- package/build/__tests__/behavioral/AuthenticatingAndReauthenticating.test.js.map +0 -1
- package/build/__tests__/behavioral/DisablingPermissionChecksWhenEmittingLocally.test.d.ts +0 -7
- package/build/__tests__/behavioral/DisablingPermissionChecksWhenEmittingLocally.test.js +0 -49
- package/build/__tests__/behavioral/DisablingPermissionChecksWhenEmittingLocally.test.js.map +0 -1
- package/build/__tests__/behavioral/EmittingAndFlatteningResponses.test.d.ts +0 -52
- package/build/__tests__/behavioral/EmittingAndFlatteningResponses.test.js +0 -123
- package/build/__tests__/behavioral/EmittingAndFlatteningResponses.test.js.map +0 -1
- package/build/__tests__/behavioral/EmittingConnectionChangeInTests.test.d.ts +0 -9
- package/build/__tests__/behavioral/EmittingConnectionChangeInTests.test.js +0 -103
- package/build/__tests__/behavioral/EmittingConnectionChangeInTests.test.js.map +0 -1
- package/build/__tests__/behavioral/LocallyHandlingAuthenticateDelaysConnect.test.d.ts +0 -7
- package/build/__tests__/behavioral/LocallyHandlingAuthenticateDelaysConnect.test.js +0 -91
- package/build/__tests__/behavioral/LocallyHandlingAuthenticateDelaysConnect.test.js.map +0 -1
- package/build/__tests__/behavioral/MercuryClientFactory.test.d.ts +0 -7
- package/build/__tests__/behavioral/MercuryClientFactory.test.js +0 -85
- package/build/__tests__/behavioral/MercuryClientFactory.test.js.map +0 -1
- package/build/__tests__/behavioral/ProxyingEvents.test.d.ts +0 -11
- package/build/__tests__/behavioral/ProxyingEvents.test.js +0 -99
- package/build/__tests__/behavioral/ProxyingEvents.test.js.map +0 -1
- package/build/__tests__/behavioral/ReconnectingAutomatically.test.d.ts +0 -33
- package/build/__tests__/behavioral/ReconnectingAutomatically.test.js +0 -390
- package/build/__tests__/behavioral/ReconnectingAutomatically.test.js.map +0 -1
- package/build/__tests__/behavioral/RequiringLocalListeners.test.d.ts +0 -7
- package/build/__tests__/behavioral/RequiringLocalListeners.test.js +0 -65
- package/build/__tests__/behavioral/RequiringLocalListeners.test.js.map +0 -1
- package/build/__tests__/behavioral/SettingNamespacesThatMustBeHandledLocally.test.d.ts +0 -9
- package/build/__tests__/behavioral/SettingNamespacesThatMustBeHandledLocally.test.js +0 -66
- package/build/__tests__/behavioral/SettingNamespacesThatMustBeHandledLocally.test.js.map +0 -1
- package/build/__tests__/behavioral/SimulatingEventsForTesting.test.d.ts +0 -51
- package/build/__tests__/behavioral/SimulatingEventsForTesting.test.js +0 -821
- package/build/__tests__/behavioral/SimulatingEventsForTesting.test.js.map +0 -1
- package/build/__tests__/behavioral/TestClientSpeedTests.test.d.ts +0 -7
- package/build/__tests__/behavioral/TestClientSpeedTests.test.js +0 -44
- package/build/__tests__/behavioral/TestClientSpeedTests.test.js.map +0 -1
- package/build/__tests__/behavioral/TestingWithDefaultContracts.test.d.ts +0 -7
- package/build/__tests__/behavioral/TestingWithDefaultContracts.test.js +0 -56
- package/build/__tests__/behavioral/TestingWithDefaultContracts.test.js.map +0 -1
- package/build/__tests__/behavioral/TurningOffAListener.test.d.ts +0 -16
- package/build/__tests__/behavioral/TurningOffAListener.test.js +0 -126
- package/build/__tests__/behavioral/TurningOffAListener.test.js.map +0 -1
- package/build/__tests__/behavioral/UsingMercuryClient.test.d.ts +0 -45
- package/build/__tests__/behavioral/UsingMercuryClient.test.js +0 -662
- package/build/__tests__/behavioral/UsingMercuryClient.test.js.map +0 -1
- /package/build/{.spruce → esm/.spruce}/errors/errors.types.js.map +0 -0
- /package/build/{.spruce → esm/.spruce}/errors/mercuryClient/connectionFailed.schema.js.map +0 -0
- /package/build/{.spruce → esm/.spruce}/errors/mercuryClient/invalidEventSignature.schema.js.map +0 -0
- /package/build/{.spruce → esm/.spruce}/errors/mercuryClient/invalidPayload.schema.js.map +0 -0
- /package/build/{.spruce → esm/.spruce}/errors/mercuryClient/invalidProtocol.schema.js.map +0 -0
- /package/build/{.spruce → esm/.spruce}/errors/mercuryClient/missingTestCacheDir.schema.js.map +0 -0
- /package/build/{.spruce → esm/.spruce}/errors/mercuryClient/mustCreateEvent.schema.js.map +0 -0
- /package/build/{.spruce → esm/.spruce}/errors/mercuryClient/mustHandleLocally.schema.js.map +0 -0
- /package/build/{.spruce → esm/.spruce}/errors/mercuryClient/notConnected.schema.js.map +0 -0
- /package/build/{.spruce → esm/.spruce}/errors/mercuryClient/timeout.schema.js.map +0 -0
- /package/build/{.spruce → esm/.spruce}/errors/mercuryClient/unauthorizedAccess.schema.js.map +0 -0
- /package/build/{.spruce → esm/.spruce}/errors/mercuryClient/unauthorizedTarget.schema.js.map +0 -0
- /package/build/{.spruce → esm/.spruce}/errors/mercuryClient/unexpectedPayload.schema.js.map +0 -0
- /package/build/{.spruce → esm/.spruce}/errors/mercuryClient/unknownError.schema.js.map +0 -0
- /package/build/{.spruce → esm/.spruce}/errors/options.types.js.map +0 -0
- /package/build/{.spruce → esm/.spruce}/schemas/fields/fieldClassMap.js.map +0 -0
- /package/build/{.spruce → esm/.spruce}/schemas/fields/fields.types.js.map +0 -0
- /package/build/{.spruce → esm/.spruce}/schemas/schemas.types.js.map +0 -0
- /package/build/{clients → esm/clients}/MercuryClientFactory.js.map +0 -0
- /package/build/{clients → esm/clients}/MercurySocketIoClient.js.map +0 -0
- /package/build/{clients → esm/clients}/MercuryTestClient.js.map +0 -0
- /package/build/{clients → esm/clients}/MutableContractClient.js.map +0 -0
- /package/build/{clients → esm/clients}/statusChangePayloadSchema.js.map +0 -0
- /package/build/{constants.js.map → esm/constants.js.map} +0 -0
- /package/build/{errors → esm/errors}/SpruceError.js.map +0 -0
- /package/build/{errors → esm/errors}/connectionFailed.builder.js.map +0 -0
- /package/build/{errors → esm/errors}/invalidEventSignature.builder.js.map +0 -0
- /package/build/{errors → esm/errors}/invalidPayload.builder.js.map +0 -0
- /package/build/{errors → esm/errors}/invalidProtocol.builder.js.map +0 -0
- /package/build/{errors → esm/errors}/missingTestCacheDir.builder.js.map +0 -0
- /package/build/{errors → esm/errors}/mustCreateEvent.builder.js.map +0 -0
- /package/build/{errors → esm/errors}/mustHandleLocally.builder.js.map +0 -0
- /package/build/{errors → esm/errors}/notConnected.builder.js.map +0 -0
- /package/build/{errors → esm/errors}/timeout.builder.js.map +0 -0
- /package/build/{errors → esm/errors}/unauthorizedAccess.builder.js.map +0 -0
- /package/build/{errors → esm/errors}/unexpectedPayload.builder.js.map +0 -0
- /package/build/{errors → esm/errors}/unknownError.builder.js.map +0 -0
- /package/build/{index.js.map → esm/index.js.map} +0 -0
- /package/build/{tests → esm/tests}/AbstractClientTest.js.map +0 -0
- /package/build/{tests → esm/tests}/constants.js.map +0 -0
- /package/build/{types → esm/types}/client.types.js.map +0 -0
- /package/build/{utilities → esm/utilities}/socketIoEventUtil.utility.js.map +0 -0
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { AbstractEventEmitter } from '@sprucelabs/mercury-event-emitter';
|
|
11
|
+
import { eventContractUtil, eventNameUtil, eventResponseUtil, } from '@sprucelabs/spruce-event-utils';
|
|
12
|
+
import clone from 'just-clone';
|
|
13
|
+
import SpruceError from '../errors/SpruceError.js';
|
|
14
|
+
import { authenticateFqen } from './MercurySocketIoClient.js';
|
|
15
|
+
import MutableContractClient from './MutableContractClient.js';
|
|
16
|
+
import { connectionStatusContract } from './statusChangePayloadSchema.js';
|
|
17
|
+
class InternalEmitter extends AbstractEventEmitter {
|
|
18
|
+
doesHandleEvent(eventName) {
|
|
19
|
+
try {
|
|
20
|
+
eventContractUtil.getSignatureByName(this.eventContract, eventName);
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
catch (_a) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
validateEmitPayload(schema, actualPayload, eventName) {
|
|
28
|
+
const payload = Object.assign({}, actualPayload);
|
|
29
|
+
delete payload.source;
|
|
30
|
+
return super.validateEmitPayload(schema, payload, eventName);
|
|
31
|
+
}
|
|
32
|
+
mixinOnlyUniqueSignatures(contract) {
|
|
33
|
+
const fqens = Object.keys(contract.eventSignatures);
|
|
34
|
+
for (const fqen of fqens) {
|
|
35
|
+
if (!this.eventContract.eventSignatures[fqen]) {
|
|
36
|
+
this.eventContract.eventSignatures[fqen] =
|
|
37
|
+
contract.eventSignatures[fqen];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
overrideSignatures(contract) {
|
|
42
|
+
const fqens = Object.keys(contract.eventSignatures);
|
|
43
|
+
for (const fqen of fqens) {
|
|
44
|
+
this.eventContract.eventSignatures[fqen] =
|
|
45
|
+
contract.eventSignatures[fqen];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
getContract() {
|
|
49
|
+
return this.eventContract;
|
|
50
|
+
}
|
|
51
|
+
setContract(contract) {
|
|
52
|
+
this.eventContract = contract;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
class MercuryTestClient extends MutableContractClient {
|
|
56
|
+
get eventContract() {
|
|
57
|
+
return MercuryTestClient.emitter.getContract();
|
|
58
|
+
}
|
|
59
|
+
set eventContract(contract) { }
|
|
60
|
+
static setShouldCheckPermissionsOnLocalEvents(should) {
|
|
61
|
+
this.shouldCheckPermissionsOnLocalEvents = should;
|
|
62
|
+
}
|
|
63
|
+
static setNamespacesThatMustBeHandledLocally(namespaces) {
|
|
64
|
+
this.namespacesThatHaveToBeHandledLocally = namespaces;
|
|
65
|
+
}
|
|
66
|
+
static getNamespacesThatMustBeHandledLocally() {
|
|
67
|
+
return this.namespacesThatHaveToBeHandledLocally;
|
|
68
|
+
}
|
|
69
|
+
constructor(options) {
|
|
70
|
+
const contract = options.eventContract;
|
|
71
|
+
super(Object.assign(Object.assign({}, options), { eventContract: contract }));
|
|
72
|
+
this._isConnected = false;
|
|
73
|
+
this.isConnectedToApi = false;
|
|
74
|
+
this.shouldHandleAuthenticateLocallyIfListenerSet = true;
|
|
75
|
+
this.shouldWaitForDelayedConnectIfAuthing = true;
|
|
76
|
+
if (!MercuryTestClient.emitter) {
|
|
77
|
+
MercuryTestClient.getInternalEmitter(contract);
|
|
78
|
+
}
|
|
79
|
+
else if (contract) {
|
|
80
|
+
MercuryTestClient.emitter.overrideSignatures(contract);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/** @ts-ignore */
|
|
84
|
+
static getInternalEmitter(contract) {
|
|
85
|
+
if (!MercuryTestClient.emitter) {
|
|
86
|
+
MercuryTestClient.emitter = new InternalEmitter({
|
|
87
|
+
eventSignatures: {},
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
const mixed = mixinConnectionEvents(contract);
|
|
91
|
+
MercuryTestClient.emitter.mixinOnlyUniqueSignatures(mixed);
|
|
92
|
+
/** @ts-ignore */
|
|
93
|
+
return MercuryTestClient.emitter;
|
|
94
|
+
}
|
|
95
|
+
off(eventName, cb) {
|
|
96
|
+
const _super = Object.create(null, {
|
|
97
|
+
off: { get: () => super.off }
|
|
98
|
+
});
|
|
99
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
100
|
+
var _a, _b;
|
|
101
|
+
yield ((_a = MercuryTestClient.emitter) === null || _a === void 0 ? void 0 : _a.off(eventName, cb));
|
|
102
|
+
if (((_b = MercuryTestClient.emitter) === null || _b === void 0 ? void 0 : _b.listenCount(eventName)) === 0) {
|
|
103
|
+
return _super.off.call(this, eventName);
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
return 1;
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
static mixinContract(contract) {
|
|
111
|
+
MutableContractClient.mixinContract(contract);
|
|
112
|
+
MercuryTestClient.emitter.mixinContract(contract);
|
|
113
|
+
}
|
|
114
|
+
mixinContract(contract) {
|
|
115
|
+
MutableContractClient.mixinContract(contract);
|
|
116
|
+
MercuryTestClient.emitter.mixinContract(contract);
|
|
117
|
+
}
|
|
118
|
+
doesHandleEvent(eventName) {
|
|
119
|
+
var _a;
|
|
120
|
+
return (super.doesHandleEvent(eventName) ||
|
|
121
|
+
((_a = MercuryTestClient.emitter) === null || _a === void 0 ? void 0 : _a.doesHandleEvent(eventName)));
|
|
122
|
+
}
|
|
123
|
+
on(...args) {
|
|
124
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
125
|
+
//@ts-ignore
|
|
126
|
+
return MercuryTestClient.emitter.on(...args);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
emit(...args) {
|
|
130
|
+
const _super = Object.create(null, {
|
|
131
|
+
emit: { get: () => super.emit }
|
|
132
|
+
});
|
|
133
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
134
|
+
var _a, _b, _c, _d, _e;
|
|
135
|
+
const fqen = args[0];
|
|
136
|
+
try {
|
|
137
|
+
if (this.shouldHandleEventLocally(fqen)) {
|
|
138
|
+
return this.handleEventLocally(args);
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
if (MercuryTestClient.shouldRequireLocalListeners &&
|
|
142
|
+
fqen !== 'connection-status-change') {
|
|
143
|
+
throw new SpruceError({
|
|
144
|
+
code: 'MUST_HANDLE_LOCALLY',
|
|
145
|
+
fqen,
|
|
146
|
+
friendlyMessage: `You need to listen to, fake a response to '${fqen}', or boot your skill. Try 'spruce create.listener', 'eventFaker.on('${fqen}')', or 'await this.bootSkill()'!`,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
yield this.connectIfNotConnected(fqen);
|
|
150
|
+
//@ts-ignore
|
|
151
|
+
const results = yield _super.emit.call(this, ...args);
|
|
152
|
+
const firstError = (_c = (_b = (_a = results.responses) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.errors) === null || _c === void 0 ? void 0 : _c[0];
|
|
153
|
+
if (firstError &&
|
|
154
|
+
((_d = firstError.options) === null || _d === void 0 ? void 0 : _d.code) === 'INVALID_EVENT_NAME') {
|
|
155
|
+
firstError.message = `Event not found! Make sure you are booting your skill in your test with \`await this.bootSkill()\`. If you haven't, you'll need to create a listener with \`spruce create.listener\`.\n\nOriginal Error:\n\n${firstError.options.friendlyMessage}`;
|
|
156
|
+
}
|
|
157
|
+
return results;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
catch (err) {
|
|
161
|
+
if (((_e = err.options) === null || _e === void 0 ? void 0 : _e.code) === 'INVALID_EVENT_NAME') {
|
|
162
|
+
err.message = `${err.message} Double check it's spelled correctly (types are passing) and that you've run \`spruce create.event\` to create the event.`;
|
|
163
|
+
}
|
|
164
|
+
throw err;
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
shouldHandleEventLocally(fqen) {
|
|
169
|
+
const emitter = MercuryTestClient.emitter;
|
|
170
|
+
if (!this.shouldHandleAuthenticateLocallyIfListenerSet &&
|
|
171
|
+
fqen === authenticateFqen) {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
if (fqen === 'connection-status-change') {
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
const { eventNamespace } = eventNameUtil.split(fqen);
|
|
178
|
+
if (eventNamespace &&
|
|
179
|
+
MercuryTestClient.namespacesThatHaveToBeHandledLocally.indexOf(eventNamespace) > -1) {
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
182
|
+
return emitter.listenCount(fqen) > 0;
|
|
183
|
+
}
|
|
184
|
+
handleEventLocally(args) {
|
|
185
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
186
|
+
var _a;
|
|
187
|
+
const emitter = MercuryTestClient.emitter;
|
|
188
|
+
const fqen = args[0];
|
|
189
|
+
const payload = args[1];
|
|
190
|
+
if (!MercuryTestClient.emitter.doesHandleEvent(fqen)) {
|
|
191
|
+
throw new SpruceError({
|
|
192
|
+
code: 'MUST_CREATE_EVENT',
|
|
193
|
+
fqen,
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
if (MercuryTestClient.shouldRequireLocalListeners !== false &&
|
|
197
|
+
MercuryTestClient.emitter.listenCount(fqen) === 0) {
|
|
198
|
+
if (fqen === 'connection-status-change') {
|
|
199
|
+
return {
|
|
200
|
+
responses: [],
|
|
201
|
+
totalContracts: 0,
|
|
202
|
+
totalErrors: 0,
|
|
203
|
+
totalResponses: 0,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
throw new SpruceError({
|
|
207
|
+
code: 'MUST_HANDLE_LOCALLY',
|
|
208
|
+
fqen,
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
this.assertValidEmitTargetAndPayload(fqen, payload);
|
|
212
|
+
let { argsWithSource } = this.buildSource(args);
|
|
213
|
+
const contract = emitter.getContract();
|
|
214
|
+
const sig = eventContractUtil.getSignatureByName(contract, fqen);
|
|
215
|
+
const { eventNamespace } = eventNameUtil.split(fqen);
|
|
216
|
+
if (eventNamespace) {
|
|
217
|
+
this.assertValidEventSignature(sig, fqen);
|
|
218
|
+
}
|
|
219
|
+
if (sig.emitPermissionContract && eventNamespace) {
|
|
220
|
+
const doesHonor = yield this.optionallyCheckPermissions(args, sig.emitPermissionContract.id, fqen);
|
|
221
|
+
if (typeof doesHonor !== 'boolean') {
|
|
222
|
+
return doesHonor;
|
|
223
|
+
}
|
|
224
|
+
if (!doesHonor) {
|
|
225
|
+
return {
|
|
226
|
+
totalContracts: 1,
|
|
227
|
+
totalErrors: 1,
|
|
228
|
+
totalResponses: 1,
|
|
229
|
+
responses: [
|
|
230
|
+
{
|
|
231
|
+
errors: [
|
|
232
|
+
new SpruceError({
|
|
233
|
+
code: 'UNAUTHORIZED_ACCESS',
|
|
234
|
+
fqen,
|
|
235
|
+
action: 'emit',
|
|
236
|
+
target: (_a = args[1]) !== null && _a !== void 0 ? _a : {},
|
|
237
|
+
permissionContractId: sig.emitPermissionContract.id,
|
|
238
|
+
}),
|
|
239
|
+
],
|
|
240
|
+
},
|
|
241
|
+
],
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
//@ts-ignore
|
|
246
|
+
const results = (yield emitter.emit(...argsWithSource));
|
|
247
|
+
return clone(results);
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
assertValidEventSignature(sig, fqen) {
|
|
251
|
+
var _a, _b;
|
|
252
|
+
if (!sig.isGlobal && !((_b = (_a = sig.emitPayloadSchema) === null || _a === void 0 ? void 0 : _a.fields) === null || _b === void 0 ? void 0 : _b.target)) {
|
|
253
|
+
throw new SpruceError({
|
|
254
|
+
code: 'INVALID_EVENT_SIGNATURE',
|
|
255
|
+
fqen,
|
|
256
|
+
instructions: 'Oh no! You have to either create an event using `spruce create.event`, set your event to global (event.options.ts, which requires special permissions) or add a target that includes an organizationId or locationId. Choose wisely!',
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
optionallyCheckPermissions(args, permissionContractId, fqen) {
|
|
261
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
262
|
+
var _a;
|
|
263
|
+
if (!MercuryTestClient.shouldCheckPermissionsOnLocalEvents) {
|
|
264
|
+
return true;
|
|
265
|
+
}
|
|
266
|
+
let { target } = (_a = args[1]) !== null && _a !== void 0 ? _a : {};
|
|
267
|
+
const permTarget = {};
|
|
268
|
+
if (target === null || target === void 0 ? void 0 : target.organizationId) {
|
|
269
|
+
permTarget.organizationId = target.organizationId;
|
|
270
|
+
}
|
|
271
|
+
if (target === null || target === void 0 ? void 0 : target.locationId) {
|
|
272
|
+
throw new Error('checking permissions against a location is not supported. Add to mercury-workspace -> mercury-client');
|
|
273
|
+
}
|
|
274
|
+
const { eventNamespace } = eventNameUtil.split(fqen);
|
|
275
|
+
const results = yield this.emit('does-honor-permission-contract::v2020_12_25', {
|
|
276
|
+
target: permTarget,
|
|
277
|
+
payload: {
|
|
278
|
+
id: `${eventNamespace}.${permissionContractId}`,
|
|
279
|
+
},
|
|
280
|
+
});
|
|
281
|
+
if (results.totalErrors > 0) {
|
|
282
|
+
return results;
|
|
283
|
+
}
|
|
284
|
+
const { doesHonor } = eventResponseUtil.getFirstResponseOrThrow(results);
|
|
285
|
+
return doesHonor;
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
buildSource(args) {
|
|
289
|
+
var _a, _b, _c;
|
|
290
|
+
let source = Object.assign({}, (_a = args[1]) === null || _a === void 0 ? void 0 : _a.source);
|
|
291
|
+
if ((_b = this.auth) === null || _b === void 0 ? void 0 : _b.person) {
|
|
292
|
+
source.personId = this.auth.person.id;
|
|
293
|
+
}
|
|
294
|
+
if ((_c = this.auth) === null || _c === void 0 ? void 0 : _c.skill) {
|
|
295
|
+
source.skillId = this.auth.skill.id;
|
|
296
|
+
}
|
|
297
|
+
if (args[0] !== 'authenticate::v2020_12_25' &&
|
|
298
|
+
!source.proxyToken &&
|
|
299
|
+
this.getProxyToken()) {
|
|
300
|
+
source.proxyToken = this.getProxyToken();
|
|
301
|
+
}
|
|
302
|
+
const argsWithSource = [...args];
|
|
303
|
+
if (typeof argsWithSource[1] !== 'function' &&
|
|
304
|
+
Object.keys(source).length > 0) {
|
|
305
|
+
argsWithSource[1] = Object.assign(Object.assign({}, argsWithSource[1]), { source });
|
|
306
|
+
}
|
|
307
|
+
return { source, argsWithSource: clone(argsWithSource) };
|
|
308
|
+
}
|
|
309
|
+
connectIfNotConnected(fqen) {
|
|
310
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
311
|
+
if (!this.isConnectedToApi) {
|
|
312
|
+
this.isConnectedToApi = true;
|
|
313
|
+
this.connectPromise = this.delayedConnectAndAuth(fqen);
|
|
314
|
+
}
|
|
315
|
+
if (!this.shouldWaitForDelayedConnectIfAuthing &&
|
|
316
|
+
fqen === authenticateFqen) {
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
yield this.connectPromise;
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
delayedConnectAndAuth(fqen) {
|
|
323
|
+
const _super = Object.create(null, {
|
|
324
|
+
connect: { get: () => super.connect }
|
|
325
|
+
});
|
|
326
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
327
|
+
yield _super.connect.call(this);
|
|
328
|
+
if (this.lastAuthOptions && fqen !== authenticateFqen) {
|
|
329
|
+
this.authPromise = undefined;
|
|
330
|
+
this.shouldHandleAuthenticateLocallyIfListenerSet = false;
|
|
331
|
+
this.shouldWaitForDelayedConnectIfAuthing = false;
|
|
332
|
+
yield this.authenticate(this.lastAuthOptions);
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
connect() {
|
|
337
|
+
const _super = Object.create(null, {
|
|
338
|
+
connect: { get: () => super.connect }
|
|
339
|
+
});
|
|
340
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
341
|
+
if (this._isConnected) {
|
|
342
|
+
yield _super.connect.call(this);
|
|
343
|
+
}
|
|
344
|
+
this._isConnected = true;
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
isConnected() {
|
|
348
|
+
return this._isConnected;
|
|
349
|
+
}
|
|
350
|
+
disconnect() {
|
|
351
|
+
const _super = Object.create(null, {
|
|
352
|
+
disconnect: { get: () => super.disconnect }
|
|
353
|
+
});
|
|
354
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
355
|
+
if (this.isConnectedToApi) {
|
|
356
|
+
yield _super.disconnect.call(this);
|
|
357
|
+
}
|
|
358
|
+
this._isConnected = false;
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
static reset() {
|
|
362
|
+
MutableContractClient.reset();
|
|
363
|
+
//@ts-ignore
|
|
364
|
+
MercuryTestClient.emitter = undefined;
|
|
365
|
+
//@ts-ignore
|
|
366
|
+
MercuryTestClient.emitter = MercuryTestClient.getInternalEmitter({
|
|
367
|
+
eventSignatures: {},
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
getIsTestClient() {
|
|
371
|
+
return true;
|
|
372
|
+
}
|
|
373
|
+
static setShouldRequireLocalListeners(shouldRequire) {
|
|
374
|
+
this.shouldRequireLocalListeners = shouldRequire;
|
|
375
|
+
}
|
|
376
|
+
static getShouldRequireLocalListeners() {
|
|
377
|
+
return this.shouldRequireLocalListeners;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
MercuryTestClient.shouldCheckPermissionsOnLocalEvents = false;
|
|
381
|
+
MercuryTestClient.namespacesThatHaveToBeHandledLocally = [];
|
|
382
|
+
MercuryTestClient.shouldRequireLocalListeners = true;
|
|
383
|
+
export default MercuryTestClient;
|
|
384
|
+
function mixinConnectionEvents(contract) {
|
|
385
|
+
return eventContractUtil.unifyContracts([
|
|
386
|
+
contract !== null && contract !== void 0 ? contract : { eventSignatures: {} },
|
|
387
|
+
connectionStatusContract,
|
|
388
|
+
]);
|
|
389
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { EventContract, EventSignature, EventNames } from '@sprucelabs/mercury-types';
|
|
2
|
+
import MercurySocketIoClient from './MercurySocketIoClient';
|
|
3
|
+
export default class MutableContractClient<Contract extends EventContract> extends MercurySocketIoClient<Contract> {
|
|
4
|
+
private static inMemoryContract?;
|
|
5
|
+
static mixinContract(contract: EventContract): void;
|
|
6
|
+
mixinContract(contract: EventContract): void;
|
|
7
|
+
static reset(): void;
|
|
8
|
+
doesHandleEvent(eventName: string): boolean;
|
|
9
|
+
protected getEventSignatureByName<EventName extends EventNames<Contract>>(eventName: EventName): EventSignature;
|
|
10
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { eventContractUtil } from '@sprucelabs/spruce-event-utils';
|
|
2
|
+
import MercurySocketIoClient from './MercurySocketIoClient.js';
|
|
3
|
+
export default class MutableContractClient extends MercurySocketIoClient {
|
|
4
|
+
static mixinContract(contract) {
|
|
5
|
+
var _a;
|
|
6
|
+
this.inMemoryContract = eventContractUtil.unifyContracts([
|
|
7
|
+
(_a = this.inMemoryContract) !== null && _a !== void 0 ? _a : { eventSignatures: {} },
|
|
8
|
+
contract,
|
|
9
|
+
]);
|
|
10
|
+
}
|
|
11
|
+
mixinContract(contract) {
|
|
12
|
+
MutableContractClient.mixinContract(contract);
|
|
13
|
+
}
|
|
14
|
+
static reset() {
|
|
15
|
+
this.inMemoryContract = {
|
|
16
|
+
eventSignatures: {},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
doesHandleEvent(eventName) {
|
|
20
|
+
try {
|
|
21
|
+
this.getEventSignatureByName(eventName);
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
catch (_a) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
getEventSignatureByName(eventName) {
|
|
29
|
+
var _a, _b;
|
|
30
|
+
try {
|
|
31
|
+
const sig = this.eventContract || MutableContractClient.inMemoryContract
|
|
32
|
+
? eventContractUtil.getSignatureByName((_a = this.eventContract) !== null && _a !== void 0 ? _a : { eventSignatures: {} }, eventName)
|
|
33
|
+
: {};
|
|
34
|
+
return sig;
|
|
35
|
+
}
|
|
36
|
+
catch (err) {
|
|
37
|
+
const inMemorySig = eventContractUtil.getSignatureByName((_b = MutableContractClient.inMemoryContract) !== null && _b !== void 0 ? _b : {
|
|
38
|
+
eventSignatures: {},
|
|
39
|
+
}, eventName);
|
|
40
|
+
return inMemorySig;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
declare const statusChangePayloadSchema: {
|
|
2
|
+
id: string;
|
|
3
|
+
fields: {
|
|
4
|
+
status: {
|
|
5
|
+
type: "select";
|
|
6
|
+
isRequired: true;
|
|
7
|
+
options: {
|
|
8
|
+
choices: ({
|
|
9
|
+
readonly label: "Connecting";
|
|
10
|
+
readonly value: "connecting";
|
|
11
|
+
} | {
|
|
12
|
+
readonly label: "Connected";
|
|
13
|
+
readonly value: "connected";
|
|
14
|
+
} | {
|
|
15
|
+
readonly label: "Disconnected";
|
|
16
|
+
readonly value: "disconnected";
|
|
17
|
+
})[];
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
declare const statusChangeTargetAndPayloadSchema: {
|
|
23
|
+
id: string;
|
|
24
|
+
fields: {
|
|
25
|
+
payload: {
|
|
26
|
+
type: "schema";
|
|
27
|
+
isRequired: true;
|
|
28
|
+
options: {
|
|
29
|
+
schema: {
|
|
30
|
+
id: string;
|
|
31
|
+
fields: {
|
|
32
|
+
status: {
|
|
33
|
+
type: "select";
|
|
34
|
+
isRequired: true;
|
|
35
|
+
options: {
|
|
36
|
+
choices: ({
|
|
37
|
+
readonly label: "Connecting";
|
|
38
|
+
readonly value: "connecting";
|
|
39
|
+
} | {
|
|
40
|
+
readonly label: "Connected";
|
|
41
|
+
readonly value: "connected";
|
|
42
|
+
} | {
|
|
43
|
+
readonly label: "Disconnected";
|
|
44
|
+
readonly value: "disconnected";
|
|
45
|
+
})[];
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
type StatusChangeTargetAndPayloadSchema = typeof statusChangeTargetAndPayloadSchema;
|
|
55
|
+
type StatusChangePayloadSchema = typeof statusChangePayloadSchema;
|
|
56
|
+
declare module '@sprucelabs/mercury-types/build/types/mercury.types' {
|
|
57
|
+
interface SkillEventSignatures {
|
|
58
|
+
'connection-status-change': {
|
|
59
|
+
emitPayloadSchema: StatusChangeTargetAndPayloadSchema;
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
export declare const connectionStatusContract: {
|
|
64
|
+
id: string;
|
|
65
|
+
eventSignatures: {
|
|
66
|
+
'connection-status-change': {
|
|
67
|
+
emitPayloadSchema: {
|
|
68
|
+
id: string;
|
|
69
|
+
fields: {
|
|
70
|
+
payload: {
|
|
71
|
+
type: "schema";
|
|
72
|
+
isRequired: true;
|
|
73
|
+
options: {
|
|
74
|
+
schema: {
|
|
75
|
+
id: string;
|
|
76
|
+
fields: {
|
|
77
|
+
status: {
|
|
78
|
+
type: "select";
|
|
79
|
+
isRequired: true;
|
|
80
|
+
options: {
|
|
81
|
+
choices: ({
|
|
82
|
+
readonly label: "Connecting";
|
|
83
|
+
readonly value: "connecting";
|
|
84
|
+
} | {
|
|
85
|
+
readonly label: "Connected";
|
|
86
|
+
readonly value: "connected";
|
|
87
|
+
} | {
|
|
88
|
+
readonly label: "Disconnected";
|
|
89
|
+
readonly value: "disconnected";
|
|
90
|
+
})[];
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
export type ConnectionStatus = StatusChangePayloadSchema['fields']['status']['options']['choices'][number]['value'];
|
|
103
|
+
export {};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { buildEventContract } from '@sprucelabs/mercury-types';
|
|
2
|
+
import { buildSchema } from '@sprucelabs/schema';
|
|
3
|
+
const statusChangePayloadSchema = buildSchema({
|
|
4
|
+
id: 'connectionStatusChangeEmitPayload',
|
|
5
|
+
fields: {
|
|
6
|
+
status: {
|
|
7
|
+
type: 'select',
|
|
8
|
+
isRequired: true,
|
|
9
|
+
options: {
|
|
10
|
+
choices: [
|
|
11
|
+
{
|
|
12
|
+
label: 'Connecting',
|
|
13
|
+
value: 'connecting',
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
label: 'Connected',
|
|
17
|
+
value: 'connected',
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
label: 'Disconnected',
|
|
21
|
+
value: 'disconnected',
|
|
22
|
+
},
|
|
23
|
+
],
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
const statusChangeTargetAndPayloadSchema = buildSchema({
|
|
29
|
+
id: 'connectionStatusChangeEmitTargetAndPayload',
|
|
30
|
+
fields: {
|
|
31
|
+
payload: {
|
|
32
|
+
type: 'schema',
|
|
33
|
+
isRequired: true,
|
|
34
|
+
options: { schema: statusChangePayloadSchema },
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
export const connectionStatusContract = buildEventContract({
|
|
39
|
+
id: 'connectionStatus',
|
|
40
|
+
eventSignatures: {
|
|
41
|
+
'connection-status-change': {
|
|
42
|
+
emitPayloadSchema: statusChangeTargetAndPayloadSchema,
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const DEFAULT_HOST = "https://mercury.spruce.ai";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const DEFAULT_HOST = 'https://mercury.spruce.ai';
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import AbstractSpruceError from '@sprucelabs/error';
|
|
2
|
+
export default class SpruceError extends AbstractSpruceError {
|
|
3
|
+
friendlyMessage() {
|
|
4
|
+
var _a;
|
|
5
|
+
const { options } = this;
|
|
6
|
+
let message;
|
|
7
|
+
switch (options === null || options === void 0 ? void 0 : options.code) {
|
|
8
|
+
case 'INVALID_PROTOCOL':
|
|
9
|
+
message = `Unable to connect to Mercury using the uri you provided. The uri you provide was "${options.uri}"!`;
|
|
10
|
+
break;
|
|
11
|
+
case 'UNEXPECTED_PAYLOAD':
|
|
12
|
+
message = `You passed a payload to "${options.eventName}" that has no emit payload defined.`;
|
|
13
|
+
break;
|
|
14
|
+
case 'INVALID_PAYLOAD':
|
|
15
|
+
message = `The emit payload you passed to "${options.eventName}" is invalid${options.originalError
|
|
16
|
+
? `:\n\n${options.originalError.message}`
|
|
17
|
+
: '.'}`;
|
|
18
|
+
break;
|
|
19
|
+
case 'CONNECTION_FAILED':
|
|
20
|
+
message = `I couldn't connect to ${options.host}. The code I got back was ${options.statusCode}.`;
|
|
21
|
+
break;
|
|
22
|
+
case 'NOT_CONNECTED':
|
|
23
|
+
message = `You cannot ${options.action} after you have manually disconnected from Mercury. Event was '${options.fqen}'.`;
|
|
24
|
+
break;
|
|
25
|
+
case 'TIMEOUT': {
|
|
26
|
+
const retries = options.totalRetries
|
|
27
|
+
? ` ${options.totalRetries} times `
|
|
28
|
+
: ' ';
|
|
29
|
+
const each = options.totalRetries ? ' each ' : ' ';
|
|
30
|
+
message = `Dang it, I didn't hear back after emitting "${options.eventName}"${retries}for ${options.timeoutMs / 1000} seconds${each}...`;
|
|
31
|
+
if (options.isConnected === false) {
|
|
32
|
+
message +=
|
|
33
|
+
"\n\nAlso, it appears I'm not connected to the api.";
|
|
34
|
+
}
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
case 'MISSING_TEST_CACHE_DIR':
|
|
38
|
+
message =
|
|
39
|
+
'You must set a test cache dir to test Mercury. Try MercuryFactory.setTestCacheDir().';
|
|
40
|
+
break;
|
|
41
|
+
case 'UNKNOWN_ERROR':
|
|
42
|
+
message = 'Oh no! An unknown error ocurred.';
|
|
43
|
+
if (options.originalError) {
|
|
44
|
+
message += ' Original error:\n\n';
|
|
45
|
+
message += options.originalError.stack;
|
|
46
|
+
}
|
|
47
|
+
return message;
|
|
48
|
+
case 'UNAUTHORIZED_ACCESS':
|
|
49
|
+
message = `Not authorized! You cannot ${options.action} \`${options.fqen}\`!`;
|
|
50
|
+
break;
|
|
51
|
+
case 'INVALID_EVENT_SIGNATURE':
|
|
52
|
+
message = `The signature for '${options.fqen}' is invalid:\n\n${options.instructions}`;
|
|
53
|
+
break;
|
|
54
|
+
case 'MUST_HANDLE_LOCALLY':
|
|
55
|
+
message = `'${options.fqen}' must be handled locally. You have 2 options:\n\n1. Make sure you have created a listener using 'spruce create.listener' and have booted your skill using 'await this.bootSkill()'\n\n2. Use eventFaker.on(...) to setup a listener in your test!`;
|
|
56
|
+
break;
|
|
57
|
+
case 'MUST_CREATE_EVENT':
|
|
58
|
+
message = `The event '${options.fqen}' was not found! That means it's time to create it with 'spruce create.event'.`;
|
|
59
|
+
break;
|
|
60
|
+
default:
|
|
61
|
+
message = super.friendlyMessage();
|
|
62
|
+
}
|
|
63
|
+
message = (_a = options.friendlyMessage) !== null && _a !== void 0 ? _a : message;
|
|
64
|
+
return message;
|
|
65
|
+
}
|
|
66
|
+
}
|