diff --git a/.changeset/mean-pumas-search.md b/.changeset/mean-pumas-search.md new file mode 100644 index 0000000000..68a3aebc85 --- /dev/null +++ b/.changeset/mean-pumas-search.md @@ -0,0 +1,5 @@ +--- +'@backstage/core-app-api': patch +--- + +fixed empty body issue for POST requests using FetchAPI with 'plugin://' prefix diff --git a/packages/core-app-api/src/apis/implementations/FetchApi/PluginProtocolResolverFetchMiddleware.test.ts b/packages/core-app-api/src/apis/implementations/FetchApi/PluginProtocolResolverFetchMiddleware.test.ts index 2f4ec6b536..157abf9d7c 100644 --- a/packages/core-app-api/src/apis/implementations/FetchApi/PluginProtocolResolverFetchMiddleware.test.ts +++ b/packages/core-app-api/src/apis/implementations/FetchApi/PluginProtocolResolverFetchMiddleware.test.ts @@ -90,4 +90,32 @@ describe('PluginProtocolResolverFetchMiddleware', () => { expect(resolve).toHaveBeenLastCalledWith(host); }, ); + + it('properly supports transferring request bodies too', async () => { + const resolve = jest.fn(); + const discoveryApi = { getBaseUrl: resolve } as unknown as DiscoveryApi; + const middleware = new PluginProtocolResolverFetchMiddleware(discoveryApi); + const inner = jest.fn(); + const outer = middleware.apply(inner); + + resolve.mockResolvedValue('https://elsewhere.com'); + + await outer('plugin://a', { + method: 'POST', + body: '123', + }); + + expect(inner.mock.calls[0][0]).toBe('https://elsewhere.com'); + expect(inner.mock.calls[0][1].body).toBe('123'); + + await outer( + new Request('plugin://a', { + method: 'POST', + body: '123', + }), + ); + + expect(inner.mock.calls[1][0]).toBe('https://elsewhere.com'); + expect(inner.mock.calls[1][1].body).toEqual(Buffer.from('123', 'utf8')); + }); }); diff --git a/packages/core-app-api/src/apis/implementations/FetchApi/PluginProtocolResolverFetchMiddleware.ts b/packages/core-app-api/src/apis/implementations/FetchApi/PluginProtocolResolverFetchMiddleware.ts index 72834731fe..6265e8d3d2 100644 --- a/packages/core-app-api/src/apis/implementations/FetchApi/PluginProtocolResolverFetchMiddleware.ts +++ b/packages/core-app-api/src/apis/implementations/FetchApi/PluginProtocolResolverFetchMiddleware.ts @@ -55,7 +55,7 @@ export class PluginProtocolResolverFetchMiddleware implements FetchMiddleware { } const target = `${join(base, pathname)}${search}${hash}`; - return next(target, request); + return next(target, typeof input === 'string' ? init : input); }; } }