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 <mr.ocenas@gmail.com>
This commit is contained in:
Cyril Tovena
2023-07-24 17:02:56 +02:00
committed by GitHub
parent ed780ce0e9
commit 5c1e8c108a
2 changed files with 13 additions and 9 deletions

View File

@@ -92,15 +92,16 @@ describe('isContentTypeApplicationJson', () => {
describe('parseBody', () => { describe('parseBody', () => {
it.each` it.each`
options | isAppJson | expected options | isAppJson | expected
${undefined} | ${false} | ${undefined} ${undefined} | ${false} | ${undefined}
${undefined} | ${true} | ${undefined} ${undefined} | ${true} | ${undefined}
${{ data: undefined }} | ${false} | ${undefined} ${{ data: undefined }} | ${false} | ${undefined}
${{ data: undefined }} | ${true} | ${undefined} ${{ data: undefined }} | ${true} | ${undefined}
${{ data: 'some data' }} | ${false} | ${'some data'} ${{ data: 'some data' }} | ${false} | ${'some data'}
${{ data: 'some data' }} | ${true} | ${'some data'} ${{ data: 'some data' }} | ${true} | ${'some data'}
${{ data: { id: '0' } }} | ${false} | ${new URLSearchParams({ id: '0' })} ${{ data: { id: '0' } }} | ${false} | ${new URLSearchParams({ id: '0' })}
${{ data: { id: '0' } }} | ${true} | ${'{"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'", "when called with options: '$options' and isAppJson: '$isAppJson' then the result should be '$expected'",
({ options, isAppJson, expected }) => { ({ options, isAppJson, expected }) => {

View File

@@ -89,6 +89,9 @@ export const parseBody = (options: BackendSrvRequest, isAppJson: boolean) => {
if (!options.data || typeof options.data === 'string') { if (!options.data || typeof options.data === 'string') {
return options.data; return options.data;
} }
if (options.data instanceof Blob) {
return options.data;
}
return isAppJson ? JSON.stringify(options.data) : new URLSearchParams(options.data); return isAppJson ? JSON.stringify(options.data) : new URLSearchParams(options.data);
}; };