From dc0c1deceee9af83cbb438a7dc6d1b2fa689e763 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Wed, 5 Nov 2025 10:21:26 -0800 Subject: [PATCH] Fix copy-curl with API key https://feedback.yaak.app/p/copy-as-curl-bug-when-auth-use-api-key-with --- plugins/action-copy-curl/src/index.ts | 2 +- plugins/action-copy-curl/tests/index.test.ts | 76 +++++++++++--------- 2 files changed, 42 insertions(+), 36 deletions(-) diff --git a/plugins/action-copy-curl/src/index.ts b/plugins/action-copy-curl/src/index.ts index 275302a7..faaae4b5 100644 --- a/plugins/action-copy-curl/src/index.ts +++ b/plugins/action-copy-curl/src/index.ts @@ -46,7 +46,7 @@ export async function convertToCurl(request: Partial) { // Add API key authentication if (request.authenticationType === 'apikey') { if (request.authentication?.location === 'query') { - const sep = request.url?.includes('?') ? '&' : '?'; + const sep = finalUrl.includes('?') ? '&' : '?'; finalUrl = [ finalUrl, sep, diff --git a/plugins/action-copy-curl/tests/index.test.ts b/plugins/action-copy-curl/tests/index.test.ts index c193cf25..816d0688 100644 --- a/plugins/action-copy-curl/tests/index.test.ts +++ b/plugins/action-copy-curl/tests/index.test.ts @@ -275,11 +275,9 @@ describe('exporter-curl', () => { }, }), ).toEqual( - [ - `curl 'https://yaak.app'`, - `--aws-sigv4 aws:amz:us-east-1:s3`, - `--user 'ak:sk'`, - ].join(` \\\n `), + [`curl 'https://yaak.app'`, `--aws-sigv4 aws:amz:us-east-1:s3`, `--user 'ak:sk'`].join( + ` \\\n `, + ), ); }); @@ -314,15 +312,40 @@ describe('exporter-curl', () => { authentication: { location: 'header', key: 'X-Header', - value: 'my-token' + value: 'my-token', }, }), - ).toEqual( - [ - `curl 'https://yaak.app'`, - `--header 'X-Header: my-token'`, - ].join(` \\\n `), - ); + ).toEqual([`curl 'https://yaak.app'`, `--header 'X-Header: my-token'`].join(` \\\n `)); + }); + + test('API key auth header query', async () => { + expect( + await convertToCurl({ + url: 'https://yaak.app?hi=there', + urlParameters: [{ name: 'param', value: 'hi' }], + authenticationType: 'apikey', + authentication: { + location: 'query', + key: 'foo', + value: 'bar', + }, + }), + ).toEqual([`curl 'https://yaak.app?hi=there¶m=hi&foo=bar'`].join(` \\\n `)); + }); + + test('API key auth header query with params', async () => { + expect( + await convertToCurl({ + url: 'https://yaak.app', + urlParameters: [{ name: 'param', value: 'hi' }], + authenticationType: 'apikey', + authentication: { + location: 'query', + key: 'foo', + value: 'bar', + }, + }), + ).toEqual([`curl 'https://yaak.app?param=hi&foo=bar'`].join(` \\\n `)); }); test('API key auth header default', async () => { @@ -334,12 +357,7 @@ describe('exporter-curl', () => { location: 'header', }, }), - ).toEqual( - [ - `curl 'https://yaak.app'`, - `--header 'X-Api-Key: '`, - ].join(` \\\n `), - ); + ).toEqual([`curl 'https://yaak.app'`, `--header 'X-Api-Key: '`].join(` \\\n `)); }); test('API key auth query', async () => { @@ -350,14 +368,10 @@ describe('exporter-curl', () => { authentication: { location: 'query', key: 'foo', - value: 'bar-baz' + value: 'bar-baz', }, }), - ).toEqual( - [ - `curl 'https://yaak.app?foo=bar-baz'`, - ].join(` \\\n `), - ); + ).toEqual([`curl 'https://yaak.app?foo=bar-baz'`].join(` \\\n `)); }); test('API key auth query with existing', async () => { @@ -368,14 +382,10 @@ describe('exporter-curl', () => { authentication: { location: 'query', key: 'hi', - value: 'there' + value: 'there', }, }), - ).toEqual( - [ - `curl 'https://yaak.app?foo=bar&baz=qux&hi=there'`, - ].join(` \\\n `), - ); + ).toEqual([`curl 'https://yaak.app?foo=bar&baz=qux&hi=there'`].join(` \\\n `)); }); test('API key auth query default', async () => { @@ -387,11 +397,7 @@ describe('exporter-curl', () => { location: 'query', }, }), - ).toEqual( - [ - `curl 'https://yaak.app?foo=bar&baz=qux&token='`, - ].join(` \\\n `), - ); + ).toEqual([`curl 'https://yaak.app?foo=bar&baz=qux&token='`].join(` \\\n `)); }); test('Stale body data', async () => {