From 095cb436bed6a100444bc1bb559456296419eee9 Mon Sep 17 00:00:00 2001 From: Alex Rybchenko Date: Fri, 18 Mar 2022 16:22:37 +0100 Subject: [PATCH 1/3] pass RequestInit instead of Request in PluginProtocolResolverFetchMiddleware Signed-off-by: Alex Rybchenko --- .../FetchApi/PluginProtocolResolverFetchMiddleware.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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..601ca3283b 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, init); }; } } From a7bb762dab384b2479edb873c6f355994621279f Mon Sep 17 00:00:00 2001 From: Alex Rybchenko Date: Fri, 18 Mar 2022 16:35:25 +0100 Subject: [PATCH 2/3] added changeset Signed-off-by: Alex Rybchenko --- .changeset/mean-pumas-search.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/mean-pumas-search.md 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 From 86c32c4902cb61382e728ed4a455670f39652d0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Mon, 21 Mar 2022 15:50:26 +0100 Subject: [PATCH 3/3] works with the Request input as well MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- ...ginProtocolResolverFetchMiddleware.test.ts | 28 +++++++++++++++++++ .../PluginProtocolResolverFetchMiddleware.ts | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) 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 601ca3283b..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, init); + return next(target, typeof input === 'string' ? init : input); }; } }