From d4d331b041ee649ee1bedea7fb1272bca5f58a5d Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Tue, 8 Oct 2024 14:45:46 -0700 Subject: [PATCH] Fix curl import when using boolean flags --- plugins/importer-curl/src/index.ts | 6 ++++-- plugins/importer-curl/tests/index.test.ts | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/plugins/importer-curl/src/index.ts b/plugins/importer-curl/src/index.ts index 00cb4ca..a1ab810 100644 --- a/plugins/importer-curl/src/index.ts +++ b/plugins/importer-curl/src/index.ts @@ -27,12 +27,13 @@ const SUPPORTED_ARGS = [ ['request', 'X'], // Request method DATA_FLAGS, ].flatMap((v) => v); +const BOOL_FLAGS = ['G', 'get', 'digest']; type Pair = string | boolean; type PairsByName = Record; -export function pluginHookImport(ctx: Context, rawData: string) { +export function pluginHookImport(_ctx: Context, rawData: string) { if (!rawData.match(/^\s*curl /)) { return null; } @@ -140,11 +141,12 @@ function importCommand(parseEntries: ParseEntry[], workspaceId: string) { let value; const nextEntry = parseEntries[i + 1]; + const hasValue = !BOOL_FLAGS.includes(name); if (isSingleDash && name.length > 1) { // Handle squished arguments like -XPOST value = name.slice(1); name = name.slice(0, 1); - } else if (typeof nextEntry === 'string' && !nextEntry.startsWith('-')) { + } else if (typeof nextEntry === 'string' && hasValue && !nextEntry.startsWith('-')) { // Next arg is not a flag, so assign it as the value value = nextEntry; i++; // Skip next one diff --git a/plugins/importer-curl/tests/index.test.ts b/plugins/importer-curl/tests/index.test.ts index bb53621..b01c838 100644 --- a/plugins/importer-curl/tests/index.test.ts +++ b/plugins/importer-curl/tests/index.test.ts @@ -196,6 +196,27 @@ describe('importer-curl', () => { }); }); + test('Imports post data into URL', () => { + expect( + pluginHookImport(ctx, 'curl -G https://api.stripe.com/v1/payment_links -d limit=3'), + ).toEqual({ + resources: { + workspaces: [baseWorkspace()], + httpRequests: [ + baseRequest({ + method: 'GET', + url: 'https://api.stripe.com/v1/payment_links', + urlParameters: [{ + enabled: true, + name: 'limit', + value: '3', + }] + }), + ], + }, + }); + }); + test('Imports multi-line JSON', () => { expect( pluginHookImport(