@supatype/client 0.3.6 → 0.8.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 +69 -30
- 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 +86 -4
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +383 -275
- 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 +170 -165
- 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 +57 -2
- package/dist/realtime.d.ts.map +1 -1
- package/dist/realtime.js +300 -88
- 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 +137 -38
- 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 +98 -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 +434 -281
- 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 +289 -208
- 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 +333 -90
- package/src/select-types.ts +184 -0
- package/src/storage.ts +223 -44
- package/src/table-subscription.ts +160 -0
- package/src/types.ts +104 -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 +187 -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 +142 -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-ack-ref.test.ts +112 -0
- 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-shared-channel.test.ts +209 -0
- 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 +187 -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 +7 -9
- 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 +17 -31
- package/tests/storage-url-encoding.test.ts +52 -0
- package/tests/subscribe-frame.test.ts +5 -11
- package/tests/support/blob.ts +10 -0
- package/tests/support/fetch-mock.ts +64 -0
- package/tests/support/storage-object.ts +37 -0
- package/tests/table-subscribe.test.ts +41 -14
- 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
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,8 @@
|
|
|
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"
|
|
10
|
+
import { createMockBlob } from "./support/blob.js"
|
|
9
11
|
|
|
10
12
|
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
|
11
13
|
|
|
@@ -17,10 +19,6 @@ function freshClient(): StorageClient {
|
|
|
17
19
|
return new StorageClient(STORAGE_URL, () => Promise.resolve(HEADERS))
|
|
18
20
|
}
|
|
19
21
|
|
|
20
|
-
function createMockBlob(sizeBytes: number): Blob {
|
|
21
|
-
return new Blob([new Uint8Array(sizeBytes)])
|
|
22
|
-
}
|
|
23
|
-
|
|
24
22
|
function mockFetchResponse(
|
|
25
23
|
status: number,
|
|
26
24
|
body: unknown,
|
|
@@ -48,7 +46,7 @@ describe("Task 93: Storage quota integration", () => {
|
|
|
48
46
|
}, false))
|
|
49
47
|
|
|
50
48
|
const client = freshClient()
|
|
51
|
-
const blob = createMockBlob(
|
|
49
|
+
const blob = createMockBlob(1024)
|
|
52
50
|
const { data, error } = await client.from("uploads").upload("file.dat", blob)
|
|
53
51
|
|
|
54
52
|
expect(data).toBeNull()
|
|
@@ -114,10 +112,10 @@ describe("Task 93: Storage quota integration", () => {
|
|
|
114
112
|
|
|
115
113
|
const client = freshClient()
|
|
116
114
|
|
|
117
|
-
const first = await client.from("bucket").upload("first.txt", createMockBlob(
|
|
115
|
+
const first = await client.from("bucket").upload("first.txt", createMockBlob(1024))
|
|
118
116
|
expect(first.error).toBeNull()
|
|
119
117
|
|
|
120
|
-
const second = await client.from("bucket").upload("second.txt", createMockBlob(
|
|
118
|
+
const second = await client.from("bucket").upload("second.txt", createMockBlob(1024))
|
|
121
119
|
expect(second.data).toBeNull()
|
|
122
120
|
expect(second.error!.status).toBe(507)
|
|
123
121
|
})
|
|
@@ -152,8 +150,8 @@ describe("Task 93: Storage quota integration", () => {
|
|
|
152
150
|
describe("Quota on other storage operations", () => {
|
|
153
151
|
it("list works normally even when quota is exceeded", async () => {
|
|
154
152
|
vi.stubGlobal("fetch", mockFetchResponse(200, [
|
|
155
|
-
{ id: "1", name: "file1.txt"
|
|
156
|
-
{ id: "2", name: "file2.txt"
|
|
153
|
+
{ ...UNOWNED, id: "1", name: "file1.txt" },
|
|
154
|
+
{ ...UNOWNED, id: "2", name: "file2.txt" },
|
|
157
155
|
]))
|
|
158
156
|
|
|
159
157
|
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
|
+
})
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An upload from a browser file input sends the file's bytes, as the file's type, untouched.
|
|
3
|
+
*
|
|
4
|
+
* Found in manual testing: a profile picture uploaded from the kitchen-sink Account screen was
|
|
5
|
+
* stored as 0 bytes. The cause was the file on disk (a 0-byte `test.jpg`), not the client, but the
|
|
6
|
+
* browser path had no test: every upload test here sent a string or a Buffer from Node. These pin
|
|
7
|
+
* what the browser path relies on, a `File` handed to `fetch` as the body, read by nothing first,
|
|
8
|
+
* typed from `File.type` and carrying every byte.
|
|
9
|
+
*/
|
|
10
|
+
import { describe, expect, it } from "vitest"
|
|
11
|
+
import { StorageClient } from "../src/storage.js"
|
|
12
|
+
import type { RequestTransport } from "../src/fetch-with-retry.js"
|
|
13
|
+
|
|
14
|
+
const URL_BASE = "http://localhost:18473/storage/v1"
|
|
15
|
+
const JPEG_BYTES = new Uint8Array([0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0xff, 0xd9])
|
|
16
|
+
|
|
17
|
+
interface Sent {
|
|
18
|
+
url: string
|
|
19
|
+
method: string | undefined
|
|
20
|
+
headers: Headers
|
|
21
|
+
body: BodyInit | null | undefined
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** A transport that records the request and answers as the storage service does. */
|
|
25
|
+
function recording(): { transport: RequestTransport; sent: Sent[] } {
|
|
26
|
+
const sent: Sent[] = []
|
|
27
|
+
const transport: RequestTransport = async (url, init) => {
|
|
28
|
+
sent.push({ url, method: init.method, headers: new Headers(init.headers), body: init.body })
|
|
29
|
+
return new Response(JSON.stringify({ Key: "speaker-headshots/avatars/a.jpg" }), { status: 200 })
|
|
30
|
+
}
|
|
31
|
+
return { transport, sent }
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function bucket(transport: RequestTransport) {
|
|
35
|
+
return new StorageClient(URL_BASE, () => Promise.resolve({ apikey: "anon" }), transport).from("speaker-headshots")
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
describe("BucketClient.upload with a browser File", () => {
|
|
39
|
+
it("sends the File itself as the body, with every byte still in it", async () => {
|
|
40
|
+
const { transport, sent } = recording()
|
|
41
|
+
const file = new File([JPEG_BYTES], "avatar.jpg", { type: "image/jpeg" })
|
|
42
|
+
|
|
43
|
+
const { error } = await bucket(transport).upload("avatars/a.jpg", file, { upsert: true })
|
|
44
|
+
|
|
45
|
+
expect(error).toBeNull()
|
|
46
|
+
expect(sent).toHaveLength(1)
|
|
47
|
+
expect(sent[0]!.method).toBe("POST")
|
|
48
|
+
expect(sent[0]!.url).toBe(`${URL_BASE}/object/speaker-headshots/avatars/a.jpg`)
|
|
49
|
+
expect(sent[0]!.body).toBe(file)
|
|
50
|
+
// What fetch would put on the wire for that body.
|
|
51
|
+
const wire = new Uint8Array(await new Response(sent[0]!.body).arrayBuffer())
|
|
52
|
+
expect(wire).toEqual(JPEG_BYTES)
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it("types the request from File.type and keeps x-upsert", async () => {
|
|
56
|
+
const { transport, sent } = recording()
|
|
57
|
+
await bucket(transport).upload("avatars/a.jpg", new File([JPEG_BYTES], "a.jpg", { type: "image/jpeg" }), {
|
|
58
|
+
upsert: true,
|
|
59
|
+
})
|
|
60
|
+
expect(sent[0]!.headers.get("Content-Type")).toBe("image/jpeg")
|
|
61
|
+
expect(sent[0]!.headers.get("x-upsert")).toBe("true")
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it("sends a Blob's bytes too, typed by the caller", async () => {
|
|
65
|
+
const { transport, sent } = recording()
|
|
66
|
+
const blob = new Blob([JPEG_BYTES], { type: "image/jpeg" })
|
|
67
|
+
await bucket(transport).upload("avatars/b.jpg", blob, { contentType: "image/jpeg" })
|
|
68
|
+
expect(sent[0]!.headers.get("Content-Type")).toBe("image/jpeg")
|
|
69
|
+
expect(new Uint8Array(await new Response(sent[0]!.body).arrayBuffer())).toEqual(JPEG_BYTES)
|
|
70
|
+
})
|
|
71
|
+
})
|
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
|
|
7
7
|
import { describe, it, expect, vi, beforeEach } from "vitest"
|
|
8
8
|
import { StorageClient } from "../src/storage.js"
|
|
9
|
+
import { fetchCall, fetchReturning, header, type FetchMock } from "./support/fetch-mock.js"
|
|
10
|
+
import { createMockBlob } from "./support/blob.js"
|
|
9
11
|
|
|
10
12
|
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
|
11
13
|
|
|
@@ -17,22 +19,8 @@ function freshClient(): StorageClient {
|
|
|
17
19
|
return new StorageClient(STORAGE_URL, () => Promise.resolve(HEADERS))
|
|
18
20
|
}
|
|
19
21
|
|
|
20
|
-
function
|
|
21
|
-
|
|
22
|
-
return new Blob([data])
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function mockFetchResponse(
|
|
26
|
-
status: number,
|
|
27
|
-
body: unknown,
|
|
28
|
-
ok?: boolean,
|
|
29
|
-
): ReturnType<typeof vi.fn> {
|
|
30
|
-
return vi.fn().mockResolvedValue({
|
|
31
|
-
ok: ok ?? (status >= 200 && status < 300),
|
|
32
|
-
status,
|
|
33
|
-
json: vi.fn().mockResolvedValue(body),
|
|
34
|
-
headers: new Headers(),
|
|
35
|
-
})
|
|
22
|
+
function mockFetchResponse(status: number, body: unknown): FetchMock {
|
|
23
|
+
return fetchReturning(body, status)
|
|
36
24
|
}
|
|
37
25
|
|
|
38
26
|
// ─── Tests ───────────────────────────────────────────────────────────────────
|
|
@@ -57,7 +45,7 @@ describe("Task 92: File upload size limit integration", () => {
|
|
|
57
45
|
vi.stubGlobal("fetch", mockFetchResponse(200, { Key: "docs/file.pdf" }))
|
|
58
46
|
|
|
59
47
|
const client = freshClient()
|
|
60
|
-
const blob = createMockBlob(
|
|
48
|
+
const blob = createMockBlob(1024) // the mocked server's answer stands for a file at the limit
|
|
61
49
|
const { data, error } = await client.from("docs").upload("file.pdf", blob, {
|
|
62
50
|
contentType: "application/pdf",
|
|
63
51
|
})
|
|
@@ -73,10 +61,10 @@ describe("Task 92: File upload size limit integration", () => {
|
|
|
73
61
|
message: "Payload too large: file exceeds the maximum upload size of 50MB",
|
|
74
62
|
statusCode: 413,
|
|
75
63
|
error: "Payload Too Large",
|
|
76
|
-
}
|
|
64
|
+
}))
|
|
77
65
|
|
|
78
66
|
const client = freshClient()
|
|
79
|
-
const blob = createMockBlob(
|
|
67
|
+
const blob = createMockBlob(1024) // the mocked 413 stands for a file over the limit
|
|
80
68
|
const { data, error } = await client.from("uploads").upload("large-video.mp4", blob)
|
|
81
69
|
|
|
82
70
|
expect(data).toBeNull()
|
|
@@ -88,10 +76,10 @@ describe("Task 92: File upload size limit integration", () => {
|
|
|
88
76
|
it("error message mentions the size limit", async () => {
|
|
89
77
|
vi.stubGlobal("fetch", mockFetchResponse(413, {
|
|
90
78
|
message: "Payload too large: file exceeds the maximum upload size of 50MB",
|
|
91
|
-
}
|
|
79
|
+
}))
|
|
92
80
|
|
|
93
81
|
const client = freshClient()
|
|
94
|
-
const { error } = await client.from("uploads").upload("huge.zip", createMockBlob(
|
|
82
|
+
const { error } = await client.from("uploads").upload("huge.zip", createMockBlob(1024))
|
|
95
83
|
|
|
96
84
|
expect(error!.message).toContain("50MB")
|
|
97
85
|
})
|
|
@@ -99,10 +87,10 @@ describe("Task 92: File upload size limit integration", () => {
|
|
|
99
87
|
it("returns 413 for just-over-limit file", async () => {
|
|
100
88
|
vi.stubGlobal("fetch", mockFetchResponse(413, {
|
|
101
89
|
message: "Payload too large: file exceeds the maximum upload size of 50MB",
|
|
102
|
-
}
|
|
90
|
+
}))
|
|
103
91
|
|
|
104
92
|
const client = freshClient()
|
|
105
|
-
const blob = createMockBlob(
|
|
93
|
+
const blob = createMockBlob(1024) // the mocked 413 stands for one byte over
|
|
106
94
|
const { data, error } = await client.from("uploads").upload("slightly-too-big.dat", blob)
|
|
107
95
|
|
|
108
96
|
expect(data).toBeNull()
|
|
@@ -118,9 +106,9 @@ describe("Task 92: File upload size limit integration", () => {
|
|
|
118
106
|
const client = freshClient()
|
|
119
107
|
await client.from("bucket").upload("file.txt", createMockBlob(100))
|
|
120
108
|
|
|
121
|
-
const
|
|
109
|
+
const { url, init } = fetchCall(fetchSpy)
|
|
122
110
|
expect(url).toBe(`${STORAGE_URL}/object/bucket/file.txt`)
|
|
123
|
-
expect(
|
|
111
|
+
expect(init.method).toBe("POST")
|
|
124
112
|
})
|
|
125
113
|
|
|
126
114
|
it("includes content-type header", async () => {
|
|
@@ -132,8 +120,7 @@ describe("Task 92: File upload size limit integration", () => {
|
|
|
132
120
|
contentType: "image/jpeg",
|
|
133
121
|
})
|
|
134
122
|
|
|
135
|
-
|
|
136
|
-
expect((opts.headers as Record<string, string>)["Content-Type"]).toBe("image/jpeg")
|
|
123
|
+
expect(header(fetchCall(fetchSpy).init, "Content-Type")).toBe("image/jpeg")
|
|
137
124
|
})
|
|
138
125
|
|
|
139
126
|
it("sends x-upsert header when upsert option is true", async () => {
|
|
@@ -143,8 +130,7 @@ describe("Task 92: File upload size limit integration", () => {
|
|
|
143
130
|
const client = freshClient()
|
|
144
131
|
await client.from("bucket").upload("file.txt", createMockBlob(100), { upsert: true })
|
|
145
132
|
|
|
146
|
-
|
|
147
|
-
expect((opts.headers as Record<string, string>)["x-upsert"]).toBe("true")
|
|
133
|
+
expect(header(fetchCall(fetchSpy).init, "x-upsert")).toBe("true")
|
|
148
134
|
})
|
|
149
135
|
})
|
|
150
136
|
|
|
@@ -152,7 +138,7 @@ describe("Task 92: File upload size limit integration", () => {
|
|
|
152
138
|
it("handles 400 Bad Request", async () => {
|
|
153
139
|
vi.stubGlobal("fetch", mockFetchResponse(400, {
|
|
154
140
|
message: "Invalid file name",
|
|
155
|
-
}
|
|
141
|
+
}))
|
|
156
142
|
|
|
157
143
|
const client = freshClient()
|
|
158
144
|
const { error } = await client.from("bucket").upload("", createMockBlob(100))
|
|
@@ -164,7 +150,7 @@ describe("Task 92: File upload size limit integration", () => {
|
|
|
164
150
|
it("handles 401 Unauthorized", async () => {
|
|
165
151
|
vi.stubGlobal("fetch", mockFetchResponse(401, {
|
|
166
152
|
message: "Unauthorized",
|
|
167
|
-
}
|
|
153
|
+
}))
|
|
168
154
|
|
|
169
155
|
const client = freshClient()
|
|
170
156
|
const { error } = await client.from("private-bucket").upload("secret.doc", createMockBlob(100))
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An object path goes into a URL one encoded segment at a time.
|
|
3
|
+
*
|
|
4
|
+
* The client interpolated the path as given, so a name holding `?` or `#` cut the URL short: the
|
|
5
|
+
* server saw `a b/c` and a query or fragment it never asked for, and answered for another object
|
|
6
|
+
* or none. The `/` between folders stays a `/`, since the server matches the path on it.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { beforeEach, describe, expect, it, vi } from "vitest"
|
|
10
|
+
import { StorageClient } from "../src/storage.js"
|
|
11
|
+
import { fetchCall, fetchReturning } from "./support/fetch-mock.js"
|
|
12
|
+
|
|
13
|
+
const STORAGE_URL = "http://localhost:18473/storage/v1"
|
|
14
|
+
const NAME = "a b/c?d#e.png"
|
|
15
|
+
const ENCODED = "a%20b/c%3Fd%23e.png"
|
|
16
|
+
|
|
17
|
+
function bucket() {
|
|
18
|
+
return new StorageClient(STORAGE_URL, () => Promise.resolve({})).from("docs")
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
vi.restoreAllMocks()
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
describe("object paths in storage URLs", () => {
|
|
26
|
+
it("are encoded per segment in a public URL", () => {
|
|
27
|
+
expect(bucket().getPublicUrl(NAME).data.publicUrl).toBe(`${STORAGE_URL}/object/public/docs/${ENCODED}`)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it("are encoded per segment when asking for a signed URL", async () => {
|
|
31
|
+
const fetch = fetchReturning({ signedURL: `/object/sign/docs/${ENCODED}?token=t` })
|
|
32
|
+
vi.stubGlobal("fetch", fetch)
|
|
33
|
+
await bucket().createSignedUrl(NAME, 60)
|
|
34
|
+
expect(fetchCall(fetch).url).toBe(`${STORAGE_URL}/object/sign/docs/${ENCODED}`)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it("are encoded per segment on upload and download", async () => {
|
|
38
|
+
const fetch = fetchReturning({})
|
|
39
|
+
vi.stubGlobal("fetch", fetch)
|
|
40
|
+
await bucket().upload(NAME, new ArrayBuffer(1))
|
|
41
|
+
await bucket().download(NAME)
|
|
42
|
+
expect(fetchCall(fetch, 0).url).toBe(`${STORAGE_URL}/object/docs/${ENCODED}`)
|
|
43
|
+
expect(fetchCall(fetch, 1).url).toBe(`${STORAGE_URL}/object/authenticated/docs/${ENCODED}`)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it("read back as the name they were built from", () => {
|
|
47
|
+
const url = new URL(bucket().getPublicUrl(NAME).data.publicUrl)
|
|
48
|
+
expect(url.search).toBe("")
|
|
49
|
+
expect(url.hash).toBe("")
|
|
50
|
+
expect(decodeURIComponent(url.pathname)).toBe(`/storage/v1/object/public/docs/${NAME}`)
|
|
51
|
+
})
|
|
52
|
+
})
|
|
@@ -15,6 +15,7 @@ import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"
|
|
|
15
15
|
import { RealtimeClient } from "../src/realtime.js"
|
|
16
16
|
import {
|
|
17
17
|
createMockWebSocketClass,
|
|
18
|
+
sentFrames,
|
|
18
19
|
simulateServerMessage,
|
|
19
20
|
type MockWebSocketInstance,
|
|
20
21
|
} from "./helpers/mock-websocket.js"
|
|
@@ -22,26 +23,21 @@ import {
|
|
|
22
23
|
const settle = (): Promise<void> => new Promise((resolve) => setTimeout(resolve, 10))
|
|
23
24
|
|
|
24
25
|
let instances: MockWebSocketInstance[]
|
|
25
|
-
let originalWebSocket: typeof WebSocket
|
|
26
26
|
|
|
27
27
|
beforeEach(() => {
|
|
28
28
|
const mock = createMockWebSocketClass()
|
|
29
29
|
instances = mock.instances
|
|
30
|
-
|
|
31
|
-
globalThis.WebSocket = mock.MockWebSocket
|
|
30
|
+
vi.stubGlobal("WebSocket", mock.MockWebSocket)
|
|
32
31
|
})
|
|
33
32
|
|
|
34
33
|
afterEach(() => {
|
|
35
|
-
|
|
34
|
+
vi.unstubAllGlobals()
|
|
36
35
|
vi.restoreAllMocks()
|
|
37
36
|
})
|
|
38
37
|
|
|
39
38
|
/** Every subscribe frame the client put on the socket. */
|
|
40
39
|
function subscribeFrames(): Array<Record<string, unknown>> {
|
|
41
|
-
return instances
|
|
42
|
-
.flatMap((i) => i.send.mock.calls.map((c) => c[0] as string))
|
|
43
|
-
.map((raw) => JSON.parse(raw) as Record<string, unknown>)
|
|
44
|
-
.filter((m) => m["type"] === "subscribe")
|
|
40
|
+
return instances.flatMap((i) => sentFrames(i)).filter((m) => m["type"] === "subscribe")
|
|
45
41
|
}
|
|
46
42
|
|
|
47
43
|
describe("the subscribe frame", () => {
|
|
@@ -129,9 +125,7 @@ describe("joining again after disconnect()", () => {
|
|
|
129
125
|
await settle()
|
|
130
126
|
|
|
131
127
|
expect(instances).toHaveLength(2)
|
|
132
|
-
const onNewSocket = instances[1]
|
|
133
|
-
.map((c) => JSON.parse(c[0] as string) as Record<string, unknown>)
|
|
134
|
-
.filter((m) => m["type"] === "subscribe")
|
|
128
|
+
const onNewSocket = sentFrames(instances[1]!).filter((m) => m["type"] === "subscribe")
|
|
135
129
|
expect(onNewSocket).toHaveLength(1)
|
|
136
130
|
})
|
|
137
131
|
})
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A file body for upload tests. The client never checks a file's size: the storage server
|
|
3
|
+
* decides limits and quotas, and these suites mock its answer. So the tests upload small blobs,
|
|
4
|
+
* because a real 500 MB one costs real memory and timed out on CI runners.
|
|
5
|
+
*
|
|
6
|
+
* Not named `*.test.ts`, so vitest's default include does not collect it as a suite.
|
|
7
|
+
*/
|
|
8
|
+
export function createMockBlob(sizeBytes: number): Blob {
|
|
9
|
+
return new Blob([new Uint8Array(sizeBytes)])
|
|
10
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { vi, type Mock } from "vitest"
|
|
2
|
+
import { isJsonObject } from "../../src/json.js"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A typed stand-in for `fetch`, and readers for what it was called with.
|
|
6
|
+
*
|
|
7
|
+
* The suites used to build `vi.fn()` (whose calls are `any[]`) and then assert each call into the
|
|
8
|
+
* shape they expected. Typed as `fetch`, a call is `[input, init?]` and nothing needs asserting: a
|
|
9
|
+
* header is read the way the server reads it (case-insensitively), and a body is parsed and checked.
|
|
10
|
+
*
|
|
11
|
+
* Not named `*.test.ts`, so vitest's default include does not collect it as a suite.
|
|
12
|
+
*/
|
|
13
|
+
export type FetchMock = Mock<typeof fetch>
|
|
14
|
+
|
|
15
|
+
/** A fetch mock, answering with `impl` when given one. */
|
|
16
|
+
export function mockFetch(impl?: typeof fetch): FetchMock {
|
|
17
|
+
return impl === undefined ? vi.fn<typeof fetch>() : vi.fn<typeof fetch>(impl)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Statuses a Response may not carry a body with. */
|
|
21
|
+
const NULL_BODY_STATUSES = new Set([101, 204, 205, 304])
|
|
22
|
+
|
|
23
|
+
/** A JSON response, as a server would send it. */
|
|
24
|
+
export function jsonResponse(body: unknown, status = 200, headers: Record<string, string> = {}): Response {
|
|
25
|
+
const text = NULL_BODY_STATUSES.has(status) ? null : JSON.stringify(body)
|
|
26
|
+
return new Response(text, { status, headers: { "Content-Type": "application/json", ...headers } })
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A fetch that answers every call with a fresh copy of the same JSON response. */
|
|
30
|
+
export function fetchReturning(body: unknown, status = 200, headers: Record<string, string> = {}): FetchMock {
|
|
31
|
+
return vi.fn<typeof fetch>(() => Promise.resolve(jsonResponse(body, status, headers)))
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The `n`th call's URL and init, as `fetch` received them. Throws when there was no such call. */
|
|
35
|
+
export function fetchCall(fetch: FetchMock, n = 0): { url: string; init: RequestInit } {
|
|
36
|
+
const call = fetch.mock.calls[n]
|
|
37
|
+
if (call === undefined) throw new Error(`fetch was called ${fetch.mock.calls.length} times, not ${n + 1}`)
|
|
38
|
+
const [input, init] = call
|
|
39
|
+
return { url: requestUrl(input), init: init ?? {} }
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Every call's URL and init, oldest first. */
|
|
43
|
+
export function fetchCalls(fetch: FetchMock): Array<{ url: string; init: RequestInit }> {
|
|
44
|
+
return fetch.mock.calls.map(([input, init]) => ({ url: requestUrl(input), init: init ?? {} }))
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The URL a `fetch` input names. */
|
|
48
|
+
export function requestUrl(input: RequestInfo | URL): string {
|
|
49
|
+
if (typeof input === "string") return input
|
|
50
|
+
return input instanceof URL ? input.href : input.url
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** One request header, matched case-insensitively as a server matches it; null when absent. */
|
|
54
|
+
export function header(init: RequestInit, name: string): string | null {
|
|
55
|
+
return new Headers(init.headers).get(name)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The request's body parsed as a JSON object. Throws when it is not one. */
|
|
59
|
+
export function jsonBody(init: RequestInit): Record<string, unknown> {
|
|
60
|
+
if (typeof init.body !== "string") throw new Error("the request body is not a string")
|
|
61
|
+
const body = JSON.parse(init.body)
|
|
62
|
+
if (!isJsonObject(body)) throw new Error(`the request body is not a JSON object: ${init.body}`)
|
|
63
|
+
return body
|
|
64
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A list entry exactly as the storage service sends one: the entry in
|
|
3
|
+
* packages/storage/tests/fixtures/list-entry.json, the contract every storage server is checked
|
|
4
|
+
* against. storage-object-shape.test.ts keeps the two equal. The suites that need a list answer
|
|
5
|
+
* build from this, so a change to the shape is made in one place.
|
|
6
|
+
*
|
|
7
|
+
* Not named `*.test.ts`, so vitest's default include does not collect it as a suite.
|
|
8
|
+
*/
|
|
9
|
+
export const LISTED = {
|
|
10
|
+
id: "c385ffde-42b3-45e5-8981-863515e0dbf6",
|
|
11
|
+
bucket_id: "avatars",
|
|
12
|
+
name: "shape-f9619cfd-f645-4f8d-bb42-3ed73bb0a36d.txt",
|
|
13
|
+
owner: "7ea80d5c-e81e-48e7-8f27-2c2218ac42ac",
|
|
14
|
+
metadata: { size: 4, mimetype: "text/plain" },
|
|
15
|
+
created_at: "2026-10-18T07:52:09.909Z",
|
|
16
|
+
updated_at: "2026-10-18T07:52:09.909Z",
|
|
17
|
+
last_accessed_at: "2026-10-18T07:52:09.909Z",
|
|
18
|
+
size: 4,
|
|
19
|
+
mimetype: "text/plain",
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** A subfolder as a list answers it: the `folder` entry of the same contract file. */
|
|
23
|
+
export const FOLDER = {
|
|
24
|
+
id: null,
|
|
25
|
+
bucket_id: "avatars",
|
|
26
|
+
name: "photos",
|
|
27
|
+
owner: null,
|
|
28
|
+
metadata: null,
|
|
29
|
+
created_at: null,
|
|
30
|
+
updated_at: null,
|
|
31
|
+
last_accessed_at: null,
|
|
32
|
+
size: null,
|
|
33
|
+
mimetype: null,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The same entry with every nullable field null, as for an object stored with no owner or metadata. */
|
|
37
|
+
export const UNOWNED = { ...LISTED, owner: null, metadata: null, size: null, mimetype: null }
|