// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 /** * @aws-blocks/bb-file-bucket/middleware * * Self-registering client middleware for local dev. * Hydrates { __blocks: 'file-bucket/download' } and { __blocks: 'file-bucket/upload' } * descriptors into live file handles with .download() / .upload() methods. */ import { registerMiddleware } from '@aws-blocks/core/client'; import type { FileDownloadClient, FileUploadClient } from './types.js'; export type { FileDownloadClient, FileUploadClient }; function isDownloadDescriptor(data: unknown): data is { __blocks: 'file-bucket/download'; url: string } { if (typeof data !== 'object' || data === null) return false; const d = data as Record; return d.__blocks === 'file-bucket/download' && typeof d.url === 'string'; } function isUploadDescriptor(data: unknown): data is { __blocks: 'file-bucket/upload'; url: string; contentType?: string } { if (typeof data !== 'object' || data === null) return false; const d = data as Record; return d.__blocks === 'file-bucket/upload' && typeof d.url === 'string'; } function hydrate(data: unknown): unknown { if (isDownloadDescriptor(data)) { const { url } = data; return { async download(): Promise { const res = await fetch(url); if (!res.ok) throw new Error(`Download failed: ${res.status}`); return res.blob(); }, getUrl(): string { return url; }, toJSON() { return { __blocks: 'file-bucket/download' as const, url }; }, } satisfies FileDownloadClient; } if (isUploadDescriptor(data)) { const { url, contentType } = data; return { async upload(body: Blob | File | ArrayBuffer): Promise { const headers: Record = {}; if (contentType) headers['Content-Type'] = contentType; const res = await fetch(url, { method: 'PUT', body, headers }); if (!res.ok) throw new Error(`Upload failed: ${res.status}`); }, getUrl(): string { return url; }, toJSON() { return { __blocks: 'file-bucket/upload' as const, url, contentType }; }, } satisfies FileUploadClient; } if (Array.isArray(data)) return data.map(hydrate); if (typeof data === 'object' && data !== null) { const result: Record = {}; for (const [k, v] of Object.entries(data)) result[k] = hydrate(v); return result; } return data; } registerMiddleware({ onResponse: hydrate });