@contello/client 1.0.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/cli.js +15 -0
- package/dist/generate-OTWB5J56.js +827 -0
- package/dist/index.cjs +476 -0
- package/dist/index.d.cts +83 -0
- package/dist/index.d.ts +83 -0
- package/dist/index.js +438 -0
- package/package.json +46 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,476 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
ContelloClient: () => ContelloClient,
|
|
34
|
+
createContelloClient: () => createContelloClient
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(index_exports);
|
|
37
|
+
|
|
38
|
+
// src/client.ts
|
|
39
|
+
var import_graphql_ws = require("graphql-ws");
|
|
40
|
+
var import_rxjs2 = require("rxjs");
|
|
41
|
+
|
|
42
|
+
// src/diagnostics.ts
|
|
43
|
+
var import_node_diagnostics_channel = __toESM(require("diagnostics_channel"), 1);
|
|
44
|
+
var channels = {
|
|
45
|
+
start: "@contello/client:start",
|
|
46
|
+
end: "@contello/client:end",
|
|
47
|
+
error: "@contello/client:error",
|
|
48
|
+
/** subscribe to decorate outgoing GraphQL WebSocket messages (e.g. inject traceparent) */
|
|
49
|
+
message: "@contello/client:message"
|
|
50
|
+
};
|
|
51
|
+
var onStart = import_node_diagnostics_channel.default.channel(channels.start);
|
|
52
|
+
var onEnd = import_node_diagnostics_channel.default.channel(channels.end);
|
|
53
|
+
var onError = import_node_diagnostics_channel.default.channel(channels.error);
|
|
54
|
+
var onMessage = import_node_diagnostics_channel.default.channel(channels.message);
|
|
55
|
+
function hasOperationSubscribers() {
|
|
56
|
+
return onStart.hasSubscribers || onEnd.hasSubscribers || onError.hasSubscribers;
|
|
57
|
+
}
|
|
58
|
+
function wrap(name, fn) {
|
|
59
|
+
if (!hasOperationSubscribers()) {
|
|
60
|
+
return fn();
|
|
61
|
+
}
|
|
62
|
+
const start = performance.now();
|
|
63
|
+
onStart.publish({ name });
|
|
64
|
+
let result;
|
|
65
|
+
try {
|
|
66
|
+
result = fn();
|
|
67
|
+
} catch (error) {
|
|
68
|
+
onError.publish({ name, error, durationMs: performance.now() - start });
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
if (result instanceof Promise) {
|
|
72
|
+
return result.then(
|
|
73
|
+
(r) => {
|
|
74
|
+
onEnd.publish({ name, durationMs: performance.now() - start });
|
|
75
|
+
return r;
|
|
76
|
+
},
|
|
77
|
+
(error) => {
|
|
78
|
+
onError.publish({ name, error, durationMs: performance.now() - start });
|
|
79
|
+
throw error;
|
|
80
|
+
}
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
onEnd.publish({ name, durationMs: performance.now() - start });
|
|
84
|
+
return result;
|
|
85
|
+
}
|
|
86
|
+
function decorateMessage(message) {
|
|
87
|
+
if (!onMessage.hasSubscribers) {
|
|
88
|
+
return message;
|
|
89
|
+
}
|
|
90
|
+
const msg = { message };
|
|
91
|
+
onMessage.publish(msg);
|
|
92
|
+
return msg.message;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// src/download.ts
|
|
96
|
+
async function downloadFile(url, token, fileId) {
|
|
97
|
+
const response = await fetch(`${url}/api/v1/assets/files/${fileId}`, {
|
|
98
|
+
headers: { token }
|
|
99
|
+
});
|
|
100
|
+
if (!response.ok) {
|
|
101
|
+
throw new Error(`download failed: ${response.status} ${response.statusText}`);
|
|
102
|
+
}
|
|
103
|
+
if (!response.body) {
|
|
104
|
+
throw new Error("download response has no body");
|
|
105
|
+
}
|
|
106
|
+
const mimeType = response.headers.get("content-type") ?? "application/octet-stream";
|
|
107
|
+
const size = Number(response.headers.get("content-length") ?? 0);
|
|
108
|
+
return {
|
|
109
|
+
mimeType,
|
|
110
|
+
size,
|
|
111
|
+
bytes: () => response.arrayBuffer().then((buf) => new Uint8Array(buf)),
|
|
112
|
+
stream: () => response.body
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// src/ping.ts
|
|
117
|
+
var PING_QUERY = `query ContelloPing { contelloPing { response } }`;
|
|
118
|
+
async function ping(execute) {
|
|
119
|
+
const data = await execute(PING_QUERY);
|
|
120
|
+
if (data.contelloPing.response !== "pong") {
|
|
121
|
+
throw new Error(`unexpected ping response: ${data.contelloPing.response}`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// src/pool.ts
|
|
126
|
+
var ConnectionPool = class {
|
|
127
|
+
constructor(createClient2, poolSize) {
|
|
128
|
+
this.createClient = createClient2;
|
|
129
|
+
this.poolSize = poolSize;
|
|
130
|
+
}
|
|
131
|
+
clients = [];
|
|
132
|
+
currentIndex = 0;
|
|
133
|
+
async connect() {
|
|
134
|
+
const connected = [];
|
|
135
|
+
for (let i = 0; i < this.poolSize; i++) {
|
|
136
|
+
const client = this.createClient(`${i + 1}`);
|
|
137
|
+
this.clients.push(client);
|
|
138
|
+
connected.push(
|
|
139
|
+
new Promise((resolve) => {
|
|
140
|
+
const unsubscribe = client.on("connected", () => {
|
|
141
|
+
unsubscribe();
|
|
142
|
+
resolve();
|
|
143
|
+
});
|
|
144
|
+
})
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
await Promise.all(connected);
|
|
148
|
+
}
|
|
149
|
+
get() {
|
|
150
|
+
if (this.clients.length === 0) {
|
|
151
|
+
throw new Error("Connection pool is empty. Please call connect() first.");
|
|
152
|
+
}
|
|
153
|
+
const client = this.clients[this.currentIndex];
|
|
154
|
+
this.currentIndex = (this.currentIndex + 1) % this.poolSize;
|
|
155
|
+
if (!client) {
|
|
156
|
+
throw new Error("No available WebSocket client");
|
|
157
|
+
}
|
|
158
|
+
return client;
|
|
159
|
+
}
|
|
160
|
+
async disconnect() {
|
|
161
|
+
const closed = this.clients.map(
|
|
162
|
+
(client) => new Promise((resolve) => {
|
|
163
|
+
const unsubscribe = client.on("closed", () => {
|
|
164
|
+
unsubscribe();
|
|
165
|
+
resolve();
|
|
166
|
+
});
|
|
167
|
+
client.dispose();
|
|
168
|
+
})
|
|
169
|
+
);
|
|
170
|
+
this.clients = [];
|
|
171
|
+
await Promise.all(closed);
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
// src/rpc.ts
|
|
176
|
+
var import_rxjs = require("rxjs");
|
|
177
|
+
|
|
178
|
+
// src/transform-response.ts
|
|
179
|
+
var FLAT_PREFIX = "_flat_";
|
|
180
|
+
var MODEL_SUFFIXES = ["Entity", "Component"];
|
|
181
|
+
function deriveModel(typename) {
|
|
182
|
+
for (const suffix of MODEL_SUFFIXES) {
|
|
183
|
+
if (typename.endsWith(suffix)) {
|
|
184
|
+
const stripped = typename.slice(0, -suffix.length);
|
|
185
|
+
return stripped.charAt(0).toLowerCase() + stripped.slice(1);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return void 0;
|
|
189
|
+
}
|
|
190
|
+
function transformResponse(data) {
|
|
191
|
+
if (data === null || data === void 0 || typeof data !== "object") {
|
|
192
|
+
return data;
|
|
193
|
+
}
|
|
194
|
+
if (Array.isArray(data)) {
|
|
195
|
+
for (const item of data) {
|
|
196
|
+
transformResponse(item);
|
|
197
|
+
}
|
|
198
|
+
return data;
|
|
199
|
+
}
|
|
200
|
+
const obj = data;
|
|
201
|
+
const typename = obj["__typename"];
|
|
202
|
+
if (typeof typename === "string") {
|
|
203
|
+
const model = deriveModel(typename);
|
|
204
|
+
if (model) {
|
|
205
|
+
obj["__model"] = model;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
const flatKeys = Object.keys(obj).filter((k) => k.startsWith(FLAT_PREFIX));
|
|
209
|
+
for (const flatKey of flatKeys) {
|
|
210
|
+
const fieldKey = flatKey.slice(FLAT_PREFIX.length);
|
|
211
|
+
const flatArray = obj[flatKey];
|
|
212
|
+
const refArray = obj[fieldKey];
|
|
213
|
+
if (!flatArray || !refArray) {
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
const flatMap = /* @__PURE__ */ new Map();
|
|
217
|
+
for (const component of flatArray) {
|
|
218
|
+
if (component._flatId) {
|
|
219
|
+
flatMap.set(component._flatId, component);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
obj[fieldKey] = resolveRefs(refArray, flatMap);
|
|
223
|
+
delete obj[flatKey];
|
|
224
|
+
}
|
|
225
|
+
for (const key of Object.keys(obj)) {
|
|
226
|
+
const value = obj[key];
|
|
227
|
+
if (value && typeof value === "object") {
|
|
228
|
+
if (Array.isArray(value)) {
|
|
229
|
+
for (const item of value) {
|
|
230
|
+
if (item && typeof item === "object") {
|
|
231
|
+
transformResponse(item);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
} else {
|
|
235
|
+
transformResponse(value);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return data;
|
|
240
|
+
}
|
|
241
|
+
function resolveRefs(refs, flatMap) {
|
|
242
|
+
return refs.flatMap((ref) => {
|
|
243
|
+
if (!ref._flatId) return [];
|
|
244
|
+
const component = flatMap.get(ref._flatId);
|
|
245
|
+
if (!component) return [];
|
|
246
|
+
for (const [key, value] of Object.entries(component)) {
|
|
247
|
+
if (Array.isArray(value) && value.length > 0 && value[0]?._flatId !== void 0) {
|
|
248
|
+
component[key] = resolveRefs(value, flatMap);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
transformResponse(component);
|
|
252
|
+
return [component];
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// src/rpc.ts
|
|
257
|
+
function buildRpc(operations, client) {
|
|
258
|
+
const rpc = {};
|
|
259
|
+
for (const [name, def] of Object.entries(operations)) {
|
|
260
|
+
if (def.kind === "subscription") {
|
|
261
|
+
rpc[name] = (variables) => client.subscribe(def.document, variables).pipe((0, import_rxjs.map)(transformResponse));
|
|
262
|
+
} else {
|
|
263
|
+
rpc[name] = (variables) => wrap(
|
|
264
|
+
`rpc:${name}`,
|
|
265
|
+
() => (0, import_rxjs.firstValueFrom)(client.subscribe(def.document, variables).pipe((0, import_rxjs.map)(transformResponse)))
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return rpc;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// src/upload.ts
|
|
273
|
+
var DEFAULT_CHUNK_SIZE = 4 * 1024 * 1024;
|
|
274
|
+
function isUint8Array(data) {
|
|
275
|
+
return data instanceof Uint8Array;
|
|
276
|
+
}
|
|
277
|
+
function upload(url, project, token, data, meta, options) {
|
|
278
|
+
const chunkSize = options?.chunkSize ?? DEFAULT_CHUNK_SIZE;
|
|
279
|
+
const wsUrl = `${url.replace(/^http/i, "ws")}/api/v1/assets/ws`;
|
|
280
|
+
const size = isUint8Array(data) ? data.length : meta.size;
|
|
281
|
+
return new Promise((resolve, reject) => {
|
|
282
|
+
const ws = new WebSocket(wsUrl, "contello-file-upload-v1");
|
|
283
|
+
let done = false;
|
|
284
|
+
const cleanup = () => {
|
|
285
|
+
options?.abort?.removeEventListener("abort", abortHandler);
|
|
286
|
+
};
|
|
287
|
+
const abortHandler = () => {
|
|
288
|
+
if (!done) {
|
|
289
|
+
ws.close();
|
|
290
|
+
reject(new Error("upload aborted"));
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
options?.abort?.addEventListener("abort", abortHandler);
|
|
294
|
+
function sendBuffer(buf) {
|
|
295
|
+
let offset = 0;
|
|
296
|
+
while (offset < buf.length) {
|
|
297
|
+
const chunk = buf.subarray(offset, offset + chunkSize);
|
|
298
|
+
ws.send(chunk);
|
|
299
|
+
offset += chunk.length;
|
|
300
|
+
}
|
|
301
|
+
ws.send(JSON.stringify({ type: "done" }));
|
|
302
|
+
}
|
|
303
|
+
async function sendStream(stream) {
|
|
304
|
+
const reader = stream.getReader();
|
|
305
|
+
try {
|
|
306
|
+
while (true) {
|
|
307
|
+
const { done: streamDone, value } = await reader.read();
|
|
308
|
+
if (streamDone) break;
|
|
309
|
+
ws.send(value);
|
|
310
|
+
}
|
|
311
|
+
ws.send(JSON.stringify({ type: "done" }));
|
|
312
|
+
} catch (err) {
|
|
313
|
+
reject(err);
|
|
314
|
+
ws.close();
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
ws.addEventListener("open", () => {
|
|
318
|
+
const initFrame = {
|
|
319
|
+
type: "init",
|
|
320
|
+
metadata: { ...meta, ...size !== void 0 ? { size } : {}, projectRef: project },
|
|
321
|
+
token
|
|
322
|
+
};
|
|
323
|
+
ws.send(JSON.stringify(initFrame));
|
|
324
|
+
});
|
|
325
|
+
ws.addEventListener("message", (event) => {
|
|
326
|
+
const message = JSON.parse(String(event.data));
|
|
327
|
+
if (message.type === "ack") {
|
|
328
|
+
if (isUint8Array(data)) {
|
|
329
|
+
sendBuffer(data);
|
|
330
|
+
} else {
|
|
331
|
+
sendStream(data);
|
|
332
|
+
}
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
if (message.type === "done") {
|
|
336
|
+
done = true;
|
|
337
|
+
cleanup();
|
|
338
|
+
ws.close();
|
|
339
|
+
resolve(message.id);
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
ws.addEventListener("error", () => {
|
|
343
|
+
cleanup();
|
|
344
|
+
reject(new Error("upload websocket error"));
|
|
345
|
+
});
|
|
346
|
+
ws.addEventListener("close", () => {
|
|
347
|
+
cleanup();
|
|
348
|
+
if (!done) {
|
|
349
|
+
reject(new Error("connection closed before upload completed"));
|
|
350
|
+
}
|
|
351
|
+
});
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// src/utils.ts
|
|
356
|
+
var MAX_DELAY_MS = 3e4;
|
|
357
|
+
var JITTER_MIN_MS = 300;
|
|
358
|
+
var JITTER_MAX_MS = 3e3;
|
|
359
|
+
function exponentialBackoff(retries) {
|
|
360
|
+
const delay = Math.min(Math.pow(2, retries) * 1e3, MAX_DELAY_MS) + Math.floor(Math.random() * (JITTER_MAX_MS - JITTER_MIN_MS) + JITTER_MIN_MS);
|
|
361
|
+
return new Promise((resolve) => setTimeout(resolve, delay));
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// src/client.ts
|
|
365
|
+
var ContelloClient = class {
|
|
366
|
+
_pool;
|
|
367
|
+
_rpc;
|
|
368
|
+
_url;
|
|
369
|
+
_project;
|
|
370
|
+
_token;
|
|
371
|
+
constructor(options) {
|
|
372
|
+
const { url, project, token, operations, connections = 1, connectionEvents } = options;
|
|
373
|
+
this._url = url;
|
|
374
|
+
this._project = project;
|
|
375
|
+
this._token = token;
|
|
376
|
+
const websocketUrl = `${url}/graphql/projects/${project}`.replace(/^http/i, "ws");
|
|
377
|
+
let state = "disconnected";
|
|
378
|
+
let connectedCount = 0;
|
|
379
|
+
const onConnectionUp = () => {
|
|
380
|
+
connectedCount++;
|
|
381
|
+
if (connectedCount === connections && state !== "connected") {
|
|
382
|
+
state = "connected";
|
|
383
|
+
options.onConnected?.();
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
const onConnectionDown = () => {
|
|
387
|
+
if (connectedCount > 0) {
|
|
388
|
+
connectedCount--;
|
|
389
|
+
}
|
|
390
|
+
if (state === "connected") {
|
|
391
|
+
state = "reconnecting";
|
|
392
|
+
options.onReconnecting?.();
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
this._pool = new ConnectionPool((id) => {
|
|
396
|
+
const context = Object.freeze({ connectionId: id, websocketUrl });
|
|
397
|
+
return (0, import_graphql_ws.createClient)({
|
|
398
|
+
url: websocketUrl,
|
|
399
|
+
connectionParams: { token },
|
|
400
|
+
lazy: false,
|
|
401
|
+
keepAlive: 3e4,
|
|
402
|
+
retryAttempts: Infinity,
|
|
403
|
+
retryWait: exponentialBackoff,
|
|
404
|
+
shouldRetry: () => true,
|
|
405
|
+
jsonMessageReplacer: (key, value) => {
|
|
406
|
+
if (!key) {
|
|
407
|
+
return decorateMessage(value);
|
|
408
|
+
}
|
|
409
|
+
return value;
|
|
410
|
+
},
|
|
411
|
+
...options.onError ? { onNonLazyError: (e) => options.onError(e) } : {},
|
|
412
|
+
on: {
|
|
413
|
+
connected: () => {
|
|
414
|
+
onConnectionUp();
|
|
415
|
+
connectionEvents?.onConnected?.(context);
|
|
416
|
+
},
|
|
417
|
+
connecting: () => {
|
|
418
|
+
connectionEvents?.onConnecting?.(context);
|
|
419
|
+
},
|
|
420
|
+
closed: () => {
|
|
421
|
+
onConnectionDown();
|
|
422
|
+
connectionEvents?.onClosed?.(context);
|
|
423
|
+
},
|
|
424
|
+
...options.onError || connectionEvents?.onError ? {
|
|
425
|
+
error: (e) => {
|
|
426
|
+
options.onError?.(e);
|
|
427
|
+
connectionEvents?.onError?.(context, e);
|
|
428
|
+
}
|
|
429
|
+
} : {}
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
}, connections);
|
|
433
|
+
this._rpc = operations ? buildRpc(operations, this) : void 0;
|
|
434
|
+
}
|
|
435
|
+
get rpc() {
|
|
436
|
+
if (!this._rpc) {
|
|
437
|
+
throw new Error("@contello/client: .rpc accessed without operations");
|
|
438
|
+
}
|
|
439
|
+
return this._rpc;
|
|
440
|
+
}
|
|
441
|
+
async init() {
|
|
442
|
+
await this._pool.connect();
|
|
443
|
+
}
|
|
444
|
+
async destroy() {
|
|
445
|
+
await this._pool.disconnect();
|
|
446
|
+
}
|
|
447
|
+
async ping() {
|
|
448
|
+
return ping((query) => (0, import_rxjs2.firstValueFrom)(this.subscribe(query)));
|
|
449
|
+
}
|
|
450
|
+
subscribe(query, variables) {
|
|
451
|
+
const wsClient = this._pool.get();
|
|
452
|
+
return new import_rxjs2.Observable((obs) => wsClient.subscribe({ query, variables }, obs)).pipe(
|
|
453
|
+
(0, import_rxjs2.map)((r) => r.data)
|
|
454
|
+
);
|
|
455
|
+
}
|
|
456
|
+
download(fileId) {
|
|
457
|
+
return wrap("@contello/client:download", () => downloadFile(this._url, this._token, fileId));
|
|
458
|
+
}
|
|
459
|
+
upload(data, meta, options) {
|
|
460
|
+
return wrap(
|
|
461
|
+
"@contello/client:upload",
|
|
462
|
+
() => upload(this._url, this._project, this._token, data, meta, options)
|
|
463
|
+
);
|
|
464
|
+
}
|
|
465
|
+
execute(query, variables) {
|
|
466
|
+
return wrap("@contello/client:execute", () => (0, import_rxjs2.firstValueFrom)(this.subscribe(query, variables)));
|
|
467
|
+
}
|
|
468
|
+
};
|
|
469
|
+
function createContelloClient(options) {
|
|
470
|
+
return new ContelloClient(options);
|
|
471
|
+
}
|
|
472
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
473
|
+
0 && (module.exports = {
|
|
474
|
+
ContelloClient,
|
|
475
|
+
createContelloClient
|
|
476
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
import { ReadableStream as ReadableStream$1 } from 'node:stream/web';
|
|
3
|
+
|
|
4
|
+
type DownloadResult = {
|
|
5
|
+
mimeType: string;
|
|
6
|
+
size: number;
|
|
7
|
+
bytes(): Promise<Uint8Array>;
|
|
8
|
+
stream(): ReadableStream<Uint8Array>;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
type OperationKind = 'query' | 'mutation' | 'subscription';
|
|
12
|
+
type OperationDef<TResult = unknown, TVariables = Record<string, unknown>, TKind extends OperationKind = OperationKind> = {
|
|
13
|
+
document: string;
|
|
14
|
+
kind: TKind;
|
|
15
|
+
/** phantom type — exists only at the type level */
|
|
16
|
+
__result?: TResult | undefined;
|
|
17
|
+
/** phantom type — exists only at the type level */
|
|
18
|
+
__variables?: TVariables | undefined;
|
|
19
|
+
};
|
|
20
|
+
type OperationMap = Record<string, OperationDef>;
|
|
21
|
+
type InferResult<T extends OperationDef> = NonNullable<T['__result']>;
|
|
22
|
+
type InferVariables<T extends OperationDef> = NonNullable<T['__variables']>;
|
|
23
|
+
type RpcMethod<T extends OperationDef> = T['kind'] extends 'subscription' ? InferVariables<T> extends Record<string, never> ? () => Observable<InferResult<T>> : (variables: InferVariables<T>) => Observable<InferResult<T>> : InferVariables<T> extends Record<string, never> ? () => Promise<InferResult<T>> : (variables: InferVariables<T>) => Promise<InferResult<T>>;
|
|
24
|
+
type Rpc<T extends OperationMap> = {
|
|
25
|
+
[K in keyof T]: RpcMethod<T[K]>;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
type UploadMetadata = {
|
|
29
|
+
name: string;
|
|
30
|
+
mimeType?: string | undefined;
|
|
31
|
+
size?: number | undefined;
|
|
32
|
+
annotations?: {
|
|
33
|
+
key: string;
|
|
34
|
+
value: string;
|
|
35
|
+
}[] | undefined;
|
|
36
|
+
collectionRefs?: string[] | undefined;
|
|
37
|
+
};
|
|
38
|
+
type UploadData = Uint8Array | ReadableStream$1<Uint8Array> | ReadableStream<Uint8Array>;
|
|
39
|
+
type UploadOptions = {
|
|
40
|
+
chunkSize?: number | undefined;
|
|
41
|
+
abort?: AbortSignal | undefined;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
type ConnectionContext = {
|
|
45
|
+
connectionId: string;
|
|
46
|
+
websocketUrl: string;
|
|
47
|
+
};
|
|
48
|
+
type ConnectionEvents = {
|
|
49
|
+
onConnected?: ((context: ConnectionContext) => void) | undefined;
|
|
50
|
+
onConnecting?: ((context: ConnectionContext) => void) | undefined;
|
|
51
|
+
onClosed?: ((context: ConnectionContext) => void) | undefined;
|
|
52
|
+
onError?: ((context: ConnectionContext, error: unknown) => void) | undefined;
|
|
53
|
+
};
|
|
54
|
+
type ContelloClientOptions<T extends OperationMap | undefined = undefined> = {
|
|
55
|
+
url: string;
|
|
56
|
+
project: string;
|
|
57
|
+
token: string;
|
|
58
|
+
operations?: T | undefined;
|
|
59
|
+
connections?: number | undefined;
|
|
60
|
+
onConnected?: (() => void) | undefined;
|
|
61
|
+
onReconnecting?: (() => void) | undefined;
|
|
62
|
+
onError?: ((error: unknown) => void) | undefined;
|
|
63
|
+
connectionEvents?: ConnectionEvents | undefined;
|
|
64
|
+
};
|
|
65
|
+
declare class ContelloClient<T extends OperationMap | undefined = undefined> {
|
|
66
|
+
private _pool;
|
|
67
|
+
private _rpc;
|
|
68
|
+
private _url;
|
|
69
|
+
private _project;
|
|
70
|
+
private _token;
|
|
71
|
+
constructor(options: ContelloClientOptions<T>);
|
|
72
|
+
get rpc(): T extends OperationMap ? Rpc<T> : never;
|
|
73
|
+
init(): Promise<void>;
|
|
74
|
+
destroy(): Promise<void>;
|
|
75
|
+
ping(): Promise<void>;
|
|
76
|
+
subscribe<TData>(query: string, variables?: Record<string, unknown> | undefined): Observable<TData>;
|
|
77
|
+
download(fileId: string): Promise<DownloadResult>;
|
|
78
|
+
upload(data: UploadData, meta: UploadMetadata, options?: UploadOptions | undefined): Promise<string>;
|
|
79
|
+
execute<TData>(query: string, variables?: Record<string, unknown> | undefined): Promise<TData>;
|
|
80
|
+
}
|
|
81
|
+
declare function createContelloClient<T extends OperationMap | undefined = undefined>(options: ContelloClientOptions<T>): ContelloClient<T>;
|
|
82
|
+
|
|
83
|
+
export { type ConnectionContext, type ConnectionEvents, ContelloClient, type ContelloClientOptions, type DownloadResult, type OperationDef, type OperationKind, type OperationMap, type Rpc, type UploadData, type UploadMetadata, type UploadOptions, createContelloClient };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
import { ReadableStream as ReadableStream$1 } from 'node:stream/web';
|
|
3
|
+
|
|
4
|
+
type DownloadResult = {
|
|
5
|
+
mimeType: string;
|
|
6
|
+
size: number;
|
|
7
|
+
bytes(): Promise<Uint8Array>;
|
|
8
|
+
stream(): ReadableStream<Uint8Array>;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
type OperationKind = 'query' | 'mutation' | 'subscription';
|
|
12
|
+
type OperationDef<TResult = unknown, TVariables = Record<string, unknown>, TKind extends OperationKind = OperationKind> = {
|
|
13
|
+
document: string;
|
|
14
|
+
kind: TKind;
|
|
15
|
+
/** phantom type — exists only at the type level */
|
|
16
|
+
__result?: TResult | undefined;
|
|
17
|
+
/** phantom type — exists only at the type level */
|
|
18
|
+
__variables?: TVariables | undefined;
|
|
19
|
+
};
|
|
20
|
+
type OperationMap = Record<string, OperationDef>;
|
|
21
|
+
type InferResult<T extends OperationDef> = NonNullable<T['__result']>;
|
|
22
|
+
type InferVariables<T extends OperationDef> = NonNullable<T['__variables']>;
|
|
23
|
+
type RpcMethod<T extends OperationDef> = T['kind'] extends 'subscription' ? InferVariables<T> extends Record<string, never> ? () => Observable<InferResult<T>> : (variables: InferVariables<T>) => Observable<InferResult<T>> : InferVariables<T> extends Record<string, never> ? () => Promise<InferResult<T>> : (variables: InferVariables<T>) => Promise<InferResult<T>>;
|
|
24
|
+
type Rpc<T extends OperationMap> = {
|
|
25
|
+
[K in keyof T]: RpcMethod<T[K]>;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
type UploadMetadata = {
|
|
29
|
+
name: string;
|
|
30
|
+
mimeType?: string | undefined;
|
|
31
|
+
size?: number | undefined;
|
|
32
|
+
annotations?: {
|
|
33
|
+
key: string;
|
|
34
|
+
value: string;
|
|
35
|
+
}[] | undefined;
|
|
36
|
+
collectionRefs?: string[] | undefined;
|
|
37
|
+
};
|
|
38
|
+
type UploadData = Uint8Array | ReadableStream$1<Uint8Array> | ReadableStream<Uint8Array>;
|
|
39
|
+
type UploadOptions = {
|
|
40
|
+
chunkSize?: number | undefined;
|
|
41
|
+
abort?: AbortSignal | undefined;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
type ConnectionContext = {
|
|
45
|
+
connectionId: string;
|
|
46
|
+
websocketUrl: string;
|
|
47
|
+
};
|
|
48
|
+
type ConnectionEvents = {
|
|
49
|
+
onConnected?: ((context: ConnectionContext) => void) | undefined;
|
|
50
|
+
onConnecting?: ((context: ConnectionContext) => void) | undefined;
|
|
51
|
+
onClosed?: ((context: ConnectionContext) => void) | undefined;
|
|
52
|
+
onError?: ((context: ConnectionContext, error: unknown) => void) | undefined;
|
|
53
|
+
};
|
|
54
|
+
type ContelloClientOptions<T extends OperationMap | undefined = undefined> = {
|
|
55
|
+
url: string;
|
|
56
|
+
project: string;
|
|
57
|
+
token: string;
|
|
58
|
+
operations?: T | undefined;
|
|
59
|
+
connections?: number | undefined;
|
|
60
|
+
onConnected?: (() => void) | undefined;
|
|
61
|
+
onReconnecting?: (() => void) | undefined;
|
|
62
|
+
onError?: ((error: unknown) => void) | undefined;
|
|
63
|
+
connectionEvents?: ConnectionEvents | undefined;
|
|
64
|
+
};
|
|
65
|
+
declare class ContelloClient<T extends OperationMap | undefined = undefined> {
|
|
66
|
+
private _pool;
|
|
67
|
+
private _rpc;
|
|
68
|
+
private _url;
|
|
69
|
+
private _project;
|
|
70
|
+
private _token;
|
|
71
|
+
constructor(options: ContelloClientOptions<T>);
|
|
72
|
+
get rpc(): T extends OperationMap ? Rpc<T> : never;
|
|
73
|
+
init(): Promise<void>;
|
|
74
|
+
destroy(): Promise<void>;
|
|
75
|
+
ping(): Promise<void>;
|
|
76
|
+
subscribe<TData>(query: string, variables?: Record<string, unknown> | undefined): Observable<TData>;
|
|
77
|
+
download(fileId: string): Promise<DownloadResult>;
|
|
78
|
+
upload(data: UploadData, meta: UploadMetadata, options?: UploadOptions | undefined): Promise<string>;
|
|
79
|
+
execute<TData>(query: string, variables?: Record<string, unknown> | undefined): Promise<TData>;
|
|
80
|
+
}
|
|
81
|
+
declare function createContelloClient<T extends OperationMap | undefined = undefined>(options: ContelloClientOptions<T>): ContelloClient<T>;
|
|
82
|
+
|
|
83
|
+
export { type ConnectionContext, type ConnectionEvents, ContelloClient, type ContelloClientOptions, type DownloadResult, type OperationDef, type OperationKind, type OperationMap, type Rpc, type UploadData, type UploadMetadata, type UploadOptions, createContelloClient };
|