mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-07-10 06:42:52 +02:00
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:
@@ -34,18 +34,18 @@ export async function convertToCurl(request: Partial<HttpRequest>) {
|
|||||||
let finalUrl = request.url || '';
|
let finalUrl = request.url || '';
|
||||||
const urlParams = (request.urlParameters ?? []).filter(onlyEnabled);
|
const urlParams = (request.urlParameters ?? []).filter(onlyEnabled);
|
||||||
if (urlParams.length > 0) {
|
if (urlParams.length > 0) {
|
||||||
// Build url
|
// Build url
|
||||||
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}` : '');
|
||||||
}
|
}
|
||||||
|
|
||||||
xs.push(quote(finalUrl));
|
xs.push(quote(finalUrl));
|
||||||
xs.push(NEWLINE);
|
xs.push(NEWLINE);
|
||||||
|
|
||||||
// Add headers
|
// Add headers
|
||||||
for (const h of (request.headers ?? []).filter(onlyEnabled)) {
|
for (const h of (request.headers ?? []).filter(onlyEnabled)) {
|
||||||
xs.push('--header', quote(`${h.name}: ${h.value}`));
|
xs.push('--header', quote(`${h.name}: ${h.value}`));
|
||||||
@@ -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);
|
||||||
}
|
}
|
||||||
@@ -116,4 +120,4 @@ function maybeParseJSON<T>(v: string, fallback: T) {
|
|||||||
} catch {
|
} catch {
|
||||||
return fallback;
|
return fallback;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 `));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user