Skip to content

Commit 5b03475

Browse files
committed
Fix extra params middleware for GET/HEAD requests
- Skip adding body to GET/HEAD requests which don't allow request bodies - Clean up global middleware registry between tests to prevent leakage - Fix query params to use modifiedRequest instead of original request
1 parent bc0d618 commit 5b03475

2 files changed

Lines changed: 18 additions & 10 deletions

File tree

packages/b2c-tooling-sdk/src/clients/middleware.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ export function createExtraParamsMiddleware(config: ExtraParamsConfig): Middlewa
212212
async onRequest({request}) {
213213
let modifiedRequest = request;
214214

215+
// HTTP methods that don't allow a request body
216+
const methodsWithoutBody = ['GET', 'HEAD'];
217+
const canHaveBody = !methodsWithoutBody.includes(modifiedRequest.method.toUpperCase());
218+
215219
// Add extra headers first (before other modifications)
216220
if (config.headers && Object.keys(config.headers).length > 0) {
217221
const newHeaders = new Headers(modifiedRequest.headers);
@@ -222,28 +226,26 @@ export function createExtraParamsMiddleware(config: ExtraParamsConfig): Middlewa
222226
modifiedRequest = new Request(modifiedRequest.url, {
223227
method: modifiedRequest.method,
224228
headers: newHeaders,
225-
body: modifiedRequest.body,
226-
duplex: modifiedRequest.body ? 'half' : undefined,
229+
...(canHaveBody && modifiedRequest.body ? {body: modifiedRequest.body, duplex: 'half'} : {}),
227230
} as RequestInit);
228231
}
229232

230233
// Add extra query parameters
231234
if (config.query && Object.keys(config.query).length > 0) {
232-
const url = new URL(request.url);
235+
const url = new URL(modifiedRequest.url);
233236
for (const [key, value] of Object.entries(config.query)) {
234237
if (value !== undefined) {
235238
url.searchParams.set(key, String(value));
236239
}
237240
}
238241
logger.trace(
239-
{extraQuery: config.query, originalUrl: request.url, newUrl: url.toString()},
242+
{extraQuery: config.query, originalUrl: modifiedRequest.url, newUrl: url.toString()},
240243
'[ExtraParams] Adding extra query params to URL',
241244
);
242245
modifiedRequest = new Request(url.toString(), {
243-
method: request.method,
244-
headers: request.headers,
245-
body: request.body,
246-
duplex: request.body ? 'half' : undefined,
246+
method: modifiedRequest.method,
247+
headers: modifiedRequest.headers,
248+
...(canHaveBody && modifiedRequest.body ? {body: modifiedRequest.body, duplex: 'half'} : {}),
247249
} as RequestInit);
248250
}
249251

@@ -268,8 +270,8 @@ export function createExtraParamsMiddleware(config: ExtraParamsConfig): Middlewa
268270
} catch {
269271
logger.warn('[ExtraParams] Could not parse request body as JSON, skipping body merge');
270272
}
271-
} else if (!modifiedRequest.body) {
272-
// No existing body, create one with extra fields
273+
} else if (!modifiedRequest.body && canHaveBody) {
274+
// No existing body, create one with extra fields (only for methods that allow a body)
273275
logger.trace({body: config.body}, '[ExtraParams] Creating new body with extra fields');
274276
const headers = new Headers(modifiedRequest.headers);
275277
headers.set('content-type', 'application/json');

packages/b2c-tooling-sdk/test/cli/base-command.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import {expect} from 'chai';
77
import {Config} from '@oclif/core';
88
import {BaseCommand} from '@salesforce/b2c-tooling-sdk/cli';
9+
import {globalMiddlewareRegistry} from '@salesforce/b2c-tooling-sdk/clients';
910

1011
// Create a concrete test command class
1112
class TestBaseCommand extends BaseCommand<typeof TestBaseCommand> {
@@ -57,6 +58,11 @@ describe('cli/base-command', () => {
5758
command = new TestBaseCommand([], config);
5859
});
5960

61+
afterEach(() => {
62+
// Clean up the global middleware registry between tests
63+
globalMiddlewareRegistry.clear();
64+
});
65+
6066
describe('init', () => {
6167
it('initializes command with default flags', async () => {
6268
// Mock parse method

0 commit comments

Comments
 (0)