From 7dde777c912fd2af92deba8eb829b2a7e783720a Mon Sep 17 00:00:00 2001 From: Marcell Toth Date: Wed, 21 Oct 2020 15:03:12 +0200 Subject: [PATCH 1/7] feat(request-maker): switch to using the upstream httpsnippet --- packages/elements/package.json | 2 +- .../src/stores/request-maker/request.ts | 3 +-- yarn.lock | 18 +----------------- 3 files changed, 3 insertions(+), 20 deletions(-) diff --git a/packages/elements/package.json b/packages/elements/package.json index f35ee043c..a3a002707 100644 --- a/packages/elements/package.json +++ b/packages/elements/package.json @@ -46,9 +46,9 @@ "@fortawesome/fontawesome-svg-core": "^1.2.31", "@fortawesome/free-solid-svg-icons": "^5.14.0", "@fortawesome/react-fontawesome": "^0.1.11", + "httpsnippet": "^1.23.0", "@stoplight/elements-utils": "beta", "@stoplight/http-spec": "^3.1.1", - "@stoplight/httpsnippet": "^1.2.1", "@stoplight/json": "^3.9.0", "@stoplight/json-schema-ref-parser": "^9.0.4", "@stoplight/json-schema-viewer": "^3.0.0-beta.33", diff --git a/packages/elements/src/stores/request-maker/request.ts b/packages/elements/src/stores/request-maker/request.ts index 176fc5eb7..56b3e182a 100644 --- a/packages/elements/src/stores/request-maker/request.ts +++ b/packages/elements/src/stores/request-maker/request.ts @@ -12,8 +12,7 @@ import { getContentType } from '../../utils/getContentType'; import { getEnabledParams, getNameValuePairs, getParamArray, getParamValue } from '../../utils/params'; import { addParamsToPath, extractQueryParams, getParamsFromPath, replaceParamsInPath } from '../../utils/url'; import { Auth, ContentType, HeaderParam, IParam, ParamType, PathParam, QueryParam } from './types'; - -const HTTPSnippet = require('@stoplight/httpsnippet'); +import HTTPSnippet = require('httpsnippet'); const DEFAULT_EMPTY_JSON = '{\n \n}'; const DEFAULT_EMPTY_GQL = 'query {\n \n}'; diff --git a/yarn.lock b/yarn.lock index 7aa2c5cf4..ec60622a1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3841,22 +3841,6 @@ type-is "^1.6.18" urijs "~1.19.2" -"@stoplight/httpsnippet@^1.2.1": - version "1.21.0" - resolved "https://registry.yarnpkg.com/@stoplight/httpsnippet/-/httpsnippet-1.21.0.tgz#67f8b03de22a0af3c39187f2bfc94bdebbea4732" - integrity sha512-mb86uiXJlEETgkCSfpg2frvWy0wnbnP7UvwxvzHA+sKcB2fTWpgRKwLFrClgxGBbiZrIbiJlMelnx8u4k6oOtQ== - dependencies: - chalk "^1.1.1" - commander "^2.9.0" - debug "^2.2.0" - event-stream "3.3.4" - form-data "^3.0.0" - fs-readfile-promise "^2.0.1" - fs-writefile-promise "^1.0.3" - har-validator "^5.0.0" - pinkie-promise "^2.0.0" - stringify-object "^3.3.0" - "@stoplight/json-schema-merge-allof@^0.7.1": version "0.7.1" resolved "https://registry.yarnpkg.com/@stoplight/json-schema-merge-allof/-/json-schema-merge-allof-0.7.1.tgz#7a2c99da9448c5b6b5cbd9afd9f1fb4957d5cbeb" @@ -11236,7 +11220,7 @@ fork-ts-checker-webpack-plugin@^4.1.4: tapable "^1.0.0" worker-rpc "^0.1.0" -form-data@^3.0.0: +form-data@3.0.0, form-data@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/form-data/-/form-data-3.0.0.tgz#31b7e39c85f1355b7139ee0c647cf0de7f83c682" integrity sha512-CKMFDglpbMi6PyN+brwB9Q/GOw0eAnsrEZDgcsH5Krhz5Od/haKHAX0NmQfha2zPPz0JpWzA7GJHGSnvCRLWsg== From 86b0e4a6f4ca6e0ef449dacdac40eea5d39a471d Mon Sep 17 00:00:00 2001 From: Marcell Toth Date: Wed, 21 Oct 2020 15:06:52 +0200 Subject: [PATCH 2/7] chore: don't cheat with types --- packages/elements/src/stores/request-maker/request.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/elements/src/stores/request-maker/request.ts b/packages/elements/src/stores/request-maker/request.ts index 56b3e182a..c3530b5ef 100644 --- a/packages/elements/src/stores/request-maker/request.ts +++ b/packages/elements/src/stores/request-maker/request.ts @@ -298,8 +298,8 @@ export class RequestStore { } try { - const snippet = new HTTPSnippet(this.toHAR(), {}); - return snippet.convert(language, library); + const snippet = new HTTPSnippet(this.toHAR()); + return snippet.convert(language, library) || null; } catch (err) { console.error(err); return null; From 1405be1b813c7471b1a4236d4091ffaf0e7baa3b Mon Sep 17 00:00:00 2001 From: Marcell Toth Date: Fri, 30 Oct 2020 17:16:39 +0100 Subject: [PATCH 3/7] fix: all the lies around typings for request.toHAR --- packages/elements/package.json | 2 + .../src/stores/request-maker/request.ts | 49 +++++++++++-------- yarn.lock | 12 +++++ 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/packages/elements/package.json b/packages/elements/package.json index a3a002707..0e9d88694 100644 --- a/packages/elements/package.json +++ b/packages/elements/package.json @@ -94,6 +94,8 @@ "@types/classnames": "^2.2.10", "@types/enzyme": "3.x.x", "@types/enzyme-adapter-react-16": "1.x.x", + "@types/har-format": "^1.2.5", + "@types/httpsnippet": "^1.23.0", "@types/jest": "25.2.1", "@types/lodash": "^4.14.149", "@types/object-hash": "^1.3.1", diff --git a/packages/elements/src/stores/request-maker/request.ts b/packages/elements/src/stores/request-maker/request.ts index c3530b5ef..9d4631c59 100644 --- a/packages/elements/src/stores/request-maker/request.ts +++ b/packages/elements/src/stores/request-maker/request.ts @@ -3,7 +3,8 @@ import { IHttpRequest as IPrismHttpRequest } from '@stoplight/prism-http'; import { HttpMethod, HttpNameValue, IHttpRequest, IServer } from '@stoplight/types'; import { safeStringify as safeStringifyYaml } from '@stoplight/yaml'; import { AxiosRequestConfig } from 'axios'; -import { isEmpty, mapKeys, pick, set } from 'lodash'; +import { Request } from 'har-format'; +import { isEmpty, mapKeys, set } from 'lodash'; import { action, computed, observable } from 'mobx'; import * as typeis from 'type-is'; import URI from 'urijs'; @@ -233,38 +234,42 @@ export class RequestStore { if (this.contentType === 'x-www-form-urlencoded') { postData = { - mimeType: 'application/x-www-form-urlencoded', - params: getEnabledParams(this.urlEncodedParams).map(p => pick(p, 'name', 'value')), + mimeType: 'application/x-www-form-urlencoded' as const, + params: getEnabledParams(this.urlEncodedParams).map(p => ({ name: p.name, value: p.value ?? '' })), }; } else if (this.contentType === 'form-data') { postData = { - mimeType: 'application/x-www-form-urlencoded', - params: getEnabledParams(this.formDataParams).map(p => pick(p, 'name', 'value')), + mimeType: 'application/x-www-form-urlencoded' as const, + params: getEnabledParams(this.formDataParams).map(p => ({ + name: p.name, + value: p.value instanceof File ? '' : p.value, + })), }; } else if (this.contentType === 'raw') { postData = { - mimeType: 'application/json', - text: safeStringify(safeParse(this.body), undefined, 0), + mimeType: 'application/json' as const, + text: safeStringify(safeParse(this.body), undefined, 0) ?? '', }; } else if (this.contentType === 'graphql') { postData = { - mimeType: 'application/json', - text: safeStringify( - { - query: safeParse(this.graphqlQuery) || this.graphqlQuery, - variables: safeParse(this.graphqlVariables) || this.graphqlVariables, - }, - undefined, - 0, - ), + mimeType: 'application/json' as const, + text: + safeStringify( + { + query: safeParse(this.graphqlQuery) || this.graphqlQuery, + variables: safeParse(this.graphqlVariables) || this.graphqlVariables, + }, + undefined, + 0, + ) ?? '', }; } else if (this.contentType === 'binary') { postData = { - mimeType: 'multipart/form-data', + mimeType: 'multipart/form-data' as const, params: getEnabledParams(this.formDataParams).map(p => ({ name: p.name, fileName: p.name, - value: p.value, + value: p.value instanceof File ? '' : p.value, contentType: p.type, })), }; @@ -273,9 +278,13 @@ export class RequestStore { return { method: this.method.toUpperCase(), url: this.url, - // @ts-ignore: Request is expecting a map, but HTTPSnippet is expecting an array - headers: getEnabledParams(this.headerParams).map(p => pick(p, 'name', 'value')), + headers: getEnabledParams(this.headerParams).map(p => ({ name: p.name, value: p.value ?? '' })), postData, + httpVersion: 'HTTP/1.1', + headersSize: -1, + bodySize: -1, + queryString: [], + cookies: [], }; } diff --git a/yarn.lock b/yarn.lock index ec60622a1..fa66c592f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5646,6 +5646,11 @@ dependencies: "@types/node" "*" +"@types/har-format@*", "@types/har-format@^1.2.5": + version "1.2.5" + resolved "https://registry.yarnpkg.com/@types/har-format/-/har-format-1.2.5.tgz#4f6648814d0fdcb6a510e3364a9db439a753c4b1" + integrity sha512-IG8AE1m2pWtPqQ7wXhFhy6Q59bwwnLwO36v5Rit2FrbXCIp8Sk8E2PfUCreyrdo17STwFSKDAkitVuVYbpEHvQ== + "@types/hast@^2.0.0": version "2.3.1" resolved "https://registry.yarnpkg.com/@types/hast/-/hast-2.3.1.tgz#b16872f2a6144c7025f296fb9636a667ebb79cd9" @@ -5663,6 +5668,13 @@ resolved "https://registry.yarnpkg.com/@types/html-minifier-terser/-/html-minifier-terser-5.1.0.tgz#551a4589b6ee2cc9c1dff08056128aec29b94880" integrity sha512-iYCgjm1dGPRuo12+BStjd1HiVQqhlRhWDOQigNxn023HcjnhsiFz9pc6CzJj4HwDCSQca9bxTL4PxJDbkdm3PA== +"@types/httpsnippet@^1.23.0": + version "1.23.0" + resolved "https://registry.yarnpkg.com/@types/httpsnippet/-/httpsnippet-1.23.0.tgz#83ba34c4ec712a54532caa93e270f66e0285eb4e" + integrity sha512-SVkF5LkgI8+7OkWXRyvw2DY2xPjqpCGzySs0vIlzYEvrus6NO0o7EhSbqzUuvkDC7N3mmXiI+x/vfKFo6+RlCw== + dependencies: + "@types/har-format" "*" + "@types/is-function@^1.0.0": version "1.0.0" resolved "https://registry.yarnpkg.com/@types/is-function/-/is-function-1.0.0.tgz#1b0b819b1636c7baf0d6785d030d12edf70c3e83" From d16b4ce5ea0e981b0f4d7e549b4329cb6df3c6cd Mon Sep 17 00:00:00 2001 From: Marcell Toth Date: Fri, 30 Oct 2020 17:25:25 +0100 Subject: [PATCH 4/7] fix: import using esmoduleinterop --- packages/elements/src/stores/request-maker/request.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/elements/src/stores/request-maker/request.ts b/packages/elements/src/stores/request-maker/request.ts index 9d4631c59..3e7db9622 100644 --- a/packages/elements/src/stores/request-maker/request.ts +++ b/packages/elements/src/stores/request-maker/request.ts @@ -4,6 +4,7 @@ import { HttpMethod, HttpNameValue, IHttpRequest, IServer } from '@stoplight/typ import { safeStringify as safeStringifyYaml } from '@stoplight/yaml'; import { AxiosRequestConfig } from 'axios'; import { Request } from 'har-format'; +import HTTPSnippet from 'httpsnippet'; import { isEmpty, mapKeys, set } from 'lodash'; import { action, computed, observable } from 'mobx'; import * as typeis from 'type-is'; @@ -13,7 +14,6 @@ import { getContentType } from '../../utils/getContentType'; import { getEnabledParams, getNameValuePairs, getParamArray, getParamValue } from '../../utils/params'; import { addParamsToPath, extractQueryParams, getParamsFromPath, replaceParamsInPath } from '../../utils/url'; import { Auth, ContentType, HeaderParam, IParam, ParamType, PathParam, QueryParam } from './types'; -import HTTPSnippet = require('httpsnippet'); const DEFAULT_EMPTY_JSON = '{\n \n}'; const DEFAULT_EMPTY_GQL = 'query {\n \n}'; From f10b66e555f5ddbeed6a049bd1c694691be65f6b Mon Sep 17 00:00:00 2001 From: Marcell Toth Date: Mon, 2 Nov 2020 08:28:32 +0100 Subject: [PATCH 5/7] test: fix --- .../src/stores/request-maker/__tests__/request.spec.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/elements/src/stores/request-maker/__tests__/request.spec.ts b/packages/elements/src/stores/request-maker/__tests__/request.spec.ts index e576fe550..9d199fe20 100644 --- a/packages/elements/src/stores/request-maker/__tests__/request.spec.ts +++ b/packages/elements/src/stores/request-maker/__tests__/request.spec.ts @@ -532,7 +532,7 @@ describe('RequestStore', () => { foo: 'bar', }; - expect(requestStore.toHAR()).toEqual({ + expect(requestStore.toHAR()).toMatchObject({ method: 'POST', url: 'http://mockbin.com/har', headers: [ @@ -563,7 +563,7 @@ describe('RequestStore', () => { foo: 'bar', }; - expect(requestStore.toHAR()).toEqual({ + expect(requestStore.toHAR()).toMatchObject({ method: 'POST', url: 'http://mockbin.com/har', headers: [ @@ -594,7 +594,7 @@ describe('RequestStore', () => { foo: 'bar', }; - expect(requestStore.toHAR()).toEqual({ + expect(requestStore.toHAR()).toMatchObject({ method: 'POST', url: 'http://mockbin.com/har', headers: [ @@ -624,7 +624,7 @@ describe('RequestStore', () => { }`; requestStore.graphqlVariables = 'episode: $episode'; - expect(requestStore.toHAR()).toEqual({ + expect(requestStore.toHAR()).toMatchObject({ method: 'POST', url: 'http://mockbin.com/har', headers: [ @@ -662,7 +662,7 @@ describe('RequestStore', () => { }, ]; - expect(requestStore.toHAR()).toEqual({ + expect(requestStore.toHAR()).toMatchObject({ method: 'POST', url: 'http://mockbin.com/har', headers: [ From 3713abb3bd8cfc8fcaa96ea257fa8ed2aecd5e6f Mon Sep 17 00:00:00 2001 From: Marcell Toth Date: Thu, 19 Nov 2020 14:11:07 +0100 Subject: [PATCH 6/7] fix: use latest httpsnippet --- packages/elements/package.json | 2 +- yarn.lock | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/elements/package.json b/packages/elements/package.json index 0e9d88694..b89a6b773 100644 --- a/packages/elements/package.json +++ b/packages/elements/package.json @@ -46,7 +46,7 @@ "@fortawesome/fontawesome-svg-core": "^1.2.31", "@fortawesome/free-solid-svg-icons": "^5.14.0", "@fortawesome/react-fontawesome": "^0.1.11", - "httpsnippet": "^1.23.0", + "httpsnippet": "^1.24.0", "@stoplight/elements-utils": "beta", "@stoplight/http-spec": "^3.1.1", "@stoplight/json": "^3.9.0", diff --git a/yarn.lock b/yarn.lock index fa66c592f..8e4737293 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3841,6 +3841,22 @@ type-is "^1.6.18" urijs "~1.19.2" +"@stoplight/httpsnippet@^1.2.1": + version "1.21.0" + resolved "https://registry.yarnpkg.com/@stoplight/httpsnippet/-/httpsnippet-1.21.0.tgz#67f8b03de22a0af3c39187f2bfc94bdebbea4732" + integrity sha512-mb86uiXJlEETgkCSfpg2frvWy0wnbnP7UvwxvzHA+sKcB2fTWpgRKwLFrClgxGBbiZrIbiJlMelnx8u4k6oOtQ== + dependencies: + chalk "^1.1.1" + commander "^2.9.0" + debug "^2.2.0" + event-stream "3.3.4" + form-data "^3.0.0" + fs-readfile-promise "^2.0.1" + fs-writefile-promise "^1.0.3" + har-validator "^5.0.0" + pinkie-promise "^2.0.0" + stringify-object "^3.3.0" + "@stoplight/json-schema-merge-allof@^0.7.1": version "0.7.1" resolved "https://registry.yarnpkg.com/@stoplight/json-schema-merge-allof/-/json-schema-merge-allof-0.7.1.tgz#7a2c99da9448c5b6b5cbd9afd9f1fb4957d5cbeb" @@ -12345,6 +12361,22 @@ https-proxy-agent@^5.0.0: agent-base "6" debug "4" +httpsnippet@^1.24.0: + version "1.24.0" + resolved "https://registry.yarnpkg.com/httpsnippet/-/httpsnippet-1.24.0.tgz#2983e15a43b7b160e8e31ac792076060a65f1de3" + integrity sha512-W2GRlKXPm+alFdkYvts7zS54Y8sjOGN1H4dMfLCcNZZrG2Rg9jY57aN/Fyiov4/Z0paFxCS1vKDbugNYfAhUBg== + dependencies: + chalk "^1.1.1" + commander "^2.9.0" + debug "^2.2.0" + event-stream "3.3.4" + form-data "3.0.0" + fs-readfile-promise "^2.0.1" + fs-writefile-promise "^1.0.3" + har-validator "^5.0.0" + pinkie-promise "^2.0.0" + stringify-object "^3.3.0" + human-signals@^1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-1.1.1.tgz#c5b1cd14f50aeae09ab6c59fe63ba3395fe4dfa3" From e3611f3d7e07373978295740bcfa11d31349062a Mon Sep 17 00:00:00 2001 From: Marcell Toth Date: Thu, 19 Nov 2020 14:24:05 +0100 Subject: [PATCH 7/7] test: fix assertion --- .../src/stores/request-maker/__tests__/request.spec.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/elements/src/stores/request-maker/__tests__/request.spec.ts b/packages/elements/src/stores/request-maker/__tests__/request.spec.ts index 9d199fe20..b001acb15 100644 --- a/packages/elements/src/stores/request-maker/__tests__/request.spec.ts +++ b/packages/elements/src/stores/request-maker/__tests__/request.spec.ts @@ -919,7 +919,12 @@ body: "postData": { "mimeType": "application/json", "text": "{\\"foo\\":\\"bar\\"}" - } + }, + "httpVersion": "HTTP/1.1", + "headersSize": -1, + "bodySize": -1, + "queryString": [], + "cookies": [] }`, ); });