diff --git a/plugins/action-copy-grpcurl/src/index.ts b/plugins/action-copy-grpcurl/src/index.ts index 8615fa49..f83255ad 100644 --- a/plugins/action-copy-grpcurl/src/index.ts +++ b/plugins/action-copy-grpcurl/src/index.ts @@ -68,16 +68,37 @@ export async function convert(request: Partial, allProtoFiles: stri } // Add basic authentication - if (request.authenticationType === 'basic') { - const user = request.authentication?.username ?? ''; - const pass = request.authentication?.password ?? ''; - const encoded = btoa(`${user}:${pass}`); - xs.push('-H', quote(`Authorization: Basic ${encoded}`)); - xs.push(NEWLINE); - } else if (request.authenticationType === 'bearer') { - // Add bearer authentication - xs.push('-H', quote(`Authorization: Bearer ${request.authentication?.token ?? ''}`)); - xs.push(NEWLINE); + if (request.authentication?.disabled !== true) { + if (request.authenticationType === 'basic') { + const user = request.authentication?.username ?? ''; + const pass = request.authentication?.password ?? ''; + const encoded = btoa(`${user}:${pass}`); + xs.push('-H', quote(`Authorization: Basic ${encoded}`)); + xs.push(NEWLINE); + } else if (request.authenticationType === 'bearer') { + // Add bearer authentication + xs.push('-H', quote(`Authorization: Bearer ${request.authentication?.token ?? ''}`)); + xs.push(NEWLINE); + } else if (request.authenticationType === 'apikey') { + if (request.authentication?.location === 'query') { + const sep = request.url?.includes('?') ? '&' : '?'; + request.url = [ + request.url, + sep, + encodeURIComponent(request.authentication?.key ?? 'token'), + '=', + encodeURIComponent(request.authentication?.value ?? ''), + ].join(''); + } else { + xs.push( + '-H', + quote( + `${request.authentication?.key ?? 'X-Api-Key'}: ${request.authentication?.value ?? ''}`, + ), + ); + } + xs.push(NEWLINE); + } } // Add form params diff --git a/plugins/action-copy-grpcurl/tests/index.test.ts b/plugins/action-copy-grpcurl/tests/index.test.ts index 7ee2ec98..c420ff8e 100644 --- a/plugins/action-copy-grpcurl/tests/index.test.ts +++ b/plugins/action-copy-grpcurl/tests/index.test.ts @@ -27,6 +27,55 @@ describe('exporter-curl', () => { ), ).toEqual([`grpcurl -H 'aaa: AAA'`, `-H 'bbb: BBB'`, `yaak.app`].join(` \\\n `)); }); + test('Basic auth', async () => { + expect( + await convert( + { + url: 'https://yaak.app', + authenticationType: 'basic', + authentication: { + username: 'user', + password: 'pass', + }, + }, + [], + ), + ).toEqual([`grpcurl -H 'Authorization: Basic dXNlcjpwYXNz'`, `yaak.app`].join(` \\\n `)); + }); + + test('API key auth', async () => { + expect( + await convert( + { + url: 'https://yaak.app', + authenticationType: 'apikey', + authentication: { + key: 'X-Token', + value: 'tok', + }, + }, + [], + ), + ).toEqual([`grpcurl -H 'X-Token: tok'`, `yaak.app`].join(` \\\n `)); + }); + + test('API key auth', async () => { + expect( + await convert( + { + url: 'https://yaak.app', + authenticationType: 'apikey', + authentication: { + location: 'query', + key: 'token', + value: 'tok 1', + }, + }, + [], + ), + ).toEqual([`grpcurl`, `yaak.app?token=tok%201`].join(` \\\n `)); + }); + test('Single proto file', async () => { expect(await convert({ url: 'https://yaak.app' }, ['/foo/bar/baz.proto'])).toEqual( [