From 5c1e8c108ad79090e5dadc5903e288b15e855f8c Mon Sep 17 00:00:00 2001 From: Cyril Tovena Date: Mon, 24 Jul 2023 17:02:56 +0200 Subject: [PATCH] Allow to pass a blob to core fetch function (#71929) * Allow to pass a blob to core fetch function * Use global blob instead of import --------- Co-authored-by: Andrej Ocenas --- public/app/core/utils/fetch.test.ts | 19 ++++++++++--------- public/app/core/utils/fetch.ts | 3 +++ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/public/app/core/utils/fetch.test.ts b/public/app/core/utils/fetch.test.ts index df817c560f4..de7130d48cf 100644 --- a/public/app/core/utils/fetch.test.ts +++ b/public/app/core/utils/fetch.test.ts @@ -92,15 +92,16 @@ describe('isContentTypeApplicationJson', () => { describe('parseBody', () => { it.each` - options | isAppJson | expected - ${undefined} | ${false} | ${undefined} - ${undefined} | ${true} | ${undefined} - ${{ data: undefined }} | ${false} | ${undefined} - ${{ data: undefined }} | ${true} | ${undefined} - ${{ data: 'some data' }} | ${false} | ${'some data'} - ${{ data: 'some data' }} | ${true} | ${'some data'} - ${{ data: { id: '0' } }} | ${false} | ${new URLSearchParams({ id: '0' })} - ${{ data: { id: '0' } }} | ${true} | ${'{"id":"0"}'} + options | isAppJson | expected + ${undefined} | ${false} | ${undefined} + ${undefined} | ${true} | ${undefined} + ${{ data: undefined }} | ${false} | ${undefined} + ${{ data: undefined }} | ${true} | ${undefined} + ${{ data: 'some data' }} | ${false} | ${'some data'} + ${{ data: 'some data' }} | ${true} | ${'some data'} + ${{ data: { id: '0' } }} | ${false} | ${new URLSearchParams({ id: '0' })} + ${{ data: { id: '0' } }} | ${true} | ${'{"id":"0"}'} + ${{ data: new Blob([new Uint8Array([1, 1])]) }} | ${false} | ${new Blob([new Uint8Array([1, 1])])} `( "when called with options: '$options' and isAppJson: '$isAppJson' then the result should be '$expected'", ({ options, isAppJson, expected }) => { diff --git a/public/app/core/utils/fetch.ts b/public/app/core/utils/fetch.ts index c057dda9111..03b5031d777 100644 --- a/public/app/core/utils/fetch.ts +++ b/public/app/core/utils/fetch.ts @@ -89,6 +89,9 @@ export const parseBody = (options: BackendSrvRequest, isAppJson: boolean) => { if (!options.data || typeof options.data === 'string') { return options.data; } + if (options.data instanceof Blob) { + return options.data; + } return isAppJson ? JSON.stringify(options.data) : new URLSearchParams(options.data); };