From e5fd4134ba0182faa97668065db7ad5bb1a1ff6d Mon Sep 17 00:00:00 2001 From: Song <1667077010@qq.com> Date: Sun, 20 Jul 2025 12:28:39 +0800 Subject: [PATCH] inline url search param and use `--data` (#239) --- plugins/action-copy-curl/src/index.ts | 29 ++++++++++++-------- plugins/action-copy-curl/tests/index.test.ts | 29 +++++++++++++++----- 2 files changed, 40 insertions(+), 18 deletions(-) diff --git a/plugins/action-copy-curl/src/index.ts b/plugins/action-copy-curl/src/index.ts index 8f04b878..c98e0154 100644 --- a/plugins/action-copy-curl/src/index.ts +++ b/plugins/action-copy-curl/src/index.ts @@ -29,16 +29,23 @@ export async function convertToCurl(request: Partial) { // Add method and URL all on first line if (request.method) xs.push('-X', request.method); - if (request.url) xs.push(quote(request.url)); - xs.push(NEWLINE); - - // Add URL params - for (const p of (request.urlParameters ?? []).filter(onlyEnabled)) { - xs.push('--url-query', quote(`${p.name}=${p.value}`)); - xs.push(NEWLINE); + // Build final URL with parameters (compatible with old curl) + let finalUrl = request.url || ''; + const urlParams = (request.urlParameters ?? []).filter(onlyEnabled); + if (urlParams.length > 0) { + // Build url + const [base, hash] = finalUrl.split('#'); + const separator = base!.includes('?') ? '&' : '?'; + const queryString = urlParams + .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}`)); @@ -63,10 +70,10 @@ export async function convertToCurl(request: Partial) { query: request.body.query || '', variables: maybeParseJSON(request.body.variables, undefined), }; - xs.push('--data-raw', `${quote(JSON.stringify(body))}`); + xs.push('--data', quote(JSON.stringify(body))); xs.push(NEWLINE); } else if (typeof request.body?.text === 'string') { - xs.push('--data-raw', `${quote(request.body.text)}`); + xs.push('--data', quote(request.body.text)); xs.push(NEWLINE); } @@ -109,4 +116,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 0f633a7b..1e358b35 100644 --- a/plugins/action-copy-curl/tests/index.test.ts +++ b/plugins/action-copy-curl/tests/index.test.ts @@ -13,7 +13,22 @@ describe('exporter-curl', () => { ], }), ).toEqual( - [`curl 'https://yaak.app'`, `--url-query 'a=aaa'`, `--url-query 'b=bbb'`].join(` \\\n `), + [`curl 'https://yaak.app/?a=aaa&b=bbb'`].join(` \\n `), + ); + }); + + test('Exports GET with params and hash', async () => { + expect( + await convertToCurl({ + url: 'https://yaak.app/path#section', + urlParameters: [ + { name: 'a', value: 'aaa' }, + { name: 'b', value: 'bbb', enabled: true }, + { name: 'c', value: 'ccc', enabled: false }, + ], + }), + ).toEqual( + [`curl 'https://yaak.app/path?a=aaa&b=bbb#section'`].join(` \\n `), ); }); test('Exports POST with url form data', async () => { @@ -47,7 +62,7 @@ describe('exporter-curl', () => { }, }), ).toEqual( - [`curl -X POST 'https://yaak.app'`, `--data-raw '{"query":"{foo,bar}","variables":{"a":"aaa","b":"bbb"}}'`].join(` \\\n `), + [`curl -X POST 'https://yaak.app'`, `--data '{"query":"{foo,bar}","variables":{"a":"aaa","b":"bbb"}}'`].join(` \\\n `), ); }); @@ -62,7 +77,7 @@ describe('exporter-curl', () => { }, }), ).toEqual( - [`curl -X POST 'https://yaak.app'`, `--data-raw '{"query":"{foo,bar}"}'`].join(` \\\n `), + [`curl -X POST 'https://yaak.app'`, `--data '{"query":"{foo,bar}"}'`].join(` \\\n `), ); }); @@ -106,7 +121,7 @@ describe('exporter-curl', () => { [ `curl -X POST 'https://yaak.app'`, `--header 'Content-Type: application/json'`, - `--data-raw '{"foo":"bar\\'s"}'`, + `--data '{"foo":"bar\\'s"}'`, ].join(` \\\n `), ); }); @@ -126,7 +141,7 @@ describe('exporter-curl', () => { [ `curl -X POST 'https://yaak.app'`, `--header 'Content-Type: application/json'`, - `--data-raw '{"foo":"bar",\n"baz":"qux"}'`, + `--data '{"foo":"bar",\n"baz":"qux"}'`, ].join(` \\\n `), ); }); @@ -140,7 +155,7 @@ describe('exporter-curl', () => { { name: 'c', value: 'ccc', enabled: false }, ], }), - ).toEqual([`curl`, `--header 'a: aaa'`, `--header 'b: bbb'`].join(` \\\n `)); + ).toEqual([`curl ''`, `--header 'a: aaa'`, `--header 'b: bbb'`].join(` \\\n `)); }); test('Basic auth', async () => { @@ -203,4 +218,4 @@ describe('exporter-curl', () => { }), ).toEqual([`curl 'https://yaak.app'`, `--header 'Authorization: Bearer '`].join(` \\\n `)); }); -}); +}); \ No newline at end of file