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
+8 -4
View File
@@ -38,7 +38,7 @@ export async function convertToCurl(request: Partial<HttpRequest>) {
const [base, hash] = finalUrl.split('#'); const [base, hash] = finalUrl.split('#');
const separator = base!.includes('?') ? '&' : '?'; const separator = base!.includes('?') ? '&' : '?';
const queryString = urlParams const queryString = urlParams
.map(p => `${encodeURIComponent(p.name)}=${encodeURIComponent(p.value)}`) .map((p) => `${encodeURIComponent(p.name)}=${encodeURIComponent(p.value)}`)
.join('&'); .join('&');
finalUrl = base + separator + queryString + (hash ? `#${hash}` : ''); finalUrl = base + separator + queryString + (hash ? `#${hash}` : '');
} }
@@ -53,7 +53,11 @@ export async function convertToCurl(request: Partial<HttpRequest>) {
} }
// Add form params // 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'; const flag = request.bodyType === 'multipart/form-data' ? '--form' : '--data';
for (const p of (request.body?.form ?? []).filter(onlyEnabled)) { for (const p of (request.body?.form ?? []).filter(onlyEnabled)) {
if (p.file) { if (p.file) {
@@ -65,14 +69,14 @@ export async function convertToCurl(request: Partial<HttpRequest>) {
} }
xs.push(NEWLINE); xs.push(NEWLINE);
} }
} else if (typeof request.body?.query === 'string') { } else if (type === 'graphql' && typeof request.body?.query === 'string') {
const body = { const body = {
query: request.body.query || '', query: request.body.query || '',
variables: maybeParseJSON(request.body.variables, undefined), variables: maybeParseJSON(request.body.variables, undefined),
}; };
xs.push('--data', quote(JSON.stringify(body))); xs.push('--data', quote(JSON.stringify(body)));
xs.push(NEWLINE); 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('--data', quote(request.body.text));
xs.push(NEWLINE); xs.push(NEWLINE);
} }
+13 -1
View File
@@ -13,7 +13,7 @@ describe('exporter-curl', () => {
], ],
}), }),
).toEqual( ).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 `)); ).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 `));
});
}); });