diff --git a/plugins/action-copy-curl/src/index.ts b/plugins/action-copy-curl/src/index.ts index fe3fc38a..275302a7 100644 --- a/plugins/action-copy-curl/src/index.ts +++ b/plugins/action-copy-curl/src/index.ts @@ -43,6 +43,26 @@ export async function convertToCurl(request: Partial) { finalUrl = base + separator + queryString + (hash ? `#${hash}` : ''); } + // Add API key authentication + if (request.authenticationType === 'apikey') { + if (request.authentication?.location === 'query') { + const sep = request.url?.includes('?') ? '&' : '?'; + finalUrl = [ + finalUrl, + sep, + encodeURIComponent(request.authentication?.key ?? 'token'), + '=', + encodeURIComponent(request.authentication?.value ?? ''), + ].join(''); + } else { + request.headers = request.headers ?? []; + request.headers.push({ + name: request.authentication?.key ?? 'X-Api-Key', + value: request.authentication?.value ?? '', + }); + } + } + xs.push(quote(finalUrl)); xs.push(NEWLINE); diff --git a/plugins/action-copy-curl/tests/index.test.ts b/plugins/action-copy-curl/tests/index.test.ts index 958de016..c193cf25 100644 --- a/plugins/action-copy-curl/tests/index.test.ts +++ b/plugins/action-copy-curl/tests/index.test.ts @@ -27,6 +27,7 @@ describe('exporter-curl', () => { }), ).toEqual([`curl 'https://yaak.app/path?a=aaa&b=bbb#section'`].join(` \\n `)); }); + test('Exports POST with url form data', async () => { expect( await convertToCurl({ @@ -305,6 +306,94 @@ describe('exporter-curl', () => { ); }); + test('API key auth header', async () => { + expect( + await convertToCurl({ + url: 'https://yaak.app', + authenticationType: 'apikey', + authentication: { + location: 'header', + key: 'X-Header', + value: 'my-token' + }, + }), + ).toEqual( + [ + `curl 'https://yaak.app'`, + `--header 'X-Header: my-token'`, + ].join(` \\\n `), + ); + }); + + test('API key auth header default', async () => { + expect( + await convertToCurl({ + url: 'https://yaak.app', + authenticationType: 'apikey', + authentication: { + location: 'header', + }, + }), + ).toEqual( + [ + `curl 'https://yaak.app'`, + `--header 'X-Api-Key: '`, + ].join(` \\\n `), + ); + }); + + test('API key auth query', async () => { + expect( + await convertToCurl({ + url: 'https://yaak.app', + authenticationType: 'apikey', + authentication: { + location: 'query', + key: 'foo', + value: 'bar-baz' + }, + }), + ).toEqual( + [ + `curl 'https://yaak.app?foo=bar-baz'`, + ].join(` \\\n `), + ); + }); + + test('API key auth query with existing', async () => { + expect( + await convertToCurl({ + url: 'https://yaak.app?foo=bar&baz=qux', + authenticationType: 'apikey', + authentication: { + location: 'query', + key: 'hi', + value: 'there' + }, + }), + ).toEqual( + [ + `curl 'https://yaak.app?foo=bar&baz=qux&hi=there'`, + ].join(` \\\n `), + ); + }); + + test('API key auth query default', async () => { + expect( + await convertToCurl({ + url: 'https://yaak.app?foo=bar&baz=qux', + authenticationType: 'apikey', + authentication: { + location: 'query', + }, + }), + ).toEqual( + [ + `curl 'https://yaak.app?foo=bar&baz=qux&token='`, + ].join(` \\\n `), + ); + }); + test('Stale body data', async () => { expect( await convertToCurl({