@slates/client 1.0.0-rc.2
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/index.cjs +409 -0
- package/dist/index.d.cts +764 -0
- package/dist/index.d.ts +764 -0
- package/dist/index.module.js +383 -0
- package/package.json +43 -0
- package/src/client.test.ts +569 -0
- package/src/client.ts +367 -0
- package/src/error.ts +157 -0
- package/src/index.ts +10 -0
- package/src/transport.ts +57 -0
- package/src/types.ts +36 -0
package/src/client.ts
ADDED
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SLATES_PROTOCOL_VERSION,
|
|
3
|
+
SlateAuthenticationMethod,
|
|
4
|
+
SlatesAction,
|
|
5
|
+
SlatesParticipant,
|
|
6
|
+
slatesRequestsByMethod,
|
|
7
|
+
slatesResponsesByMethod
|
|
8
|
+
} from '@slates/proto';
|
|
9
|
+
import { randomUUID } from 'crypto';
|
|
10
|
+
import z from 'zod';
|
|
11
|
+
import { SlateProtocolError } from './error';
|
|
12
|
+
import { SlatesClientState, SlatesProtocolClientOptions } from './types';
|
|
13
|
+
|
|
14
|
+
let createDefaultParticipants = (): SlatesParticipant[] => [
|
|
15
|
+
{
|
|
16
|
+
type: 'consumer',
|
|
17
|
+
id: 'slates-client',
|
|
18
|
+
name: 'Slates Client'
|
|
19
|
+
}
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
export class SlatesProtocolClient {
|
|
23
|
+
readonly transport: SlatesProtocolClientOptions['transport'];
|
|
24
|
+
state: SlatesClientState;
|
|
25
|
+
|
|
26
|
+
constructor(opts: SlatesProtocolClientOptions) {
|
|
27
|
+
this.transport = opts.transport;
|
|
28
|
+
this.state = {
|
|
29
|
+
protocol: SLATES_PROTOCOL_VERSION,
|
|
30
|
+
participants: opts.participants ?? createDefaultParticipants(),
|
|
31
|
+
config: opts.state?.config ?? null,
|
|
32
|
+
auth: opts.state?.auth ?? null,
|
|
33
|
+
session: opts.state?.session ?? null
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
setParticipants(participants: SlatesParticipant[]) {
|
|
38
|
+
this.state.participants = participants;
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
setConfig(config: Record<string, any> | null) {
|
|
43
|
+
this.state.config = config;
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
setAuth(auth: SlatesClientState['auth']) {
|
|
48
|
+
this.state.auth = auth;
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
clearAuth() {
|
|
53
|
+
this.state.auth = null;
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
setSession(session: SlatesClientState['session']) {
|
|
58
|
+
this.state.session = session;
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
ensureSession() {
|
|
63
|
+
if (!this.state.session) {
|
|
64
|
+
this.state.session = {
|
|
65
|
+
id: randomUUID(),
|
|
66
|
+
state: {}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return this.state.session;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
private buildStateMessages() {
|
|
74
|
+
return [
|
|
75
|
+
{
|
|
76
|
+
jsonrpc: '2.0' as const,
|
|
77
|
+
method: 'slates/hello' as const,
|
|
78
|
+
params: { protocol: this.state.protocol }
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
jsonrpc: '2.0' as const,
|
|
82
|
+
method: 'slates/participant.set' as const,
|
|
83
|
+
params: { participants: this.state.participants }
|
|
84
|
+
},
|
|
85
|
+
...(this.state.config
|
|
86
|
+
? [
|
|
87
|
+
{
|
|
88
|
+
jsonrpc: '2.0' as const,
|
|
89
|
+
method: 'slates/config.set' as const,
|
|
90
|
+
params: { config: this.state.config }
|
|
91
|
+
}
|
|
92
|
+
]
|
|
93
|
+
: []),
|
|
94
|
+
...(this.state.auth
|
|
95
|
+
? [
|
|
96
|
+
{
|
|
97
|
+
jsonrpc: '2.0' as const,
|
|
98
|
+
method: 'slates/auth.set' as const,
|
|
99
|
+
params: {
|
|
100
|
+
authenticationMethodId: this.state.auth.authenticationMethodId,
|
|
101
|
+
output: this.state.auth.output
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
]
|
|
105
|
+
: []),
|
|
106
|
+
...(this.state.session
|
|
107
|
+
? [
|
|
108
|
+
{
|
|
109
|
+
jsonrpc: '2.0' as const,
|
|
110
|
+
method: 'slates/session.start' as const,
|
|
111
|
+
params: {
|
|
112
|
+
sessionId: this.state.session.id,
|
|
113
|
+
state: this.state.session.state
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
]
|
|
117
|
+
: [])
|
|
118
|
+
];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async request<Key extends keyof typeof slatesRequestsByMethod>(
|
|
122
|
+
method: Key,
|
|
123
|
+
params: z.infer<(typeof slatesRequestsByMethod)[Key]>['params']
|
|
124
|
+
): Promise<z.infer<(typeof slatesResponsesByMethod)[Key]>['result']> {
|
|
125
|
+
let id = randomUUID();
|
|
126
|
+
let responses = await this.transport.send([
|
|
127
|
+
...this.buildStateMessages(),
|
|
128
|
+
{
|
|
129
|
+
jsonrpc: '2.0',
|
|
130
|
+
id,
|
|
131
|
+
method,
|
|
132
|
+
params
|
|
133
|
+
} as z.infer<(typeof slatesRequestsByMethod)[Key]>
|
|
134
|
+
]);
|
|
135
|
+
|
|
136
|
+
let response = responses.find(message => 'id' in message && message.id === id) as
|
|
137
|
+
| { result?: any; error?: any }
|
|
138
|
+
| undefined;
|
|
139
|
+
|
|
140
|
+
if (!response) {
|
|
141
|
+
throw new Error(`No response was returned for method ${String(method)}.`);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (response.error) {
|
|
145
|
+
throw SlateProtocolError.fromResponse(response.error);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return response.result;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async identify() {
|
|
152
|
+
return this.request('slates/provider.identify', {});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async listActions() {
|
|
156
|
+
return this.request('slates/actions.list', {});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async listTools(): Promise<SlatesAction[]> {
|
|
160
|
+
let result = await this.listActions();
|
|
161
|
+
return result.actions.filter(action => action.type === 'action.tool');
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async listTriggers(): Promise<SlatesAction[]> {
|
|
165
|
+
let result = await this.listActions();
|
|
166
|
+
return result.actions.filter(action => action.type === 'action.trigger');
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async getAction(actionId: string) {
|
|
170
|
+
return this.request('slates/action.get', { actionId });
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async getTool(actionId: string) {
|
|
174
|
+
let result = await this.getAction(actionId);
|
|
175
|
+
if (result.action.type !== 'action.tool') {
|
|
176
|
+
throw new Error(`Action ${actionId} is not a tool.`);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return result.action;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async getTrigger(actionId: string) {
|
|
183
|
+
let result = await this.getAction(actionId);
|
|
184
|
+
if (result.action.type !== 'action.trigger') {
|
|
185
|
+
throw new Error(`Action ${actionId} is not a trigger.`);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return result.action;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
async getConfigSchema() {
|
|
192
|
+
return this.request('slates/config.schema.get', {});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
async getDefaultConfig() {
|
|
196
|
+
return this.request('slates/config.get_default', {});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async updateConfig(
|
|
200
|
+
previousConfig: Record<string, any> | null,
|
|
201
|
+
newConfig: Record<string, any>
|
|
202
|
+
) {
|
|
203
|
+
return this.request('slates/config.changed', {
|
|
204
|
+
previousConfig,
|
|
205
|
+
newConfig
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
async listAuthMethods(): Promise<{ authenticationMethods: SlateAuthenticationMethod[] }> {
|
|
210
|
+
return this.request('slates/auth.methods.list', {});
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
async getAuthMethod(authenticationMethodId: string) {
|
|
214
|
+
return this.request('slates/auth.method.get', {
|
|
215
|
+
authenticationMethodId
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
async getDefaultAuthInput(authenticationMethodId: string) {
|
|
220
|
+
return this.request('slates/auth.input.get_default', {
|
|
221
|
+
authenticationMethodId
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
async updateAuthInput(d: {
|
|
226
|
+
authenticationMethodId: string;
|
|
227
|
+
previousInput: Record<string, any> | null;
|
|
228
|
+
newInput: Record<string, any>;
|
|
229
|
+
}) {
|
|
230
|
+
return this.request('slates/auth.input.changed', {
|
|
231
|
+
authenticationMethodId: d.authenticationMethodId,
|
|
232
|
+
previousInput: d.previousInput,
|
|
233
|
+
newInput: d.newInput
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
async getAuthOutput(d: { authenticationMethodId: string; input: Record<string, any> }) {
|
|
238
|
+
return this.request('slates/auth.output.get', {
|
|
239
|
+
authenticationMethodId: d.authenticationMethodId,
|
|
240
|
+
input: d.input
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
async getAuthorizationUrl(d: {
|
|
245
|
+
authenticationMethodId: string;
|
|
246
|
+
redirectUri: string;
|
|
247
|
+
state: string;
|
|
248
|
+
input: Record<string, any>;
|
|
249
|
+
clientId: string;
|
|
250
|
+
clientSecret: string;
|
|
251
|
+
scopes: string[];
|
|
252
|
+
}) {
|
|
253
|
+
return this.request('slates/auth.authorization_url.get', d);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
async handleAuthorizationCallback(d: {
|
|
257
|
+
authenticationMethodId: string;
|
|
258
|
+
code: string;
|
|
259
|
+
state: string;
|
|
260
|
+
redirectUri: string;
|
|
261
|
+
input: Record<string, any>;
|
|
262
|
+
clientId: string;
|
|
263
|
+
clientSecret: string;
|
|
264
|
+
scopes: string[];
|
|
265
|
+
callbackState?: Record<string, any>;
|
|
266
|
+
}): Promise<{
|
|
267
|
+
output: Record<string, any>;
|
|
268
|
+
input?: Record<string, any>;
|
|
269
|
+
scopes?: string[];
|
|
270
|
+
}> {
|
|
271
|
+
return this.request('slates/auth.authorization_callback.handle', d);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
async refreshToken(d: {
|
|
275
|
+
authenticationMethodId: string;
|
|
276
|
+
output: Record<string, any>;
|
|
277
|
+
input: Record<string, any>;
|
|
278
|
+
clientId: string;
|
|
279
|
+
clientSecret: string;
|
|
280
|
+
scopes: string[];
|
|
281
|
+
}) {
|
|
282
|
+
return this.request('slates/auth.token_refresh.handle', d);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
async getAuthProfile(d: {
|
|
286
|
+
authenticationMethodId: string;
|
|
287
|
+
output: Record<string, any>;
|
|
288
|
+
input: Record<string, any>;
|
|
289
|
+
scopes: string[];
|
|
290
|
+
}) {
|
|
291
|
+
return this.request('slates/auth.profile.get', d);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
async invokeTool(actionId: string, input: Record<string, any>) {
|
|
295
|
+
this.ensureSession();
|
|
296
|
+
return this.request('slates/action.tool.invoke', {
|
|
297
|
+
actionId,
|
|
298
|
+
input
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
async mapTriggerEvent(actionId: string, input: Record<string, any>) {
|
|
303
|
+
this.ensureSession();
|
|
304
|
+
return this.request('slates/action.trigger.map_event', {
|
|
305
|
+
actionId,
|
|
306
|
+
input
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
async registerTriggerWebhook(actionId: string, webhookBaseUrl: string) {
|
|
311
|
+
this.ensureSession();
|
|
312
|
+
return this.request('slates/action.trigger.webhook_register', {
|
|
313
|
+
actionId,
|
|
314
|
+
webhookBaseUrl
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
async handleTriggerWebhook(d: {
|
|
319
|
+
actionId: string;
|
|
320
|
+
url: string;
|
|
321
|
+
method: string;
|
|
322
|
+
headers?: Record<string, string>;
|
|
323
|
+
body?: string | Uint8Array | null;
|
|
324
|
+
state?: any;
|
|
325
|
+
}) {
|
|
326
|
+
this.ensureSession();
|
|
327
|
+
let encodedBody =
|
|
328
|
+
typeof d.body === 'string'
|
|
329
|
+
? Buffer.from(d.body, 'utf-8').toString('base64')
|
|
330
|
+
: d.body
|
|
331
|
+
? Buffer.from(d.body).toString('base64')
|
|
332
|
+
: null;
|
|
333
|
+
|
|
334
|
+
return this.request('slates/action.trigger.webhook_handle', {
|
|
335
|
+
actionId: d.actionId,
|
|
336
|
+
url: d.url,
|
|
337
|
+
method: d.method,
|
|
338
|
+
headers: d.headers ?? {},
|
|
339
|
+
body: encodedBody
|
|
340
|
+
? {
|
|
341
|
+
encoding: 'base64',
|
|
342
|
+
content: encodedBody
|
|
343
|
+
}
|
|
344
|
+
: null,
|
|
345
|
+
state: d.state ?? null
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
async unregisterTriggerWebhook(d: {
|
|
350
|
+
actionId: string;
|
|
351
|
+
webhookBaseUrl: string;
|
|
352
|
+
registrationDetails: any;
|
|
353
|
+
state?: any;
|
|
354
|
+
}) {
|
|
355
|
+
this.ensureSession();
|
|
356
|
+
return this.request('slates/action.trigger.webhook_unregister', {
|
|
357
|
+
actionId: d.actionId,
|
|
358
|
+
webhookBaseUrl: d.webhookBaseUrl,
|
|
359
|
+
registrationDetails: d.registrationDetails,
|
|
360
|
+
state: d.state ?? null
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
async close() {
|
|
365
|
+
await this.transport.close?.();
|
|
366
|
+
}
|
|
367
|
+
}
|
package/src/error.ts
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
export type SlateProtocolErrorSource = 'provider' | 'transport';
|
|
2
|
+
|
|
3
|
+
export type SlateProtocolErrorKind =
|
|
4
|
+
| 'declaration'
|
|
5
|
+
| 'validation'
|
|
6
|
+
| 'request'
|
|
7
|
+
| 'auth'
|
|
8
|
+
| 'config'
|
|
9
|
+
| 'resource'
|
|
10
|
+
| 'payment'
|
|
11
|
+
| 'upstream'
|
|
12
|
+
| 'transport'
|
|
13
|
+
| 'internal';
|
|
14
|
+
|
|
15
|
+
export interface SlateProtocolErrorResponse {
|
|
16
|
+
code: string;
|
|
17
|
+
message: string;
|
|
18
|
+
kind: SlateProtocolErrorKind;
|
|
19
|
+
retryable?: boolean;
|
|
20
|
+
status?: number;
|
|
21
|
+
issues?: Array<Record<string, unknown>>;
|
|
22
|
+
provider?: Record<string, unknown>;
|
|
23
|
+
upstream?: Record<string, unknown>;
|
|
24
|
+
baggage?: Record<string, unknown>;
|
|
25
|
+
[key: string]: unknown;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
29
|
+
typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
30
|
+
|
|
31
|
+
let isSlateErrorResponse = (error: unknown): error is SlateProtocolErrorResponse =>
|
|
32
|
+
isRecord(error) && typeof error.code === 'string' && typeof error.message === 'string';
|
|
33
|
+
|
|
34
|
+
let inferKindFromCode = (code: string): SlateProtocolErrorKind => {
|
|
35
|
+
if (code.startsWith('declaration.')) return 'declaration';
|
|
36
|
+
if (code.startsWith('input.')) return 'validation';
|
|
37
|
+
if (code.startsWith('request.')) return 'request';
|
|
38
|
+
if (code.startsWith('config.')) return 'config';
|
|
39
|
+
if (code.startsWith('auth.') || code.startsWith('permission.')) return 'auth';
|
|
40
|
+
if (code.startsWith('resource.')) return 'resource';
|
|
41
|
+
if (code.startsWith('payment.')) return 'payment';
|
|
42
|
+
if (code.startsWith('transport.')) return 'transport';
|
|
43
|
+
if (code.startsWith('upstream.')) return 'upstream';
|
|
44
|
+
return 'internal';
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
let normalizeResponse = (
|
|
48
|
+
error: unknown,
|
|
49
|
+
source: SlateProtocolErrorSource,
|
|
50
|
+
defaults: Partial<SlateProtocolErrorResponse> = {}
|
|
51
|
+
): SlateProtocolErrorResponse => {
|
|
52
|
+
if (isSlateErrorResponse(error)) {
|
|
53
|
+
return {
|
|
54
|
+
...defaults,
|
|
55
|
+
...error,
|
|
56
|
+
kind: error.kind ?? inferKindFromCode(error.code)
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (isRecord(error) && typeof error.message === 'string') {
|
|
61
|
+
let code =
|
|
62
|
+
typeof error.code === 'string'
|
|
63
|
+
? error.code
|
|
64
|
+
: defaults.code ?? (source === 'transport' ? 'transport.invoke_failed' : 'internal.unexpected');
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
...defaults,
|
|
68
|
+
...error,
|
|
69
|
+
code,
|
|
70
|
+
message: error.message,
|
|
71
|
+
kind:
|
|
72
|
+
(typeof error.kind === 'string' ? (error.kind as SlateProtocolErrorKind) : defaults.kind) ??
|
|
73
|
+
inferKindFromCode(code)
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
let code =
|
|
78
|
+
defaults.code ?? (source === 'transport' ? 'transport.invoke_failed' : 'internal.unexpected');
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
...defaults,
|
|
82
|
+
code,
|
|
83
|
+
message:
|
|
84
|
+
error instanceof Error
|
|
85
|
+
? error.message
|
|
86
|
+
: defaults.message ?? 'The slate returned an unexpected error.',
|
|
87
|
+
kind: defaults.kind ?? inferKindFromCode(code),
|
|
88
|
+
baggage: {
|
|
89
|
+
...(defaults.baggage ?? {}),
|
|
90
|
+
...(error instanceof Error
|
|
91
|
+
? { originalName: error.name }
|
|
92
|
+
: { originalValue: error })
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export class SlateProtocolError extends Error {
|
|
98
|
+
cause?: unknown;
|
|
99
|
+
data: SlateProtocolErrorResponse;
|
|
100
|
+
source: SlateProtocolErrorSource;
|
|
101
|
+
|
|
102
|
+
constructor(
|
|
103
|
+
data: SlateProtocolErrorResponse,
|
|
104
|
+
source: SlateProtocolErrorSource = 'provider',
|
|
105
|
+
cause?: unknown
|
|
106
|
+
) {
|
|
107
|
+
super(data.message);
|
|
108
|
+
this.name = 'SlateProtocolError';
|
|
109
|
+
this.data = data;
|
|
110
|
+
this.source = source;
|
|
111
|
+
this.cause = cause;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
get code() {
|
|
115
|
+
return this.data.code;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
get kind() {
|
|
119
|
+
return this.data.kind;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
get retryable() {
|
|
123
|
+
return this.data.retryable;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
get status() {
|
|
127
|
+
return this.data.status;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
toJSON() {
|
|
131
|
+
return {
|
|
132
|
+
...this.data,
|
|
133
|
+
source: this.source
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
static is(error: unknown): error is SlateProtocolError {
|
|
138
|
+
return error instanceof SlateProtocolError;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
static fromResponse(
|
|
142
|
+
error: unknown,
|
|
143
|
+
source: SlateProtocolErrorSource = 'provider'
|
|
144
|
+
): SlateProtocolError {
|
|
145
|
+
if (SlateProtocolError.is(error)) return error;
|
|
146
|
+
return new SlateProtocolError(normalizeResponse(error, source), source, error);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
static fromUnknown(
|
|
150
|
+
error: unknown,
|
|
151
|
+
defaults: Partial<SlateProtocolErrorResponse> = {},
|
|
152
|
+
source: SlateProtocolErrorSource = 'transport'
|
|
153
|
+
) {
|
|
154
|
+
if (SlateProtocolError.is(error)) return error;
|
|
155
|
+
return new SlateProtocolError(normalizeResponse(error, source, defaults), source, error);
|
|
156
|
+
}
|
|
157
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { SlatesProtocolClient } from './client';
|
|
2
|
+
import { SlatesProtocolClientOptions } from './types';
|
|
3
|
+
|
|
4
|
+
export * from './client';
|
|
5
|
+
export * from './error';
|
|
6
|
+
export * from './transport';
|
|
7
|
+
export * from './types';
|
|
8
|
+
|
|
9
|
+
export let createSlatesClient = (opts: SlatesProtocolClientOptions) =>
|
|
10
|
+
new SlatesProtocolClient(opts);
|
package/src/transport.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SlatesNotifications,
|
|
3
|
+
SlatesProviderProtoHandlerManager,
|
|
4
|
+
SlatesRequests,
|
|
5
|
+
SlatesResponses
|
|
6
|
+
} from '@slates/proto';
|
|
7
|
+
import { Slate, SlateLogListener } from '@slates/provider';
|
|
8
|
+
import { createProviderHandler } from '@slates/provider-handler';
|
|
9
|
+
import { SlateProtocolError } from './error';
|
|
10
|
+
import { SlatesMessageTransport } from './types';
|
|
11
|
+
|
|
12
|
+
type ProviderMessage = SlatesNotifications | SlatesRequests;
|
|
13
|
+
type ProviderResponse = SlatesNotifications | SlatesResponses;
|
|
14
|
+
|
|
15
|
+
let toTransportError = (value: unknown, defaultMessage: string) =>
|
|
16
|
+
SlateProtocolError.fromUnknown(
|
|
17
|
+
value,
|
|
18
|
+
{
|
|
19
|
+
code: 'transport.invoke_failed',
|
|
20
|
+
kind: 'transport',
|
|
21
|
+
message: defaultMessage,
|
|
22
|
+
retryable: true,
|
|
23
|
+
baggage: {
|
|
24
|
+
response: value as any
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
'transport'
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
export let createLocalSlateTransport = <ConfigType extends {}, AuthType extends {}>(d: {
|
|
31
|
+
slate: Slate<ConfigType, AuthType>;
|
|
32
|
+
listeners?: SlateLogListener[];
|
|
33
|
+
}): SlatesMessageTransport => {
|
|
34
|
+
let managerPromise = createProviderHandler(d.slate, d.listeners ?? []).run();
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
async send(messages) {
|
|
38
|
+
let manager = await managerPromise;
|
|
39
|
+
let responses: ProviderResponse[] = [];
|
|
40
|
+
|
|
41
|
+
for (let message of messages as ProviderMessage[]) {
|
|
42
|
+
let response;
|
|
43
|
+
try {
|
|
44
|
+
response = await SlatesProviderProtoHandlerManager.handleInput(manager, message);
|
|
45
|
+
} catch (error) {
|
|
46
|
+
throw toTransportError(error, 'Local slate invocation failed');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (response) {
|
|
50
|
+
responses.push(response as ProviderResponse);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return responses;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
};
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SlatesNotifications,
|
|
3
|
+
SlatesParticipant,
|
|
4
|
+
SlatesProtocolVersion,
|
|
5
|
+
SlatesRequests,
|
|
6
|
+
SlatesResponses
|
|
7
|
+
} from '@slates/proto';
|
|
8
|
+
|
|
9
|
+
export type SlatesJsonObject = Record<string, any>;
|
|
10
|
+
export type SlatesProtocolMessage = SlatesNotifications | SlatesRequests;
|
|
11
|
+
export type SlatesProtocolResponse = SlatesNotifications | SlatesResponses;
|
|
12
|
+
|
|
13
|
+
export interface SlatesClientState {
|
|
14
|
+
protocol: SlatesProtocolVersion;
|
|
15
|
+
participants: SlatesParticipant[];
|
|
16
|
+
config: SlatesJsonObject | null;
|
|
17
|
+
auth: {
|
|
18
|
+
authenticationMethodId: string;
|
|
19
|
+
output: SlatesJsonObject;
|
|
20
|
+
} | null;
|
|
21
|
+
session: {
|
|
22
|
+
id: string;
|
|
23
|
+
state: SlatesJsonObject;
|
|
24
|
+
} | null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface SlatesMessageTransport {
|
|
28
|
+
send(messages: SlatesProtocolMessage[]): Promise<SlatesProtocolResponse[]>;
|
|
29
|
+
close?(): Promise<void> | void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface SlatesProtocolClientOptions {
|
|
33
|
+
transport: SlatesMessageTransport;
|
|
34
|
+
participants?: SlatesParticipant[];
|
|
35
|
+
state?: Partial<SlatesClientState>;
|
|
36
|
+
}
|