@supacloud/js 0.24.0 → 0.25.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/index.d.ts +2 -1
- package/dist/index.js +299 -63
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -353,11 +353,12 @@ export declare class SupaCloudApiError extends Error {
|
|
|
353
353
|
constructor(message: string, status: number, responseBody: unknown);
|
|
354
354
|
}
|
|
355
355
|
type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
356
|
+
type ResponseDecoder<T> = (value: unknown) => T;
|
|
356
357
|
declare class SupaCloudManagementClient<TClient extends SupabaseClient = SupabaseClient> {
|
|
357
358
|
protected readonly options: Required<SupaCloudClientOptions<TClient>>;
|
|
358
359
|
constructor(options: Required<SupaCloudClientOptions<TClient>>);
|
|
359
360
|
protected resolveAccessToken(): Promise<string>;
|
|
360
|
-
protected request<T>(path: string, method: HttpMethod, body
|
|
361
|
+
protected request<T>(path: string, method: HttpMethod, body: unknown, decode: ResponseDecoder<T>): Promise<T>;
|
|
361
362
|
}
|
|
362
363
|
declare class SupaCloudTasksClient<TClient extends SupabaseClient = SupabaseClient> extends SupaCloudManagementClient<TClient> {
|
|
363
364
|
private createReceipt;
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// src/service-role-rpc.ts
|
|
2
2
|
async function invokeServiceRoleRpc(supabase, functionName, request) {
|
|
3
|
-
const
|
|
4
|
-
const { data: rpcResponse, error: rpcError } =
|
|
3
|
+
const rpcResult = await supabase.rpc(functionName, { request });
|
|
4
|
+
const { data: rpcResponse, error: rpcError } = rpcResult;
|
|
5
5
|
if (rpcError)
|
|
6
6
|
throw rpcError;
|
|
7
7
|
return rpcResponse;
|
|
@@ -102,7 +102,7 @@ function parseRefreshBody(body, contentType) {
|
|
|
102
102
|
}
|
|
103
103
|
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
104
104
|
return null;
|
|
105
|
-
const record = value;
|
|
105
|
+
const record = valueRecord(value);
|
|
106
106
|
const refreshToken = typeof record.refresh_token === "string" ? record.refresh_token.trim() : "";
|
|
107
107
|
if (!refreshToken)
|
|
108
108
|
return null;
|
|
@@ -116,6 +116,14 @@ function parseRefreshBody(body, contentType) {
|
|
|
116
116
|
const params = new URLSearchParams(body);
|
|
117
117
|
return params.get("refresh_token")?.trim() ? params : null;
|
|
118
118
|
}
|
|
119
|
+
function valueRecord(value) {
|
|
120
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
121
|
+
return {};
|
|
122
|
+
const record = {};
|
|
123
|
+
for (const [key, item] of Object.entries(value))
|
|
124
|
+
record[key] = item;
|
|
125
|
+
return record;
|
|
126
|
+
}
|
|
119
127
|
function replacementRequest(request, tokenEndpoint, body) {
|
|
120
128
|
const headers = new Headers(request.headers);
|
|
121
129
|
headers.set("content-type", "application/x-www-form-urlencoded");
|
|
@@ -267,14 +275,236 @@ function normalizeMessageId(value) {
|
|
|
267
275
|
return Number.isSafeInteger(parsed) && parsed > 0 ? parsed : 0;
|
|
268
276
|
}
|
|
269
277
|
function firstRpcValue(value) {
|
|
270
|
-
return
|
|
278
|
+
return isUnknownArray(value) ? value[0] : value;
|
|
279
|
+
}
|
|
280
|
+
function isUnknownArray(value) {
|
|
281
|
+
return Array.isArray(value);
|
|
282
|
+
}
|
|
283
|
+
function valueRecord2(value) {
|
|
284
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
285
|
+
return {};
|
|
286
|
+
const record = {};
|
|
287
|
+
for (const [key, item] of Object.entries(value))
|
|
288
|
+
record[key] = item;
|
|
289
|
+
return record;
|
|
290
|
+
}
|
|
291
|
+
function responseRecord(value, label) {
|
|
292
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
293
|
+
throw new Error(`Invalid ${label} response`);
|
|
294
|
+
}
|
|
295
|
+
return valueRecord2(value);
|
|
296
|
+
}
|
|
297
|
+
function responseString(record, key, label) {
|
|
298
|
+
const value = record[key];
|
|
299
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
300
|
+
throw new Error(`Invalid ${label} response: ${key} is required`);
|
|
301
|
+
}
|
|
302
|
+
return value;
|
|
303
|
+
}
|
|
304
|
+
function responseNumber(record, key, label) {
|
|
305
|
+
const value = record[key];
|
|
306
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
307
|
+
throw new Error(`Invalid ${label} response: ${key} is required`);
|
|
308
|
+
}
|
|
309
|
+
return value;
|
|
310
|
+
}
|
|
311
|
+
function responseBoolean(record, key, label) {
|
|
312
|
+
const value = record[key];
|
|
313
|
+
if (typeof value !== "boolean") {
|
|
314
|
+
throw new Error(`Invalid ${label} response: ${key} is required`);
|
|
315
|
+
}
|
|
316
|
+
return value;
|
|
317
|
+
}
|
|
318
|
+
function decodeVoid(value) {
|
|
319
|
+
if (value !== undefined)
|
|
320
|
+
throw new Error("Expected an empty response");
|
|
321
|
+
}
|
|
322
|
+
function decodeTaskDetail(value) {
|
|
323
|
+
const record = responseRecord(value, "task");
|
|
324
|
+
return {
|
|
325
|
+
...record,
|
|
326
|
+
id: responseString(record, "id", "task"),
|
|
327
|
+
status: responseString(record, "status", "task")
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
function decodeTaskDetails(value) {
|
|
331
|
+
if (!Array.isArray(value))
|
|
332
|
+
throw new Error("Invalid task list response");
|
|
333
|
+
return value.map(decodeTaskDetail);
|
|
334
|
+
}
|
|
335
|
+
function decodeQueueMessage(value) {
|
|
336
|
+
const record = responseRecord(value, "queue message");
|
|
337
|
+
const messageValue = record.message ?? record.payload;
|
|
338
|
+
const message = valueRecord2(messageValue);
|
|
339
|
+
return {
|
|
340
|
+
...record,
|
|
341
|
+
id: responseString(record, "id", "queue message"),
|
|
342
|
+
msg_id: responseNumber(record, "msg_id", "queue message"),
|
|
343
|
+
message,
|
|
344
|
+
payload: message
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
function decodeQueueMessages(value) {
|
|
348
|
+
if (!Array.isArray(value))
|
|
349
|
+
throw new Error("Invalid queue message list response");
|
|
350
|
+
return value.map(decodeQueueMessage);
|
|
351
|
+
}
|
|
352
|
+
function decodeQueueStats(value) {
|
|
353
|
+
const record = responseRecord(value, "queue stats");
|
|
354
|
+
return {
|
|
355
|
+
...record,
|
|
356
|
+
queue_name: responseString(record, "queue_name", "queue stats"),
|
|
357
|
+
queue_length: responseNumber(record, "queue_length", "queue stats"),
|
|
358
|
+
newest_msg_age_sec: record.newest_msg_age_sec === null ? null : responseNumber(record, "newest_msg_age_sec", "queue stats"),
|
|
359
|
+
oldest_msg_age_sec: record.oldest_msg_age_sec === null ? null : responseNumber(record, "oldest_msg_age_sec", "queue stats"),
|
|
360
|
+
total_messages: responseNumber(record, "total_messages", "queue stats"),
|
|
361
|
+
scrape_time: responseString(record, "scrape_time", "queue stats")
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
function decodeQueueSettings(value) {
|
|
365
|
+
const record = responseRecord(value, "queue settings");
|
|
366
|
+
return {
|
|
367
|
+
max_in_flight: responseNumber(record, "max_in_flight", "queue settings"),
|
|
368
|
+
default_visibility_timeout_sec: responseNumber(record, "default_visibility_timeout_sec", "queue settings"),
|
|
369
|
+
max_attempts: responseNumber(record, "max_attempts", "queue settings"),
|
|
370
|
+
rate_limit_per_minute: responseNumber(record, "rate_limit_per_minute", "queue settings")
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
function decodeQueueInfo(value) {
|
|
374
|
+
const record = responseRecord(value, "queue info");
|
|
375
|
+
return {
|
|
376
|
+
...record,
|
|
377
|
+
queue_name: responseString(record, "queue_name", "queue info")
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
function decodeQueueInfos(value) {
|
|
381
|
+
if (!Array.isArray(value))
|
|
382
|
+
throw new Error("Invalid queue info list response");
|
|
383
|
+
return value.map(decodeQueueInfo);
|
|
384
|
+
}
|
|
385
|
+
function decodeOAuthServerStatus(value) {
|
|
386
|
+
const record = responseRecord(value, "OAuth Server");
|
|
387
|
+
return {
|
|
388
|
+
...record,
|
|
389
|
+
project_ref: responseString(record, "project_ref", "OAuth Server"),
|
|
390
|
+
account_isolated: responseBoolean(record, "account_isolated", "OAuth Server"),
|
|
391
|
+
enabled: responseBoolean(record, "enabled", "OAuth Server"),
|
|
392
|
+
allow_dynamic_registration: responseBoolean(record, "allow_dynamic_registration", "OAuth Server"),
|
|
393
|
+
issuer: responseString(record, "issuer", "OAuth Server"),
|
|
394
|
+
discovery_url: responseString(record, "discovery_url", "OAuth Server"),
|
|
395
|
+
jwks_url: responseString(record, "jwks_url", "OAuth Server"),
|
|
396
|
+
authorization_endpoint: responseString(record, "authorization_endpoint", "OAuth Server"),
|
|
397
|
+
token_endpoint: responseString(record, "token_endpoint", "OAuth Server")
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
function decodeOAuthClient(value) {
|
|
401
|
+
const record = responseRecord(value, "OAuth client");
|
|
402
|
+
return {
|
|
403
|
+
...record,
|
|
404
|
+
client_id: responseString(record, "client_id", "OAuth client")
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
function decodeOAuthClientList(value) {
|
|
408
|
+
const record = responseRecord(value, "OAuth client list");
|
|
409
|
+
const clients = record.clients;
|
|
410
|
+
if (clients === undefined)
|
|
411
|
+
return record;
|
|
412
|
+
if (!Array.isArray(clients))
|
|
413
|
+
throw new Error("Invalid OAuth client list response");
|
|
414
|
+
return { ...record, clients: clients.map(decodeOAuthClient) };
|
|
415
|
+
}
|
|
416
|
+
function decodeSupAuthStepResult(value) {
|
|
417
|
+
const record = responseRecord(value, "SupAuth step");
|
|
418
|
+
return {
|
|
419
|
+
...record,
|
|
420
|
+
step: responseString(record, "step", "SupAuth step"),
|
|
421
|
+
status: responseString(record, "status", "SupAuth step")
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
function decodeSupAuthProvisionResult(value) {
|
|
425
|
+
const record = responseRecord(value, "SupAuth provision");
|
|
426
|
+
const steps = record.steps;
|
|
427
|
+
if (steps !== undefined && !Array.isArray(steps)) {
|
|
428
|
+
throw new Error("Invalid SupAuth provision response");
|
|
429
|
+
}
|
|
430
|
+
return {
|
|
431
|
+
...record,
|
|
432
|
+
projectRef: responseString(record, "projectRef", "SupAuth provision"),
|
|
433
|
+
status: responseString(record, "status", "SupAuth provision"),
|
|
434
|
+
...steps === undefined ? {} : { steps: steps.map(decodeSupAuthStepResult) }
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
function decodeSupAuthReconcileResult(value) {
|
|
438
|
+
const record = responseRecord(value, "SupAuth reconcile");
|
|
439
|
+
const steps = record.steps;
|
|
440
|
+
if (steps !== undefined && !Array.isArray(steps)) {
|
|
441
|
+
throw new Error("Invalid SupAuth reconcile response");
|
|
442
|
+
}
|
|
443
|
+
return {
|
|
444
|
+
...record,
|
|
445
|
+
projectRef: responseString(record, "projectRef", "SupAuth reconcile"),
|
|
446
|
+
changed: responseBoolean(record, "changed", "SupAuth reconcile"),
|
|
447
|
+
...steps === undefined ? {} : { steps: steps.map(decodeSupAuthStepResult) }
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
function decodeSupAuthRollbackResult(value) {
|
|
451
|
+
const record = responseRecord(value, "SupAuth rollback");
|
|
452
|
+
const steps = record.steps;
|
|
453
|
+
if (steps !== undefined && !Array.isArray(steps)) {
|
|
454
|
+
throw new Error("Invalid SupAuth rollback response");
|
|
455
|
+
}
|
|
456
|
+
return {
|
|
457
|
+
...record,
|
|
458
|
+
projectRef: responseString(record, "projectRef", "SupAuth rollback"),
|
|
459
|
+
status: responseString(record, "status", "SupAuth rollback"),
|
|
460
|
+
...steps === undefined ? {} : { steps: steps.map(decodeSupAuthStepResult) }
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
function decodeSupAuthClientConfig(value) {
|
|
464
|
+
const record = responseRecord(value, "SupAuth client config");
|
|
465
|
+
return {
|
|
466
|
+
...record,
|
|
467
|
+
projectRef: responseString(record, "projectRef", "SupAuth client config"),
|
|
468
|
+
supabaseUrl: responseString(record, "supabaseUrl", "SupAuth client config"),
|
|
469
|
+
authUrl: responseString(record, "authUrl", "SupAuth client config"),
|
|
470
|
+
restUrl: responseString(record, "restUrl", "SupAuth client config"),
|
|
471
|
+
storageUrl: responseString(record, "storageUrl", "SupAuth client config"),
|
|
472
|
+
realtimeUrl: responseString(record, "realtimeUrl", "SupAuth client config"),
|
|
473
|
+
functionsUrl: responseString(record, "functionsUrl", "SupAuth client config")
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
function decodeSupAuthVerification(value) {
|
|
477
|
+
const record = responseRecord(value, "SupAuth verification");
|
|
478
|
+
const checks = record.checks;
|
|
479
|
+
if (!isUnknownArray(checks))
|
|
480
|
+
throw new Error("Invalid SupAuth verification response");
|
|
481
|
+
return {
|
|
482
|
+
...record,
|
|
483
|
+
projectRef: responseString(record, "projectRef", "SupAuth verification"),
|
|
484
|
+
healthy: responseBoolean(record, "healthy", "SupAuth verification"),
|
|
485
|
+
checks: checks.map((check) => {
|
|
486
|
+
const record2 = responseRecord(check, "SupAuth check");
|
|
487
|
+
return {
|
|
488
|
+
...record2,
|
|
489
|
+
name: responseString(record2, "name", "SupAuth check"),
|
|
490
|
+
status: responseString(record2, "status", "SupAuth check")
|
|
491
|
+
};
|
|
492
|
+
})
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
function decodePurgeResult(value) {
|
|
496
|
+
const record = responseRecord(value, "queue purge");
|
|
497
|
+
return {
|
|
498
|
+
queue_name: responseString(record, "queue_name", "queue purge"),
|
|
499
|
+
purged: responseNumber(record, "purged", "queue purge")
|
|
500
|
+
};
|
|
271
501
|
}
|
|
272
502
|
function normalizeRpcMessage(queueName, value, status) {
|
|
273
503
|
const row = firstRpcValue(value);
|
|
274
504
|
if (!row || typeof row !== "object")
|
|
275
505
|
return null;
|
|
276
|
-
const record = row;
|
|
277
|
-
const message = record.message && typeof record.message === "object" && !Array.isArray(record.message) ? record.message : {};
|
|
506
|
+
const record = valueRecord2(row);
|
|
507
|
+
const message = record.message && typeof record.message === "object" && !Array.isArray(record.message) ? valueRecord2(record.message) : {};
|
|
278
508
|
const msgId = normalizeMessageId(record.msg_id ?? record.id);
|
|
279
509
|
return {
|
|
280
510
|
...record,
|
|
@@ -288,25 +518,25 @@ function normalizeRpcMessage(queueName, value, status) {
|
|
|
288
518
|
};
|
|
289
519
|
}
|
|
290
520
|
function normalizeRpcMessages(queueName, value, status) {
|
|
291
|
-
const rows =
|
|
521
|
+
const rows = isUnknownArray(value) ? value : value == null ? [] : [value];
|
|
292
522
|
return rows.map((row) => normalizeRpcMessage(queueName, row, status)).filter((row) => Boolean(row));
|
|
293
523
|
}
|
|
294
524
|
function normalizeRpcMessageId(value) {
|
|
295
525
|
const row = firstRpcValue(value);
|
|
296
526
|
if (row && typeof row === "object") {
|
|
297
|
-
const record = row;
|
|
527
|
+
const record = valueRecord2(row);
|
|
298
528
|
return normalizeMessageId(record.msg_id ?? record.send ?? record.send_batch ?? record.id);
|
|
299
529
|
}
|
|
300
530
|
return normalizeMessageId(row);
|
|
301
531
|
}
|
|
302
532
|
function normalizeRpcMessageIds(value) {
|
|
303
|
-
const rows =
|
|
533
|
+
const rows = isUnknownArray(value) ? value : value == null ? [] : [value];
|
|
304
534
|
return rows.map((row) => normalizeRpcMessageId(row)).filter((id) => id > 0);
|
|
305
535
|
}
|
|
306
536
|
function extractErrorCode(body) {
|
|
307
537
|
if (!body || typeof body !== "object")
|
|
308
538
|
return null;
|
|
309
|
-
const code = body.code;
|
|
539
|
+
const code = valueRecord2(body).code;
|
|
310
540
|
return typeof code === "string" ? code : null;
|
|
311
541
|
}
|
|
312
542
|
async function readResponseBody(response) {
|
|
@@ -314,7 +544,8 @@ async function readResponseBody(response) {
|
|
|
314
544
|
if (!text)
|
|
315
545
|
return;
|
|
316
546
|
try {
|
|
317
|
-
|
|
547
|
+
const parsed = JSON.parse(text);
|
|
548
|
+
return parsed;
|
|
318
549
|
} catch {
|
|
319
550
|
return text;
|
|
320
551
|
}
|
|
@@ -326,7 +557,7 @@ async function defaultAccessTokenResolver(supabase) {
|
|
|
326
557
|
return data.session?.access_token ?? null;
|
|
327
558
|
}
|
|
328
559
|
function normalizeTaskSnapshot(task) {
|
|
329
|
-
const value = task ?? {};
|
|
560
|
+
const value = valueRecord2(task ?? {});
|
|
330
561
|
return {
|
|
331
562
|
id: String(value.id ?? ""),
|
|
332
563
|
status: String(value.status ?? "unknown"),
|
|
@@ -349,7 +580,7 @@ class SupaCloudManagementClient {
|
|
|
349
580
|
}
|
|
350
581
|
return token;
|
|
351
582
|
}
|
|
352
|
-
async request(path, method, body) {
|
|
583
|
+
async request(path, method, body, decode) {
|
|
353
584
|
const accessToken = await this.resolveAccessToken();
|
|
354
585
|
const response = await fetch(`${this.options.managementApiUrl}${path}`, {
|
|
355
586
|
method,
|
|
@@ -360,13 +591,14 @@ class SupaCloudManagementClient {
|
|
|
360
591
|
...body !== undefined ? { body: JSON.stringify(body) } : {}
|
|
361
592
|
});
|
|
362
593
|
if (response.status === 204)
|
|
363
|
-
return;
|
|
594
|
+
return decode(undefined);
|
|
364
595
|
const responseBody = await readResponseBody(response);
|
|
365
596
|
if (!response.ok) {
|
|
366
|
-
const
|
|
597
|
+
const responseRecord2 = valueRecord2(responseBody);
|
|
598
|
+
const message = typeof responseRecord2.message === "string" ? responseRecord2.message : `SupaCloud request failed (${response.status})`;
|
|
367
599
|
throw new SupaCloudApiError(message, response.status, responseBody);
|
|
368
600
|
}
|
|
369
|
-
return responseBody;
|
|
601
|
+
return decode(responseBody);
|
|
370
602
|
}
|
|
371
603
|
}
|
|
372
604
|
|
|
@@ -391,14 +623,15 @@ class SupaCloudTasksClient extends SupaCloudManagementClient {
|
|
|
391
623
|
...options.businessTaskId ? { "x-supacloud-business-task-id": options.businessTaskId } : {},
|
|
392
624
|
...options.metadata ? { "x-supacloud-task-metadata": JSON.stringify(options.metadata) } : {}
|
|
393
625
|
};
|
|
394
|
-
const
|
|
626
|
+
const invocation = await this.options.supabase.functions.invoke(functionName, {
|
|
395
627
|
body,
|
|
396
628
|
method,
|
|
397
629
|
headers: invokeHeaders
|
|
398
630
|
});
|
|
631
|
+
const { data, error } = invocation;
|
|
399
632
|
if (error)
|
|
400
633
|
throw error;
|
|
401
|
-
const payload = data ?? {};
|
|
634
|
+
const payload = valueRecord2(data ?? {});
|
|
402
635
|
const taskId = typeof payload.task_id === "string" ? payload.task_id : typeof payload.taskId === "string" ? payload.taskId : null;
|
|
403
636
|
if (!taskId) {
|
|
404
637
|
throw new SupaCloudTaskSubmitError("Background task was not enqueued", data);
|
|
@@ -406,19 +639,19 @@ class SupaCloudTasksClient extends SupaCloudManagementClient {
|
|
|
406
639
|
return this.createReceipt(taskId, String(payload.status ?? "enqueued"));
|
|
407
640
|
}
|
|
408
641
|
async get(taskId) {
|
|
409
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks/${taskId}`, "GET");
|
|
642
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/${taskId}`, "GET", undefined, decodeTaskDetail);
|
|
410
643
|
}
|
|
411
644
|
async list(filters = {}) {
|
|
412
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks${createQueryString(filters)}`, "GET");
|
|
645
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks${createQueryString(filters)}`, "GET", undefined, decodeTaskDetails);
|
|
413
646
|
}
|
|
414
647
|
async cancel(taskId) {
|
|
415
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks/${taskId}/cancel`, "POST");
|
|
648
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/${taskId}/cancel`, "POST", undefined, decodeTaskDetail);
|
|
416
649
|
}
|
|
417
650
|
async retry(taskId) {
|
|
418
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks/${taskId}/retry`, "POST");
|
|
651
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/${taskId}/retry`, "POST", undefined, decodeTaskDetail);
|
|
419
652
|
}
|
|
420
653
|
async listDlq(limit = 100) {
|
|
421
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks/dlq?limit=${limit}`, "GET");
|
|
654
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/dlq?limit=${limit}`, "GET", undefined, decodeTaskDetails);
|
|
422
655
|
}
|
|
423
656
|
async wait(taskId, options = {}) {
|
|
424
657
|
const intervalMs = options.intervalMs ?? this.options.pollingIntervalMs;
|
|
@@ -598,19 +831,20 @@ class SupaCloudQueueClient extends SupaCloudManagementClient {
|
|
|
598
831
|
get encodedName() {
|
|
599
832
|
return encodeURIComponent(this.name);
|
|
600
833
|
}
|
|
601
|
-
async rpc(fn, params) {
|
|
834
|
+
async rpc(fn, params, decode) {
|
|
602
835
|
const scopedClient = this.options.supabase.schema("pgmq_public");
|
|
603
|
-
const
|
|
836
|
+
const rpcResult = await scopedClient.rpc(fn, params);
|
|
837
|
+
const { data, error } = rpcResult;
|
|
604
838
|
if (error)
|
|
605
839
|
throw error;
|
|
606
|
-
return data;
|
|
840
|
+
return decode(data);
|
|
607
841
|
}
|
|
608
842
|
async send(payload = {}, options = {}) {
|
|
609
|
-
const msgId =
|
|
843
|
+
const msgId = await this.rpc("send", {
|
|
610
844
|
queue_name: this.name,
|
|
611
845
|
message: payload,
|
|
612
846
|
sleep_seconds: normalizeSecondsFromOptions(options)
|
|
613
|
-
})
|
|
847
|
+
}, normalizeRpcMessageId);
|
|
614
848
|
return {
|
|
615
849
|
id: String(msgId),
|
|
616
850
|
msg_id: msgId,
|
|
@@ -620,11 +854,11 @@ class SupaCloudQueueClient extends SupaCloudManagementClient {
|
|
|
620
854
|
};
|
|
621
855
|
}
|
|
622
856
|
async sendBatch(messages, options = {}) {
|
|
623
|
-
const ids =
|
|
857
|
+
const ids = await this.rpc("send_batch", {
|
|
624
858
|
queue_name: this.name,
|
|
625
859
|
messages,
|
|
626
860
|
sleep_seconds: normalizeSecondsFromOptions(options)
|
|
627
|
-
})
|
|
861
|
+
}, normalizeRpcMessageIds);
|
|
628
862
|
return ids.map((msgId, index) => ({
|
|
629
863
|
id: String(msgId),
|
|
630
864
|
msg_id: msgId,
|
|
@@ -634,21 +868,21 @@ class SupaCloudQueueClient extends SupaCloudManagementClient {
|
|
|
634
868
|
}));
|
|
635
869
|
}
|
|
636
870
|
async read(options = {}) {
|
|
637
|
-
return
|
|
871
|
+
return this.rpc("read", {
|
|
638
872
|
queue_name: this.name,
|
|
639
873
|
sleep_seconds: normalizeSecondsFromOptions(options),
|
|
640
874
|
n: Math.max(1, Math.floor(options.n ?? options.count ?? 1))
|
|
641
|
-
}), "leased");
|
|
875
|
+
}, (value) => normalizeRpcMessages(this.name, value, "leased"));
|
|
642
876
|
}
|
|
643
877
|
async receive(options = {}) {
|
|
644
878
|
const messages = await this.read({ ...options, n: 1 });
|
|
645
879
|
return messages[0] ?? null;
|
|
646
880
|
}
|
|
647
881
|
async pop() {
|
|
648
|
-
return
|
|
882
|
+
return this.rpc("pop", { queue_name: this.name }, (value) => normalizeRpcMessage(this.name, value, "deleted"));
|
|
649
883
|
}
|
|
650
884
|
async list(filters = {}) {
|
|
651
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/messages${createQueueQueryString(filters)}`, "GET");
|
|
885
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/messages${createQueueQueryString(filters)}`, "GET", undefined, decodeQueueMessages);
|
|
652
886
|
}
|
|
653
887
|
async listFailed(limit = 100) {
|
|
654
888
|
return this.list({ archived: true, dlq: true, limit });
|
|
@@ -657,20 +891,20 @@ class SupaCloudQueueClient extends SupaCloudManagementClient {
|
|
|
657
891
|
return this.list({ archived: true, limit });
|
|
658
892
|
}
|
|
659
893
|
async stats() {
|
|
660
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/stats`, "GET");
|
|
894
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/stats`, "GET", undefined, decodeQueueStats);
|
|
661
895
|
}
|
|
662
896
|
async getSettings() {
|
|
663
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/settings`, "GET");
|
|
897
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/settings`, "GET", undefined, decodeQueueSettings);
|
|
664
898
|
}
|
|
665
899
|
async updateSettings(settings) {
|
|
666
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/settings`, "PATCH", settings);
|
|
900
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/settings`, "PATCH", settings, decodeQueueSettings);
|
|
667
901
|
}
|
|
668
902
|
async archive(messageId) {
|
|
669
903
|
const msgId = normalizeMessageId(messageId);
|
|
670
|
-
const success =
|
|
904
|
+
const success = await this.rpc("archive", {
|
|
671
905
|
queue_name: this.name,
|
|
672
906
|
message_id: msgId
|
|
673
|
-
})));
|
|
907
|
+
}, (value) => Boolean(firstRpcValue(value)));
|
|
674
908
|
return { id: String(msgId), msg_id: msgId, queue_name: this.name, status: "archived", success };
|
|
675
909
|
}
|
|
676
910
|
async ack(messageId) {
|
|
@@ -678,67 +912,69 @@ class SupaCloudQueueClient extends SupaCloudManagementClient {
|
|
|
678
912
|
}
|
|
679
913
|
async delete(messageId) {
|
|
680
914
|
const msgId = normalizeMessageId(messageId);
|
|
681
|
-
const success =
|
|
915
|
+
const success = await this.rpc("delete", {
|
|
682
916
|
queue_name: this.name,
|
|
683
917
|
message_id: msgId
|
|
684
|
-
})));
|
|
918
|
+
}, (value) => Boolean(firstRpcValue(value)));
|
|
685
919
|
return { id: String(msgId), msg_id: msgId, queue_name: this.name, status: "deleted", success };
|
|
686
920
|
}
|
|
687
921
|
async release(messageId, options = {}) {
|
|
688
922
|
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/messages/${messageId}/release`, "POST", {
|
|
689
923
|
sleep_seconds: normalizeSecondsFromOptions(options),
|
|
690
924
|
...options.error ? { error: options.error } : {}
|
|
691
|
-
});
|
|
925
|
+
}, decodeQueueMessage);
|
|
692
926
|
}
|
|
693
927
|
async purge() {
|
|
694
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/purge`, "POST");
|
|
928
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/purge`, "POST", undefined, decodePurgeResult);
|
|
695
929
|
}
|
|
696
930
|
async fail(messageId, _options = {}) {
|
|
697
931
|
return this.archive(messageId);
|
|
698
932
|
}
|
|
699
933
|
async get(messageId) {
|
|
700
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/messages/${messageId}`, "GET");
|
|
934
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/messages/${messageId}`, "GET", undefined, decodeQueueMessage);
|
|
701
935
|
}
|
|
702
936
|
async retry(messageId) {
|
|
703
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/messages/${messageId}/retry`, "POST");
|
|
937
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/messages/${messageId}/retry`, "POST", undefined, decodeQueueMessage);
|
|
704
938
|
}
|
|
705
939
|
}
|
|
706
940
|
|
|
707
941
|
class SupaCloudQueuesClient extends SupaCloudManagementClient {
|
|
708
942
|
async list() {
|
|
709
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues`, "GET");
|
|
943
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues`, "GET", undefined, decodeQueueInfos);
|
|
710
944
|
}
|
|
711
945
|
async create(queueName, options = {}) {
|
|
712
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues`, "POST", { queue_name: queueName, unlogged: options.unlogged });
|
|
946
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues`, "POST", { queue_name: queueName, unlogged: options.unlogged }, decodeQueueInfo);
|
|
713
947
|
}
|
|
714
948
|
async drop(queueName) {
|
|
715
|
-
await this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${encodeURIComponent(queueName)}`, "DELETE");
|
|
949
|
+
await this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${encodeURIComponent(queueName)}`, "DELETE", undefined, decodeVoid);
|
|
716
950
|
}
|
|
717
951
|
}
|
|
718
952
|
|
|
719
953
|
class SupaCloudOAuthServerClient extends SupaCloudManagementClient {
|
|
720
954
|
async getStatus() {
|
|
721
|
-
return this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-server`, "GET");
|
|
955
|
+
return this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-server`, "GET", undefined, decodeOAuthServerStatus);
|
|
722
956
|
}
|
|
723
957
|
async migrateToOidc(options = {}) {
|
|
724
958
|
return this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-server/migrate`, "POST", {
|
|
725
959
|
allow_dynamic_registration: options.allowDynamicRegistration === true,
|
|
726
960
|
...options.authorizationPath === undefined ? {} : { authorization_path: options.authorizationPath }
|
|
727
|
-
});
|
|
961
|
+
}, decodeOAuthServerStatus);
|
|
728
962
|
}
|
|
729
963
|
async getDiscovery() {
|
|
730
964
|
const status = await this.getStatus();
|
|
731
965
|
const response = await fetch(status.discovery_url);
|
|
732
966
|
if (!response.ok)
|
|
733
967
|
throw new Error(`SupaCloud OIDC discovery failed (${response.status})`);
|
|
734
|
-
|
|
968
|
+
const payload = await response.json();
|
|
969
|
+
return valueRecord2(payload);
|
|
735
970
|
}
|
|
736
971
|
async getJwks() {
|
|
737
972
|
const status = await this.getStatus();
|
|
738
973
|
const response = await fetch(status.jwks_url);
|
|
739
974
|
if (!response.ok)
|
|
740
975
|
throw new Error(`SupaCloud JWKS fetch failed (${response.status})`);
|
|
741
|
-
|
|
976
|
+
const payload = await response.json();
|
|
977
|
+
return valueRecord2(payload);
|
|
742
978
|
}
|
|
743
979
|
async buildAuthorizeUrl(options) {
|
|
744
980
|
const status = await this.getStatus();
|
|
@@ -765,22 +1001,22 @@ class SupaCloudOAuthServerClient extends SupaCloudManagementClient {
|
|
|
765
1001
|
|
|
766
1002
|
class SupaCloudOAuthClientsClient extends SupaCloudManagementClient {
|
|
767
1003
|
async list() {
|
|
768
|
-
return this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-clients`, "GET");
|
|
1004
|
+
return this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-clients`, "GET", undefined, decodeOAuthClientList);
|
|
769
1005
|
}
|
|
770
1006
|
async create(input) {
|
|
771
|
-
return this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-clients`, "POST", input);
|
|
1007
|
+
return this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-clients`, "POST", input, decodeOAuthClient);
|
|
772
1008
|
}
|
|
773
1009
|
async get(clientId) {
|
|
774
|
-
return this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-clients/${encodeURIComponent(clientId)}`, "GET");
|
|
1010
|
+
return this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-clients/${encodeURIComponent(clientId)}`, "GET", undefined, decodeOAuthClient);
|
|
775
1011
|
}
|
|
776
1012
|
async update(clientId, patch) {
|
|
777
|
-
return this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-clients/${encodeURIComponent(clientId)}`, "PUT", patch);
|
|
1013
|
+
return this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-clients/${encodeURIComponent(clientId)}`, "PUT", patch, decodeOAuthClient);
|
|
778
1014
|
}
|
|
779
1015
|
async delete(clientId) {
|
|
780
|
-
await this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-clients/${encodeURIComponent(clientId)}`, "DELETE");
|
|
1016
|
+
await this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-clients/${encodeURIComponent(clientId)}`, "DELETE", undefined, decodeVoid);
|
|
781
1017
|
}
|
|
782
1018
|
async regenerateSecret(clientId) {
|
|
783
|
-
return this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-clients/${encodeURIComponent(clientId)}/regenerate-secret`, "POST");
|
|
1019
|
+
return this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-clients/${encodeURIComponent(clientId)}/regenerate-secret`, "POST", undefined, decodeOAuthClient);
|
|
784
1020
|
}
|
|
785
1021
|
}
|
|
786
1022
|
|
|
@@ -789,19 +1025,19 @@ class SupaCloudSupAuthClient extends SupaCloudManagementClient {
|
|
|
789
1025
|
return `/v1/projects/${this.options.projectRef}/supauth`;
|
|
790
1026
|
}
|
|
791
1027
|
async provision(options = {}) {
|
|
792
|
-
return this.request(`${this.basePath}/provision`, "POST", options);
|
|
1028
|
+
return this.request(`${this.basePath}/provision`, "POST", options, decodeSupAuthProvisionResult);
|
|
793
1029
|
}
|
|
794
1030
|
async reconcile(options = {}) {
|
|
795
|
-
return this.request(`${this.basePath}/reconcile`, "POST", options);
|
|
1031
|
+
return this.request(`${this.basePath}/reconcile`, "POST", options, decodeSupAuthReconcileResult);
|
|
796
1032
|
}
|
|
797
1033
|
async rollback() {
|
|
798
|
-
return this.request(`${this.basePath}/rollback`, "POST");
|
|
1034
|
+
return this.request(`${this.basePath}/rollback`, "POST", undefined, decodeSupAuthRollbackResult);
|
|
799
1035
|
}
|
|
800
1036
|
async getClientConfig() {
|
|
801
|
-
return this.request(`${this.basePath}/client-config`, "GET");
|
|
1037
|
+
return this.request(`${this.basePath}/client-config`, "GET", undefined, decodeSupAuthClientConfig);
|
|
802
1038
|
}
|
|
803
1039
|
async verify() {
|
|
804
|
-
return this.request(`${this.basePath}/verify`, "GET");
|
|
1040
|
+
return this.request(`${this.basePath}/verify`, "GET", undefined, decodeSupAuthVerification);
|
|
805
1041
|
}
|
|
806
1042
|
}
|
|
807
1043
|
function createSupaCloudClient(options) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@supacloud/js",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.25.0",
|
|
4
4
|
"description": "Platform SDK for SupaCloud, built on top of supabase-js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -42,11 +42,11 @@
|
|
|
42
42
|
"directory": "packages/supacloud-js"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
|
-
"@supabase/supabase-js": "^2.
|
|
45
|
+
"@supabase/supabase-js": "^2.115.0"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"@types/bun": "^1.4.0",
|
|
49
|
-
"@supabase/supabase-js": "^2.
|
|
49
|
+
"@supabase/supabase-js": "^2.115.0",
|
|
50
50
|
"typescript": "^7.0.2"
|
|
51
51
|
}
|
|
52
52
|
}
|