Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/elements/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.24.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",
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ describe('RequestStore', () => {
foo: 'bar',
};

expect(requestStore.toHAR()).toEqual({
expect(requestStore.toHAR()).toMatchObject({
method: 'POST',
url: 'http://mockbin.com/har',
headers: [
Expand Down Expand Up @@ -563,7 +563,7 @@ describe('RequestStore', () => {
foo: 'bar',
};

expect(requestStore.toHAR()).toEqual({
expect(requestStore.toHAR()).toMatchObject({
method: 'POST',
url: 'http://mockbin.com/har',
headers: [
Expand Down Expand Up @@ -594,7 +594,7 @@ describe('RequestStore', () => {
foo: 'bar',
};

expect(requestStore.toHAR()).toEqual({
expect(requestStore.toHAR()).toMatchObject({
method: 'POST',
url: 'http://mockbin.com/har',
headers: [
Expand Down Expand Up @@ -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: [
Expand Down Expand Up @@ -662,7 +662,7 @@ describe('RequestStore', () => {
},
];

expect(requestStore.toHAR()).toEqual({
expect(requestStore.toHAR()).toMatchObject({
method: 'POST',
url: 'http://mockbin.com/har',
headers: [
Expand Down Expand Up @@ -919,7 +919,12 @@ body:
"postData": {
"mimeType": "application/json",
"text": "{\\"foo\\":\\"bar\\"}"
}
},
"httpVersion": "HTTP/1.1",
"headersSize": -1,
"bodySize": -1,
"queryString": [],
"cookies": []
}`,
);
});
Expand Down
56 changes: 32 additions & 24 deletions packages/elements/src/stores/request-maker/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ 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 HTTPSnippet from 'httpsnippet';
import { isEmpty, mapKeys, set } from 'lodash';
import { action, computed, observable } from 'mobx';
import * as typeis from 'type-is';
import URI from 'urijs';
Expand All @@ -13,8 +15,6 @@ import { getEnabledParams, getNameValuePairs, getParamArray, getParamValue } fro
import { addParamsToPath, extractQueryParams, getParamsFromPath, replaceParamsInPath } from '../../utils/url';
import { Auth, ContentType, HeaderParam, IParam, ParamType, PathParam, QueryParam } from './types';

const HTTPSnippet = require('@stoplight/httpsnippet');

const DEFAULT_EMPTY_JSON = '{\n \n}';
const DEFAULT_EMPTY_GQL = 'query {\n \n}';

Expand Down Expand Up @@ -234,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 ? '<file contents>' : 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 ? '<file contents>' : p.value,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are intentionally dummy. I wanted to make the least intrusive change possible while correcting the type violations.

contentType: p.type,
})),
};
Expand All @@ -274,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: [],
};
}

Expand All @@ -299,8 +307,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;
Expand Down
30 changes: 29 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5662,6 +5662,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"
Expand All @@ -5679,6 +5684,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"
Expand Down Expand Up @@ -11236,7 +11248,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==
Expand Down Expand Up @@ -12349,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"
Expand Down