@supatype/client 0.3.5 → 0.7.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/.turbo/turbo-build.log +1 -1
- package/.turbo/turbo-test.log +58 -31
- package/.turbo/turbo-typecheck.log +2 -2
- package/LICENSE +1 -1
- package/dist/auth-redirect.d.ts +48 -0
- package/dist/auth-redirect.d.ts.map +1 -0
- package/dist/auth-redirect.js +70 -0
- package/dist/auth-redirect.js.map +1 -0
- package/dist/auth.d.ts +78 -4
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +373 -274
- package/dist/auth.js.map +1 -1
- package/dist/bindings.d.ts +16 -0
- package/dist/bindings.d.ts.map +1 -0
- package/dist/bindings.js +13 -0
- package/dist/bindings.js.map +1 -0
- package/dist/caller-types.d.ts +18 -0
- package/dist/caller-types.d.ts.map +1 -0
- package/dist/caller-types.js +22 -0
- package/dist/caller-types.js.map +1 -0
- package/dist/draft-create.d.ts +12 -0
- package/dist/draft-create.d.ts.map +1 -0
- package/dist/draft-create.js +47 -0
- package/dist/draft-create.js.map +1 -0
- package/dist/draft-models-registry.d.ts +27 -0
- package/dist/draft-models-registry.d.ts.map +1 -0
- package/dist/draft-models-registry.js +61 -0
- package/dist/draft-models-registry.js.map +1 -0
- package/dist/error-codes-doc.d.ts +1 -1
- package/dist/error-codes-doc.d.ts.map +1 -1
- package/dist/error-codes-doc.js +42 -34
- package/dist/error-codes-doc.js.map +1 -1
- package/dist/errors.d.ts +102 -60
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +69 -28
- package/dist/errors.js.map +1 -1
- package/dist/exact-response.d.ts.map +1 -1
- package/dist/exact-response.js +2 -1
- package/dist/exact-response.js.map +1 -1
- package/dist/exact-values.d.ts +2 -0
- package/dist/exact-values.d.ts.map +1 -1
- package/dist/exact-values.js +12 -10
- package/dist/exact-values.js.map +1 -1
- package/dist/fetch-with-retry.d.ts +21 -6
- package/dist/fetch-with-retry.d.ts.map +1 -1
- package/dist/fetch-with-retry.js +67 -53
- package/dist/fetch-with-retry.js.map +1 -1
- package/dist/field-kinds-registry.d.ts +11 -3
- package/dist/field-kinds-registry.d.ts.map +1 -1
- package/dist/field-kinds-registry.js +10 -6
- package/dist/field-kinds-registry.js.map +1 -1
- package/dist/filters.d.ts +51 -0
- package/dist/filters.d.ts.map +1 -0
- package/dist/filters.js +117 -0
- package/dist/filters.js.map +1 -0
- package/dist/generated-client.d.ts +7 -0
- package/dist/generated-client.d.ts.map +1 -0
- package/dist/generated-client.js +7 -0
- package/dist/generated-client.js.map +1 -0
- package/dist/graphql-document.d.ts +39 -0
- package/dist/graphql-document.d.ts.map +1 -0
- package/dist/graphql-document.js +228 -0
- package/dist/graphql-document.js.map +1 -0
- package/dist/graphql-json.d.ts +56 -0
- package/dist/graphql-json.d.ts.map +1 -0
- package/dist/graphql-json.js +220 -0
- package/dist/graphql-json.js.map +1 -0
- package/dist/graphql-transport.d.ts +28 -0
- package/dist/graphql-transport.d.ts.map +1 -0
- package/dist/graphql-transport.js +77 -0
- package/dist/graphql-transport.js.map +1 -0
- package/dist/index.d.ts +103 -40
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +153 -157
- package/dist/index.js.map +1 -1
- package/dist/json.d.ts +61 -0
- package/dist/json.d.ts.map +1 -0
- package/dist/json.js +83 -0
- package/dist/json.js.map +1 -0
- package/dist/jwt.d.ts +16 -0
- package/dist/jwt.d.ts.map +1 -0
- package/dist/jwt.js +37 -0
- package/dist/jwt.js.map +1 -0
- package/dist/live-preview.d.ts +23 -0
- package/dist/live-preview.d.ts.map +1 -0
- package/dist/live-preview.js +28 -0
- package/dist/live-preview.js.map +1 -0
- package/dist/mutation-request.d.ts +17 -0
- package/dist/mutation-request.d.ts.map +1 -0
- package/dist/mutation-request.js +41 -0
- package/dist/mutation-request.js.map +1 -0
- package/dist/preview.d.ts.map +1 -1
- package/dist/preview.js +8 -6
- package/dist/preview.js.map +1 -1
- package/dist/query-cache.d.ts.map +1 -1
- package/dist/query-cache.js +19 -13
- package/dist/query-cache.js.map +1 -1
- package/dist/query.d.ts +108 -29
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +294 -224
- package/dist/query.js.map +1 -1
- package/dist/realtime.d.ts +20 -2
- package/dist/realtime.d.ts.map +1 -1
- package/dist/realtime.js +105 -12
- package/dist/realtime.js.map +1 -1
- package/dist/select-types.d.ts +99 -0
- package/dist/select-types.d.ts.map +1 -0
- package/dist/select-types.js +2 -0
- package/dist/select-types.js.map +1 -0
- package/dist/storage.d.ts +81 -11
- package/dist/storage.d.ts.map +1 -1
- package/dist/storage.js +129 -37
- package/dist/storage.js.map +1 -1
- package/dist/table-subscription.d.ts +69 -0
- package/dist/table-subscription.d.ts.map +1 -0
- package/dist/table-subscription.js +93 -0
- package/dist/table-subscription.js.map +1 -0
- package/dist/types.d.ts +84 -5
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +6 -1
- package/dist/types.js.map +1 -1
- package/package.json +6 -2
- package/src/auth-redirect.ts +90 -0
- package/src/auth.ts +421 -280
- package/src/bindings.ts +22 -0
- package/src/caller-types.ts +27 -0
- package/src/draft-create.ts +51 -0
- package/src/draft-models-registry.ts +81 -0
- package/src/error-codes-doc.ts +44 -35
- package/src/errors.ts +72 -28
- package/src/exact-response.ts +2 -1
- package/src/exact-values.ts +16 -9
- package/src/fetch-with-retry.ts +80 -70
- package/src/field-kinds-registry.ts +22 -9
- package/src/filters.ts +133 -0
- package/src/generated-client.ts +6 -0
- package/src/graphql-document.ts +234 -0
- package/src/graphql-json.ts +243 -0
- package/src/graphql-transport.ts +97 -0
- package/src/index.ts +272 -201
- package/src/json.ts +117 -0
- package/src/jwt.ts +37 -0
- package/src/live-preview.ts +37 -0
- package/src/mutation-request.ts +66 -0
- package/src/preview.ts +9 -11
- package/src/query-cache.ts +19 -16
- package/src/query.ts +389 -270
- package/src/realtime.ts +116 -13
- package/src/select-types.ts +184 -0
- package/src/storage.ts +214 -43
- package/src/table-subscription.ts +160 -0
- package/src/types.ts +90 -5
- package/tests/auth-error-codes.test.ts +53 -0
- package/tests/auth-errors.test.ts +84 -0
- package/tests/auth-events.test.ts +136 -0
- package/tests/auth-url-session.test.ts +204 -0
- package/tests/auth.test.ts +122 -26
- package/tests/count.test.ts +60 -0
- package/tests/data-plane-auth.test.ts +21 -46
- package/tests/draft-insert.integration.test.ts +118 -0
- package/tests/draft-insert.test.ts +225 -0
- package/tests/draft-profile.test.ts +55 -84
- package/tests/draft-update.integration.test.ts +122 -0
- package/tests/draft-update.test.ts +174 -0
- package/tests/dynamic-headers.test.ts +45 -0
- package/tests/entry-points.test.ts +42 -0
- package/tests/exact-response.test.ts +4 -4
- package/tests/exact-values.test.ts +14 -25
- package/tests/field-kinds-registry.test.ts +3 -3
- package/tests/filters.test.ts +146 -0
- package/tests/graphql-json.test.ts +199 -0
- package/tests/helpers/mock-websocket.ts +44 -11
- package/tests/helpers/oauth-binding.ts +92 -0
- package/tests/helpers/signup-binding.ts +73 -0
- package/tests/helpers/socket-swap.ts +139 -0
- package/tests/mfa-totp-integration.test.ts +13 -20
- package/tests/postgrest-error-codes.test.ts +85 -0
- package/tests/query-cache.test.ts +116 -117
- package/tests/query.test.ts +29 -31
- package/tests/rate-limit-integration.test.ts +4 -12
- package/tests/realtime-exact-values.test.ts +2 -4
- package/tests/realtime-presence.test.ts +75 -0
- package/tests/realtime-session-auth.test.ts +2 -4
- package/tests/realtime-swap-status.test.ts +62 -0
- package/tests/retry-integration.test.ts +3 -8
- package/tests/retry-policy.test.ts +166 -0
- package/tests/rpc-resend.test.ts +24 -0
- package/tests/server-bodies.test.ts +139 -0
- package/tests/serverless.test.ts +2 -2
- package/tests/signed-url-absolute.test.ts +2 -5
- package/tests/storage-object-shape.test.ts +101 -0
- package/tests/storage-quota-integration.test.ts +3 -2
- package/tests/storage-reference-url.test.ts +51 -0
- package/tests/storage-upload-browser.test.ts +71 -0
- package/tests/storage-upload-limit-integration.test.ts +12 -22
- package/tests/subscribe-frame.test.ts +5 -11
- package/tests/support/fetch-mock.ts +64 -0
- package/tests/support/storage-object.ts +37 -0
- package/tests/table-subscribe.test.ts +13 -13
- package/tests/table-subscription.test.ts +105 -0
- package/tests/table-transport.test.ts +94 -0
- package/tests/tsconfig.json +1 -1
- package/tests/types.test.ts +153 -1
- package/tsconfig.json +1 -1
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A channel's status follows the socket swap that signing in causes.
|
|
3
|
+
*
|
|
4
|
+
* Signing in replaces the realtime socket: the old one, opened as anon, is closed and a new one is
|
|
5
|
+
* opened with the user's token, and every channel joins again on it. The swap emitted no status at
|
|
6
|
+
* all, so a channel that had reported SUBSCRIBED on the anon socket went on reading SUBSCRIBED
|
|
7
|
+
* while it had no join anywhere, and an insert made in that gap was never going to arrive. Found by
|
|
8
|
+
* the framework-parity live suite, which had to watch the frames to know when the join was real.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { describe, it, expect, beforeEach, afterEach } from "vitest"
|
|
12
|
+
import type { ChannelStatus } from "../src/realtime.js"
|
|
13
|
+
import { createSocketSwapHarness, type SocketSwapHarness } from "./helpers/socket-swap.js"
|
|
14
|
+
|
|
15
|
+
let harness: SocketSwapHarness
|
|
16
|
+
let seen: ChannelStatus[]
|
|
17
|
+
|
|
18
|
+
beforeEach(async () => {
|
|
19
|
+
harness = createSocketSwapHarness()
|
|
20
|
+
seen = []
|
|
21
|
+
harness.realtime.channel("public:task").subscribe((status) => seen.push(status))
|
|
22
|
+
await harness.settle()
|
|
23
|
+
harness.ackJoins()
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
afterEach(() => harness.restore())
|
|
27
|
+
|
|
28
|
+
describe("channel status across the socket swap on sign-in", () => {
|
|
29
|
+
it("reports SUBSCRIBING from the swap until the new socket acknowledges the join", async () => {
|
|
30
|
+
expect(seen).toEqual(["SUBSCRIBED"])
|
|
31
|
+
|
|
32
|
+
await harness.signIn()
|
|
33
|
+
expect(harness.sockets).toHaveLength(2)
|
|
34
|
+
expect(seen).toEqual(["SUBSCRIBED", "SUBSCRIBING"])
|
|
35
|
+
|
|
36
|
+
harness.ackJoins()
|
|
37
|
+
expect(seen).toEqual(["SUBSCRIBED", "SUBSCRIBING", "SUBSCRIBED"])
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it("reports CHANNEL_ERROR when the new socket refuses the join", async () => {
|
|
41
|
+
await harness.signIn()
|
|
42
|
+
harness.refuseJoin()
|
|
43
|
+
|
|
44
|
+
expect(seen).toEqual(["SUBSCRIBED", "SUBSCRIBING", "CHANNEL_ERROR"])
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it("reports nothing when the token did not change, since no swap happens", async () => {
|
|
48
|
+
await harness.realtime.reauthenticate()
|
|
49
|
+
await harness.settle()
|
|
50
|
+
|
|
51
|
+
expect(harness.sockets).toHaveLength(1)
|
|
52
|
+
expect(seen).toEqual(["SUBSCRIBED"])
|
|
53
|
+
})
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
describe("channel status on an explicit disconnect", () => {
|
|
57
|
+
it("reports CLOSED, since nothing will reconnect the join it had", () => {
|
|
58
|
+
harness.realtime.disconnect()
|
|
59
|
+
|
|
60
|
+
expect(seen).toEqual(["SUBSCRIBED", "CLOSED"])
|
|
61
|
+
})
|
|
62
|
+
})
|
|
@@ -11,13 +11,8 @@ import { NetworkError } from "../src/errors.js"
|
|
|
11
11
|
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
|
12
12
|
|
|
13
13
|
function mockResponse(status: number, body: unknown = {}): Response {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
status,
|
|
17
|
-
json: vi.fn().mockResolvedValue(body),
|
|
18
|
-
headers: new Headers(),
|
|
19
|
-
statusText: status === 503 ? "Service Unavailable" : "OK",
|
|
20
|
-
} as unknown as Response
|
|
14
|
+
const statusText = status === 503 ? "Service Unavailable" : "OK"
|
|
15
|
+
return new Response(JSON.stringify(body), { status, statusText, headers: { "Content-Type": "application/json" } })
|
|
21
16
|
}
|
|
22
17
|
|
|
23
18
|
// ─── Tests ───────────────────────────────────────────────────────────────────
|
|
@@ -154,7 +149,7 @@ describe("Task 90: SDK retry integration", () => {
|
|
|
154
149
|
expect.fail("Should have thrown")
|
|
155
150
|
} catch (err) {
|
|
156
151
|
expect(err).toBeInstanceOf(NetworkError)
|
|
157
|
-
expect(
|
|
152
|
+
expect(err).toMatchObject({ code: "NETWORK_CONNECTION_REFUSED" })
|
|
158
153
|
}
|
|
159
154
|
vi.useFakeTimers()
|
|
160
155
|
})
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from "vitest"
|
|
2
|
+
import { fetchWithRetry } from "../src/fetch-with-retry.js"
|
|
3
|
+
import { createClient } from "../src/index.js"
|
|
4
|
+
import { fetchReturning, jsonResponse, type FetchMock, mockFetch } from "./support/fetch-mock.js"
|
|
5
|
+
|
|
6
|
+
const URL_BASE = "http://localhost:18473"
|
|
7
|
+
|
|
8
|
+
/** 503 once, then 200 with `body`. */
|
|
9
|
+
function flaky(body: unknown): FetchMock {
|
|
10
|
+
const fetch = mockFetch()
|
|
11
|
+
.mockResolvedValueOnce(jsonResponse({ message: "unavailable" }, 503))
|
|
12
|
+
.mockImplementation(() => Promise.resolve(jsonResponse(body)))
|
|
13
|
+
vi.stubGlobal("fetch", fetch)
|
|
14
|
+
return fetch
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** A fetch that never answers, and gives up only when its signal is aborted. */
|
|
18
|
+
function hanging(): FetchMock {
|
|
19
|
+
const fetch = mockFetch((_input, init) =>
|
|
20
|
+
new Promise<Response>((_resolve, reject) => {
|
|
21
|
+
init?.signal?.addEventListener("abort", () => {
|
|
22
|
+
const err = new Error("aborted")
|
|
23
|
+
err.name = "AbortError"
|
|
24
|
+
reject(err)
|
|
25
|
+
})
|
|
26
|
+
}),
|
|
27
|
+
)
|
|
28
|
+
vi.stubGlobal("fetch", fetch)
|
|
29
|
+
return fetch
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function client(opts: { timeout?: number } = {}) {
|
|
33
|
+
return createClient({ url: URL_BASE, anonKey: "anon", auth: { persistSession: false }, ...opts })
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
describe("fetchWithRetry retries only GET and HEAD unless the call opts in", () => {
|
|
37
|
+
beforeEach(() => vi.restoreAllMocks())
|
|
38
|
+
|
|
39
|
+
it("a POST that fails with a 503 is sent once", async () => {
|
|
40
|
+
const fetch = flaky({ ok: true })
|
|
41
|
+
const res = await fetchWithRetry("http://api.test/x", { method: "POST", body: "{}" })
|
|
42
|
+
expect(res.status).toBe(503)
|
|
43
|
+
expect(fetch).toHaveBeenCalledTimes(1)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it("a POST the caller marks idempotent is retried", { timeout: 5000 }, async () => {
|
|
47
|
+
const fetch = flaky({ ok: true })
|
|
48
|
+
const res = await fetchWithRetry("http://api.test/x", { method: "POST", body: "{}", idempotent: true })
|
|
49
|
+
expect(res.status).toBe(200)
|
|
50
|
+
expect(fetch).toHaveBeenCalledTimes(2)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it("a GET is retried", { timeout: 5000 }, async () => {
|
|
54
|
+
const fetch = flaky({ ok: true })
|
|
55
|
+
const res = await fetchWithRetry("http://api.test/x")
|
|
56
|
+
expect(res.status).toBe(200)
|
|
57
|
+
expect(fetch).toHaveBeenCalledTimes(2)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it("a 429 on a request that may not be retried comes back as the response, not a throw", async () => {
|
|
61
|
+
vi.stubGlobal("fetch", fetchReturning({ msg: "slow down" }, 429))
|
|
62
|
+
const res = await fetchWithRetry("http://api.test/x", { method: "POST" })
|
|
63
|
+
expect(res.status).toBe(429)
|
|
64
|
+
})
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
describe("rpc, graphql and functions: retried only when the call says it is idempotent", () => {
|
|
68
|
+
beforeEach(() => vi.restoreAllMocks())
|
|
69
|
+
|
|
70
|
+
it("rpc is not retried by default", async () => {
|
|
71
|
+
const fetch = flaky(1)
|
|
72
|
+
const { error } = await client().rpc("do_thing")
|
|
73
|
+
expect(error?.status).toBe(503)
|
|
74
|
+
expect(fetch).toHaveBeenCalledTimes(1)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it("rpc with idempotent: true is retried", { timeout: 5000 }, async () => {
|
|
78
|
+
const fetch = flaky(1)
|
|
79
|
+
const { data, error } = await client().rpc("read_thing", undefined, { idempotent: true })
|
|
80
|
+
expect(error).toBeNull()
|
|
81
|
+
expect(data).toBe(1)
|
|
82
|
+
expect(fetch).toHaveBeenCalledTimes(2)
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it("graphql is not retried by default", async () => {
|
|
86
|
+
const fetch = flaky({ data: {} })
|
|
87
|
+
await client().graphql("{ a }")
|
|
88
|
+
expect(fetch).toHaveBeenCalledTimes(1)
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it("graphql with idempotent: true is retried", { timeout: 5000 }, async () => {
|
|
92
|
+
const fetch = flaky({ data: { a: 1 } })
|
|
93
|
+
const { data, error } = await client().graphql("{ a }", undefined, { idempotent: true })
|
|
94
|
+
expect(error).toBeNull()
|
|
95
|
+
expect(data).toEqual({ a: 1 })
|
|
96
|
+
expect(fetch).toHaveBeenCalledTimes(2)
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it("functions.invoke (POST) is not retried by default", async () => {
|
|
100
|
+
const fetch = flaky({ ok: true })
|
|
101
|
+
await client().functions.invoke("fn")
|
|
102
|
+
expect(fetch).toHaveBeenCalledTimes(1)
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
it("functions.invoke with idempotent: true is retried", { timeout: 5000 }, async () => {
|
|
106
|
+
const fetch = flaky({ ok: true })
|
|
107
|
+
const { error } = await client().functions.invoke("fn", { idempotent: true })
|
|
108
|
+
expect(error).toBeNull()
|
|
109
|
+
expect(fetch).toHaveBeenCalledTimes(2)
|
|
110
|
+
})
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
describe("auth and storage use the client's transport", () => {
|
|
114
|
+
beforeEach(() => vi.restoreAllMocks())
|
|
115
|
+
|
|
116
|
+
it("a sign-in that never answers ends at the timeout", { timeout: 2000 }, async () => {
|
|
117
|
+
hanging()
|
|
118
|
+
await expect(
|
|
119
|
+
client({ timeout: 50 }).auth.signInWithPassword({ email: "a@example.com", password: "pw" }),
|
|
120
|
+
).rejects.toMatchObject({ code: "NETWORK_TIMEOUT" })
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
it("a sign-in is not retried after a 503", async () => {
|
|
124
|
+
const fetch = flaky({})
|
|
125
|
+
const { error } = await client().auth.signInWithPassword({ email: "a@example.com", password: "pw" })
|
|
126
|
+
expect(error?.status).toBe(503)
|
|
127
|
+
expect(fetch).toHaveBeenCalledTimes(1)
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
it("a rate-limited sign-in returns the server's error rather than throwing", async () => {
|
|
131
|
+
vi.stubGlobal(
|
|
132
|
+
"fetch",
|
|
133
|
+
fetchReturning({ code: 429, error_code: "over_request_rate_limit", msg: "Request rate limit reached" }, 429),
|
|
134
|
+
)
|
|
135
|
+
const { error } = await client().auth.signInWithPassword({ email: "a@example.com", password: "pw" })
|
|
136
|
+
expect(error).toEqual({ message: "Request rate limit reached", status: 429, code: "over_request_rate_limit" })
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
it("a storage upload that never answers ends at the timeout", { timeout: 2000 }, async () => {
|
|
140
|
+
hanging()
|
|
141
|
+
await expect(
|
|
142
|
+
client({ timeout: 50 }).storage.from("b").upload("f.txt", new Blob(["x"])),
|
|
143
|
+
).rejects.toMatchObject({ code: "NETWORK_TIMEOUT" })
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
it("a storage download (GET) is retried after a 503", { timeout: 5000 }, async () => {
|
|
147
|
+
const fetch = flaky({ ok: true })
|
|
148
|
+
const { error } = await client().storage.from("b").download("f.txt")
|
|
149
|
+
expect(error).toBeNull()
|
|
150
|
+
expect(fetch).toHaveBeenCalledTimes(2)
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
it("a storage list (a read sent as POST) is retried after a 503", { timeout: 5000 }, async () => {
|
|
154
|
+
const fetch = flaky([])
|
|
155
|
+
const { error } = await client().storage.from("b").list()
|
|
156
|
+
expect(error).toBeNull()
|
|
157
|
+
expect(fetch).toHaveBeenCalledTimes(2)
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
it("a storage upload is not retried after a 503", async () => {
|
|
161
|
+
const fetch = flaky({ Key: "b/f.txt" })
|
|
162
|
+
const { error } = await client().storage.from("b").upload("f.txt", new Blob(["x"]))
|
|
163
|
+
expect(error?.status).toBe(503)
|
|
164
|
+
expect(fetch).toHaveBeenCalledTimes(1)
|
|
165
|
+
})
|
|
166
|
+
})
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from "vitest"
|
|
2
|
+
import { createClient } from "../src/index.js"
|
|
3
|
+
import { fetchCalls, header, jsonResponse, mockFetch } from "./support/fetch-mock.js"
|
|
4
|
+
|
|
5
|
+
describe("rpc: the resend after a 401 is the same request", () => {
|
|
6
|
+
beforeEach(() => vi.restoreAllMocks())
|
|
7
|
+
|
|
8
|
+
it("keeps Content-Profile and Prefer on the second send", async () => {
|
|
9
|
+
const fetch = mockFetch()
|
|
10
|
+
.mockResolvedValueOnce(jsonResponse({ message: "JWT expired" }, 401))
|
|
11
|
+
.mockResolvedValueOnce(jsonResponse(1, 200))
|
|
12
|
+
vi.stubGlobal("fetch", fetch)
|
|
13
|
+
const client = createClient({ url: "http://localhost:18473", anonKey: "anon", auth: { persistSession: false } })
|
|
14
|
+
|
|
15
|
+
const { error } = await client.rpc("publish", undefined, { schema: "supatype", count: "exact" })
|
|
16
|
+
|
|
17
|
+
expect(error).toBeNull()
|
|
18
|
+
expect(fetch).toHaveBeenCalledTimes(2)
|
|
19
|
+
for (const { init } of fetchCalls(fetch)) {
|
|
20
|
+
expect(header(init, "Content-Profile")).toBe("supatype")
|
|
21
|
+
expect(header(init, "Prefer")).toBe("count=exact")
|
|
22
|
+
}
|
|
23
|
+
})
|
|
24
|
+
})
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the client does with a body the server really sends, rather than the one its types describe.
|
|
3
|
+
*
|
|
4
|
+
* Each case here was a cast that claimed a shape and, for some body the server can produce, was
|
|
5
|
+
* wrong: a JWT payload `atob` cannot read, a bigint that node-postgres sends as a string, an error
|
|
6
|
+
* the storage service writes under `error`, a JSON `null` where an object was assumed.
|
|
7
|
+
*/
|
|
8
|
+
import { describe, it, expect, vi, afterEach } from "vitest"
|
|
9
|
+
import { decodeJwtPayload } from "../src/jwt.js"
|
|
10
|
+
import { identityFingerprint } from "../src/query-cache.js"
|
|
11
|
+
import { QueryBuilder } from "../src/query.js"
|
|
12
|
+
import { StorageClient } from "../src/storage.js"
|
|
13
|
+
import { createClient } from "../src/index.js"
|
|
14
|
+
import { applyRowChange } from "../src/table-subscription.js"
|
|
15
|
+
import { fetchReturning, jsonResponse, mockFetch } from "./support/fetch-mock.js"
|
|
16
|
+
|
|
17
|
+
afterEach(() => {
|
|
18
|
+
vi.unstubAllGlobals()
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
/** A payload whose base64url encoding has a `-` or `_`, which `atob` refuses. */
|
|
22
|
+
const URL_SAFE_CLAIMS = { sub: "user-1", role: "authenticated", note: "a>?~b", name: "Zoë" }
|
|
23
|
+
const URL_SAFE_TOKEN = `e30.${Buffer.from(JSON.stringify(URL_SAFE_CLAIMS)).toString("base64url")}.sig`
|
|
24
|
+
|
|
25
|
+
describe("a JWT payload in base64url", () => {
|
|
26
|
+
it("is one atob alone cannot read", () => {
|
|
27
|
+
const segment = URL_SAFE_TOKEN.split(".")[1] ?? ""
|
|
28
|
+
expect(segment).toMatch(/[-_]/)
|
|
29
|
+
expect(() => atob(segment)).toThrow()
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it("is decoded, so its claims are read", () => {
|
|
33
|
+
expect(decodeJwtPayload(URL_SAFE_TOKEN.split(".")[1] ?? "")).toMatchObject({ sub: "user-1", role: "authenticated" })
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it("keys the query cache by role and subject", () => {
|
|
37
|
+
expect(identityFingerprint({ Authorization: `Bearer ${URL_SAFE_TOKEN}` }, "user")).toBe("authenticated:user-1")
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it("is null when the segment is not JSON of an object", () => {
|
|
41
|
+
expect(decodeJwtPayload(Buffer.from("[1]").toString("base64url"))).toBeNull()
|
|
42
|
+
expect(decodeJwtPayload("not base64 at all!")).toBeNull()
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
describe("storage bodies", () => {
|
|
47
|
+
const storage = (): StorageClient => new StorageClient("http://storage.test", () => Promise.resolve({}))
|
|
48
|
+
const BUCKET = {
|
|
49
|
+
id: "avatars",
|
|
50
|
+
name: "avatars",
|
|
51
|
+
public: true,
|
|
52
|
+
allowed_mime_types: null,
|
|
53
|
+
created_at: "2026-01-01T00:00:00Z",
|
|
54
|
+
updated_at: "2026-01-01T00:00:00Z",
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
it("reads a bucket's size limit as the number its type declares", async () => {
|
|
58
|
+
// A Postgres bigint: node-postgres sends it as a string.
|
|
59
|
+
vi.stubGlobal("fetch", fetchReturning([{ ...BUCKET, file_size_limit: "1048576" }]))
|
|
60
|
+
const { data, error } = await storage().listBuckets()
|
|
61
|
+
expect(error).toBeNull()
|
|
62
|
+
expect(data?.[0]?.file_size_limit).toBe(1048576)
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
it("passes the service's reason for refusing a bucket, which it writes under `error`", async () => {
|
|
66
|
+
vi.stubGlobal("fetch", fetchReturning({ error: "Bucket avatars already exists" }, 409))
|
|
67
|
+
const { error } = await storage().createBucket("avatars")
|
|
68
|
+
expect(error?.message).toBe("Bucket avatars already exists")
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
it("reports a refusal whose body is null with the fallback message", async () => {
|
|
72
|
+
vi.stubGlobal("fetch", fetchReturning(null, 500))
|
|
73
|
+
const { error } = await storage().from("avatars").upload("a.png", new Blob(["x"]))
|
|
74
|
+
expect(error).toEqual({ message: "Upload failed", status: 500 })
|
|
75
|
+
})
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
describe("graphql", () => {
|
|
79
|
+
it("reports a JSON null body as an error rather than throwing", async () => {
|
|
80
|
+
vi.stubGlobal("fetch", fetchReturning(null))
|
|
81
|
+
const client = createClient({ url: "http://api.test", anonKey: "anon", auth: { persistSession: false } })
|
|
82
|
+
const { data, error } = await client.graphql("{ a }")
|
|
83
|
+
expect(data).toBeNull()
|
|
84
|
+
expect(error?.message).toMatch(/not an object/)
|
|
85
|
+
})
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
describe("reads that answer with no body", () => {
|
|
89
|
+
it("maybeSingle() on a 204 is null rather than a throw", async () => {
|
|
90
|
+
vi.stubGlobal("fetch", mockFetch(() => Promise.resolve(jsonResponse(null, 204))))
|
|
91
|
+
const { data, error } = await new QueryBuilder("http://api.test", "/rest/v1/posts", {}).maybeSingle()
|
|
92
|
+
expect(error).toBeNull()
|
|
93
|
+
expect(data).toBeNull()
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
it("a body that is JSON but not rows is an error, not rows", async () => {
|
|
97
|
+
vi.stubGlobal("fetch", fetchReturning("not rows"))
|
|
98
|
+
const { data, error } = await new QueryBuilder("http://api.test", "/rest/v1/posts", {})
|
|
99
|
+
expect(data).toBeNull()
|
|
100
|
+
expect(error?.message).toMatch(/expected rows/)
|
|
101
|
+
})
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
describe("applyRowChange", () => {
|
|
105
|
+
const change = (eventType: "INSERT" | "UPDATE") => ({
|
|
106
|
+
eventType,
|
|
107
|
+
new: null,
|
|
108
|
+
old: null,
|
|
109
|
+
schema: "public",
|
|
110
|
+
table: "post",
|
|
111
|
+
commitTimestamp: "2026-01-01T00:00:00Z",
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
it("does not put a null among the rows for a change without its new row", () => {
|
|
115
|
+
const rows = [{ id: 1 }]
|
|
116
|
+
expect(applyRowChange(rows, change("INSERT"))).toEqual([{ id: 1 }])
|
|
117
|
+
expect(applyRowChange([{ title: "no id" }], change("UPDATE"))).toEqual([{ title: "no id" }])
|
|
118
|
+
})
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
describe("a refusal worded under `error`", () => {
|
|
122
|
+
// The Studio proxy and the server's guards answer `{ "error": "..." }`. The RPC path read only
|
|
123
|
+
// `message` and said "PostgREST error"; the table path said "Request failed".
|
|
124
|
+
const REFUSED = 'Studio role "viewer" cannot perform this request'
|
|
125
|
+
|
|
126
|
+
it("keeps the sentence on an RPC call", async () => {
|
|
127
|
+
vi.stubGlobal("fetch", fetchReturning({ error: REFUSED }, 403))
|
|
128
|
+
const client = createClient({ url: "http://api.test", anonKey: "anon", auth: { persistSession: false } })
|
|
129
|
+
const { error } = await client.rpc("create_draft", {})
|
|
130
|
+
expect(error?.message).toBe(REFUSED)
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
it("keeps the sentence on a table write", async () => {
|
|
134
|
+
vi.stubGlobal("fetch", fetchReturning({ error: REFUSED }, 403))
|
|
135
|
+
const client = createClient({ url: "http://api.test", anonKey: "anon", auth: { persistSession: false } })
|
|
136
|
+
const { error } = await client.from("posts").update({ title: "x" }).eq("id", 1)
|
|
137
|
+
expect(error?.message).toBe(REFUSED)
|
|
138
|
+
})
|
|
139
|
+
})
|
package/tests/serverless.test.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach, type MockInstance } from "vitest"
|
|
2
2
|
import { detectServerlessEnvironment, warnIfServerlessDirectConnection } from "../src/fetch-with-retry.js"
|
|
3
3
|
|
|
4
4
|
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
|
@@ -75,7 +75,7 @@ describe("detectServerlessEnvironment", () => {
|
|
|
75
75
|
|
|
76
76
|
describe("warnIfServerlessDirectConnection", () => {
|
|
77
77
|
let restoreEnv: (() => void) | undefined
|
|
78
|
-
let warnSpy:
|
|
78
|
+
let warnSpy: MockInstance<typeof console.warn>
|
|
79
79
|
|
|
80
80
|
beforeEach(() => {
|
|
81
81
|
warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {})
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
|
|
14
14
|
import { describe, it, expect, vi, beforeEach } from "vitest"
|
|
15
15
|
import { StorageClient } from "../src/storage.js"
|
|
16
|
+
import { fetchReturning } from "./support/fetch-mock.js"
|
|
16
17
|
|
|
17
18
|
const STORAGE_URL = "http://localhost:18473/storage/v1"
|
|
18
19
|
const HEADERS = { apikey: "test-anon-key", Authorization: "Bearer test-token" }
|
|
@@ -23,11 +24,7 @@ function client(url = STORAGE_URL): StorageClient {
|
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
function respondWith(signedURL: string): void {
|
|
26
|
-
|
|
27
|
-
ok: true,
|
|
28
|
-
status: 200,
|
|
29
|
-
json: () => Promise.resolve({ signedURL }),
|
|
30
|
-
}) as unknown as typeof fetch
|
|
27
|
+
vi.stubGlobal("fetch", fetchReturning({ signedURL }))
|
|
31
28
|
}
|
|
32
29
|
|
|
33
30
|
beforeEach(() => {
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `StorageObject` and `RemovedObject` are what the storage service sends, field for field.
|
|
3
|
+
*
|
|
4
|
+
* `StorageObject` used to declare `owner` and `metadata` optional, which the service sends as null,
|
|
5
|
+
* and left out the `size` and `mimetype` every list entry carries. `remove()` claimed to answer with
|
|
6
|
+
* whole objects when the service sends only each one's name and bucket, so `data[0].id` typechecked
|
|
7
|
+
* and was undefined. And the runtime check read only `name`, so a body of the wrong shape went
|
|
8
|
+
* through as if it were a list.
|
|
9
|
+
*
|
|
10
|
+
* `LISTED` is a real entry, captured from packages/storage's `list` route by the caller-rules suite,
|
|
11
|
+
* which also pins the service's side of this to the same fields.
|
|
12
|
+
*/
|
|
13
|
+
import { afterEach, describe, expect, expectTypeOf, it, vi } from "vitest"
|
|
14
|
+
import { StorageClient } from "../src/storage.js"
|
|
15
|
+
import type { RemovedObject, StorageFolder, StorageObject } from "../src/index.js"
|
|
16
|
+
import { fetchCall, fetchReturning } from "./support/fetch-mock.js"
|
|
17
|
+
import { FOLDER, LISTED, UNOWNED } from "./support/storage-object.js"
|
|
18
|
+
import { readFileSync } from "node:fs"
|
|
19
|
+
import { isArrayOf, isJsonObject, isShape, isString } from "../src/json.js"
|
|
20
|
+
|
|
21
|
+
/** packages/storage's list-entry contract, which supatype-server's local objstore also checks. */
|
|
22
|
+
const CONTRACT = new URL("../../storage/tests/fixtures/list-entry.json", import.meta.url)
|
|
23
|
+
|
|
24
|
+
const bucket = (): ReturnType<StorageClient["from"]> =>
|
|
25
|
+
new StorageClient("http://storage.test", () => Promise.resolve({})).from("avatars")
|
|
26
|
+
|
|
27
|
+
afterEach(() => {
|
|
28
|
+
vi.unstubAllGlobals()
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
describe("StorageObject", () => {
|
|
32
|
+
it("declares exactly the fields a list entry carries", () => {
|
|
33
|
+
expectTypeOf<StorageObject>().toEqualTypeOf<{
|
|
34
|
+
id: string
|
|
35
|
+
bucket_id: string
|
|
36
|
+
name: string
|
|
37
|
+
owner: string | null
|
|
38
|
+
metadata: Record<string, unknown> | null
|
|
39
|
+
created_at: string
|
|
40
|
+
updated_at: string
|
|
41
|
+
last_accessed_at: string
|
|
42
|
+
size: number | null
|
|
43
|
+
mimetype: string | null
|
|
44
|
+
}>()
|
|
45
|
+
expectTypeOf(LISTED).toExtend<StorageObject>()
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it("reads a real list answer", async () => {
|
|
49
|
+
vi.stubGlobal("fetch", fetchReturning([LISTED, UNOWNED]))
|
|
50
|
+
const { data, error } = await bucket().list()
|
|
51
|
+
expect(error).toBeNull()
|
|
52
|
+
expect(data).toEqual([LISTED, UNOWNED])
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
// A list answers a folder's direct children: subfolders, then objects, each named relative to it.
|
|
56
|
+
it("reads a folder's subfolders alongside its objects", async () => {
|
|
57
|
+
expectTypeOf<StorageFolder>().toEqualTypeOf<typeof FOLDER>()
|
|
58
|
+
expectTypeOf<Awaited<ReturnType<ReturnType<typeof bucket>["list"]>>["data"]>().toEqualTypeOf<Array<StorageObject | StorageFolder> | null>()
|
|
59
|
+
vi.stubGlobal("fetch", fetchReturning([FOLDER, LISTED]))
|
|
60
|
+
const { data } = await bucket().list()
|
|
61
|
+
expect(data).toEqual([FOLDER, LISTED])
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it("sends a search and answers with the total the server counts", async () => {
|
|
65
|
+
const fetch = fetchReturning([LISTED], 200, { "X-Total-Count": "42" })
|
|
66
|
+
vi.stubGlobal("fetch", fetch)
|
|
67
|
+
const { count } = await bucket().list("photos", { search: "shape", limit: 10, offset: 20 })
|
|
68
|
+
expect(count).toBe(42)
|
|
69
|
+
expect(JSON.parse(String(fetchCall(fetch).init.body))).toEqual({ prefix: "photos", search: "shape", limit: 10, offset: 20 })
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it("answers a null total when the server sends none", async () => {
|
|
73
|
+
vi.stubGlobal("fetch", fetchReturning([LISTED]))
|
|
74
|
+
expect((await bucket().list()).count).toBeNull()
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it("refuses a list answer that is not a list of objects", async () => {
|
|
78
|
+
vi.stubGlobal("fetch", fetchReturning([{ name: "a.txt", bucket_id: "avatars" }]))
|
|
79
|
+
await expect(bucket().list()).rejects.toThrow(/Unexpected response/)
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
// The contract every storage server is checked against: if it changes, this type must follow.
|
|
83
|
+
it("is the storage servers' list-entry contract", () => {
|
|
84
|
+
const contract: unknown = JSON.parse(readFileSync(CONTRACT, "utf8"))
|
|
85
|
+
const isContract = isShape({ entry: isJsonObject, folder: isJsonObject, nullable: isArrayOf(isString) })
|
|
86
|
+
if (!isContract(contract)) throw new Error("list-entry.json is not { entry, folder, nullable }")
|
|
87
|
+
expect(contract.entry).toEqual(LISTED)
|
|
88
|
+
expect(contract.folder).toEqual(FOLDER)
|
|
89
|
+
expect([...contract.nullable].sort()).toEqual(["metadata", "mimetype", "owner", "size"])
|
|
90
|
+
})
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
describe("RemovedObject", () => {
|
|
94
|
+
it("is what remove() answers: each removed object's name and bucket", async () => {
|
|
95
|
+
expectTypeOf<RemovedObject>().toEqualTypeOf<{ name: string; bucket_id: string }>()
|
|
96
|
+
expectTypeOf<Awaited<ReturnType<ReturnType<typeof bucket>["remove"]>>["data"]>().toEqualTypeOf<RemovedObject[] | null>()
|
|
97
|
+
vi.stubGlobal("fetch", fetchReturning([{ name: "a.txt", bucket_id: "avatars" }]))
|
|
98
|
+
const { data } = await bucket().remove(["a.txt"])
|
|
99
|
+
expect(data).toEqual([{ name: "a.txt", bucket_id: "avatars" }])
|
|
100
|
+
})
|
|
101
|
+
})
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
import { describe, it, expect, vi, beforeEach } from "vitest"
|
|
8
8
|
import { StorageClient } from "../src/storage.js"
|
|
9
|
+
import { UNOWNED } from "./support/storage-object.js"
|
|
9
10
|
|
|
10
11
|
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
|
11
12
|
|
|
@@ -152,8 +153,8 @@ describe("Task 93: Storage quota integration", () => {
|
|
|
152
153
|
describe("Quota on other storage operations", () => {
|
|
153
154
|
it("list works normally even when quota is exceeded", async () => {
|
|
154
155
|
vi.stubGlobal("fetch", mockFetchResponse(200, [
|
|
155
|
-
{ id: "1", name: "file1.txt"
|
|
156
|
-
{ id: "2", name: "file2.txt"
|
|
156
|
+
{ ...UNOWNED, id: "1", name: "file1.txt" },
|
|
157
|
+
{ ...UNOWNED, id: "2", name: "file2.txt" },
|
|
157
158
|
]))
|
|
158
159
|
|
|
159
160
|
const client = freshClient()
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An `ImageAsset` or `FileAsset` column stores a reference, not a URL.
|
|
3
|
+
*
|
|
4
|
+
* What lands in the column is what `storage.upload()` produced and where it went:
|
|
5
|
+
* `{ bucket, path }`, plus `mimeType` and `size` when Studio wrote it. A URL is not stored because a
|
|
6
|
+
* stored one goes stale and bakes in the host. Every app reading such a column therefore has to turn
|
|
7
|
+
* the reference back into a URL, and the kitchen-sink app instead read a `url` field that is never
|
|
8
|
+
* there, so every speaker photo rendered as initials.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { describe, it, expect } from "vitest"
|
|
12
|
+
import { StorageClient } from "../src/storage.js"
|
|
13
|
+
|
|
14
|
+
const STORAGE_URL = "http://localhost:18473/storage/v1"
|
|
15
|
+
|
|
16
|
+
function storage(): StorageClient {
|
|
17
|
+
return new StorageClient(STORAGE_URL, () => Promise.resolve({}))
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
describe("storage.publicUrl(reference)", () => {
|
|
21
|
+
it("builds the public object URL from the stored bucket and path", () => {
|
|
22
|
+
const url = storage().publicUrl({ bucket: "speaker-headshots", path: "avatars/ada.jpg" })
|
|
23
|
+
expect(url).toBe(`${STORAGE_URL}/object/public/speaker-headshots/avatars/ada.jpg`)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it("is the same URL the bucket client gives for that path", () => {
|
|
27
|
+
const ref = { bucket: "speaker-headshots", path: "avatars/ada.jpg", mimeType: "image/jpeg", size: 812 }
|
|
28
|
+
const transform = { width: 104, height: 104, resize: "cover", format: "webp" } as const
|
|
29
|
+
expect(storage().publicUrl(ref, { transform })).toBe(
|
|
30
|
+
storage().from(ref.bucket).getPublicUrl(ref.path, { transform }).data.publicUrl,
|
|
31
|
+
)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it("asks for a transform on read when given one", () => {
|
|
35
|
+
const url = storage().publicUrl(
|
|
36
|
+
{ bucket: "speaker-headshots", path: "avatars/ada.jpg" },
|
|
37
|
+
{ transform: { width: 52, height: 52, resize: "cover" } },
|
|
38
|
+
)
|
|
39
|
+
expect(url).toBe(`${STORAGE_URL}/object/public/speaker-headshots/avatars/ada.jpg?width=52&height=52&resize=cover`)
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it("answers null for an empty column, so a caller can fall back without a check of its own", () => {
|
|
43
|
+
expect(storage().publicUrl(null)).toBeNull()
|
|
44
|
+
expect(storage().publicUrl(undefined)).toBeNull()
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it("answers null for a reference missing its bucket or path rather than a URL to nothing", () => {
|
|
48
|
+
expect(storage().publicUrl({ bucket: "", path: "avatars/ada.jpg" })).toBeNull()
|
|
49
|
+
expect(storage().publicUrl({ bucket: "speaker-headshots", path: "" })).toBeNull()
|
|
50
|
+
})
|
|
51
|
+
})
|