@supatype/client 0.1.0-alpha.10
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/.turbo/turbo-build.log +4 -0
- package/.turbo/turbo-test.log +23 -0
- package/.turbo/turbo-typecheck.log +4 -0
- package/dist/auth.d.ts +279 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +848 -0
- package/dist/auth.js.map +1 -0
- package/dist/error-codes-doc.d.ts +31 -0
- package/dist/error-codes-doc.d.ts.map +1 -0
- package/dist/error-codes-doc.js +294 -0
- package/dist/error-codes-doc.js.map +1 -0
- package/dist/errors.d.ts +197 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +162 -0
- package/dist/errors.js.map +1 -0
- package/dist/fetch-with-retry.d.ts +49 -0
- package/dist/fetch-with-retry.d.ts.map +1 -0
- package/dist/fetch-with-retry.js +150 -0
- package/dist/fetch-with-retry.js.map +1 -0
- package/dist/index.d.ts +151 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +285 -0
- package/dist/index.js.map +1 -0
- package/dist/query.d.ts +55 -0
- package/dist/query.d.ts.map +1 -0
- package/dist/query.js +264 -0
- package/dist/query.js.map +1 -0
- package/dist/realtime.d.ts +69 -0
- package/dist/realtime.d.ts.map +1 -0
- package/dist/realtime.js +279 -0
- package/dist/realtime.js.map +1 -0
- package/dist/retry.d.ts +24 -0
- package/dist/retry.d.ts.map +1 -0
- package/dist/retry.js +25 -0
- package/dist/retry.js.map +1 -0
- package/dist/serverless-docs.d.ts +38 -0
- package/dist/serverless-docs.d.ts.map +1 -0
- package/dist/serverless-docs.js +83 -0
- package/dist/serverless-docs.js.map +1 -0
- package/dist/storage.d.ts +102 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +169 -0
- package/dist/storage.js.map +1 -0
- package/dist/types.d.ts +212 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +26 -0
- package/src/auth.ts +1024 -0
- package/src/error-codes-doc.ts +315 -0
- package/src/errors.ts +201 -0
- package/src/fetch-with-retry.ts +200 -0
- package/src/index.ts +518 -0
- package/src/query.ts +331 -0
- package/src/realtime.ts +418 -0
- package/src/retry.ts +35 -0
- package/src/serverless-docs.ts +107 -0
- package/src/storage.ts +234 -0
- package/src/types.ts +264 -0
- package/tests/auth.test.ts +351 -0
- package/tests/mfa-totp-integration.test.ts +359 -0
- package/tests/query.test.ts +286 -0
- package/tests/rate-limit-integration.test.ts +185 -0
- package/tests/realtime-multi-tenant-integration.test.ts +372 -0
- package/tests/retry-integration.test.ts +212 -0
- package/tests/serverless.test.ts +115 -0
- package/tests/storage-quota-integration.test.ts +189 -0
- package/tests/storage-upload-limit-integration.test.ts +185 -0
- package/tests/types.test.ts +137 -0
- package/tsconfig.json +11 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export type RealtimeEvent = "INSERT" | "UPDATE" | "DELETE" | "*";
|
|
2
|
+
export interface RealtimePayload<TRow> {
|
|
3
|
+
eventType: RealtimeEvent;
|
|
4
|
+
new: TRow | null;
|
|
5
|
+
old: TRow | null;
|
|
6
|
+
schema: string;
|
|
7
|
+
table: string;
|
|
8
|
+
commitTimestamp: string;
|
|
9
|
+
}
|
|
10
|
+
type RealtimeCallback<TRow> = (payload: RealtimePayload<TRow>) => void;
|
|
11
|
+
export type ChannelStatus = "SUBSCRIBED" | "SUBSCRIBING" | "CLOSED" | "CHANNEL_ERROR" | "TIMED_OUT";
|
|
12
|
+
interface ChannelSubscription<TRow> {
|
|
13
|
+
on(event: "postgres_changes", opts: {
|
|
14
|
+
event: RealtimeEvent;
|
|
15
|
+
schema?: string | undefined;
|
|
16
|
+
table?: string | undefined;
|
|
17
|
+
filter?: string | undefined;
|
|
18
|
+
}, callback: RealtimeCallback<TRow>): ChannelSubscription<TRow>;
|
|
19
|
+
/** Listen for presence changes. */
|
|
20
|
+
onPresence(callback: (event: {
|
|
21
|
+
joins: PresenceEntry[];
|
|
22
|
+
leaves: PresenceEntry[];
|
|
23
|
+
}) => void): ChannelSubscription<TRow>;
|
|
24
|
+
/** Listen for broadcast events. */
|
|
25
|
+
onBroadcast(event: string, callback: (payload: Record<string, unknown>) => void): ChannelSubscription<TRow>;
|
|
26
|
+
/** Subscribe to the channel, opening the WebSocket if needed. */
|
|
27
|
+
subscribe(callback?: ((status: ChannelStatus) => void) | undefined): ChannelSubscription<TRow>;
|
|
28
|
+
/** Unsubscribe and clean up. */
|
|
29
|
+
unsubscribe(): void;
|
|
30
|
+
/** Send a broadcast event to the channel. */
|
|
31
|
+
broadcast(event: string, payload: Record<string, unknown>): void;
|
|
32
|
+
/** Track presence in this channel. */
|
|
33
|
+
track(payload: Record<string, unknown>): void;
|
|
34
|
+
/** Stop tracking presence. */
|
|
35
|
+
untrack(): void;
|
|
36
|
+
}
|
|
37
|
+
export interface PresenceEntry {
|
|
38
|
+
user_id: string;
|
|
39
|
+
[key: string]: unknown;
|
|
40
|
+
}
|
|
41
|
+
export declare class RealtimeClient {
|
|
42
|
+
private readonly url;
|
|
43
|
+
private readonly headers;
|
|
44
|
+
private ws;
|
|
45
|
+
private channels;
|
|
46
|
+
private reconnectTimer;
|
|
47
|
+
private reconnectAttempts;
|
|
48
|
+
private maxReconnectAttempts;
|
|
49
|
+
private baseReconnectDelay;
|
|
50
|
+
constructor(url: string, headers: Record<string, string>);
|
|
51
|
+
channel<TRow = Record<string, unknown>>(name: string): ChannelSubscription<TRow>;
|
|
52
|
+
/** Remove a channel by name. */
|
|
53
|
+
removeChannel(name: string): void;
|
|
54
|
+
/** Disconnect the WebSocket entirely. */
|
|
55
|
+
disconnect(): void;
|
|
56
|
+
private getOrCreateChannel;
|
|
57
|
+
private ensureConnection;
|
|
58
|
+
private scheduleReconnect;
|
|
59
|
+
private handleServerMessage;
|
|
60
|
+
private handleSystem;
|
|
61
|
+
private handleChange;
|
|
62
|
+
private handlePresence;
|
|
63
|
+
private handleBroadcastMsg;
|
|
64
|
+
private sendSubscribe;
|
|
65
|
+
private sendUnsubscribe;
|
|
66
|
+
private sendMessage;
|
|
67
|
+
}
|
|
68
|
+
export {};
|
|
69
|
+
//# sourceMappingURL=realtime.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"realtime.d.ts","sourceRoot":"","sources":["../src/realtime.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,aAAa,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,GAAG,GAAG,CAAA;AAEhE,MAAM,WAAW,eAAe,CAAC,IAAI;IACnC,SAAS,EAAE,aAAa,CAAA;IACxB,GAAG,EAAE,IAAI,GAAG,IAAI,CAAA;IAChB,GAAG,EAAE,IAAI,GAAG,IAAI,CAAA;IAChB,MAAM,EAAE,MAAM,CAAA;IACd,KAAK,EAAE,MAAM,CAAA;IACb,eAAe,EAAE,MAAM,CAAA;CACxB;AAED,KAAK,gBAAgB,CAAC,IAAI,IAAI,CAAC,OAAO,EAAE,eAAe,CAAC,IAAI,CAAC,KAAK,IAAI,CAAA;AAEtE,MAAM,MAAM,aAAa,GAAG,YAAY,GAAG,aAAa,GAAG,QAAQ,GAAG,eAAe,GAAG,WAAW,CAAA;AAEnG,UAAU,mBAAmB,CAAC,IAAI;IAChC,EAAE,CACA,KAAK,EAAE,kBAAkB,EACzB,IAAI,EAAE;QACJ,KAAK,EAAE,aAAa,CAAA;QACpB,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;QAC3B,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;QAC1B,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;KAC5B,EACD,QAAQ,EAAE,gBAAgB,CAAC,IAAI,CAAC,GAC/B,mBAAmB,CAAC,IAAI,CAAC,CAAA;IAC5B,mCAAmC;IACnC,UAAU,CACR,QAAQ,EAAE,CAAC,KAAK,EAAE;QAAE,KAAK,EAAE,a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AE,MAAM,CAAC;IAMxD,OAAO,CAAC,IAAI,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,mBAAmB,CAAC,IAAI,CAAC;IAwEhF,gCAAgC;IAChC,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAWjC,yCAAyC;IACzC,UAAU,IAAI,IAAI;IAclB,OAAO,CAAC,kBAAkB;IAgB1B,OAAO,CAAC,gBAAgB;IA+CxB,OAAO,CAAC,iBAAiB;IAoBzB,OAAO,CAAC,mBAAmB;IAiB3B,OAAO,CAAC,YAAY;IAmBpB,OAAO,CAAC,YAAY;IAwBpB,OAAO,CAAC,cAAc;IAStB,OAAO,CAAC,kBAAkB;IAY1B,OAAO,CAAC,aAAa;IAwBrB,OAAO,CAAC,eAAe;IAIvB,OAAO,CAAC,WAAW;CAKpB"}
|
package/dist/realtime.js
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
// ─── Realtime client ─────────────────────────────────────────────────────────
|
|
2
|
+
export class RealtimeClient {
|
|
3
|
+
url;
|
|
4
|
+
headers;
|
|
5
|
+
ws = null;
|
|
6
|
+
channels = new Map();
|
|
7
|
+
reconnectTimer = null;
|
|
8
|
+
reconnectAttempts = 0;
|
|
9
|
+
maxReconnectAttempts = 10;
|
|
10
|
+
baseReconnectDelay = 1000;
|
|
11
|
+
constructor(url, headers) {
|
|
12
|
+
// Convert http(s) URL to ws(s) URL
|
|
13
|
+
this.url = url.replace(/^http/, "ws");
|
|
14
|
+
this.headers = headers;
|
|
15
|
+
}
|
|
16
|
+
channel(name) {
|
|
17
|
+
const state = this.getOrCreateChannel(name);
|
|
18
|
+
const sub = {
|
|
19
|
+
on(_event, opts, callback) {
|
|
20
|
+
state.pgListeners.push({
|
|
21
|
+
event: opts.event,
|
|
22
|
+
schema: opts.schema ?? "public",
|
|
23
|
+
table: opts.table ?? name.split(":").pop() ?? name,
|
|
24
|
+
filter: opts.filter,
|
|
25
|
+
callback: callback,
|
|
26
|
+
});
|
|
27
|
+
return sub;
|
|
28
|
+
},
|
|
29
|
+
onPresence(callback) {
|
|
30
|
+
state.presenceListeners.push(callback);
|
|
31
|
+
return sub;
|
|
32
|
+
},
|
|
33
|
+
onBroadcast(event, callback) {
|
|
34
|
+
if (!state.broadcastListeners.has(event)) {
|
|
35
|
+
state.broadcastListeners.set(event, []);
|
|
36
|
+
}
|
|
37
|
+
state.broadcastListeners.get(event).push(callback);
|
|
38
|
+
return sub;
|
|
39
|
+
},
|
|
40
|
+
subscribe: (callback) => {
|
|
41
|
+
state.statusCallback = callback ?? null;
|
|
42
|
+
this.ensureConnection();
|
|
43
|
+
this.sendSubscribe(state);
|
|
44
|
+
return sub;
|
|
45
|
+
},
|
|
46
|
+
unsubscribe: () => {
|
|
47
|
+
this.sendUnsubscribe(state);
|
|
48
|
+
this.channels.delete(name);
|
|
49
|
+
// Close WebSocket if no channels left
|
|
50
|
+
if (this.channels.size === 0) {
|
|
51
|
+
this.disconnect();
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
broadcast: (event, payload) => {
|
|
55
|
+
this.sendMessage({
|
|
56
|
+
type: "broadcast",
|
|
57
|
+
channel: name,
|
|
58
|
+
event,
|
|
59
|
+
payload,
|
|
60
|
+
});
|
|
61
|
+
},
|
|
62
|
+
track: (payload) => {
|
|
63
|
+
this.sendMessage({
|
|
64
|
+
type: "presence_track",
|
|
65
|
+
channel: name,
|
|
66
|
+
payload,
|
|
67
|
+
});
|
|
68
|
+
},
|
|
69
|
+
untrack: () => {
|
|
70
|
+
this.sendMessage({
|
|
71
|
+
type: "presence_untrack",
|
|
72
|
+
channel: name,
|
|
73
|
+
});
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
return sub;
|
|
77
|
+
}
|
|
78
|
+
/** Remove a channel by name. */
|
|
79
|
+
removeChannel(name) {
|
|
80
|
+
const state = this.channels.get(name);
|
|
81
|
+
if (state) {
|
|
82
|
+
this.sendUnsubscribe(state);
|
|
83
|
+
this.channels.delete(name);
|
|
84
|
+
}
|
|
85
|
+
if (this.channels.size === 0) {
|
|
86
|
+
this.disconnect();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/** Disconnect the WebSocket entirely. */
|
|
90
|
+
disconnect() {
|
|
91
|
+
if (this.reconnectTimer) {
|
|
92
|
+
clearTimeout(this.reconnectTimer);
|
|
93
|
+
this.reconnectTimer = null;
|
|
94
|
+
}
|
|
95
|
+
if (this.ws) {
|
|
96
|
+
this.ws.close(1000, "client disconnect");
|
|
97
|
+
this.ws = null;
|
|
98
|
+
}
|
|
99
|
+
this.reconnectAttempts = 0;
|
|
100
|
+
}
|
|
101
|
+
// ─── Internal ──────────────────────────────────────────────────────────
|
|
102
|
+
getOrCreateChannel(name) {
|
|
103
|
+
let state = this.channels.get(name);
|
|
104
|
+
if (!state) {
|
|
105
|
+
state = {
|
|
106
|
+
name,
|
|
107
|
+
pgListeners: [],
|
|
108
|
+
presenceListeners: [],
|
|
109
|
+
broadcastListeners: new Map(),
|
|
110
|
+
statusCallback: null,
|
|
111
|
+
subscribed: false,
|
|
112
|
+
};
|
|
113
|
+
this.channels.set(name, state);
|
|
114
|
+
}
|
|
115
|
+
return state;
|
|
116
|
+
}
|
|
117
|
+
ensureConnection() {
|
|
118
|
+
if (this.ws && (this.ws.readyState === WebSocket.OPEN || this.ws.readyState === WebSocket.CONNECTING)) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
// Build URL with token from apikey header
|
|
122
|
+
const token = this.headers["apikey"] ?? this.headers["Authorization"]?.replace("Bearer ", "");
|
|
123
|
+
const wsUrl = token ? `${this.url}?token=${encodeURIComponent(token)}` : this.url;
|
|
124
|
+
this.ws = new WebSocket(wsUrl);
|
|
125
|
+
this.ws.onopen = () => {
|
|
126
|
+
this.reconnectAttempts = 0;
|
|
127
|
+
// Re-subscribe all channels
|
|
128
|
+
for (const state of this.channels.values()) {
|
|
129
|
+
if (!state.subscribed) {
|
|
130
|
+
this.sendSubscribe(state);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
this.ws.onmessage = (event) => {
|
|
135
|
+
try {
|
|
136
|
+
const msg = JSON.parse(String(event.data));
|
|
137
|
+
this.handleServerMessage(msg);
|
|
138
|
+
}
|
|
139
|
+
catch {
|
|
140
|
+
// Ignore unparseable messages
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
this.ws.onclose = () => {
|
|
144
|
+
this.ws = null;
|
|
145
|
+
// Mark all channels as unsubscribed
|
|
146
|
+
for (const state of this.channels.values()) {
|
|
147
|
+
state.subscribed = false;
|
|
148
|
+
}
|
|
149
|
+
// Attempt reconnection with exponential backoff
|
|
150
|
+
if (this.channels.size > 0) {
|
|
151
|
+
this.scheduleReconnect();
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
this.ws.onerror = () => {
|
|
155
|
+
// onclose will fire after onerror
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
scheduleReconnect() {
|
|
159
|
+
if (this.reconnectAttempts >= this.maxReconnectAttempts) {
|
|
160
|
+
for (const state of this.channels.values()) {
|
|
161
|
+
state.statusCallback?.("TIMED_OUT");
|
|
162
|
+
}
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const delay = this.baseReconnectDelay * Math.pow(2, this.reconnectAttempts);
|
|
166
|
+
this.reconnectAttempts++;
|
|
167
|
+
for (const state of this.channels.values()) {
|
|
168
|
+
state.statusCallback?.("SUBSCRIBING");
|
|
169
|
+
}
|
|
170
|
+
this.reconnectTimer = setTimeout(() => {
|
|
171
|
+
this.ensureConnection();
|
|
172
|
+
}, delay);
|
|
173
|
+
}
|
|
174
|
+
handleServerMessage(msg) {
|
|
175
|
+
switch (msg.type) {
|
|
176
|
+
case "system":
|
|
177
|
+
this.handleSystem(msg);
|
|
178
|
+
break;
|
|
179
|
+
case "change":
|
|
180
|
+
this.handleChange(msg);
|
|
181
|
+
break;
|
|
182
|
+
case "presence":
|
|
183
|
+
this.handlePresence(msg);
|
|
184
|
+
break;
|
|
185
|
+
case "broadcast":
|
|
186
|
+
this.handleBroadcastMsg(msg);
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
handleSystem(msg) {
|
|
191
|
+
// When server confirms subscription
|
|
192
|
+
if (msg.status === "ok" && msg.message.startsWith("subscribed to ")) {
|
|
193
|
+
const channel = msg.message.replace("subscribed to ", "");
|
|
194
|
+
const state = this.channels.get(channel);
|
|
195
|
+
if (state) {
|
|
196
|
+
state.subscribed = true;
|
|
197
|
+
state.statusCallback?.("SUBSCRIBED");
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
if (msg.status === "error") {
|
|
201
|
+
// Find which channel this error relates to — broadcast to all as fallback
|
|
202
|
+
for (const state of this.channels.values()) {
|
|
203
|
+
state.statusCallback?.("CHANNEL_ERROR");
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
handleChange(msg) {
|
|
208
|
+
const state = this.channels.get(msg.channel);
|
|
209
|
+
if (!state)
|
|
210
|
+
return;
|
|
211
|
+
// Parse channel to get schema/table
|
|
212
|
+
const parts = msg.channel.split(":");
|
|
213
|
+
const schema = parts.length >= 2 ? parts[0] : "public";
|
|
214
|
+
const table = parts.length >= 2 ? parts[1] : parts[0];
|
|
215
|
+
const payload = {
|
|
216
|
+
eventType: msg.event,
|
|
217
|
+
new: msg.payload.new,
|
|
218
|
+
old: msg.payload.old,
|
|
219
|
+
schema,
|
|
220
|
+
table,
|
|
221
|
+
commitTimestamp: msg.timestamp,
|
|
222
|
+
};
|
|
223
|
+
for (const listener of state.pgListeners) {
|
|
224
|
+
if (listener.event !== "*" && listener.event !== msg.event)
|
|
225
|
+
continue;
|
|
226
|
+
listener.callback(payload);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
handlePresence(msg) {
|
|
230
|
+
const state = this.channels.get(msg.channel);
|
|
231
|
+
if (!state)
|
|
232
|
+
return;
|
|
233
|
+
for (const listener of state.presenceListeners) {
|
|
234
|
+
listener({ joins: msg.joins, leaves: msg.leaves });
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
handleBroadcastMsg(msg) {
|
|
238
|
+
const state = this.channels.get(msg.channel);
|
|
239
|
+
if (!state)
|
|
240
|
+
return;
|
|
241
|
+
const listeners = state.broadcastListeners.get(msg.event);
|
|
242
|
+
if (listeners) {
|
|
243
|
+
for (const listener of listeners) {
|
|
244
|
+
listener(msg.payload);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
sendSubscribe(state) {
|
|
249
|
+
if (!this.ws || this.ws.readyState !== WebSocket.OPEN)
|
|
250
|
+
return;
|
|
251
|
+
// Build filter from the first pg listener (if any)
|
|
252
|
+
const firstListener = state.pgListeners[0];
|
|
253
|
+
const filter = {};
|
|
254
|
+
if (firstListener?.filter) {
|
|
255
|
+
// Parse PostgREST filter format "column=eq.value" into { column: "eq.value" }
|
|
256
|
+
for (const part of firstListener.filter.split(",")) {
|
|
257
|
+
const eqIdx = part.indexOf("=");
|
|
258
|
+
if (eqIdx > 0) {
|
|
259
|
+
filter[part.slice(0, eqIdx)] = part.slice(eqIdx + 1);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
this.sendMessage({
|
|
264
|
+
type: "subscribe",
|
|
265
|
+
channel: state.name,
|
|
266
|
+
event: firstListener?.event ?? "*",
|
|
267
|
+
filter: Object.keys(filter).length > 0 ? filter : undefined,
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
sendUnsubscribe(state) {
|
|
271
|
+
this.sendMessage({ type: "unsubscribe", channel: state.name });
|
|
272
|
+
}
|
|
273
|
+
sendMessage(msg) {
|
|
274
|
+
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
|
275
|
+
this.ws.send(JSON.stringify(msg));
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|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AG,CAAC,CAAC,CAAA;gBACtD,CAAC;YACH,CAAC;QACH,CAAC;QAED,IAAI,CAAC,WAAW,CAAC;YACf,IAAI,EAAE,WAAW;YACjB,OAAO,EAAE,KAAK,CAAC,IAAI;YACnB,KAAK,EAAE,aAAa,EAAE,KAAK,IAAI,GAAG;YAClC,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS;SAC5D,CAAC,CAAA;IACJ,CAAC;IAEO,eAAe,CAAC,KAAmB;QACzC,IAAI,CAAC,WAAW,CAAC,EAAE,IAAI,EAAE,aAAa,EAAE,OAAO,EAAE,KAAK,CAAC,IAAI,EAAE,CAAC,CAAA;IAChE,CAAC;IAEO,WAAW,CAAC,GAA4B;QAC9C,IAAI,IAAI,CAAC,EAAE,IAAI,IAAI,CAAC,EAAE,CAAC,UAAU,KAAK,SAAS,CAAC,IAAI,EAAE,CAAC;YACrD,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAA;QACnC,CAAC;IACH,CAAC;CACF"}
|
package/dist/retry.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retry configuration module.
|
|
3
|
+
*
|
|
4
|
+
* Re-exports the core fetchWithRetry and provides a factory that binds
|
|
5
|
+
* client-level retry/timeout options so callers don't repeat them.
|
|
6
|
+
*/
|
|
7
|
+
import { type FetchOptions } from "./fetch-with-retry.js";
|
|
8
|
+
export type { FetchOptions } from "./fetch-with-retry.js";
|
|
9
|
+
export interface RetryConfig {
|
|
10
|
+
/** Disable automatic retry. When false, no retries are attempted. */
|
|
11
|
+
retry?: boolean | undefined;
|
|
12
|
+
/** Request timeout in milliseconds. Default: 30000. */
|
|
13
|
+
timeout?: number | undefined;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Create a fetch function pre-configured with client-level retry/timeout.
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* const doFetch = createRetryFetch({ retry: true, timeout: 10000 })
|
|
20
|
+
* const res = await doFetch('https://api.example.com/data', { method: 'GET' })
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare function createRetryFetch(config: RetryConfig): (url: string, init?: RequestInit & Partial<FetchOptions>) => Promise<Response>;
|
|
24
|
+
//# sourceMappingURL=retry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retry.d.ts","sourceRoot":"","sources":["../src/retry.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAkB,KAAK,YAAY,EAAE,MAAM,uBAAuB,CAAA;AAEzE,YAAY,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAA;AAEzD,MAAM,WAAW,WAAW;IAC1B,qEAAqE;IACrE,KAAK,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;IAC3B,uDAAuD;IACvD,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAC7B;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,WAAW,IAC1C,KAAK,MAAM,EAAE,OAAO,WAAW,GAAG,OAAO,CAAC,YAAY,CAAC,KAAG,OAAO,CAAC,QAAQ,CAAC,CAOpF"}
|
package/dist/retry.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retry configuration module.
|
|
3
|
+
*
|
|
4
|
+
* Re-exports the core fetchWithRetry and provides a factory that binds
|
|
5
|
+
* client-level retry/timeout options so callers don't repeat them.
|
|
6
|
+
*/
|
|
7
|
+
import { fetchWithRetry } from "./fetch-with-retry.js";
|
|
8
|
+
/**
|
|
9
|
+
* Create a fetch function pre-configured with client-level retry/timeout.
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* const doFetch = createRetryFetch({ retry: true, timeout: 10000 })
|
|
13
|
+
* const res = await doFetch('https://api.example.com/data', { method: 'GET' })
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export function createRetryFetch(config) {
|
|
17
|
+
return (url, init) => {
|
|
18
|
+
return fetchWithRetry(url, {
|
|
19
|
+
...init,
|
|
20
|
+
retry: init?.retry ?? config.retry,
|
|
21
|
+
timeout: init?.timeout ?? config.timeout,
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=retry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retry.js","sourceRoot":"","sources":["../src/retry.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,cAAc,EAAqB,MAAM,uBAAuB,CAAA;AAWzE;;;;;;;GAOG;AACH,MAAM,UAAU,gBAAgB,CAAC,MAAmB;IAClD,OAAO,CAAC,GAAW,EAAE,IAA0C,EAAqB,EAAE;QACpF,OAAO,cAAc,CAAC,GAAG,EAAE;YACzB,GAAG,IAAI;YACP,KAAK,EAAE,IAAI,EAAE,KAAK,IAAI,MAAM,CAAC,KAAK;YAClC,OAAO,EAAE,IAAI,EAAE,OAAO,IAAI,MAAM,CAAC,OAAO;SACzC,CAAC,CAAA;IACJ,CAAC,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Serverless connection documentation — Gap Appendices task 84
|
|
3
|
+
*
|
|
4
|
+
* Code-accessible documentation for connection modes.
|
|
5
|
+
* Rendered in Studio's getting-started view and available to CLI help output.
|
|
6
|
+
*/
|
|
7
|
+
export interface ConnectionModeDoc {
|
|
8
|
+
/** Short label for this mode. */
|
|
9
|
+
label: string;
|
|
10
|
+
/** When to use this mode (environment/context). */
|
|
11
|
+
recommended: string;
|
|
12
|
+
/** Whether this mode is safe in serverless contexts. */
|
|
13
|
+
serverlessSafe: boolean;
|
|
14
|
+
/** How it handles connection pooling. */
|
|
15
|
+
pooling: string;
|
|
16
|
+
/** Brief description. */
|
|
17
|
+
description: string;
|
|
18
|
+
/** Example code snippet (TypeScript). */
|
|
19
|
+
example: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Documents the two primary ways to connect to a Supatype project,
|
|
23
|
+
* with clear guidance on serverless vs. long-lived server environments.
|
|
24
|
+
*/
|
|
25
|
+
export declare const CONNECTION_MODES: readonly ConnectionModeDoc[];
|
|
26
|
+
/**
|
|
27
|
+
* A short, actionable warning suitable for display in Studio banners,
|
|
28
|
+
* CLI output, and getting-started guides.
|
|
29
|
+
*/
|
|
30
|
+
export declare const SERVERLESS_CONNECTION_WARNING: string;
|
|
31
|
+
/**
|
|
32
|
+
* Structured FAQ entries for the connection modes documentation.
|
|
33
|
+
*/
|
|
34
|
+
export declare const CONNECTION_FAQ: ReadonlyArray<{
|
|
35
|
+
question: string;
|
|
36
|
+
answer: string;
|
|
37
|
+
}>;
|
|
38
|
+
//# sourceMappingURL=serverless-docs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serverless-docs.d.ts","sourceRoot":"","sources":["../src/serverless-docs.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,WAAW,iBAAiB;IAChC,iCAAiC;IACjC,KAAK,EAAE,MAAM,CAAA;IACb,mDAAmD;IACnD,WAAW,EAAE,MAAM,CAAA;IACnB,wDAAwD;IACxD,cAAc,EAAE,OAAO,CAAA;IACvB,yCAAyC;IACzC,OAAO,EAAE,MAAM,CAAA;IACf,yBAAyB;IACzB,WAAW,EAAE,MAAM,CAAA;IACnB,yCAAyC;IACzC,OAAO,EAAE,MAAM,CAAA;CAChB;AAED;;;GAGG;AACH,eAAO,MAAM,gBAAgB,EAAE,SAAS,iBAAiB,EAuC/C,CAAA;AAEV;;;GAGG;AACH,eAAO,MAAM,6BAA6B,QAIwC,CAAA;AAElF;;GAEG;AACH,eAAO,MAAM,cAAc,EAAE,aAAa,CAAC;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CA0B9E,CAAA"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Serverless connection documentation — Gap Appendices task 84
|
|
3
|
+
*
|
|
4
|
+
* Code-accessible documentation for connection modes.
|
|
5
|
+
* Rendered in Studio's getting-started view and available to CLI help output.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Documents the two primary ways to connect to a Supatype project,
|
|
9
|
+
* with clear guidance on serverless vs. long-lived server environments.
|
|
10
|
+
*/
|
|
11
|
+
export const CONNECTION_MODES = [
|
|
12
|
+
{
|
|
13
|
+
label: "Supatype SDK (via PostgREST)",
|
|
14
|
+
recommended: "Serverless environments: Next.js API routes, Vercel Edge Functions, AWS Lambda, Cloudflare Workers, Deno Deploy",
|
|
15
|
+
serverlessSafe: true,
|
|
16
|
+
pooling: "PostgREST maintains a persistent connection pool to Postgres. Each SDK request is an HTTP call that reuses an existing database connection from the pool.",
|
|
17
|
+
description: "Use the Supatype SDK in serverless environments. PostgREST handles connection pooling for you. " +
|
|
18
|
+
"Each serverless invocation makes an HTTP request to PostgREST, which maps it to a pre-established " +
|
|
19
|
+
"database connection. This avoids the connection-per-invocation problem entirely.",
|
|
20
|
+
example: [
|
|
21
|
+
'import { createClient } from "@supatype/client"',
|
|
22
|
+
"",
|
|
23
|
+
"const client = createClient(process.env.SUPATYPE_URL!, process.env.SUPATYPE_ANON_KEY!)",
|
|
24
|
+
"",
|
|
25
|
+
"// Safe in serverless — each call is an HTTP request, not a new DB connection",
|
|
26
|
+
"const users = await client.from(\"users\").select()",
|
|
27
|
+
].join("\n"),
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
label: "Direct database connection",
|
|
31
|
+
recommended: "Long-lived servers: Express/Fastify/Hono on a VM or container, background workers, migration scripts, CLI tools",
|
|
32
|
+
serverlessSafe: false,
|
|
33
|
+
pooling: "Each new connection opens a TCP socket to Postgres. In serverless, each invocation creates a new connection that is never reused, exhausting the connection pool within seconds under load.",
|
|
34
|
+
description: "Do NOT use direct database connections from serverless environments. " +
|
|
35
|
+
"Each invocation opens a new connection, which will exhaust your connection pool " +
|
|
36
|
+
"within seconds under load. Direct connections are only appropriate for long-lived " +
|
|
37
|
+
"server processes that open a connection once and reuse it across many requests.",
|
|
38
|
+
example: [
|
|
39
|
+
'// ONLY for long-lived servers — NOT for serverless',
|
|
40
|
+
'import pg from "pg"',
|
|
41
|
+
"",
|
|
42
|
+
"const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL })",
|
|
43
|
+
"",
|
|
44
|
+
"// Connection is reused across requests in a long-lived process",
|
|
45
|
+
"const result = await pool.query(\"SELECT * FROM users\")",
|
|
46
|
+
].join("\n"),
|
|
47
|
+
},
|
|
48
|
+
];
|
|
49
|
+
/**
|
|
50
|
+
* A short, actionable warning suitable for display in Studio banners,
|
|
51
|
+
* CLI output, and getting-started guides.
|
|
52
|
+
*/
|
|
53
|
+
export const SERVERLESS_CONNECTION_WARNING = "Use the Supatype SDK (via PostgREST) in serverless environments (Next.js API routes, " +
|
|
54
|
+
"Vercel Edge Functions, AWS Lambda). PostgREST handles connection pooling for you. " +
|
|
55
|
+
"Do NOT use direct database connections from serverless — each invocation opens a new " +
|
|
56
|
+
"connection, which will exhaust your connection pool within seconds under load.";
|
|
57
|
+
/**
|
|
58
|
+
* Structured FAQ entries for the connection modes documentation.
|
|
59
|
+
*/
|
|
60
|
+
export const CONNECTION_FAQ = [
|
|
61
|
+
{
|
|
62
|
+
question: "Can I use Prisma/Drizzle/Knex in serverless?",
|
|
63
|
+
answer: "These ORMs require a direct database connection. In serverless, use the Supatype SDK instead. " +
|
|
64
|
+
"If you must use an ORM, deploy it on a long-lived server (container, VM) or use a connection " +
|
|
65
|
+
"pooler like PgBouncer in transaction mode.",
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
question: "What about Edge Functions?",
|
|
69
|
+
answer: "Edge Functions (Vercel Edge, Cloudflare Workers) are serverless. Use the Supatype SDK. " +
|
|
70
|
+
"Direct TCP connections are not available in most edge runtimes anyway.",
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
question: "How many connections does PostgREST use?",
|
|
74
|
+
answer: "PostgREST maintains a configurable pool (default: 10 connections). All incoming HTTP requests " +
|
|
75
|
+
"share this pool, so even thousands of concurrent SDK requests only use 10 database connections.",
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
question: "When should I use a direct connection?",
|
|
79
|
+
answer: "Use direct connections for: migration scripts, long-lived API servers (Express, Fastify), " +
|
|
80
|
+
"background job processors, and CLI tooling. These processes start once and reuse connections.",
|
|
81
|
+
},
|
|
82
|
+
];
|
|
83
|
+
//# sourceMappingURL=serverless-docs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serverless-docs.js","sourceRoot":"","sources":["../src/serverless-docs.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAiBH;;;GAGG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAiC;IAC5D;QACE,KAAK,EAAE,8BAA8B;QACrC,WAAW,EAAE,iHAAiH;QAC9H,cAAc,EAAE,IAAI;QACpB,OAAO,EAAE,2JAA2J;QACpK,WAAW,EACT,iGAAiG;YACjG,oGAAoG;YACpG,kFAAkF;QACpF,OAAO,EAAE;YACP,iDAAiD;YACjD,EAAE;YACF,wFAAwF;YACxF,EAAE;YACF,+EAA+E;YAC/E,qDAAqD;SACtD,CAAC,IAAI,CAAC,IAAI,CAAC;KACb;IACD;QACE,KAAK,EAAE,4BAA4B;QACnC,WAAW,EAAE,iHAAiH;QAC9H,cAAc,EAAE,KAAK;QACrB,OAAO,EAAE,6LAA6L;QACtM,WAAW,EACT,uEAAuE;YACvE,kFAAkF;YAClF,oFAAoF;YACpF,iFAAiF;QACnF,OAAO,EAAE;YACP,qDAAqD;YACrD,qBAAqB;YACrB,EAAE;YACF,0EAA0E;YAC1E,EAAE;YACF,iEAAiE;YACjE,0DAA0D;SAC3D,CAAC,IAAI,CAAC,IAAI,CAAC;KACb;CACO,CAAA;AAEV;;;GAGG;AACH,MAAM,CAAC,MAAM,6BAA6B,GACxC,uFAAuF;IACvF,oFAAoF;IACpF,uFAAuF;IACvF,gFAAgF,CAAA;AAElF;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAwD;IACjF;QACE,QAAQ,EAAE,8CAA8C;QACxD,MAAM,EACJ,gGAAgG;YAChG,+FAA+F;YAC/F,4CAA4C;KAC/C;IACD;QACE,QAAQ,EAAE,4BAA4B;QACtC,MAAM,EACJ,yFAAyF;YACzF,wEAAwE;KAC3E;IACD;QACE,QAAQ,EAAE,0CAA0C;QACpD,MAAM,EACJ,gGAAgG;YAChG,iGAAiG;KACpG;IACD;QACE,QAAQ,EAAE,wCAAwC;QAClD,MAAM,EACJ,4FAA4F;YAC5F,+FAA+F;KAClG;CACF,CAAA"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type { SupatypeError } from "./types.js";
|
|
2
|
+
export interface StorageObject {
|
|
3
|
+
id: string;
|
|
4
|
+
name: string;
|
|
5
|
+
bucket_id: string;
|
|
6
|
+
owner?: string | undefined;
|
|
7
|
+
created_at: string;
|
|
8
|
+
updated_at: string;
|
|
9
|
+
last_accessed_at: string;
|
|
10
|
+
metadata?: Record<string, unknown> | undefined;
|
|
11
|
+
}
|
|
12
|
+
export interface TransformOptions {
|
|
13
|
+
width?: number | undefined;
|
|
14
|
+
height?: number | undefined;
|
|
15
|
+
format?: "webp" | "avif" | "png" | "jpeg" | undefined;
|
|
16
|
+
quality?: number | undefined;
|
|
17
|
+
resize?: "cover" | "contain" | "fill" | "inside" | "outside" | undefined;
|
|
18
|
+
}
|
|
19
|
+
export interface StorageBucketMeta {
|
|
20
|
+
id: string;
|
|
21
|
+
name: string;
|
|
22
|
+
public: boolean;
|
|
23
|
+
file_size_limit: number | null;
|
|
24
|
+
allowed_mime_types: string[] | null;
|
|
25
|
+
created_at: string;
|
|
26
|
+
updated_at: string;
|
|
27
|
+
}
|
|
28
|
+
export declare class StorageClient {
|
|
29
|
+
private readonly url;
|
|
30
|
+
private readonly headers;
|
|
31
|
+
constructor(url: string, headers: Record<string, string>);
|
|
32
|
+
from(bucket: string): BucketClient;
|
|
33
|
+
listBuckets(): Promise<{
|
|
34
|
+
data: StorageBucketMeta[] | null;
|
|
35
|
+
error: SupatypeError | null;
|
|
36
|
+
}>;
|
|
37
|
+
createBucket(name: string, options?: {
|
|
38
|
+
public?: boolean | undefined;
|
|
39
|
+
fileSizeLimit?: number | undefined;
|
|
40
|
+
allowedMimeTypes?: string[] | undefined;
|
|
41
|
+
} | undefined): Promise<{
|
|
42
|
+
data: {
|
|
43
|
+
name: string;
|
|
44
|
+
} | null;
|
|
45
|
+
error: SupatypeError | null;
|
|
46
|
+
}>;
|
|
47
|
+
}
|
|
48
|
+
export declare class BucketClient {
|
|
49
|
+
private readonly url;
|
|
50
|
+
private readonly bucket;
|
|
51
|
+
private readonly headers;
|
|
52
|
+
constructor(url: string, bucket: string, headers: Record<string, string>);
|
|
53
|
+
upload(path: string, file: Blob | File | ArrayBuffer, options?: {
|
|
54
|
+
contentType?: string | undefined;
|
|
55
|
+
upsert?: boolean | undefined;
|
|
56
|
+
} | undefined): Promise<{
|
|
57
|
+
data: {
|
|
58
|
+
path: string;
|
|
59
|
+
} | null;
|
|
60
|
+
error: SupatypeError | null;
|
|
61
|
+
}>;
|
|
62
|
+
download(path: string, options?: {
|
|
63
|
+
transform?: TransformOptions | undefined;
|
|
64
|
+
} | undefined): Promise<{
|
|
65
|
+
data: Blob | null;
|
|
66
|
+
error: SupatypeError | null;
|
|
67
|
+
}>;
|
|
68
|
+
getPublicUrl(path: string, options?: {
|
|
69
|
+
transform?: TransformOptions | undefined;
|
|
70
|
+
} | undefined): {
|
|
71
|
+
data: {
|
|
72
|
+
publicUrl: string;
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
createSignedUrl(path: string, expiresIn: number): Promise<{
|
|
76
|
+
data: {
|
|
77
|
+
signedUrl: string;
|
|
78
|
+
} | null;
|
|
79
|
+
error: SupatypeError | null;
|
|
80
|
+
}>;
|
|
81
|
+
createSignedUrls(paths: string[], expiresIn: number): Promise<{
|
|
82
|
+
data: {
|
|
83
|
+
path: string;
|
|
84
|
+
signedUrl: string;
|
|
85
|
+
error: string | null;
|
|
86
|
+
}[] | null;
|
|
87
|
+
error: SupatypeError | null;
|
|
88
|
+
}>;
|
|
89
|
+
remove(paths: string[]): Promise<{
|
|
90
|
+
data: StorageObject[] | null;
|
|
91
|
+
error: SupatypeError | null;
|
|
92
|
+
}>;
|
|
93
|
+
list(prefix?: string | undefined, options?: {
|
|
94
|
+
limit?: number | undefined;
|
|
95
|
+
offset?: number | undefined;
|
|
96
|
+
} | undefined): Promise<{
|
|
97
|
+
data: StorageObject[] | null;
|
|
98
|
+
error: SupatypeError | null;
|
|
99
|
+
}>;
|
|
100
|
+
private buildObjectUrl;
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=storage.d.ts.map
|