Fix copy-curl with API key

https://feedback.yaak.app/p/copy-as-curl-bug-when-auth-use-api-key-with
This commit is contained in:
Gregory Schier
2025-11-05 10:21:26 -08:00
parent 32d56f2274
commit dc0c1decee
2 changed files with 42 additions and 36 deletions

View File

@@ -46,7 +46,7 @@ export async function convertToCurl(request: Partial<HttpRequest>) {
// Add API key authentication // Add API key authentication
if (request.authenticationType === 'apikey') { if (request.authenticationType === 'apikey') {
if (request.authentication?.location === 'query') { if (request.authentication?.location === 'query') {
const sep = request.url?.includes('?') ? '&' : '?'; const sep = finalUrl.includes('?') ? '&' : '?';
finalUrl = [ finalUrl = [
finalUrl, finalUrl,
sep, sep,

View File

@@ -275,11 +275,9 @@ describe('exporter-curl', () => {
}, },
}), }),
).toEqual( ).toEqual(
[ [`curl 'https://yaak.app'`, `--aws-sigv4 aws:amz:us-east-1:s3`, `--user 'ak:sk'`].join(
`curl 'https://yaak.app'`, ` \\\n `,
`--aws-sigv4 aws:amz:us-east-1:s3`, ),
`--user 'ak:sk'`,
].join(` \\\n `),
); );
}); });
@@ -314,15 +312,40 @@ describe('exporter-curl', () => {
authentication: { authentication: {
location: 'header', location: 'header',
key: 'X-Header', key: 'X-Header',
value: 'my-token' value: 'my-token',
}, },
}), }),
).toEqual( ).toEqual([`curl 'https://yaak.app'`, `--header 'X-Header: my-token'`].join(` \\\n `));
[ });
`curl 'https://yaak.app'`,
`--header 'X-Header: my-token'`, test('API key auth header query', async () => {
].join(` \\\n `), 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&param=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 () => { test('API key auth header default', async () => {
@@ -334,12 +357,7 @@ describe('exporter-curl', () => {
location: 'header', location: 'header',
}, },
}), }),
).toEqual( ).toEqual([`curl 'https://yaak.app'`, `--header 'X-Api-Key: '`].join(` \\\n `));
[
`curl 'https://yaak.app'`,
`--header 'X-Api-Key: '`,
].join(` \\\n `),
);
}); });
test('API key auth query', async () => { test('API key auth query', async () => {
@@ -350,14 +368,10 @@ describe('exporter-curl', () => {
authentication: { authentication: {
location: 'query', location: 'query',
key: 'foo', key: 'foo',
value: 'bar-baz' value: 'bar-baz',
}, },
}), }),
).toEqual( ).toEqual([`curl 'https://yaak.app?foo=bar-baz'`].join(` \\\n `));
[
`curl 'https://yaak.app?foo=bar-baz'`,
].join(` \\\n `),
);
}); });
test('API key auth query with existing', async () => { test('API key auth query with existing', async () => {
@@ -368,14 +382,10 @@ describe('exporter-curl', () => {
authentication: { authentication: {
location: 'query', location: 'query',
key: 'hi', key: 'hi',
value: 'there' value: 'there',
}, },
}), }),
).toEqual( ).toEqual([`curl 'https://yaak.app?foo=bar&baz=qux&hi=there'`].join(` \\\n `));
[
`curl 'https://yaak.app?foo=bar&baz=qux&hi=there'`,
].join(` \\\n `),
);
}); });
test('API key auth query default', async () => { test('API key auth query default', async () => {
@@ -387,11 +397,7 @@ describe('exporter-curl', () => {
location: 'query', location: 'query',
}, },
}), }),
).toEqual( ).toEqual([`curl 'https://yaak.app?foo=bar&baz=qux&token='`].join(` \\\n `));
[
`curl 'https://yaak.app?foo=bar&baz=qux&token='`,
].join(` \\\n `),
);
}); });
test('Stale body data', async () => { test('Stale body data', async () => {