Prevent curl copy from using stale body data

https://feedback.yaak.app/p/copy-as-curl-includes-wrong-data-property
This commit is contained in:
Gregory Schier
2025-08-02 10:03:35 -07:00
parent 0e28079965
commit 8b84545b67
2 changed files with 26 additions and 10 deletions

View File

@@ -34,18 +34,18 @@ export async function convertToCurl(request: Partial<HttpRequest>) {
let finalUrl = request.url || '';
const urlParams = (request.urlParameters ?? []).filter(onlyEnabled);
if (urlParams.length > 0) {
// Build url
// Build url
const [base, hash] = finalUrl.split('#');
const separator = base!.includes('?') ? '&' : '?';
const queryString = urlParams
.map(p => `${encodeURIComponent(p.name)}=${encodeURIComponent(p.value)}`)
.map((p) => `${encodeURIComponent(p.name)}=${encodeURIComponent(p.value)}`)
.join('&');
finalUrl = base + separator + queryString + (hash ? `#${hash}` : '');
}
xs.push(quote(finalUrl));
xs.push(NEWLINE);
// Add headers
for (const h of (request.headers ?? []).filter(onlyEnabled)) {
xs.push('--header', quote(`${h.name}: ${h.value}`));
@@ -53,7 +53,11 @@ export async function convertToCurl(request: Partial<HttpRequest>) {
}
// Add form params
if (Array.isArray(request.body?.form)) {
const type = request.bodyType ?? 'none';
if (
(type === 'multipart/form-data' || type === 'application/x-www-form-urlencoded') &&
Array.isArray(request.body?.form)
) {
const flag = request.bodyType === 'multipart/form-data' ? '--form' : '--data';
for (const p of (request.body?.form ?? []).filter(onlyEnabled)) {
if (p.file) {
@@ -65,14 +69,14 @@ export async function convertToCurl(request: Partial<HttpRequest>) {
}
xs.push(NEWLINE);
}
} else if (typeof request.body?.query === 'string') {
} else if (type === 'graphql' && typeof request.body?.query === 'string') {
const body = {
query: request.body.query || '',
variables: maybeParseJSON(request.body.variables, undefined),
};
xs.push('--data', quote(JSON.stringify(body)));
xs.push(NEWLINE);
} else if (typeof request.body?.text === 'string') {
} else if (type !== 'none' && typeof request.body?.text === 'string') {
xs.push('--data', quote(request.body.text));
xs.push(NEWLINE);
}
@@ -116,4 +120,4 @@ function maybeParseJSON<T>(v: string, fallback: T) {
} catch {
return fallback;
}
}
}

View File

@@ -13,7 +13,7 @@ describe('exporter-curl', () => {
],
}),
).toEqual(
[`curl 'https://yaak.app/?a=aaa&b=bbb'`].join(` \\n `),
[`curl 'https://yaak.app?a=aaa&b=bbb'`].join(` \\n `),
);
});
@@ -218,4 +218,16 @@ describe('exporter-curl', () => {
}),
).toEqual([`curl 'https://yaak.app'`, `--header 'Authorization: Bearer '`].join(` \\\n `));
});
});
test('Stale body data', async () => {
expect(
await convertToCurl({
url: 'https://yaak.app',
bodyType: 'none',
body: {
text: 'ignore me',
}
}),
).toEqual([`curl 'https://yaak.app'`].join(` \\\n `));
});
});