From 5cc32370465bcec272024466b8d468cd917b78b1 Mon Sep 17 00:00:00 2001 From: Dmitry Ostrikov Date: Thu, 22 Aug 2024 13:06:48 +0300 Subject: [PATCH 1/4] fix(postman): introduce support for v2.0.0 auth object fixes #250 --- .../postman/src/converter/DefaultConverter.ts | 8 ++- .../postman/tests/DefaultConverter.spec.ts | 5 ++ .../v2.0.0-auth.postman_collection.json | 68 +++++++++++++++++++ ...v2.0.0-auth.postman_collection.result.json | 21 ++++++ 4 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 packages/postman/tests/fixtures/v2.0.0-auth.postman_collection.json create mode 100644 packages/postman/tests/fixtures/v2.0.0-auth.postman_collection.result.json diff --git a/packages/postman/src/converter/DefaultConverter.ts b/packages/postman/src/converter/DefaultConverter.ts index af4c404b..57d6155d 100644 --- a/packages/postman/src/converter/DefaultConverter.ts +++ b/packages/postman/src/converter/DefaultConverter.ts @@ -121,12 +121,18 @@ export class DefaultConverter implements Converter { /* istanbul ignore next */ private authRequest(request: Request, auth: Postman.RequestAuth): void { - const params: Postman.Variable[] | undefined = auth[auth.type]; + let params: Postman.Variable[] | Record | undefined = + auth[auth.type]; if (!params) { return; } + // ADHOC: convert 2.0.0 auth carried as a Record + if (!Array.isArray(params)) { + params = Object.entries(params).map((x) => ({ key: x[0], value: x[1] })); + } + const options = Object.fromEntries( params.map((val: Postman.Variable) => [val.key, val.value].map(String)) ); diff --git a/packages/postman/tests/DefaultConverter.spec.ts b/packages/postman/tests/DefaultConverter.spec.ts index 0739b0f3..f8a12159 100644 --- a/packages/postman/tests/DefaultConverter.spec.ts +++ b/packages/postman/tests/DefaultConverter.spec.ts @@ -20,6 +20,11 @@ describe('DefaultConverter', () => { }); it.each([ + { + inputPath: './fixtures/v2.0.0-auth.postman_collection.json', + expectedPath: './fixtures/v2.0.0-auth.postman_collection.result.json', + label: 'v2.0.0 auth' + }, { inputPath: './fixtures/trailing-slash.postman_collection.json', expectedPath: diff --git a/packages/postman/tests/fixtures/v2.0.0-auth.postman_collection.json b/packages/postman/tests/fixtures/v2.0.0-auth.postman_collection.json new file mode 100644 index 00000000..6b72bcf1 --- /dev/null +++ b/packages/postman/tests/fixtures/v2.0.0-auth.postman_collection.json @@ -0,0 +1,68 @@ +{ + "info": { + "name": "Auth", + "schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json" + }, + "item": [ + { + "name": "users", + "item": [ + { + "name": "Users", + "request": { + "auth": { + "type": "bearer", + "bearer": { + "token": "token-value" + } + }, + "method": "GET", + "header": [ + { + "key": "Accept", + "value": "application/json" + } + ], + "url": "{{baseUrl}}/users" + }, + "response": [ + { + "name": "Success", + "originalRequest": { + "method": "GET", + "header": [ + { + "key": "Accept", + "value": "application/json" + }, + { + "key": "Authorization", + "value": "Bearer ", + "description": "Added as a part of security scheme: bearer" + } + ], + "url": "{{baseUrl}}/user" + }, + "status": "OK", + "code": 200, + "header": [ + { + "key": "Content-Type", + "value": "application/json" + } + ], + "cookie": [], + "body": "{\n \"username\": \"\",\n \"name\": \"\" \n}" + } + ] + } + ] + } + ], + "variable": [ + { + "key": "baseUrl", + "value": "https://example.com/api/v1" + } + ] +} diff --git a/packages/postman/tests/fixtures/v2.0.0-auth.postman_collection.result.json b/packages/postman/tests/fixtures/v2.0.0-auth.postman_collection.result.json new file mode 100644 index 00000000..6f4ec645 --- /dev/null +++ b/packages/postman/tests/fixtures/v2.0.0-auth.postman_collection.result.json @@ -0,0 +1,21 @@ +[ + { + "bodySize": -1, + "cookies": [], + "headers": [ + { + "name": "Accept", + "value": "application/json" + }, + { + "name": "authorization", + "value": "Bearer token-value" + } + ], + "headersSize": -1, + "httpVersion": "HTTP/1.1", + "method": "GET", + "queryString": [], + "url": "https://example.com/api/v1/users" + } +] From 5881b31c1f304a4ce3bd0a6296be728546c0982e Mon Sep 17 00:00:00 2001 From: Dmitry Ostrikov Date: Thu, 22 Aug 2024 13:14:14 +0300 Subject: [PATCH 2/4] fix(postman): address complexity issue fixes #250 --- .../postman/src/converter/DefaultConverter.ts | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/packages/postman/src/converter/DefaultConverter.ts b/packages/postman/src/converter/DefaultConverter.ts index 57d6155d..7b8687a6 100644 --- a/packages/postman/src/converter/DefaultConverter.ts +++ b/packages/postman/src/converter/DefaultConverter.ts @@ -121,21 +121,20 @@ export class DefaultConverter implements Converter { /* istanbul ignore next */ private authRequest(request: Request, auth: Postman.RequestAuth): void { - let params: Postman.Variable[] | Record | undefined = + const params: Postman.Variable[] | Record | undefined = auth[auth.type]; if (!params) { return; } - // ADHOC: convert 2.0.0 auth carried as a Record - if (!Array.isArray(params)) { - params = Object.entries(params).map((x) => ({ key: x[0], value: x[1] })); - } - - const options = Object.fromEntries( - params.map((val: Postman.Variable) => [val.key, val.value].map(String)) - ); + const options = Array.isArray(params) + ? Object.fromEntries( + params.map((val: Postman.Variable) => + [val.key, val.value].map(String) + ) + ) + : params; switch (auth.type) { case 'apikey': From 8bc4c2a0e755176f8f2d60ffad25a2688a7658b0 Mon Sep 17 00:00:00 2001 From: Dmitry Ostrikov Date: Thu, 22 Aug 2024 13:23:44 +0300 Subject: [PATCH 3/4] fix(postman): address complexity issue fixes #250 --- .../postman/src/converter/DefaultConverter.ts | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/packages/postman/src/converter/DefaultConverter.ts b/packages/postman/src/converter/DefaultConverter.ts index 7b8687a6..9f86351e 100644 --- a/packages/postman/src/converter/DefaultConverter.ts +++ b/packages/postman/src/converter/DefaultConverter.ts @@ -121,21 +121,12 @@ export class DefaultConverter implements Converter { /* istanbul ignore next */ private authRequest(request: Request, auth: Postman.RequestAuth): void { - const params: Postman.Variable[] | Record | undefined = - auth[auth.type]; + const options = this.getAuthOptions(auth); - if (!params) { + if (!options) { return; } - const options = Array.isArray(params) - ? Object.fromEntries( - params.map((val: Postman.Variable) => - [val.key, val.value].map(String) - ) - ) - : params; - switch (auth.type) { case 'apikey': this.apiKeyAuth(request, options); @@ -155,6 +146,23 @@ export class DefaultConverter implements Converter { } } + private getAuthOptions( + auth: Postman.RequestAuth + ): Record | undefined { + const params: Postman.Variable[] | Record | undefined = + auth[auth.type]; + + return !params + ? params + : Array.isArray(params) + ? Object.fromEntries( + params.map((val: Postman.Variable) => + [val.key, val.value].map(String) + ) + ) + : params; + } + /* istanbul ignore next */ private oauth2(request: Request, options: Record): void { if (!options.accessToken || options.tokenType === 'mac') { From df6de7d8ce2ce51e900e01cf239f02877ab6d1df Mon Sep 17 00:00:00 2001 From: Dmitry Ostrikov Date: Thu, 22 Aug 2024 15:00:05 +0300 Subject: [PATCH 4/4] fix(postman): address complexity issue fixes #250 --- packages/postman/src/converter/DefaultConverter.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/postman/src/converter/DefaultConverter.ts b/packages/postman/src/converter/DefaultConverter.ts index 9f86351e..a558545e 100644 --- a/packages/postman/src/converter/DefaultConverter.ts +++ b/packages/postman/src/converter/DefaultConverter.ts @@ -152,9 +152,7 @@ export class DefaultConverter implements Converter { const params: Postman.Variable[] | Record | undefined = auth[auth.type]; - return !params - ? params - : Array.isArray(params) + return Array.isArray(params) ? Object.fromEntries( params.map((val: Postman.Variable) => [val.key, val.value].map(String)