@supacloud/js 0.11.4 → 0.12.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 +47 -4
- package/dist/index.js +64 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -57,6 +57,31 @@ export type SupaCloudTaskSubmitOptions = {
|
|
|
57
57
|
idempotencyKey?: string;
|
|
58
58
|
method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
59
59
|
};
|
|
60
|
+
export type SupaCloudQueueSendOptions = {
|
|
61
|
+
delayMs?: number;
|
|
62
|
+
maxAttempts?: number;
|
|
63
|
+
idempotencyKey?: string;
|
|
64
|
+
traceId?: string;
|
|
65
|
+
};
|
|
66
|
+
export type SupaCloudQueueReceiveOptions = {
|
|
67
|
+
visibilityTimeoutSec?: number;
|
|
68
|
+
};
|
|
69
|
+
export type SupaCloudQueueReleaseOptions = {
|
|
70
|
+
delayMs?: number;
|
|
71
|
+
error?: string;
|
|
72
|
+
};
|
|
73
|
+
export type SupaCloudQueueFailOptions = {
|
|
74
|
+
error?: string;
|
|
75
|
+
deadLetter?: boolean;
|
|
76
|
+
};
|
|
77
|
+
export type SupaCloudQueueListFilters = {
|
|
78
|
+
status?: string | string[];
|
|
79
|
+
dlq?: boolean;
|
|
80
|
+
limit?: number;
|
|
81
|
+
};
|
|
82
|
+
export type SupaCloudQueueMessage = SupaCloudTaskDetail & {
|
|
83
|
+
payload: Record<string, unknown>;
|
|
84
|
+
};
|
|
60
85
|
export type SupaCloudTaskWaitOptions = {
|
|
61
86
|
intervalMs?: number;
|
|
62
87
|
signal?: AbortSignal;
|
|
@@ -96,11 +121,14 @@ export declare class SupaCloudTaskSubmitError extends Error {
|
|
|
96
121
|
readonly responseBody: unknown;
|
|
97
122
|
constructor(message: string, responseBody: unknown);
|
|
98
123
|
}
|
|
99
|
-
|
|
100
|
-
|
|
124
|
+
type HttpMethod = "GET" | "POST" | "DELETE";
|
|
125
|
+
declare class SupaCloudManagementClient<TClient extends SupabaseClient = SupabaseClient> {
|
|
126
|
+
protected readonly options: Required<SupaCloudClientOptions<TClient>>;
|
|
101
127
|
constructor(options: Required<SupaCloudClientOptions<TClient>>);
|
|
102
|
-
|
|
103
|
-
|
|
128
|
+
protected resolveAccessToken(): Promise<string>;
|
|
129
|
+
protected request<T>(path: string, method: HttpMethod, body?: unknown): Promise<T>;
|
|
130
|
+
}
|
|
131
|
+
declare class SupaCloudTasksClient<TClient extends SupabaseClient = SupabaseClient> extends SupaCloudManagementClient<TClient> {
|
|
104
132
|
private createReceipt;
|
|
105
133
|
submit(functionName: string, options?: SupaCloudTaskSubmitOptions): Promise<SupaCloudTaskReceipt>;
|
|
106
134
|
get(taskId: string): Promise<SupaCloudTaskDetail>;
|
|
@@ -114,11 +142,26 @@ declare class SupaCloudTasksClient<TClient extends SupabaseClient = SupabaseClie
|
|
|
114
142
|
unsubscribe(): void;
|
|
115
143
|
};
|
|
116
144
|
}
|
|
145
|
+
declare class SupaCloudQueueClient<TClient extends SupabaseClient = SupabaseClient> extends SupaCloudManagementClient<TClient> {
|
|
146
|
+
private readonly name;
|
|
147
|
+
constructor(options: Required<SupaCloudClientOptions<TClient>>, name: string);
|
|
148
|
+
private get encodedName();
|
|
149
|
+
send(payload?: Record<string, unknown>, options?: SupaCloudQueueSendOptions): Promise<SupaCloudQueueMessage>;
|
|
150
|
+
receive(options?: SupaCloudQueueReceiveOptions): Promise<SupaCloudQueueMessage | null>;
|
|
151
|
+
list(filters?: SupaCloudQueueListFilters): Promise<SupaCloudQueueMessage[]>;
|
|
152
|
+
listFailed(limit?: number): Promise<SupaCloudQueueMessage[]>;
|
|
153
|
+
get(messageId: string): Promise<SupaCloudQueueMessage>;
|
|
154
|
+
ack(messageId: string, result?: Record<string, unknown>): Promise<SupaCloudQueueMessage>;
|
|
155
|
+
delete(messageId: string): Promise<void>;
|
|
156
|
+
release(messageId: string, options?: SupaCloudQueueReleaseOptions): Promise<SupaCloudQueueMessage>;
|
|
157
|
+
fail(messageId: string, options?: SupaCloudQueueFailOptions): Promise<SupaCloudQueueMessage>;
|
|
158
|
+
}
|
|
117
159
|
export declare function createSupaCloudClient<TClient extends SupabaseClient = SupabaseClient>(options: SupaCloudClientOptions<TClient>): {
|
|
118
160
|
supabase: TClient;
|
|
119
161
|
projectRef: string;
|
|
120
162
|
managementApiUrl: string;
|
|
121
163
|
tasks: SupaCloudTasksClient<TClient>;
|
|
164
|
+
queue: (name: string) => SupaCloudQueueClient<TClient>;
|
|
122
165
|
functions: {
|
|
123
166
|
invokeBackground: (functionName: string, submitOptions?: SupaCloudTaskSubmitOptions) => Promise<SupaCloudTaskReceipt>;
|
|
124
167
|
};
|
package/dist/index.js
CHANGED
|
@@ -39,6 +39,18 @@ function createQueryString(filters = {}) {
|
|
|
39
39
|
const query = params.toString();
|
|
40
40
|
return query.length > 0 ? `?${query}` : "";
|
|
41
41
|
}
|
|
42
|
+
function createQueueQueryString(filters = {}) {
|
|
43
|
+
const params = new URLSearchParams;
|
|
44
|
+
const statuses = toArray(filters.status);
|
|
45
|
+
if (statuses?.length)
|
|
46
|
+
params.set("status", statuses.join(","));
|
|
47
|
+
if (filters.dlq)
|
|
48
|
+
params.set("dlq", "true");
|
|
49
|
+
if (filters.limit !== undefined)
|
|
50
|
+
params.set("limit", String(filters.limit));
|
|
51
|
+
const query = params.toString();
|
|
52
|
+
return query.length > 0 ? `?${query}` : "";
|
|
53
|
+
}
|
|
42
54
|
async function defaultAccessTokenResolver(supabase) {
|
|
43
55
|
const { data, error } = await supabase.auth.getSession();
|
|
44
56
|
if (error)
|
|
@@ -57,7 +69,7 @@ function normalizeTaskSnapshot(task) {
|
|
|
57
69
|
};
|
|
58
70
|
}
|
|
59
71
|
|
|
60
|
-
class
|
|
72
|
+
class SupaCloudManagementClient {
|
|
61
73
|
options;
|
|
62
74
|
constructor(options) {
|
|
63
75
|
this.options = options;
|
|
@@ -82,8 +94,13 @@ class SupaCloudTasksClient {
|
|
|
82
94
|
if (!response.ok) {
|
|
83
95
|
throw new Error(`SupaCloud request failed (${response.status})`);
|
|
84
96
|
}
|
|
97
|
+
if (response.status === 204)
|
|
98
|
+
return;
|
|
85
99
|
return await response.json();
|
|
86
100
|
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
class SupaCloudTasksClient extends SupaCloudManagementClient {
|
|
87
104
|
createReceipt(taskId, status) {
|
|
88
105
|
return {
|
|
89
106
|
taskId,
|
|
@@ -304,6 +321,51 @@ class SupaCloudTasksClient {
|
|
|
304
321
|
};
|
|
305
322
|
}
|
|
306
323
|
}
|
|
324
|
+
|
|
325
|
+
class SupaCloudQueueClient extends SupaCloudManagementClient {
|
|
326
|
+
name;
|
|
327
|
+
constructor(options, name) {
|
|
328
|
+
super(options);
|
|
329
|
+
this.name = name;
|
|
330
|
+
}
|
|
331
|
+
get encodedName() {
|
|
332
|
+
return encodeURIComponent(this.name);
|
|
333
|
+
}
|
|
334
|
+
async send(payload = {}, options = {}) {
|
|
335
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/messages`, "POST", {
|
|
336
|
+
payload,
|
|
337
|
+
delayMs: options.delayMs,
|
|
338
|
+
maxAttempts: options.maxAttempts,
|
|
339
|
+
idempotencyKey: options.idempotencyKey,
|
|
340
|
+
traceId: options.traceId
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
async receive(options = {}) {
|
|
344
|
+
const message = await this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/messages/receive`, "POST", options);
|
|
345
|
+
return message ?? null;
|
|
346
|
+
}
|
|
347
|
+
async list(filters = {}) {
|
|
348
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/messages${createQueueQueryString(filters)}`, "GET");
|
|
349
|
+
}
|
|
350
|
+
async listFailed(limit = 100) {
|
|
351
|
+
return this.list({ dlq: true, limit });
|
|
352
|
+
}
|
|
353
|
+
async get(messageId) {
|
|
354
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/messages/${messageId}`, "GET");
|
|
355
|
+
}
|
|
356
|
+
async ack(messageId, result) {
|
|
357
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/messages/${messageId}/ack`, "POST", result ? { result } : {});
|
|
358
|
+
}
|
|
359
|
+
async delete(messageId) {
|
|
360
|
+
await this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/messages/${messageId}`, "DELETE");
|
|
361
|
+
}
|
|
362
|
+
async release(messageId, options = {}) {
|
|
363
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/messages/${messageId}/release`, "POST", options);
|
|
364
|
+
}
|
|
365
|
+
async fail(messageId, options = {}) {
|
|
366
|
+
return this.request(`/v1/projects/${this.options.projectRef}/tasks/queues/${this.encodedName}/messages/${messageId}/fail`, "POST", options);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
307
369
|
function createSupaCloudClient(options) {
|
|
308
370
|
const normalized = {
|
|
309
371
|
...options,
|
|
@@ -317,6 +379,7 @@ function createSupaCloudClient(options) {
|
|
|
317
379
|
projectRef: normalized.projectRef,
|
|
318
380
|
managementApiUrl: normalized.managementApiUrl,
|
|
319
381
|
tasks,
|
|
382
|
+
queue: (name) => new SupaCloudQueueClient(normalized, name),
|
|
320
383
|
functions: {
|
|
321
384
|
invokeBackground: (functionName, submitOptions) => tasks.submit(functionName, submitOptions)
|
|
322
385
|
}
|