@supacloud/js 0.24.1 → 0.29.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/README.md +62 -0
- package/dist/commands.d.ts +17 -16
- package/dist/index.d.ts +115 -22
- package/dist/index.js +551 -76
- package/package.json +8 -5
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;
|
|
@@ -49,18 +49,44 @@ class SupaCloudWorkflowsClient {
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
// src/commands.ts
|
|
52
|
+
import { decodeCommandStatus } from "@supacloud/contracts";
|
|
53
|
+
|
|
52
54
|
class SupaCloudCommandsClient {
|
|
53
55
|
supabase;
|
|
54
56
|
constructor(supabase) {
|
|
55
57
|
this.supabase = supabase;
|
|
56
58
|
}
|
|
57
|
-
submit(request) {
|
|
58
|
-
|
|
59
|
+
async submit(request) {
|
|
60
|
+
const raw = await this.request("supacloud_command_submit", request);
|
|
61
|
+
const status = decodeCommandStatus(raw);
|
|
62
|
+
if (status.commandId !== request.commandId.toLowerCase())
|
|
63
|
+
throw new TypeError("Invalid command submission");
|
|
64
|
+
return status;
|
|
65
|
+
}
|
|
66
|
+
async get(lookup) {
|
|
67
|
+
const request = typeof lookup === "string" ? { commandId: lookup } : { ...lookup };
|
|
68
|
+
const raw = await this.request("supacloud_command_get", request);
|
|
69
|
+
if (raw === null)
|
|
70
|
+
return null;
|
|
71
|
+
const status = decodeCommandStatus(raw);
|
|
72
|
+
if ("commandId" in request) {
|
|
73
|
+
if (status.commandId !== request.commandId.toLowerCase())
|
|
74
|
+
throw new TypeError("Mismatched command status");
|
|
75
|
+
} else {
|
|
76
|
+
if (status.kind !== "execution")
|
|
77
|
+
throw new TypeError("Missing command execution");
|
|
78
|
+
for (const key of ["tenantId", "actorId", "command", "operationId"]) {
|
|
79
|
+
if (status.execution[key] !== request[key])
|
|
80
|
+
throw new TypeError("Mismatched command reference");
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return status;
|
|
59
84
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
85
|
+
async request(functionName, request) {
|
|
86
|
+
const result = await this.supabase.rpc(functionName, { request });
|
|
87
|
+
if (result.error)
|
|
88
|
+
throw result.error;
|
|
89
|
+
return result.data;
|
|
64
90
|
}
|
|
65
91
|
}
|
|
66
92
|
|
|
@@ -102,20 +128,28 @@ function parseRefreshBody(body, contentType) {
|
|
|
102
128
|
}
|
|
103
129
|
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
104
130
|
return null;
|
|
105
|
-
const record = value;
|
|
131
|
+
const record = valueRecord(value);
|
|
106
132
|
const refreshToken = typeof record.refresh_token === "string" ? record.refresh_token.trim() : "";
|
|
107
133
|
if (!refreshToken)
|
|
108
134
|
return null;
|
|
109
|
-
const
|
|
135
|
+
const params = new URLSearchParams;
|
|
110
136
|
for (const [key, item] of Object.entries(record)) {
|
|
111
137
|
if (typeof item === "string")
|
|
112
|
-
|
|
138
|
+
params.set(key, item);
|
|
113
139
|
}
|
|
114
|
-
return
|
|
140
|
+
return params;
|
|
115
141
|
}
|
|
116
142
|
const params = new URLSearchParams(body);
|
|
117
143
|
return params.get("refresh_token")?.trim() ? params : null;
|
|
118
144
|
}
|
|
145
|
+
function valueRecord(value) {
|
|
146
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
147
|
+
return {};
|
|
148
|
+
const record = {};
|
|
149
|
+
for (const [key, item] of Object.entries(value))
|
|
150
|
+
record[key] = item;
|
|
151
|
+
return record;
|
|
152
|
+
}
|
|
119
153
|
function replacementRequest(request, tokenEndpoint, body) {
|
|
120
154
|
const headers = new Headers(request.headers);
|
|
121
155
|
headers.set("content-type", "application/x-www-form-urlencoded");
|
|
@@ -164,6 +198,8 @@ function createSupaCloudOAuthFetch(options = {}) {
|
|
|
164
198
|
}
|
|
165
199
|
|
|
166
200
|
// src/index.ts
|
|
201
|
+
var SUPACLOUD_JS_VERSION = "0.27.1";
|
|
202
|
+
|
|
167
203
|
class SupaCloudTaskSubmitError extends Error {
|
|
168
204
|
responseBody;
|
|
169
205
|
constructor(message, responseBody) {
|
|
@@ -185,6 +221,22 @@ class SupaCloudApiError extends Error {
|
|
|
185
221
|
this.code = extractErrorCode(responseBody);
|
|
186
222
|
}
|
|
187
223
|
}
|
|
224
|
+
|
|
225
|
+
class SupaCloudTaskDecoderError extends SupaCloudApiError {
|
|
226
|
+
operation;
|
|
227
|
+
code = "TASK_RESULT_INVALID";
|
|
228
|
+
mutationMayHaveApplied;
|
|
229
|
+
constructor(operation) {
|
|
230
|
+
const mutation = operation === "cancel" || operation === "retry";
|
|
231
|
+
super("Task result could not be decoded", 0, {
|
|
232
|
+
code: "TASK_RESULT_INVALID",
|
|
233
|
+
mutation_may_have_applied: mutation
|
|
234
|
+
});
|
|
235
|
+
this.operation = operation;
|
|
236
|
+
this.name = "SupaCloudTaskDecoderError";
|
|
237
|
+
this.mutationMayHaveApplied = mutation;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
188
240
|
var TERMINAL_STATUSES = new Set([
|
|
189
241
|
"succeeded",
|
|
190
242
|
"failed",
|
|
@@ -267,14 +319,286 @@ function normalizeMessageId(value) {
|
|
|
267
319
|
return Number.isSafeInteger(parsed) && parsed > 0 ? parsed : 0;
|
|
268
320
|
}
|
|
269
321
|
function firstRpcValue(value) {
|
|
270
|
-
return
|
|
322
|
+
return isUnknownArray(value) ? value[0] : value;
|
|
323
|
+
}
|
|
324
|
+
function isUnknownArray(value) {
|
|
325
|
+
return Array.isArray(value);
|
|
326
|
+
}
|
|
327
|
+
function valueRecord2(value) {
|
|
328
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
329
|
+
return {};
|
|
330
|
+
const record = {};
|
|
331
|
+
for (const [key, item] of Object.entries(value))
|
|
332
|
+
record[key] = item;
|
|
333
|
+
return record;
|
|
334
|
+
}
|
|
335
|
+
function responseRecord(value, label) {
|
|
336
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
337
|
+
throw new Error(`Invalid ${label} response`);
|
|
338
|
+
}
|
|
339
|
+
return valueRecord2(value);
|
|
340
|
+
}
|
|
341
|
+
function responseString(record, key, label) {
|
|
342
|
+
const value = record[key];
|
|
343
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
344
|
+
throw new Error(`Invalid ${label} response: ${key} is required`);
|
|
345
|
+
}
|
|
346
|
+
return value;
|
|
347
|
+
}
|
|
348
|
+
function responseNumber(record, key, label) {
|
|
349
|
+
const value = record[key];
|
|
350
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
351
|
+
throw new Error(`Invalid ${label} response: ${key} is required`);
|
|
352
|
+
}
|
|
353
|
+
return value;
|
|
354
|
+
}
|
|
355
|
+
function responseBoolean(record, key, label) {
|
|
356
|
+
const value = record[key];
|
|
357
|
+
if (typeof value !== "boolean") {
|
|
358
|
+
throw new Error(`Invalid ${label} response: ${key} is required`);
|
|
359
|
+
}
|
|
360
|
+
return value;
|
|
361
|
+
}
|
|
362
|
+
function decodeVoid(value) {
|
|
363
|
+
if (value !== undefined)
|
|
364
|
+
throw new Error("Expected an empty response");
|
|
365
|
+
}
|
|
366
|
+
function decodeTaskDetail(value) {
|
|
367
|
+
const record = responseRecord(value, "task");
|
|
368
|
+
return {
|
|
369
|
+
...record,
|
|
370
|
+
id: responseString(record, "id", "task"),
|
|
371
|
+
status: responseString(record, "status", "task")
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
function decodeTaskDetails(value) {
|
|
375
|
+
if (!Array.isArray(value))
|
|
376
|
+
throw new Error("Invalid task list response");
|
|
377
|
+
return value.map(decodeTaskDetail);
|
|
378
|
+
}
|
|
379
|
+
function captureTaskDecoder(value) {
|
|
380
|
+
if (typeof value !== "function")
|
|
381
|
+
throw new TypeError("Invalid task result decoder");
|
|
382
|
+
return value;
|
|
383
|
+
}
|
|
384
|
+
function decodeTaskResult(task, decoder, operation) {
|
|
385
|
+
if (!Object.hasOwn(task, "result"))
|
|
386
|
+
return task;
|
|
387
|
+
try {
|
|
388
|
+
return { ...task, result: decoder(task.result) };
|
|
389
|
+
} catch {
|
|
390
|
+
throw new SupaCloudTaskDecoderError(operation);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
function decodeQueueMessage(value) {
|
|
394
|
+
const record = responseRecord(value, "queue message");
|
|
395
|
+
const messageValue = record.message ?? record.payload;
|
|
396
|
+
const message = valueRecord2(messageValue);
|
|
397
|
+
return {
|
|
398
|
+
...record,
|
|
399
|
+
id: responseString(record, "id", "queue message"),
|
|
400
|
+
msg_id: responseNumber(record, "msg_id", "queue message"),
|
|
401
|
+
message,
|
|
402
|
+
payload: message
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
function decodeQueueMessages(value) {
|
|
406
|
+
if (!Array.isArray(value))
|
|
407
|
+
throw new Error("Invalid queue message list response");
|
|
408
|
+
return value.map(decodeQueueMessage);
|
|
409
|
+
}
|
|
410
|
+
function decodeQueueStats(value) {
|
|
411
|
+
const record = responseRecord(value, "queue stats");
|
|
412
|
+
return {
|
|
413
|
+
...record,
|
|
414
|
+
queue_name: responseString(record, "queue_name", "queue stats"),
|
|
415
|
+
queue_length: responseNumber(record, "queue_length", "queue stats"),
|
|
416
|
+
newest_msg_age_sec: record.newest_msg_age_sec === null ? null : responseNumber(record, "newest_msg_age_sec", "queue stats"),
|
|
417
|
+
oldest_msg_age_sec: record.oldest_msg_age_sec === null ? null : responseNumber(record, "oldest_msg_age_sec", "queue stats"),
|
|
418
|
+
total_messages: responseNumber(record, "total_messages", "queue stats"),
|
|
419
|
+
scrape_time: responseString(record, "scrape_time", "queue stats")
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
function decodeQueueSettings(value) {
|
|
423
|
+
const record = responseRecord(value, "queue settings");
|
|
424
|
+
return {
|
|
425
|
+
max_in_flight: responseNumber(record, "max_in_flight", "queue settings"),
|
|
426
|
+
default_visibility_timeout_sec: responseNumber(record, "default_visibility_timeout_sec", "queue settings"),
|
|
427
|
+
max_attempts: responseNumber(record, "max_attempts", "queue settings"),
|
|
428
|
+
rate_limit_per_minute: responseNumber(record, "rate_limit_per_minute", "queue settings")
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
function decodeQueueInfo(value) {
|
|
432
|
+
const record = responseRecord(value, "queue info");
|
|
433
|
+
return {
|
|
434
|
+
...record,
|
|
435
|
+
queue_name: responseString(record, "queue_name", "queue info")
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
function decodeQueueInfos(value) {
|
|
439
|
+
if (!Array.isArray(value))
|
|
440
|
+
throw new Error("Invalid queue info list response");
|
|
441
|
+
return value.map(decodeQueueInfo);
|
|
442
|
+
}
|
|
443
|
+
function decodeOAuthServerStatus(value) {
|
|
444
|
+
const record = responseRecord(value, "OAuth Server");
|
|
445
|
+
return {
|
|
446
|
+
...record,
|
|
447
|
+
project_ref: responseString(record, "project_ref", "OAuth Server"),
|
|
448
|
+
account_isolated: responseBoolean(record, "account_isolated", "OAuth Server"),
|
|
449
|
+
enabled: responseBoolean(record, "enabled", "OAuth Server"),
|
|
450
|
+
allow_dynamic_registration: responseBoolean(record, "allow_dynamic_registration", "OAuth Server"),
|
|
451
|
+
issuer: responseString(record, "issuer", "OAuth Server"),
|
|
452
|
+
discovery_url: responseString(record, "discovery_url", "OAuth Server"),
|
|
453
|
+
jwks_url: responseString(record, "jwks_url", "OAuth Server"),
|
|
454
|
+
authorization_endpoint: responseString(record, "authorization_endpoint", "OAuth Server"),
|
|
455
|
+
token_endpoint: responseString(record, "token_endpoint", "OAuth Server")
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
function decodeOAuthClient(value) {
|
|
459
|
+
const record = responseRecord(value, "OAuth client");
|
|
460
|
+
return {
|
|
461
|
+
...record,
|
|
462
|
+
client_id: responseString(record, "client_id", "OAuth client")
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
function decodeOAuthClientList(value) {
|
|
466
|
+
const record = responseRecord(value, "OAuth client list");
|
|
467
|
+
const clients = record.clients;
|
|
468
|
+
if (clients === undefined)
|
|
469
|
+
return record;
|
|
470
|
+
if (!Array.isArray(clients))
|
|
471
|
+
throw new Error("Invalid OAuth client list response");
|
|
472
|
+
return { ...record, clients: clients.map(decodeOAuthClient) };
|
|
473
|
+
}
|
|
474
|
+
function decodeSupAuthStepResult(value) {
|
|
475
|
+
const record = responseRecord(value, "SupAuth step");
|
|
476
|
+
return {
|
|
477
|
+
...record,
|
|
478
|
+
step: responseString(record, "step", "SupAuth step"),
|
|
479
|
+
status: responseString(record, "status", "SupAuth step")
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
function decodeSupAuthProvisionResult(value) {
|
|
483
|
+
const record = responseRecord(value, "SupAuth provision");
|
|
484
|
+
const steps = record.steps;
|
|
485
|
+
if (steps !== undefined && !Array.isArray(steps)) {
|
|
486
|
+
throw new Error("Invalid SupAuth provision response");
|
|
487
|
+
}
|
|
488
|
+
return {
|
|
489
|
+
...record,
|
|
490
|
+
projectRef: responseString(record, "projectRef", "SupAuth provision"),
|
|
491
|
+
status: responseString(record, "status", "SupAuth provision"),
|
|
492
|
+
...steps === undefined ? {} : { steps: steps.map(decodeSupAuthStepResult) }
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
function decodeSupAuthReconcileResult(value) {
|
|
496
|
+
const record = responseRecord(value, "SupAuth reconcile");
|
|
497
|
+
const steps = record.steps;
|
|
498
|
+
if (steps !== undefined && !Array.isArray(steps)) {
|
|
499
|
+
throw new Error("Invalid SupAuth reconcile response");
|
|
500
|
+
}
|
|
501
|
+
return {
|
|
502
|
+
...record,
|
|
503
|
+
projectRef: responseString(record, "projectRef", "SupAuth reconcile"),
|
|
504
|
+
changed: responseBoolean(record, "changed", "SupAuth reconcile"),
|
|
505
|
+
...steps === undefined ? {} : { steps: steps.map(decodeSupAuthStepResult) }
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
function decodeSupAuthRollbackResult(value) {
|
|
509
|
+
const record = responseRecord(value, "SupAuth rollback");
|
|
510
|
+
const steps = record.steps;
|
|
511
|
+
if (steps !== undefined && !Array.isArray(steps)) {
|
|
512
|
+
throw new Error("Invalid SupAuth rollback response");
|
|
513
|
+
}
|
|
514
|
+
return {
|
|
515
|
+
...record,
|
|
516
|
+
projectRef: responseString(record, "projectRef", "SupAuth rollback"),
|
|
517
|
+
status: responseString(record, "status", "SupAuth rollback"),
|
|
518
|
+
...steps === undefined ? {} : { steps: steps.map(decodeSupAuthStepResult) }
|
|
519
|
+
};
|
|
520
|
+
}
|
|
521
|
+
function decodeSupAuthClientConfig(value) {
|
|
522
|
+
const record = responseRecord(value, "SupAuth client config");
|
|
523
|
+
return {
|
|
524
|
+
...record,
|
|
525
|
+
projectRef: responseString(record, "projectRef", "SupAuth client config"),
|
|
526
|
+
supabaseUrl: responseString(record, "supabaseUrl", "SupAuth client config"),
|
|
527
|
+
authUrl: responseString(record, "authUrl", "SupAuth client config"),
|
|
528
|
+
restUrl: responseString(record, "restUrl", "SupAuth client config"),
|
|
529
|
+
storageUrl: responseString(record, "storageUrl", "SupAuth client config"),
|
|
530
|
+
realtimeUrl: responseString(record, "realtimeUrl", "SupAuth client config"),
|
|
531
|
+
functionsUrl: responseString(record, "functionsUrl", "SupAuth client config")
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
function decodeSupAuthVerification(value) {
|
|
535
|
+
const record = responseRecord(value, "SupAuth verification");
|
|
536
|
+
const checks = record.checks;
|
|
537
|
+
if (!isUnknownArray(checks))
|
|
538
|
+
throw new Error("Invalid SupAuth verification response");
|
|
539
|
+
return {
|
|
540
|
+
...record,
|
|
541
|
+
projectRef: responseString(record, "projectRef", "SupAuth verification"),
|
|
542
|
+
healthy: responseBoolean(record, "healthy", "SupAuth verification"),
|
|
543
|
+
checks: checks.map((check) => {
|
|
544
|
+
const record = responseRecord(check, "SupAuth check");
|
|
545
|
+
return {
|
|
546
|
+
...record,
|
|
547
|
+
name: responseString(record, "name", "SupAuth check"),
|
|
548
|
+
status: responseString(record, "status", "SupAuth check")
|
|
549
|
+
};
|
|
550
|
+
})
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
function decodePurgeResult(value) {
|
|
554
|
+
const record = responseRecord(value, "queue purge");
|
|
555
|
+
return {
|
|
556
|
+
queue_name: responseString(record, "queue_name", "queue purge"),
|
|
557
|
+
purged: responseNumber(record, "purged", "queue purge")
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
function decodeProjectCapabilities(value) {
|
|
561
|
+
const record = responseRecord(value, "project capabilities");
|
|
562
|
+
const rawCapabilities = valueRecord2(record.capabilities);
|
|
563
|
+
const capabilities = {};
|
|
564
|
+
for (const [key, item] of Object.entries(rawCapabilities)) {
|
|
565
|
+
const capRecord = valueRecord2(item);
|
|
566
|
+
capabilities[key] = {
|
|
567
|
+
available: typeof capRecord.available === "boolean" ? capRecord.available : false,
|
|
568
|
+
source: typeof capRecord.source === "string" ? capRecord.source : "unknown",
|
|
569
|
+
version: typeof capRecord.version === "string" ? capRecord.version : null,
|
|
570
|
+
reason_code: typeof capRecord.reason_code === "string" ? capRecord.reason_code : null,
|
|
571
|
+
...typeof capRecord.authority_project_ref === "string" ? { authority_project_ref: capRecord.authority_project_ref } : {},
|
|
572
|
+
...typeof capRecord.managed_by_owner === "boolean" ? { managed_by_owner: capRecord.managed_by_owner } : {}
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
return {
|
|
576
|
+
...record,
|
|
577
|
+
project_ref: responseString(record, "project_ref", "project capabilities"),
|
|
578
|
+
auth_runtime: typeof record.auth_runtime === "string" ? record.auth_runtime : "unknown",
|
|
579
|
+
schema_version: typeof record.schema_version === "number" ? record.schema_version : 1,
|
|
580
|
+
platform_version: typeof record.platform_version === "string" ? record.platform_version : undefined,
|
|
581
|
+
environment: typeof record.environment === "string" ? record.environment : undefined,
|
|
582
|
+
storage_backend: typeof record.storage_backend === "string" ? record.storage_backend : undefined,
|
|
583
|
+
capabilities
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
function decodeStoragePlatformConstraints(value) {
|
|
587
|
+
const record = responseRecord(value, "storage constraints");
|
|
588
|
+
return {
|
|
589
|
+
max_upload_size_bytes: responseNumber(record, "max_upload_size_bytes", "storage constraints"),
|
|
590
|
+
max_upload_size_mb: responseNumber(record, "max_upload_size_mb", "storage constraints"),
|
|
591
|
+
tus_max_size_bytes: responseNumber(record, "tus_max_size_bytes", "storage constraints"),
|
|
592
|
+
tus_chunk_max_size_bytes: responseNumber(record, "tus_chunk_max_size_bytes", "storage constraints"),
|
|
593
|
+
streaming_upload_supported: responseBoolean(record, "streaming_upload_supported", "storage constraints")
|
|
594
|
+
};
|
|
271
595
|
}
|
|
272
596
|
function normalizeRpcMessage(queueName, value, status) {
|
|
273
597
|
const row = firstRpcValue(value);
|
|
274
598
|
if (!row || typeof row !== "object")
|
|
275
599
|
return null;
|
|
276
|
-
const record = row;
|
|
277
|
-
const message = record.message && typeof record.message === "object" && !Array.isArray(record.message) ? record.message : {};
|
|
600
|
+
const record = valueRecord2(row);
|
|
601
|
+
const message = record.message && typeof record.message === "object" && !Array.isArray(record.message) ? valueRecord2(record.message) : {};
|
|
278
602
|
const msgId = normalizeMessageId(record.msg_id ?? record.id);
|
|
279
603
|
return {
|
|
280
604
|
...record,
|
|
@@ -288,25 +612,25 @@ function normalizeRpcMessage(queueName, value, status) {
|
|
|
288
612
|
};
|
|
289
613
|
}
|
|
290
614
|
function normalizeRpcMessages(queueName, value, status) {
|
|
291
|
-
const rows =
|
|
615
|
+
const rows = isUnknownArray(value) ? value : value == null ? [] : [value];
|
|
292
616
|
return rows.map((row) => normalizeRpcMessage(queueName, row, status)).filter((row) => Boolean(row));
|
|
293
617
|
}
|
|
294
618
|
function normalizeRpcMessageId(value) {
|
|
295
619
|
const row = firstRpcValue(value);
|
|
296
620
|
if (row && typeof row === "object") {
|
|
297
|
-
const record = row;
|
|
621
|
+
const record = valueRecord2(row);
|
|
298
622
|
return normalizeMessageId(record.msg_id ?? record.send ?? record.send_batch ?? record.id);
|
|
299
623
|
}
|
|
300
624
|
return normalizeMessageId(row);
|
|
301
625
|
}
|
|
302
626
|
function normalizeRpcMessageIds(value) {
|
|
303
|
-
const rows =
|
|
627
|
+
const rows = isUnknownArray(value) ? value : value == null ? [] : [value];
|
|
304
628
|
return rows.map((row) => normalizeRpcMessageId(row)).filter((id) => id > 0);
|
|
305
629
|
}
|
|
306
630
|
function extractErrorCode(body) {
|
|
307
631
|
if (!body || typeof body !== "object")
|
|
308
632
|
return null;
|
|
309
|
-
const code = body.code;
|
|
633
|
+
const code = valueRecord2(body).code;
|
|
310
634
|
return typeof code === "string" ? code : null;
|
|
311
635
|
}
|
|
312
636
|
async function readResponseBody(response) {
|
|
@@ -314,7 +638,8 @@ async function readResponseBody(response) {
|
|
|
314
638
|
if (!text)
|
|
315
639
|
return;
|
|
316
640
|
try {
|
|
317
|
-
|
|
641
|
+
const parsed = JSON.parse(text);
|
|
642
|
+
return parsed;
|
|
318
643
|
} catch {
|
|
319
644
|
return text;
|
|
320
645
|
}
|
|
@@ -325,15 +650,16 @@ async function defaultAccessTokenResolver(supabase) {
|
|
|
325
650
|
throw error;
|
|
326
651
|
return data.session?.access_token ?? null;
|
|
327
652
|
}
|
|
328
|
-
function normalizeTaskSnapshot(task) {
|
|
329
|
-
const
|
|
653
|
+
function normalizeTaskSnapshot(task, decoder) {
|
|
654
|
+
const raw = decoder ? decodeTaskResult(decodeTaskDetail(task), decoder, "subscribe") : task;
|
|
655
|
+
const value = valueRecord2(raw);
|
|
330
656
|
return {
|
|
331
657
|
id: String(value.id ?? ""),
|
|
332
658
|
status: String(value.status ?? "unknown"),
|
|
333
659
|
progress: typeof value.progress === "number" ? value.progress : null,
|
|
334
660
|
error: typeof value.error === "string" ? value.error : typeof value.error_message === "string" ? value.error_message : null,
|
|
335
661
|
updatedAt: typeof value.updated_at === "string" ? value.updated_at : typeof value.updatedAt === "string" ? value.updatedAt : null,
|
|
336
|
-
raw
|
|
662
|
+
raw
|
|
337
663
|
};
|
|
338
664
|
}
|
|
339
665
|
|
|
@@ -349,24 +675,26 @@ class SupaCloudManagementClient {
|
|
|
349
675
|
}
|
|
350
676
|
return token;
|
|
351
677
|
}
|
|
352
|
-
async request(path, method, body) {
|
|
678
|
+
async request(path, method, body, decode) {
|
|
353
679
|
const accessToken = await this.resolveAccessToken();
|
|
354
680
|
const response = await fetch(`${this.options.managementApiUrl}${path}`, {
|
|
355
681
|
method,
|
|
356
682
|
headers: {
|
|
357
683
|
authorization: `Bearer ${accessToken}`,
|
|
684
|
+
"x-client-info": `supacloud-js/${SUPACLOUD_JS_VERSION}`,
|
|
358
685
|
...body !== undefined ? { "content-type": "application/json" } : {}
|
|
359
686
|
},
|
|
360
687
|
...body !== undefined ? { body: JSON.stringify(body) } : {}
|
|
361
688
|
});
|
|
362
689
|
if (response.status === 204)
|
|
363
|
-
return;
|
|
690
|
+
return decode(undefined);
|
|
364
691
|
const responseBody = await readResponseBody(response);
|
|
365
692
|
if (!response.ok) {
|
|
366
|
-
const
|
|
693
|
+
const responseRecord = valueRecord2(responseBody);
|
|
694
|
+
const message = typeof responseRecord.message === "string" ? responseRecord.message : `SupaCloud request failed (${response.status})`;
|
|
367
695
|
throw new SupaCloudApiError(message, response.status, responseBody);
|
|
368
696
|
}
|
|
369
|
-
return responseBody;
|
|
697
|
+
return decode(responseBody);
|
|
370
698
|
}
|
|
371
699
|
}
|
|
372
700
|
|
|
@@ -379,7 +707,18 @@ class SupaCloudTasksClient extends SupaCloudManagementClient {
|
|
|
379
707
|
wait: (options) => this.wait(taskId, options),
|
|
380
708
|
cancel: () => this.cancel(taskId),
|
|
381
709
|
retry: () => this.retry(taskId),
|
|
382
|
-
subscribe: (options) => this.
|
|
710
|
+
subscribe: (options) => this.subscribeInternal(taskId, options)
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
createTypedReceipt(taskId, status, decoder) {
|
|
714
|
+
return {
|
|
715
|
+
taskId,
|
|
716
|
+
status,
|
|
717
|
+
get: () => this.getTyped(taskId, decoder),
|
|
718
|
+
wait: (options) => this.waitTyped(taskId, decoder, options),
|
|
719
|
+
cancel: () => this.cancelTyped(taskId, decoder),
|
|
720
|
+
retry: () => this.retryTyped(taskId, decoder),
|
|
721
|
+
subscribe: (options) => this.subscribeTyped(taskId, decoder, options)
|
|
383
722
|
};
|
|
384
723
|
}
|
|
385
724
|
async submit(functionName, options = {}) {
|
|
@@ -391,34 +730,66 @@ class SupaCloudTasksClient extends SupaCloudManagementClient {
|
|
|
391
730
|
...options.businessTaskId ? { "x-supacloud-business-task-id": options.businessTaskId } : {},
|
|
392
731
|
...options.metadata ? { "x-supacloud-task-metadata": JSON.stringify(options.metadata) } : {}
|
|
393
732
|
};
|
|
394
|
-
const
|
|
733
|
+
const invocation = await this.options.supabase.functions.invoke(functionName, {
|
|
395
734
|
body,
|
|
396
735
|
method,
|
|
397
736
|
headers: invokeHeaders
|
|
398
737
|
});
|
|
738
|
+
const { data, error } = invocation;
|
|
399
739
|
if (error)
|
|
400
740
|
throw error;
|
|
401
|
-
const payload = data ?? {};
|
|
741
|
+
const payload = valueRecord2(data ?? {});
|
|
402
742
|
const taskId = typeof payload.task_id === "string" ? payload.task_id : typeof payload.taskId === "string" ? payload.taskId : null;
|
|
403
743
|
if (!taskId) {
|
|
404
744
|
throw new SupaCloudTaskSubmitError("Background task was not enqueued", data);
|
|
405
745
|
}
|
|
406
746
|
return this.createReceipt(taskId, String(payload.status ?? "enqueued"));
|
|
407
747
|
}
|
|
748
|
+
async submitTyped(functionName, decoderOrOptions, optionsOrDecoder) {
|
|
749
|
+
let decoder;
|
|
750
|
+
let options;
|
|
751
|
+
if (typeof decoderOrOptions === "function") {
|
|
752
|
+
decoder = captureTaskDecoder(decoderOrOptions);
|
|
753
|
+
options = typeof optionsOrDecoder === "function" ? undefined : optionsOrDecoder;
|
|
754
|
+
} else {
|
|
755
|
+
options = decoderOrOptions;
|
|
756
|
+
decoder = captureTaskDecoder(optionsOrDecoder);
|
|
757
|
+
}
|
|
758
|
+
const receipt = await this.submit(functionName, options);
|
|
759
|
+
return this.createTypedReceipt(receipt.taskId, receipt.status, decoder);
|
|
760
|
+
}
|
|
408
761
|
async get(taskId) {
|
|
409
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks/${taskId}`, "GET");
|
|
762
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/${taskId}`, "GET", undefined, decodeTaskDetail);
|
|
763
|
+
}
|
|
764
|
+
async getTyped(taskId, decoder) {
|
|
765
|
+
const resultDecoder = captureTaskDecoder(decoder);
|
|
766
|
+
return decodeTaskResult(await this.get(taskId), resultDecoder, "get");
|
|
410
767
|
}
|
|
411
768
|
async list(filters = {}) {
|
|
412
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks${createQueryString(filters)}`, "GET");
|
|
769
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks${createQueryString(filters)}`, "GET", undefined, decodeTaskDetails);
|
|
770
|
+
}
|
|
771
|
+
async listTyped(decoderOrFilters, filtersOrDecoder) {
|
|
772
|
+
const decoder = typeof decoderOrFilters === "function" ? captureTaskDecoder(decoderOrFilters) : captureTaskDecoder(filtersOrDecoder);
|
|
773
|
+
const filters = typeof decoderOrFilters === "function" ? filtersOrDecoder : decoderOrFilters;
|
|
774
|
+
return (await this.list(filters)).map((task) => decodeTaskResult(task, decoder, "list"));
|
|
413
775
|
}
|
|
414
776
|
async cancel(taskId) {
|
|
415
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks/${taskId}/cancel`, "POST");
|
|
777
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/${taskId}/cancel`, "POST", undefined, decodeTaskDetail);
|
|
778
|
+
}
|
|
779
|
+
async cancelTyped(taskId, decoder) {
|
|
780
|
+
return decodeTaskResult(await this.cancel(taskId), captureTaskDecoder(decoder), "cancel");
|
|
416
781
|
}
|
|
417
782
|
async retry(taskId) {
|
|
418
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks/${taskId}/retry`, "POST");
|
|
783
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/${taskId}/retry`, "POST", undefined, decodeTaskDetail);
|
|
784
|
+
}
|
|
785
|
+
async retryTyped(taskId, decoder) {
|
|
786
|
+
return decodeTaskResult(await this.retry(taskId), captureTaskDecoder(decoder), "retry");
|
|
419
787
|
}
|
|
420
788
|
async listDlq(limit = 100) {
|
|
421
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks/dlq?limit=${limit}`, "GET");
|
|
789
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/dlq?limit=${limit}`, "GET", undefined, decodeTaskDetails);
|
|
790
|
+
}
|
|
791
|
+
async listDlqTyped(decoder, limit = 100) {
|
|
792
|
+
return this.listTyped({ dlq: true, limit }, captureTaskDecoder(decoder));
|
|
422
793
|
}
|
|
423
794
|
async wait(taskId, options = {}) {
|
|
424
795
|
const intervalMs = options.intervalMs ?? this.options.pollingIntervalMs;
|
|
@@ -432,7 +803,20 @@ class SupaCloudTasksClient extends SupaCloudManagementClient {
|
|
|
432
803
|
await waitForPollingInterval(intervalMs, options.signal);
|
|
433
804
|
}
|
|
434
805
|
}
|
|
806
|
+
async waitTyped(taskId, decoderOrOptions, optionsOrDecoder) {
|
|
807
|
+
const decoder = typeof decoderOrOptions === "function" ? captureTaskDecoder(decoderOrOptions) : captureTaskDecoder(optionsOrDecoder);
|
|
808
|
+
const options = typeof decoderOrOptions === "function" ? optionsOrDecoder : decoderOrOptions;
|
|
809
|
+
return decodeTaskResult(await this.wait(taskId, options), decoder, "wait");
|
|
810
|
+
}
|
|
435
811
|
subscribe(taskId, options) {
|
|
812
|
+
return this.subscribeInternal(taskId, options);
|
|
813
|
+
}
|
|
814
|
+
subscribeTyped(taskId, decoderOrOptions, optionsOrDecoder) {
|
|
815
|
+
const decoder = typeof decoderOrOptions === "function" ? captureTaskDecoder(decoderOrOptions) : captureTaskDecoder(optionsOrDecoder);
|
|
816
|
+
const options = typeof decoderOrOptions === "function" ? optionsOrDecoder : decoderOrOptions;
|
|
817
|
+
return this.subscribeInternal(taskId, options, decoder);
|
|
818
|
+
}
|
|
819
|
+
subscribeInternal(taskId, options, decoder) {
|
|
436
820
|
let closed = false;
|
|
437
821
|
let channel = null;
|
|
438
822
|
let pollTimer = null;
|
|
@@ -485,7 +869,7 @@ class SupaCloudTasksClient extends SupaCloudManagementClient {
|
|
|
485
869
|
closed = true;
|
|
486
870
|
};
|
|
487
871
|
const emitTask = (task) => {
|
|
488
|
-
const snapshot = normalizeTaskSnapshot(task);
|
|
872
|
+
const snapshot = normalizeTaskSnapshot(task, decoder);
|
|
489
873
|
options.onUpdate(snapshot);
|
|
490
874
|
if (stopOnTerminal && TERMINAL_STATUSES.has(snapshot.status)) {
|
|
491
875
|
close();
|
|
@@ -598,19 +982,20 @@ class SupaCloudQueueClient extends SupaCloudManagementClient {
|
|
|
598
982
|
get encodedName() {
|
|
599
983
|
return encodeURIComponent(this.name);
|
|
600
984
|
}
|
|
601
|
-
async rpc(fn, params) {
|
|
985
|
+
async rpc(fn, params, decode) {
|
|
602
986
|
const scopedClient = this.options.supabase.schema("pgmq_public");
|
|
603
|
-
const
|
|
987
|
+
const rpcResult = await scopedClient.rpc(fn, params);
|
|
988
|
+
const { data, error } = rpcResult;
|
|
604
989
|
if (error)
|
|
605
990
|
throw error;
|
|
606
|
-
return data;
|
|
991
|
+
return decode(data);
|
|
607
992
|
}
|
|
608
993
|
async send(payload = {}, options = {}) {
|
|
609
|
-
const msgId =
|
|
994
|
+
const msgId = await this.rpc("send", {
|
|
610
995
|
queue_name: this.name,
|
|
611
996
|
message: payload,
|
|
612
997
|
sleep_seconds: normalizeSecondsFromOptions(options)
|
|
613
|
-
})
|
|
998
|
+
}, normalizeRpcMessageId);
|
|
614
999
|
return {
|
|
615
1000
|
id: String(msgId),
|
|
616
1001
|
msg_id: msgId,
|
|
@@ -620,11 +1005,11 @@ class SupaCloudQueueClient extends SupaCloudManagementClient {
|
|
|
620
1005
|
};
|
|
621
1006
|
}
|
|
622
1007
|
async sendBatch(messages, options = {}) {
|
|
623
|
-
const ids =
|
|
1008
|
+
const ids = await this.rpc("send_batch", {
|
|
624
1009
|
queue_name: this.name,
|
|
625
1010
|
messages,
|
|
626
1011
|
sleep_seconds: normalizeSecondsFromOptions(options)
|
|
627
|
-
})
|
|
1012
|
+
}, normalizeRpcMessageIds);
|
|
628
1013
|
return ids.map((msgId, index) => ({
|
|
629
1014
|
id: String(msgId),
|
|
630
1015
|
msg_id: msgId,
|
|
@@ -634,21 +1019,21 @@ class SupaCloudQueueClient extends SupaCloudManagementClient {
|
|
|
634
1019
|
}));
|
|
635
1020
|
}
|
|
636
1021
|
async read(options = {}) {
|
|
637
|
-
return
|
|
1022
|
+
return this.rpc("read", {
|
|
638
1023
|
queue_name: this.name,
|
|
639
1024
|
sleep_seconds: normalizeSecondsFromOptions(options),
|
|
640
1025
|
n: Math.max(1, Math.floor(options.n ?? options.count ?? 1))
|
|
641
|
-
}), "leased");
|
|
1026
|
+
}, (value) => normalizeRpcMessages(this.name, value, "leased"));
|
|
642
1027
|
}
|
|
643
1028
|
async receive(options = {}) {
|
|
644
1029
|
const messages = await this.read({ ...options, n: 1 });
|
|
645
1030
|
return messages[0] ?? null;
|
|
646
1031
|
}
|
|
647
1032
|
async pop() {
|
|
648
|
-
return
|
|
1033
|
+
return this.rpc("pop", { queue_name: this.name }, (value) => normalizeRpcMessage(this.name, value, "deleted"));
|
|
649
1034
|
}
|
|
650
1035
|
async list(filters = {}) {
|
|
651
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/messages${createQueueQueryString(filters)}`, "GET");
|
|
1036
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/messages${createQueueQueryString(filters)}`, "GET", undefined, decodeQueueMessages);
|
|
652
1037
|
}
|
|
653
1038
|
async listFailed(limit = 100) {
|
|
654
1039
|
return this.list({ archived: true, dlq: true, limit });
|
|
@@ -657,20 +1042,20 @@ class SupaCloudQueueClient extends SupaCloudManagementClient {
|
|
|
657
1042
|
return this.list({ archived: true, limit });
|
|
658
1043
|
}
|
|
659
1044
|
async stats() {
|
|
660
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/stats`, "GET");
|
|
1045
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/stats`, "GET", undefined, decodeQueueStats);
|
|
661
1046
|
}
|
|
662
1047
|
async getSettings() {
|
|
663
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/settings`, "GET");
|
|
1048
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/settings`, "GET", undefined, decodeQueueSettings);
|
|
664
1049
|
}
|
|
665
1050
|
async updateSettings(settings) {
|
|
666
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/settings`, "PATCH", settings);
|
|
1051
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/settings`, "PATCH", settings, decodeQueueSettings);
|
|
667
1052
|
}
|
|
668
1053
|
async archive(messageId) {
|
|
669
1054
|
const msgId = normalizeMessageId(messageId);
|
|
670
|
-
const success =
|
|
1055
|
+
const success = await this.rpc("archive", {
|
|
671
1056
|
queue_name: this.name,
|
|
672
1057
|
message_id: msgId
|
|
673
|
-
})));
|
|
1058
|
+
}, (value) => Boolean(firstRpcValue(value)));
|
|
674
1059
|
return { id: String(msgId), msg_id: msgId, queue_name: this.name, status: "archived", success };
|
|
675
1060
|
}
|
|
676
1061
|
async ack(messageId) {
|
|
@@ -678,67 +1063,69 @@ class SupaCloudQueueClient extends SupaCloudManagementClient {
|
|
|
678
1063
|
}
|
|
679
1064
|
async delete(messageId) {
|
|
680
1065
|
const msgId = normalizeMessageId(messageId);
|
|
681
|
-
const success =
|
|
1066
|
+
const success = await this.rpc("delete", {
|
|
682
1067
|
queue_name: this.name,
|
|
683
1068
|
message_id: msgId
|
|
684
|
-
})));
|
|
1069
|
+
}, (value) => Boolean(firstRpcValue(value)));
|
|
685
1070
|
return { id: String(msgId), msg_id: msgId, queue_name: this.name, status: "deleted", success };
|
|
686
1071
|
}
|
|
687
1072
|
async release(messageId, options = {}) {
|
|
688
1073
|
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/messages/${messageId}/release`, "POST", {
|
|
689
1074
|
sleep_seconds: normalizeSecondsFromOptions(options),
|
|
690
1075
|
...options.error ? { error: options.error } : {}
|
|
691
|
-
});
|
|
1076
|
+
}, decodeQueueMessage);
|
|
692
1077
|
}
|
|
693
1078
|
async purge() {
|
|
694
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/purge`, "POST");
|
|
1079
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/purge`, "POST", undefined, decodePurgeResult);
|
|
695
1080
|
}
|
|
696
1081
|
async fail(messageId, _options = {}) {
|
|
697
1082
|
return this.archive(messageId);
|
|
698
1083
|
}
|
|
699
1084
|
async get(messageId) {
|
|
700
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/messages/${messageId}`, "GET");
|
|
1085
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/messages/${messageId}`, "GET", undefined, decodeQueueMessage);
|
|
701
1086
|
}
|
|
702
1087
|
async retry(messageId) {
|
|
703
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/messages/${messageId}/retry`, "POST");
|
|
1088
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/messages/${messageId}/retry`, "POST", undefined, decodeQueueMessage);
|
|
704
1089
|
}
|
|
705
1090
|
}
|
|
706
1091
|
|
|
707
1092
|
class SupaCloudQueuesClient extends SupaCloudManagementClient {
|
|
708
1093
|
async list() {
|
|
709
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues`, "GET");
|
|
1094
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues`, "GET", undefined, decodeQueueInfos);
|
|
710
1095
|
}
|
|
711
1096
|
async create(queueName, options = {}) {
|
|
712
|
-
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues`, "POST", { queue_name: queueName, unlogged: options.unlogged });
|
|
1097
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues`, "POST", { queue_name: queueName, unlogged: options.unlogged }, decodeQueueInfo);
|
|
713
1098
|
}
|
|
714
1099
|
async drop(queueName) {
|
|
715
|
-
await this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${encodeURIComponent(queueName)}`, "DELETE");
|
|
1100
|
+
await this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${encodeURIComponent(queueName)}`, "DELETE", undefined, decodeVoid);
|
|
716
1101
|
}
|
|
717
1102
|
}
|
|
718
1103
|
|
|
719
1104
|
class SupaCloudOAuthServerClient extends SupaCloudManagementClient {
|
|
720
1105
|
async getStatus() {
|
|
721
|
-
return this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-server`, "GET");
|
|
1106
|
+
return this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-server`, "GET", undefined, decodeOAuthServerStatus);
|
|
722
1107
|
}
|
|
723
1108
|
async migrateToOidc(options = {}) {
|
|
724
1109
|
return this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-server/migrate`, "POST", {
|
|
725
1110
|
allow_dynamic_registration: options.allowDynamicRegistration === true,
|
|
726
1111
|
...options.authorizationPath === undefined ? {} : { authorization_path: options.authorizationPath }
|
|
727
|
-
});
|
|
1112
|
+
}, decodeOAuthServerStatus);
|
|
728
1113
|
}
|
|
729
1114
|
async getDiscovery() {
|
|
730
1115
|
const status = await this.getStatus();
|
|
731
1116
|
const response = await fetch(status.discovery_url);
|
|
732
1117
|
if (!response.ok)
|
|
733
1118
|
throw new Error(`SupaCloud OIDC discovery failed (${response.status})`);
|
|
734
|
-
|
|
1119
|
+
const payload = await response.json();
|
|
1120
|
+
return valueRecord2(payload);
|
|
735
1121
|
}
|
|
736
1122
|
async getJwks() {
|
|
737
1123
|
const status = await this.getStatus();
|
|
738
1124
|
const response = await fetch(status.jwks_url);
|
|
739
1125
|
if (!response.ok)
|
|
740
1126
|
throw new Error(`SupaCloud JWKS fetch failed (${response.status})`);
|
|
741
|
-
|
|
1127
|
+
const payload = await response.json();
|
|
1128
|
+
return valueRecord2(payload);
|
|
742
1129
|
}
|
|
743
1130
|
async buildAuthorizeUrl(options) {
|
|
744
1131
|
const status = await this.getStatus();
|
|
@@ -765,22 +1152,22 @@ class SupaCloudOAuthServerClient extends SupaCloudManagementClient {
|
|
|
765
1152
|
|
|
766
1153
|
class SupaCloudOAuthClientsClient extends SupaCloudManagementClient {
|
|
767
1154
|
async list() {
|
|
768
|
-
return this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-clients`, "GET");
|
|
1155
|
+
return this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-clients`, "GET", undefined, decodeOAuthClientList);
|
|
769
1156
|
}
|
|
770
1157
|
async create(input) {
|
|
771
|
-
return this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-clients`, "POST", input);
|
|
1158
|
+
return this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-clients`, "POST", input, decodeOAuthClient);
|
|
772
1159
|
}
|
|
773
1160
|
async get(clientId) {
|
|
774
|
-
return this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-clients/${encodeURIComponent(clientId)}`, "GET");
|
|
1161
|
+
return this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-clients/${encodeURIComponent(clientId)}`, "GET", undefined, decodeOAuthClient);
|
|
775
1162
|
}
|
|
776
1163
|
async update(clientId, patch) {
|
|
777
|
-
return this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-clients/${encodeURIComponent(clientId)}`, "PUT", patch);
|
|
1164
|
+
return this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-clients/${encodeURIComponent(clientId)}`, "PUT", patch, decodeOAuthClient);
|
|
778
1165
|
}
|
|
779
1166
|
async delete(clientId) {
|
|
780
|
-
await this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-clients/${encodeURIComponent(clientId)}`, "DELETE");
|
|
1167
|
+
await this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-clients/${encodeURIComponent(clientId)}`, "DELETE", undefined, decodeVoid);
|
|
781
1168
|
}
|
|
782
1169
|
async regenerateSecret(clientId) {
|
|
783
|
-
return this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-clients/${encodeURIComponent(clientId)}/regenerate-secret`, "POST");
|
|
1170
|
+
return this.request(`/v1/projects/${this.options.projectRef}/auth/oauth-clients/${encodeURIComponent(clientId)}/regenerate-secret`, "POST", undefined, decodeOAuthClient);
|
|
784
1171
|
}
|
|
785
1172
|
}
|
|
786
1173
|
|
|
@@ -789,19 +1176,96 @@ class SupaCloudSupAuthClient extends SupaCloudManagementClient {
|
|
|
789
1176
|
return `/v1/projects/${this.options.projectRef}/supauth`;
|
|
790
1177
|
}
|
|
791
1178
|
async provision(options = {}) {
|
|
792
|
-
return this.request(`${this.basePath}/provision`, "POST", options);
|
|
1179
|
+
return this.request(`${this.basePath}/provision`, "POST", options, decodeSupAuthProvisionResult);
|
|
793
1180
|
}
|
|
794
1181
|
async reconcile(options = {}) {
|
|
795
|
-
return this.request(`${this.basePath}/reconcile`, "POST", options);
|
|
1182
|
+
return this.request(`${this.basePath}/reconcile`, "POST", options, decodeSupAuthReconcileResult);
|
|
796
1183
|
}
|
|
797
1184
|
async rollback() {
|
|
798
|
-
return this.request(`${this.basePath}/rollback`, "POST");
|
|
1185
|
+
return this.request(`${this.basePath}/rollback`, "POST", undefined, decodeSupAuthRollbackResult);
|
|
799
1186
|
}
|
|
800
1187
|
async getClientConfig() {
|
|
801
|
-
return this.request(`${this.basePath}/client-config`, "GET");
|
|
1188
|
+
return this.request(`${this.basePath}/client-config`, "GET", undefined, decodeSupAuthClientConfig);
|
|
802
1189
|
}
|
|
803
1190
|
async verify() {
|
|
804
|
-
return this.request(`${this.basePath}/verify`, "GET");
|
|
1191
|
+
return this.request(`${this.basePath}/verify`, "GET", undefined, decodeSupAuthVerification);
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
class SupaCloudCapabilitiesClient extends SupaCloudManagementClient {
|
|
1196
|
+
async get() {
|
|
1197
|
+
return this.request(`/v1/projects/${this.options.projectRef}/capabilities`, "GET", undefined, decodeProjectCapabilities);
|
|
1198
|
+
}
|
|
1199
|
+
async isAvailable(capabilityKey) {
|
|
1200
|
+
const res = await this.get();
|
|
1201
|
+
return res.capabilities?.[capabilityKey]?.available === true;
|
|
1202
|
+
}
|
|
1203
|
+
async getVersion() {
|
|
1204
|
+
const res = await this.get();
|
|
1205
|
+
return res.platform_version ?? "unknown";
|
|
1206
|
+
}
|
|
1207
|
+
async getEnvironment() {
|
|
1208
|
+
const res = await this.get();
|
|
1209
|
+
return {
|
|
1210
|
+
projectRef: res.project_ref,
|
|
1211
|
+
platformVersion: res.platform_version ?? "unknown",
|
|
1212
|
+
environment: res.environment ?? "production",
|
|
1213
|
+
schemaVersion: res.schema_version,
|
|
1214
|
+
authRuntime: res.auth_runtime,
|
|
1215
|
+
storageBackend: res.storage_backend,
|
|
1216
|
+
capabilities: res.capabilities
|
|
1217
|
+
};
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
class SupaCloudStorageClient extends SupaCloudManagementClient {
|
|
1222
|
+
async getPlatformConstraints() {
|
|
1223
|
+
return this.request(`/storage/v1/constraints`, "GET", undefined, decodeStoragePlatformConstraints);
|
|
1224
|
+
}
|
|
1225
|
+
async getUploadConstraints(bucketId) {
|
|
1226
|
+
try {
|
|
1227
|
+
const { data, error } = await this.options.supabase.storage.getBucket(bucketId);
|
|
1228
|
+
if (!error && data) {
|
|
1229
|
+
return {
|
|
1230
|
+
bucketId,
|
|
1231
|
+
fileSizeLimit: typeof data.file_size_limit === "number" ? data.file_size_limit : data.file_size_limit ? Number(data.file_size_limit) : null,
|
|
1232
|
+
allowedMimeTypes: Array.isArray(data.allowed_mime_types) ? data.allowed_mime_types : null
|
|
1233
|
+
};
|
|
1234
|
+
}
|
|
1235
|
+
} catch {}
|
|
1236
|
+
try {
|
|
1237
|
+
const bucket = await this.request(`/v1/projects/${this.options.projectRef}/storage/buckets/${encodeURIComponent(bucketId)}`, "GET", undefined, (val) => responseRecord(val, "storage bucket"));
|
|
1238
|
+
const rawLimit = bucket.file_size_limit;
|
|
1239
|
+
const fileSizeLimit = typeof rawLimit === "number" ? rawLimit : rawLimit ? Number(rawLimit) : null;
|
|
1240
|
+
const allowedMimeTypes = Array.isArray(bucket.allowed_mime_types) ? bucket.allowed_mime_types : null;
|
|
1241
|
+
return {
|
|
1242
|
+
bucketId,
|
|
1243
|
+
fileSizeLimit,
|
|
1244
|
+
allowedMimeTypes
|
|
1245
|
+
};
|
|
1246
|
+
} catch {
|
|
1247
|
+
return {
|
|
1248
|
+
bucketId,
|
|
1249
|
+
fileSizeLimit: null,
|
|
1250
|
+
allowedMimeTypes: null
|
|
1251
|
+
};
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
async validateUpload(bucketId, file) {
|
|
1255
|
+
const constraints = await this.getUploadConstraints(bucketId);
|
|
1256
|
+
if (constraints.fileSizeLimit !== null && file.size > constraints.fileSizeLimit) {
|
|
1257
|
+
return {
|
|
1258
|
+
valid: false,
|
|
1259
|
+
error: `File size ${file.size} bytes exceeds bucket limit of ${constraints.fileSizeLimit} bytes`
|
|
1260
|
+
};
|
|
1261
|
+
}
|
|
1262
|
+
if (constraints.allowedMimeTypes && constraints.allowedMimeTypes.length > 0 && file.type && !constraints.allowedMimeTypes.includes(file.type)) {
|
|
1263
|
+
return {
|
|
1264
|
+
valid: false,
|
|
1265
|
+
error: `MIME type "${file.type}" is not in the allowed list: ${constraints.allowedMimeTypes.join(", ")}`
|
|
1266
|
+
};
|
|
1267
|
+
}
|
|
1268
|
+
return { valid: true };
|
|
805
1269
|
}
|
|
806
1270
|
}
|
|
807
1271
|
function createSupaCloudClient(options) {
|
|
@@ -816,6 +1280,8 @@ function createSupaCloudClient(options) {
|
|
|
816
1280
|
const oauthClients = new SupaCloudOAuthClientsClient(normalized);
|
|
817
1281
|
const supauth = new SupaCloudSupAuthClient(normalized);
|
|
818
1282
|
const queues = new SupaCloudQueuesClient(normalized);
|
|
1283
|
+
const capabilities = new SupaCloudCapabilitiesClient(normalized);
|
|
1284
|
+
const storage = new SupaCloudStorageClient(normalized);
|
|
819
1285
|
const workflows2 = new SupaCloudWorkflowsClient(options.supabase);
|
|
820
1286
|
const commands2 = new SupaCloudCommandsClient(options.supabase);
|
|
821
1287
|
const artifacts2 = new SupaCloudArtifactsClient(options.supabase);
|
|
@@ -833,6 +1299,10 @@ function createSupaCloudClient(options) {
|
|
|
833
1299
|
artifacts: artifacts2,
|
|
834
1300
|
supauth,
|
|
835
1301
|
queues,
|
|
1302
|
+
capabilities,
|
|
1303
|
+
storage,
|
|
1304
|
+
getEnvironment: () => capabilities.getEnvironment(),
|
|
1305
|
+
getVersion: () => capabilities.getVersion(),
|
|
836
1306
|
queue: (name) => new SupaCloudQueueClient(normalized, name),
|
|
837
1307
|
functions: {
|
|
838
1308
|
invokeBackground: (functionName, submitOptions) => tasks.submit(functionName, submitOptions)
|
|
@@ -840,9 +1310,14 @@ function createSupaCloudClient(options) {
|
|
|
840
1310
|
};
|
|
841
1311
|
}
|
|
842
1312
|
export {
|
|
1313
|
+
SUPACLOUD_JS_VERSION,
|
|
843
1314
|
SupaCloudApiError,
|
|
844
1315
|
SupaCloudArtifactsClient,
|
|
1316
|
+
SupaCloudCapabilitiesClient,
|
|
845
1317
|
SupaCloudCommandsClient,
|
|
1318
|
+
SupaCloudStorageClient,
|
|
1319
|
+
SupaCloudTaskDecoderError as SupaCloudTaskDecodeError,
|
|
1320
|
+
SupaCloudTaskDecoderError,
|
|
846
1321
|
SupaCloudTaskSubmitError,
|
|
847
1322
|
SupaCloudWorkflowsClient,
|
|
848
1323
|
createSupaCloudClient,
|