diff --git a/plugins/action-copy-curl/src/index.ts b/plugins/action-copy-curl/src/index.ts index c98e0154..fba4fbe0 100644 --- a/plugins/action-copy-curl/src/index.ts +++ b/plugins/action-copy-curl/src/index.ts @@ -34,18 +34,18 @@ export async function convertToCurl(request: Partial) { 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) { } // 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) { } 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(v: string, fallback: T) { } catch { return fallback; } -} \ No newline at end of file +} diff --git a/plugins/action-copy-curl/tests/index.test.ts b/plugins/action-copy-curl/tests/index.test.ts index 1e358b35..0fc04607 100644 --- a/plugins/action-copy-curl/tests/index.test.ts +++ b/plugins/action-copy-curl/tests/index.test.ts @@ -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 `)); }); -}); \ No newline at end of file + + 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 `)); + }); +});