From 08fb0236fd02e532ef2e4e3742c73c9629822f74 Mon Sep 17 00:00:00 2001 From: Bastian Eicher Date: Sat, 14 Feb 2026 02:23:06 +0100 Subject: [PATCH] Made UploadEndpoint.upload() extract file name from File object passed as blob instead of separate fileName parameter --- endpoints/raw/UploadEndpoint.test.ts | 16 ++++++++++++++-- endpoints/raw/UploadEndpoint.ts | 7 ++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/endpoints/raw/UploadEndpoint.test.ts b/endpoints/raw/UploadEndpoint.test.ts index 977e70a..5ca8c2a 100644 --- a/endpoints/raw/UploadEndpoint.test.ts +++ b/endpoints/raw/UploadEndpoint.test.ts @@ -25,7 +25,10 @@ test('uploadRaw', async () => { test('uploadForm', async () => { const endpoint = new UploadEndpoint(new EntryEndpoint('http://localhost/'), 'endpoint', 'data'); - const data = new Blob([new Uint8Array([1, 2, 3])], { type: 'mock/type' }); + const file = new File([new Uint8Array([1, 2, 3])], 'file.dat', { type: 'mock/type' }); + + // Spy on FormData.prototype.set to verify what's being set + const setSpy = jest.spyOn(FormData.prototype, 'set'); fetchMock.mockOnceIf( req => req.method === HttpMethod.Post && req.url === 'http://localhost/endpoint', @@ -34,5 +37,14 @@ test('uploadForm', async () => { return {}; } ); - await endpoint.upload(data, 'file.dat'); + + await endpoint.upload(file); + + // Verify the FormData was populated correctly with file name and type + expect(setSpy).toHaveBeenCalledWith('data', file, 'file.dat'); + const capturedFile = setSpy.mock.calls[0][1] as File; + expect(capturedFile.name).toBe('file.dat'); + expect(capturedFile.type).toBe('mock/type'); + + setSpy.mockRestore(); }); diff --git a/endpoints/raw/UploadEndpoint.ts b/endpoints/raw/UploadEndpoint.ts index 8e00066..6f5f44a 100644 --- a/endpoints/raw/UploadEndpoint.ts +++ b/endpoints/raw/UploadEndpoint.ts @@ -18,8 +18,7 @@ export class UploadEndpoint extends Endpoint { /** * Uploads data to the endpoint. - * @param blob The blob to read the upload data from. - * @param fileName The name of the uploaded file. + * @param blob The blob or file to read the upload data from. * @param signal Used to cancel the request. * @throws {@link errors!BadRequestError}: {@link http!HttpStatusCode.BadRequest} * @throws {@link errors!AuthenticationError}: {@link http!HttpStatusCode.Unauthorized} @@ -27,7 +26,9 @@ export class UploadEndpoint extends Endpoint { * @throws {@link errors!NotFoundError}: {@link http!HttpStatusCode.NotFound} or {@link http!HttpStatusCode.Gone} * @throws {@link errors!HttpError}: Other non-success status code */ - async upload(blob: Blob, fileName?: string, signal?: AbortSignal) { + async upload(blob: Blob | File, signal?: AbortSignal) { + const fileName = blob instanceof File ? blob.name : undefined; + if (this.formField) { const formData = new FormData(); formData.set(this.formField, blob, fileName);