@supatype/client 0.2.1 → 0.3.1
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 +1 -1
- package/.turbo/turbo-test.log +25 -21
- package/.turbo/turbo-typecheck.log +1 -1
- package/dist/auth.d.ts +1 -1
- package/dist/auth.d.ts.map +1 -1
- package/dist/identity.d.ts +29 -0
- package/dist/identity.d.ts.map +1 -0
- package/dist/identity.js +30 -0
- package/dist/identity.js.map +1 -0
- package/dist/index.d.ts +46 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +96 -14
- package/dist/index.js.map +1 -1
- package/dist/preview.d.ts +45 -0
- package/dist/preview.d.ts.map +1 -0
- package/dist/preview.js +92 -0
- package/dist/preview.js.map +1 -0
- package/dist/query-cache.d.ts.map +1 -1
- package/dist/query-cache.js +2 -8
- package/dist/query-cache.js.map +1 -1
- package/dist/query.d.ts +12 -0
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +23 -1
- package/dist/query.js.map +1 -1
- package/dist/realtime.d.ts +61 -3
- package/dist/realtime.d.ts.map +1 -1
- package/dist/realtime.js +162 -22
- package/dist/realtime.js.map +1 -1
- package/dist/storage.d.ts +27 -2
- package/dist/storage.d.ts.map +1 -1
- package/dist/storage.js +35 -8
- package/dist/storage.js.map +1 -1
- package/dist/types.d.ts +50 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +9 -1
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
- package/src/auth.ts +1 -1
- package/src/identity.ts +43 -0
- package/src/index.ts +123 -16
- package/src/preview.ts +102 -0
- package/src/query-cache.ts +2 -9
- package/src/query.ts +23 -1
- package/src/realtime.ts +166 -25
- package/src/storage.ts +42 -12
- package/src/types.ts +52 -0
- package/tests/draft-profile.test.ts +296 -0
- package/tests/helpers/mock-websocket.ts +70 -0
- package/tests/identity-change.test.ts +120 -0
- package/tests/realtime-multi-tenant-integration.test.ts +5 -50
- package/tests/realtime-session-auth.test.ts +177 -0
- package/tests/signed-url-absolute.test.ts +84 -0
- package/tests/storage-quota-integration.test.ts +2 -1
- package/tests/storage-upload-limit-integration.test.ts +2 -1
- package/tests/subscribe-frame.test.ts +102 -0
- package/tsconfig.tsbuildinfo +1 -1
package/src/realtime.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { asHeadersProvider, bearerToken, type HeadersProvider } from "./query.js"
|
|
2
|
+
|
|
1
3
|
export type RealtimeEvent = "INSERT" | "UPDATE" | "DELETE" | "*"
|
|
2
4
|
|
|
3
5
|
export interface RealtimePayload<TRow> {
|
|
@@ -86,18 +88,100 @@ type ServerMessage = ServerChangeMessage | ServerPresenceMessage | ServerBroadca
|
|
|
86
88
|
|
|
87
89
|
export class RealtimeClient {
|
|
88
90
|
private readonly url: string
|
|
89
|
-
private readonly
|
|
91
|
+
private readonly getHeaders: HeadersProvider
|
|
90
92
|
private ws: WebSocket | null = null
|
|
91
93
|
private channels = new Map<string, ChannelState>()
|
|
92
94
|
private reconnectTimer: ReturnType<typeof setTimeout> | null = null
|
|
93
95
|
private reconnectAttempts = 0
|
|
94
96
|
private maxReconnectAttempts = 10
|
|
95
97
|
private baseReconnectDelay = 1000
|
|
96
|
-
|
|
97
|
-
|
|
98
|
+
/** The token the live socket was opened with, so an auth change can tell whether it still holds. */
|
|
99
|
+
private currentToken: string | undefined
|
|
100
|
+
/**
|
|
101
|
+
* The connect in flight, used as its own identity.
|
|
102
|
+
*
|
|
103
|
+
* A counter was tried first and produced a socket that never came back: abandoning an attempt
|
|
104
|
+
* bumped the counter but left this field set, so the replacement connect returned at the
|
|
105
|
+
* one-at-a-time guard, and the abandoned attempt then bailed on the counter. Nothing was
|
|
106
|
+
* connected and nothing was scheduled. Clearing this field both abandons an attempt and unblocks
|
|
107
|
+
* the next one, so the two cannot disagree.
|
|
108
|
+
*/
|
|
109
|
+
private attempt: Promise<void> | null = null
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* `auth` is either a headers provider or a fixed set of headers: the same pair `QueryBuilder` and
|
|
113
|
+
* `MutationBuilder` accept, normalised by the same helper.
|
|
114
|
+
*
|
|
115
|
+
* The provider is what `createClient` passes, and it is why this takes two shapes. A fixed set of
|
|
116
|
+
* headers is read once at construction, so a socket opened before anybody signed in stayed anon
|
|
117
|
+
* for the life of the page: on a table that is not anon-readable it reported SUBSCRIBED and then
|
|
118
|
+
* delivered nothing, which is indistinguishable from a quiet table.
|
|
119
|
+
*/
|
|
120
|
+
constructor(url: string, auth: Record<string, string> | HeadersProvider) {
|
|
98
121
|
// Convert http(s) URL to ws(s) URL
|
|
99
122
|
this.url = url.replace(/^http/, "ws")
|
|
100
|
-
this.
|
|
123
|
+
this.getHeaders = asHeadersProvider(auth)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** The token this socket should authenticate with, or undefined when there is none. */
|
|
127
|
+
private async resolveToken(): Promise<string | undefined> {
|
|
128
|
+
const token = bearerToken(await this.getHeaders())
|
|
129
|
+
return token === "" ? undefined : token
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Re-open the socket if the token it was opened with is no longer the current one.
|
|
134
|
+
*
|
|
135
|
+
* `createClient` calls this on every auth change. Without it the token preference above would
|
|
136
|
+
* still not be enough: every binding subscribes on mount, which is before the user has signed in,
|
|
137
|
+
* so the socket that matters is always one that was opened while signed out.
|
|
138
|
+
*/
|
|
139
|
+
async reauthenticate(): Promise<void> {
|
|
140
|
+
if (this.ws === null && this.attempt === null) return
|
|
141
|
+
let token: string | undefined
|
|
142
|
+
try {
|
|
143
|
+
token = await this.resolveToken()
|
|
144
|
+
} catch {
|
|
145
|
+
return
|
|
146
|
+
}
|
|
147
|
+
if (token === this.currentToken) return
|
|
148
|
+
// Passed on rather than resolved a second time, so the socket opens with the token that
|
|
149
|
+
// justified opening it. Resolving twice left `currentToken` describing a token nobody checked.
|
|
150
|
+
this.cycleConnection(token)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Abandon the current socket and immediately open a replacement, keeping every channel. */
|
|
154
|
+
private cycleConnection(token: string | undefined): void {
|
|
155
|
+
this.abandonSocket("reauthenticating")
|
|
156
|
+
for (const state of this.channels.values()) {
|
|
157
|
+
state.subscribed = false
|
|
158
|
+
}
|
|
159
|
+
this.ensureConnection(token)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Drop the socket and any connect in flight, without deciding what happens next.
|
|
164
|
+
*
|
|
165
|
+
* Handlers are detached before closing, because `onclose` schedules a backoff reconnect that
|
|
166
|
+
* would otherwise race whatever the caller does instead.
|
|
167
|
+
*/
|
|
168
|
+
private abandonSocket(reason: string): void {
|
|
169
|
+
this.attempt = null
|
|
170
|
+
if (this.reconnectTimer !== null) {
|
|
171
|
+
clearTimeout(this.reconnectTimer)
|
|
172
|
+
this.reconnectTimer = null
|
|
173
|
+
}
|
|
174
|
+
const old = this.ws
|
|
175
|
+
this.ws = null
|
|
176
|
+
this.currentToken = undefined
|
|
177
|
+
if (old !== null) {
|
|
178
|
+
old.onopen = null
|
|
179
|
+
old.onmessage = null
|
|
180
|
+
old.onclose = null
|
|
181
|
+
old.onerror = null
|
|
182
|
+
old.close(1000, reason)
|
|
183
|
+
}
|
|
184
|
+
this.reconnectAttempts = 0
|
|
101
185
|
}
|
|
102
186
|
|
|
103
187
|
channel<TRow = Record<string, unknown>>(name: string): ChannelSubscription<TRow> {
|
|
@@ -184,17 +268,16 @@ export class RealtimeClient {
|
|
|
184
268
|
}
|
|
185
269
|
}
|
|
186
270
|
|
|
187
|
-
/**
|
|
271
|
+
/**
|
|
272
|
+
* Disconnect the WebSocket entirely.
|
|
273
|
+
*
|
|
274
|
+
* Shares `abandonSocket` with the re-authentication path, which is what stops an explicit
|
|
275
|
+
* disconnect from scheduling its own reconnect: the close handler is detached first. It also
|
|
276
|
+
* cancels a connect still waiting on its token, which would otherwise install a socket after the
|
|
277
|
+
* caller asked for none.
|
|
278
|
+
*/
|
|
188
279
|
disconnect(): void {
|
|
189
|
-
|
|
190
|
-
clearTimeout(this.reconnectTimer)
|
|
191
|
-
this.reconnectTimer = null
|
|
192
|
-
}
|
|
193
|
-
if (this.ws) {
|
|
194
|
-
this.ws.close(1000, "client disconnect")
|
|
195
|
-
this.ws = null
|
|
196
|
-
}
|
|
197
|
-
this.reconnectAttempts = 0
|
|
280
|
+
this.abandonSocket("client disconnect")
|
|
198
281
|
}
|
|
199
282
|
|
|
200
283
|
// ─── Internal ──────────────────────────────────────────────────────────
|
|
@@ -215,18 +298,65 @@ export class RealtimeClient {
|
|
|
215
298
|
return state
|
|
216
299
|
}
|
|
217
300
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
301
|
+
/** True while a socket exists and is usable. */
|
|
302
|
+
private isLive(): boolean {
|
|
303
|
+
if (this.ws === null) return false
|
|
304
|
+
return this.ws.readyState === WebSocket.OPEN || this.ws.readyState === WebSocket.CONNECTING
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
private ensureConnection(token?: string | undefined): void {
|
|
308
|
+
// One connect at a time: resolving the token is asynchronous, so two subscribes in the same
|
|
309
|
+
// tick would otherwise each open a socket and the second would orphan the first.
|
|
310
|
+
if (this.isLive() || this.attempt !== null) return
|
|
311
|
+
|
|
312
|
+
let attempt: Promise<void>
|
|
313
|
+
const isCurrent = (): boolean => this.attempt === attempt
|
|
314
|
+
// Started on a microtask so `this.attempt` is assigned before `openSocket` can ask whether it
|
|
315
|
+
// is still the current one. Called directly, the path that already has a token reaches that
|
|
316
|
+
// question with no await in front of it, compares against a handle nobody had stored yet, and
|
|
317
|
+
// abandons itself.
|
|
318
|
+
attempt = Promise.resolve().then(() => this.openSocket(isCurrent, token))
|
|
319
|
+
this.attempt = attempt
|
|
320
|
+
void attempt.finally(() => {
|
|
321
|
+
// Only if it is still ours. An attempt abandoned mid-flight must not clear the handle of the
|
|
322
|
+
// replacement that took its place.
|
|
323
|
+
if (this.attempt === attempt) this.attempt = null
|
|
324
|
+
})
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Resolve a token, then open the socket with it.
|
|
329
|
+
*
|
|
330
|
+
* Split out of ensureConnection because the token is asynchronous: the session may have to be
|
|
331
|
+
* refreshed before it is known. `epoch` is read before the await so a socket abandoned in the
|
|
332
|
+
* meantime, by disconnect() or by reauthenticate(), does not install itself afterwards.
|
|
333
|
+
*/
|
|
334
|
+
private async openSocket(
|
|
335
|
+
isCurrent: () => boolean,
|
|
336
|
+
known?: string | undefined,
|
|
337
|
+
): Promise<void> {
|
|
338
|
+
let token = known
|
|
339
|
+
if (token === undefined) {
|
|
340
|
+
try {
|
|
341
|
+
token = await this.resolveToken()
|
|
342
|
+
} catch {
|
|
343
|
+
// A token that cannot be resolved is not a reason to open an unauthenticated socket: the
|
|
344
|
+
// server would accept it and deliver nothing, which reads as a broken table rather than a
|
|
345
|
+
// failed sign-in. Let the backoff try again.
|
|
346
|
+
if (isCurrent() && this.channels.size > 0) this.scheduleReconnect()
|
|
347
|
+
return
|
|
348
|
+
}
|
|
221
349
|
}
|
|
350
|
+
// Abandoned while the token was resolving, by disconnect() or by a re-authentication that has
|
|
351
|
+
// already started its own attempt. Installing this socket now would orphan that one.
|
|
352
|
+
if (!isCurrent()) return
|
|
222
353
|
|
|
223
|
-
|
|
224
|
-
const token = this.headers["apikey"] ?? this.headers["Authorization"]?.replace("Bearer ", "")
|
|
354
|
+
this.currentToken = token
|
|
225
355
|
const wsUrl = token ? `${this.url}?token=${encodeURIComponent(token)}` : this.url
|
|
356
|
+
const ws = new WebSocket(wsUrl)
|
|
357
|
+
this.ws = ws
|
|
226
358
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
this.ws.onopen = () => {
|
|
359
|
+
ws.onopen = () => {
|
|
230
360
|
this.reconnectAttempts = 0
|
|
231
361
|
// Re-subscribe all channels
|
|
232
362
|
for (const state of this.channels.values()) {
|
|
@@ -236,7 +366,7 @@ export class RealtimeClient {
|
|
|
236
366
|
}
|
|
237
367
|
}
|
|
238
368
|
|
|
239
|
-
|
|
369
|
+
ws.onmessage = (event) => {
|
|
240
370
|
try {
|
|
241
371
|
const msg = JSON.parse(String(event.data)) as ServerMessage
|
|
242
372
|
this.handleServerMessage(msg)
|
|
@@ -245,7 +375,7 @@ export class RealtimeClient {
|
|
|
245
375
|
}
|
|
246
376
|
}
|
|
247
377
|
|
|
248
|
-
|
|
378
|
+
ws.onclose = () => {
|
|
249
379
|
this.ws = null
|
|
250
380
|
// Mark all channels as unsubscribed
|
|
251
381
|
for (const state of this.channels.values()) {
|
|
@@ -257,7 +387,7 @@ export class RealtimeClient {
|
|
|
257
387
|
}
|
|
258
388
|
}
|
|
259
389
|
|
|
260
|
-
|
|
390
|
+
ws.onerror = () => {
|
|
261
391
|
// onclose will fire after onerror
|
|
262
392
|
}
|
|
263
393
|
}
|
|
@@ -379,10 +509,21 @@ export class RealtimeClient {
|
|
|
379
509
|
}
|
|
380
510
|
}
|
|
381
511
|
|
|
512
|
+
// `table` and `schema` travel with the subscription, rather than being inferred from the
|
|
513
|
+
// channel name at the other end.
|
|
514
|
+
//
|
|
515
|
+
// They were resolved here and then dropped, and the server falls back to reading the channel
|
|
516
|
+
// name as `schema:table`. So a subscription worked only when the channel happened to be named
|
|
517
|
+
// after its table, which is what `from(table).subscribe()` does. Anything else -- a channel
|
|
518
|
+
// called `lobby-chat` carrying `table: "chat_message"`, which is what `useSubscription` exists
|
|
519
|
+
// to let you write -- registered against a table no change can match. The socket reported
|
|
520
|
+
// SUBSCRIBED and delivered nothing, which is indistinguishable from a quiet table.
|
|
382
521
|
this.sendMessage({
|
|
383
522
|
type: "subscribe",
|
|
384
523
|
channel: state.name,
|
|
385
524
|
event: firstListener?.event ?? "*",
|
|
525
|
+
...(firstListener?.table !== undefined && { table: firstListener.table }),
|
|
526
|
+
...(firstListener?.schema !== undefined && { schema: firstListener.schema }),
|
|
386
527
|
filter: Object.keys(filter).length > 0 ? filter : undefined,
|
|
387
528
|
})
|
|
388
529
|
}
|
package/src/storage.ts
CHANGED
|
@@ -29,11 +29,25 @@ export interface StorageBucketMeta {
|
|
|
29
29
|
updated_at: string
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
/** Resolved fresh per request, so a session that arrives later is used. */
|
|
33
|
+
export type AuthHeaders = () => Promise<Record<string, string>>
|
|
34
|
+
|
|
32
35
|
export class StorageClient {
|
|
33
36
|
private readonly url: string
|
|
34
|
-
private readonly headers:
|
|
37
|
+
private readonly headers: AuthHeaders
|
|
35
38
|
|
|
36
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Headers are asked for per request, not captured here.
|
|
41
|
+
*
|
|
42
|
+
* They used to be a plain object built when the client was constructed, which is before anybody
|
|
43
|
+
* has signed in. Every storage request therefore carried the anon key for the life of the page,
|
|
44
|
+
* whatever session the caller later had: an app's per-user storage policies saw `anon`, and
|
|
45
|
+
* Studio's proxy refused the request outright because an anon key carries no subject.
|
|
46
|
+
*
|
|
47
|
+
* Every other client on this object already took the same function. Storage was the one that did
|
|
48
|
+
* not.
|
|
49
|
+
*/
|
|
50
|
+
constructor(url: string, headers: AuthHeaders) {
|
|
37
51
|
this.url = url
|
|
38
52
|
this.headers = headers
|
|
39
53
|
}
|
|
@@ -44,7 +58,7 @@ export class StorageClient {
|
|
|
44
58
|
|
|
45
59
|
async listBuckets(): Promise<{ data: StorageBucketMeta[] | null; error: SupatypeError | null }> {
|
|
46
60
|
try {
|
|
47
|
-
const res = await fetch(`${this.url}/bucket`, { headers: this.headers })
|
|
61
|
+
const res = await fetch(`${this.url}/bucket`, { headers: await this.headers() })
|
|
48
62
|
if (!res.ok) {
|
|
49
63
|
const err = await res.json().catch(() => ({ message: "Error" })) as { message?: string }
|
|
50
64
|
return { data: null, error: { message: err.message ?? "Failed to list buckets" } }
|
|
@@ -70,7 +84,7 @@ export class StorageClient {
|
|
|
70
84
|
}
|
|
71
85
|
const res = await fetch(`${this.url}/bucket`, {
|
|
72
86
|
method: "POST",
|
|
73
|
-
headers: { ...this.headers, "Content-Type": "application/json" },
|
|
87
|
+
headers: { ...(await this.headers()), "Content-Type": "application/json" },
|
|
74
88
|
body: JSON.stringify(body),
|
|
75
89
|
})
|
|
76
90
|
if (!res.ok) {
|
|
@@ -87,9 +101,9 @@ export class StorageClient {
|
|
|
87
101
|
export class BucketClient {
|
|
88
102
|
private readonly url: string
|
|
89
103
|
private readonly bucket: string
|
|
90
|
-
private readonly headers:
|
|
104
|
+
private readonly headers: AuthHeaders
|
|
91
105
|
|
|
92
|
-
constructor(url: string, bucket: string, headers:
|
|
106
|
+
constructor(url: string, bucket: string, headers: AuthHeaders) {
|
|
93
107
|
this.url = url
|
|
94
108
|
this.bucket = bucket
|
|
95
109
|
this.headers = headers
|
|
@@ -102,7 +116,7 @@ export class BucketClient {
|
|
|
102
116
|
): Promise<{ data: { path: string } | null; error: SupatypeError | null }> {
|
|
103
117
|
const inferredType = file instanceof File && file.type ? file.type : "application/octet-stream"
|
|
104
118
|
const headers: Record<string, string> = {
|
|
105
|
-
...this.headers,
|
|
119
|
+
...(await this.headers()),
|
|
106
120
|
"Content-Type": options?.contentType ?? inferredType,
|
|
107
121
|
}
|
|
108
122
|
if (options?.upsert === true) {
|
|
@@ -125,7 +139,7 @@ export class BucketClient {
|
|
|
125
139
|
options?: { transform?: TransformOptions | undefined } | undefined,
|
|
126
140
|
): Promise<{ data: Blob | null; error: SupatypeError | null }> {
|
|
127
141
|
const url = this.buildObjectUrl("authenticated", path, options?.transform)
|
|
128
|
-
const res = await fetch(url, { headers: this.headers })
|
|
142
|
+
const res = await fetch(url, { headers: await this.headers() })
|
|
129
143
|
if (!res.ok) {
|
|
130
144
|
const err = await res.json().catch(() => ({ message: "Download failed" })) as Record<string, unknown>
|
|
131
145
|
return { data: null, error: { message: String(err["message"] ?? err["error"] ?? "Download failed"), status: res.status } }
|
|
@@ -142,13 +156,29 @@ export class BucketClient {
|
|
|
142
156
|
return { data: { publicUrl } }
|
|
143
157
|
}
|
|
144
158
|
|
|
159
|
+
/**
|
|
160
|
+
* Resolve what the storage API returned against this client's base URL.
|
|
161
|
+
*
|
|
162
|
+
* `getPublicUrl` hands back something fetchable; this used to hand back the server's response
|
|
163
|
+
* verbatim, and the server answers with a path (`/object/sign/...`). So one of the two URL
|
|
164
|
+
* methods returned a URL and the other returned half of one, which fails at the caller as
|
|
165
|
+
* `TypeError: Failed to parse URL` rather than anywhere near here.
|
|
166
|
+
*
|
|
167
|
+
* Absolute answers are passed through untouched, so a deployment whose storage API returns a
|
|
168
|
+
* full URL (a CDN, a signed S3 link) is not mangled by being prefixed a second time.
|
|
169
|
+
*/
|
|
170
|
+
private absoluteObjectUrl(value: string): string {
|
|
171
|
+
if (/^[a-z][a-z0-9+.-]*:\/\//i.test(value)) return value
|
|
172
|
+
return `${this.url.replace(/\/$/, "")}/${value.replace(/^\//, "")}`
|
|
173
|
+
}
|
|
174
|
+
|
|
145
175
|
async createSignedUrl(
|
|
146
176
|
path: string,
|
|
147
177
|
expiresIn: number,
|
|
148
178
|
): Promise<{ data: { signedUrl: string } | null; error: SupatypeError | null }> {
|
|
149
179
|
const res = await fetch(`${this.url}/object/sign/${this.bucket}/${path}`, {
|
|
150
180
|
method: "POST",
|
|
151
|
-
headers: { ...this.headers, "Content-Type": "application/json" },
|
|
181
|
+
headers: { ...(await this.headers()), "Content-Type": "application/json" },
|
|
152
182
|
body: JSON.stringify({ expiresIn }),
|
|
153
183
|
})
|
|
154
184
|
if (!res.ok) {
|
|
@@ -156,7 +186,7 @@ export class BucketClient {
|
|
|
156
186
|
return { data: null, error: { message: String(err["message"] ?? err["error"] ?? "Failed to create signed URL"), status: res.status } }
|
|
157
187
|
}
|
|
158
188
|
const json = await res.json() as { signedURL: string }
|
|
159
|
-
return { data: { signedUrl: json.signedURL }, error: null }
|
|
189
|
+
return { data: { signedUrl: this.absoluteObjectUrl(json.signedURL) }, error: null }
|
|
160
190
|
}
|
|
161
191
|
|
|
162
192
|
async createSignedUrls(
|
|
@@ -178,7 +208,7 @@ export class BucketClient {
|
|
|
178
208
|
async remove(paths: string[]): Promise<{ data: StorageObject[] | null; error: SupatypeError | null }> {
|
|
179
209
|
const res = await fetch(`${this.url}/object/${this.bucket}`, {
|
|
180
210
|
method: "DELETE",
|
|
181
|
-
headers: { ...this.headers, "Content-Type": "application/json" },
|
|
211
|
+
headers: { ...(await this.headers()), "Content-Type": "application/json" },
|
|
182
212
|
body: JSON.stringify({ prefixes: paths }),
|
|
183
213
|
})
|
|
184
214
|
if (!res.ok) {
|
|
@@ -200,7 +230,7 @@ export class BucketClient {
|
|
|
200
230
|
}
|
|
201
231
|
const res = await fetch(`${this.url}/object/list/${this.bucket}`, {
|
|
202
232
|
method: "POST",
|
|
203
|
-
headers: { ...this.headers, "Content-Type": "application/json" },
|
|
233
|
+
headers: { ...(await this.headers()), "Content-Type": "application/json" },
|
|
204
234
|
body: JSON.stringify(body),
|
|
205
235
|
})
|
|
206
236
|
if (!res.ok) {
|
package/src/types.ts
CHANGED
|
@@ -177,6 +177,23 @@ export interface QueryResult<TData> {
|
|
|
177
177
|
|
|
178
178
|
// ─── RPC result ──────────────────────────────────────────────────────────────
|
|
179
179
|
|
|
180
|
+
/**
|
|
181
|
+
* Options for {@link SupatypeClient.rpc}.
|
|
182
|
+
*
|
|
183
|
+
* `schema` is not decoration. PostgREST resolves `/rpc/<name>` against the
|
|
184
|
+
* **default profile**, which is the first schema on the exposed list, so a
|
|
185
|
+
* function living anywhere else is unreachable without naming its schema. The
|
|
186
|
+
* generated publishing functions live in `supatype` for exactly the reason they
|
|
187
|
+
* are generated at all: they are the stack's, not the project's, and putting them
|
|
188
|
+
* in the managed schema would collide with a model called `publish`.
|
|
189
|
+
*/
|
|
190
|
+
export interface RpcOptions {
|
|
191
|
+
head?: boolean | undefined
|
|
192
|
+
count?: "exact" | "planned" | "estimated" | undefined
|
|
193
|
+
/** Postgres schema holding the function, sent as `Content-Profile`. */
|
|
194
|
+
schema?: string | undefined
|
|
195
|
+
}
|
|
196
|
+
|
|
180
197
|
export interface RpcResult<TData> {
|
|
181
198
|
data: TData | null
|
|
182
199
|
error: SupatypeError | null
|
|
@@ -276,9 +293,27 @@ export interface AuthStorage {
|
|
|
276
293
|
removeItem(key: string): void | Promise<void>
|
|
277
294
|
}
|
|
278
295
|
|
|
296
|
+
/**
|
|
297
|
+
* The schema holding one draft view per versioned model, selected by `Accept-Profile`.
|
|
298
|
+
*
|
|
299
|
+
* Stated here as well as in the CLI (`project-config.ts`, which puts it on `PGRST_DB_SCHEMA`) and
|
|
300
|
+
* the engine (which creates the views), because the SDK ships standalone and depending on a
|
|
301
|
+
* workspace package for one identifier would be a worse trade than three call sites naming it.
|
|
302
|
+
* Changing it means changing all three, the same as `api` for the field-masking views.
|
|
303
|
+
*/
|
|
304
|
+
export const DRAFT_SCHEMA = "draft"
|
|
305
|
+
|
|
279
306
|
export interface SelectQueryOptions {
|
|
280
307
|
count?: "exact" | "planned" | "estimated" | undefined
|
|
281
308
|
head?: boolean | undefined
|
|
309
|
+
/**
|
|
310
|
+
* PostgREST schema to read from, sent as `Accept-Profile`.
|
|
311
|
+
*
|
|
312
|
+
* Set by `.draft()` rather than by hand: the point of a profile here is that a draft has the same
|
|
313
|
+
* table name and the same row type as what it is a draft of, so the only thing that changes
|
|
314
|
+
* between reading live content and reading the pending edit is which schema answers.
|
|
315
|
+
*/
|
|
316
|
+
profile?: string | undefined
|
|
282
317
|
}
|
|
283
318
|
|
|
284
319
|
export interface SupatypeClientConfig {
|
|
@@ -329,6 +364,23 @@ export interface SupatypeClientConfig {
|
|
|
329
364
|
* before any requests are made.
|
|
330
365
|
*/
|
|
331
366
|
initialSession?: Session | undefined
|
|
367
|
+
/**
|
|
368
|
+
* A preview link's code, used as this client's whole credential.
|
|
369
|
+
*
|
|
370
|
+
* Set it on a client built to serve one preview request. The code names a link the project can
|
|
371
|
+
* revoke, and the client exchanges it for a token that lives about a minute. That token carries
|
|
372
|
+
* no subject, so the bearer is nobody in particular: the database's own policy decides what they
|
|
373
|
+
* may read, and there is no admin key anywhere on the path.
|
|
374
|
+
*
|
|
375
|
+
* **It wins over a signed-in session, deliberately.** A preview route is opened by whoever was
|
|
376
|
+
* sent the link, and some of them will already be logged in as someone with no access to the
|
|
377
|
+
* draft. Falling back to that session would show them "not found" and send them to ask why the
|
|
378
|
+
* post had been deleted.
|
|
379
|
+
*
|
|
380
|
+
* Refused alongside `serviceRoleKey`: a route that has both is a route sending admin credentials
|
|
381
|
+
* down a path meant for strangers, which is the mistake this feature exists to remove.
|
|
382
|
+
*/
|
|
383
|
+
previewCode?: string | undefined
|
|
332
384
|
/**
|
|
333
385
|
* Extra headers merged into every request (e.g. Studio `X-Supatype-Environment`).
|
|
334
386
|
*/
|