|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Storage wire-dialect proof (#3689) — the SDK's storage methods against the |
| 5 | + * envelope `service-storage` actually emits, and against the one it emitted |
| 6 | + * before. |
| 7 | + * |
| 8 | + * The gap this pins: `storage.zod.ts` declared every storage response as |
| 9 | + * `BaseResponseSchema.extend({ data })`, `PresignedUrlResponse` and friends are |
| 10 | + * `z.infer`red from those schemas and published as these methods' return types |
| 11 | + * — and the methods returned `res.json()` raw. `res.json()` is `any`, so the |
| 12 | + * declaration could say `success: boolean` while the wire said nothing at all |
| 13 | + * and TypeScript would never know. `getDownloadUrl` was the one method that |
| 14 | + * read INTO a body, and it read `data.url` off a bare `{ url }`. |
| 15 | + * |
| 16 | + * #3689 moved the wire onto the declaration. This SDK ships as its own npm |
| 17 | + * package, so it meets servers on both sides of that change: the enveloped and |
| 18 | + * the bare body are both asserted here, and `unwrapResponse` — the client's one |
| 19 | + * standard envelope seam, not a fallback grown for this route — is what spans |
| 20 | + * them. |
| 21 | + */ |
| 22 | + |
| 23 | +import { describe, it, expect, vi } from 'vitest'; |
| 24 | +import { ObjectStackClient } from './index'; |
| 25 | + |
| 26 | +function clientReturning(body: any) { |
| 27 | + const fetchMock = vi.fn().mockResolvedValue({ |
| 28 | + ok: true, |
| 29 | + status: 200, |
| 30 | + statusText: 'OK', |
| 31 | + json: async () => body, |
| 32 | + headers: new Headers(), |
| 33 | + }); |
| 34 | + const client = new ObjectStackClient({ baseUrl: 'http://localhost:3000', fetch: fetchMock }); |
| 35 | + return { client, fetchMock }; |
| 36 | +} |
| 37 | + |
| 38 | +describe('storage.getDownloadUrl reads the signed URL off either dialect (#3689)', () => { |
| 39 | + it('resolves from the declared { success: true, data: { url } } envelope', async () => { |
| 40 | + const { client, fetchMock } = clientReturning({ |
| 41 | + success: true, |
| 42 | + data: { url: '/api/v1/storage/_local/raw/eyJrIjoi.c2ln' }, |
| 43 | + }); |
| 44 | + |
| 45 | + await expect(client.storage.getDownloadUrl('f1')).resolves.toBe( |
| 46 | + '/api/v1/storage/_local/raw/eyJrIjoi.c2ln', |
| 47 | + ); |
| 48 | + expect(fetchMock.mock.calls[0][0]).toContain('/api/v1/storage/files/f1/url'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('still resolves from the bare { url } an older server answers', async () => { |
| 52 | + // Not a tolerated dialect going forward — a version-skew allowance. The |
| 53 | + // client is published separately from the server it talks to, so a build |
| 54 | + // predating the #3689 rollout must keep resolving downloads. |
| 55 | + const { client } = clientReturning({ url: 'https://bucket.s3.amazonaws.com/user/f1.png?sig=abc' }); |
| 56 | + |
| 57 | + await expect(client.storage.getDownloadUrl('f1')).resolves.toBe( |
| 58 | + 'https://bucket.s3.amazonaws.com/user/f1.png?sig=abc', |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + it('resolves an absolute S3-style URL out of the envelope unchanged', async () => { |
| 63 | + const { client } = clientReturning({ |
| 64 | + success: true, |
| 65 | + data: { url: 'https://bucket.s3.amazonaws.com/user/f1.png?X-Amz-Signature=abc' }, |
| 66 | + }); |
| 67 | + |
| 68 | + await expect(client.storage.getDownloadUrl('f1')).resolves.toBe( |
| 69 | + 'https://bucket.s3.amazonaws.com/user/f1.png?X-Amz-Signature=abc', |
| 70 | + ); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +describe('the enveloped storage responses match their declared return types (#3689)', () => { |
| 75 | + /** |
| 76 | + * These methods hand back the whole envelope by design — their declared |
| 77 | + * return types ARE `BaseResponseSchema.extend({ data })`. Before #3689 the |
| 78 | + * `success` half of that declaration was fiction. Asserting it here is what |
| 79 | + * makes the published type honest, since `res.json()` erases to `any` and |
| 80 | + * the compiler cannot. |
| 81 | + */ |
| 82 | + it('getPresignedUrl carries success alongside data', async () => { |
| 83 | + const { client } = clientReturning({ |
| 84 | + success: true, |
| 85 | + data: { |
| 86 | + uploadUrl: '/api/v1/storage/_local/raw/tok', |
| 87 | + method: 'PUT', |
| 88 | + fileId: 'f1', |
| 89 | + expiresIn: 3600, |
| 90 | + downloadUrl: '/api/v1/storage/files/f1/url', |
| 91 | + }, |
| 92 | + }); |
| 93 | + |
| 94 | + const res = await client.storage.getPresignedUrl({ |
| 95 | + filename: 'a.png', |
| 96 | + mimeType: 'image/png', |
| 97 | + size: 10, |
| 98 | + scope: 'user', |
| 99 | + }); |
| 100 | + expect(res.success).toBe(true); |
| 101 | + expect(res.data.fileId).toBe('f1'); |
| 102 | + }); |
| 103 | + |
| 104 | + it('initChunkedUpload carries success alongside data', async () => { |
| 105 | + const { client } = clientReturning({ |
| 106 | + success: true, |
| 107 | + data: { |
| 108 | + uploadId: 'up1', |
| 109 | + resumeToken: 'tok', |
| 110 | + fileId: 'f1', |
| 111 | + totalChunks: 1, |
| 112 | + chunkSize: 5242880, |
| 113 | + expiresAt: '2026-01-01T00:00:00.000Z', |
| 114 | + }, |
| 115 | + }); |
| 116 | + |
| 117 | + const res = await client.storage.initChunkedUpload({ |
| 118 | + filename: 'big.bin', |
| 119 | + mimeType: 'application/octet-stream', |
| 120 | + totalSize: 100, |
| 121 | + chunkSize: 5242880, |
| 122 | + scope: 'user', |
| 123 | + }); |
| 124 | + expect(res.success).toBe(true); |
| 125 | + expect(res.data.uploadId).toBe('up1'); |
| 126 | + }); |
| 127 | + |
| 128 | + it('uploadPart carries success alongside data', async () => { |
| 129 | + const { client } = clientReturning({ |
| 130 | + success: true, |
| 131 | + data: { chunkIndex: 0, eTag: '"abc"', bytesReceived: 100 }, |
| 132 | + }); |
| 133 | + |
| 134 | + const res = await client.storage.uploadPart('up1', 0, 'tok', Buffer.from('x')); |
| 135 | + expect(res.success).toBe(true); |
| 136 | + expect(res.data.eTag).toBe('"abc"'); |
| 137 | + }); |
| 138 | +}); |
0 commit comments