From 2006025ffe44b17cab422dfcec43635574c77a12 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Fri, 14 Dec 2018 18:10:57 -0500 Subject: [PATCH] Explicitly pass request when saving in React components (#1306) * Started work on it * Finish refactoring I think --- .../ui/components/dropdowns/auth-dropdown.js | 51 +++-- .../ui/components/editors/auth/asap-auth.js | 93 ++++---- .../components/editors/auth/auth-wrapper.js | 65 +++--- .../ui/components/editors/auth/aws-auth.js | 144 ++++++------- .../ui/components/editors/auth/basic-auth.js | 200 ++++++++--------- .../ui/components/editors/auth/bearer-auth.js | 203 +++++++++--------- .../ui/components/editors/auth/digest-auth.js | 201 ++++++++--------- .../ui/components/editors/auth/hawk-auth.js | 28 +-- .../ui/components/editors/auth/ntlm-auth.js | 200 ++++++++--------- .../components/editors/auth/o-auth-2-auth.js | 16 +- .../ui/components/editors/body/body-editor.js | 26 +-- .../editors/request-headers-editor.js | 70 +++--- .../app/ui/components/request-pane.js | 132 ++++++------ .../app/ui/components/request-url-bar.js | 138 ++++++------ .../insomnia-app/app/ui/components/wrapper.js | 98 +++++---- 15 files changed, 867 insertions(+), 798 deletions(-) diff --git a/packages/insomnia-app/app/ui/components/dropdowns/auth-dropdown.js b/packages/insomnia-app/app/ui/components/dropdowns/auth-dropdown.js index 93371d52f9..31023bb532 100644 --- a/packages/insomnia-app/app/ui/components/dropdowns/auth-dropdown.js +++ b/packages/insomnia-app/app/ui/components/dropdowns/auth-dropdown.js @@ -1,10 +1,12 @@ -import React, { PureComponent } from 'react'; -import PropTypes from 'prop-types'; +// @flow + +import * as React from 'react'; import autobind from 'autobind-decorator'; import { Dropdown, DropdownButton, DropdownDivider, DropdownItem } from '../base/dropdown'; import { showModal } from '../modals'; import AlertModal from '../modals/alert-modal'; import * as models from '../../../models'; +import type { Request, RequestAuthentication } from '../../../models/request'; import { AUTH_BASIC, AUTH_DIGEST, @@ -20,24 +22,36 @@ import { getAuthTypeName, } from '../../../common/constants'; +type Props = { + onChange: (r: Request, RequestAuthentication) => Promise, + request: Request, + + // Optional + className: string, + children: React.Node, +}; + @autobind -class AuthDropdown extends PureComponent { - async _handleTypeChange(type) { - if (type === this.props.authentication.type) { +class AuthDropdown extends React.PureComponent { + async _handleTypeChange(type: string) { + const { request, onChange } = this.props; + const { authentication } = request; + + if (type === authentication.type) { // Type didn't change return; } - const newAuthentication = models.request.newAuth(type, this.props.authentication); - const defaultAuthentication = models.request.newAuth(this.props.authentication.type); + const newAuthentication = models.request.newAuth(type, authentication); + const defaultAuthentication = models.request.newAuth(authentication.type); // Prompt the user if fields will change between new and old - for (const key of Object.keys(this.props.authentication)) { + for (const key of Object.keys(authentication)) { if (key === 'type') { continue; } - const value = this.props.authentication[key]; + const value = authentication[key]; const changedSinceDefault = defaultAuthentication[key] !== value; const willChange = newAuthentication[key] !== value; @@ -51,14 +65,16 @@ class AuthDropdown extends PureComponent { } } - this.props.onChange(newAuthentication); + onChange(request, newAuthentication); } - renderAuthType(type, nameOverride = null) { - const currentType = this.props.authentication.type || AUTH_NONE; + renderAuthType(type: string, nameOverride: string | null = null) { + const {authentication} = this.props.request; + const currentType = authentication.type || AUTH_NONE; + return ( - {currentType === type ? : }{' '} + {currentType === type ? : }{' '} {nameOverride || getAuthTypeName(type, true)} ); @@ -87,13 +103,4 @@ class AuthDropdown extends PureComponent { } } -AuthDropdown.propTypes = { - onChange: PropTypes.func.isRequired, - authentication: PropTypes.object.isRequired, - - // Optional - className: PropTypes.string, - children: PropTypes.node, -}; - export default AuthDropdown; diff --git a/packages/insomnia-app/app/ui/components/editors/auth/asap-auth.js b/packages/insomnia-app/app/ui/components/editors/auth/asap-auth.js index 9a0860c6e6..a13d4a670a 100644 --- a/packages/insomnia-app/app/ui/components/editors/auth/asap-auth.js +++ b/packages/insomnia-app/app/ui/components/editors/auth/asap-auth.js @@ -1,5 +1,4 @@ // @flow -import type { RequestAuthentication } from '../../../../models/request'; import classnames from 'classnames'; import * as React from 'react'; @@ -9,14 +8,15 @@ import HelpTooltip from '../../help-tooltip'; import { showModal } from '../../modals'; import CodePromptModal from '../../modals/code-prompt-modal'; import Button from '../../base/button'; +import type { Request, RequestAuthentication } from '../../../../models/request'; type Props = { - authentication: RequestAuthentication, + request: Request, handleRender: Function, handleGetRenderContext: Function, nunjucksPowerUserMode: boolean, isVariableUncovered: boolean, - onChange: Function, + onChange: (Request, RequestAuthentication) => Promise, }; const PRIVATE_KEY_PLACEHOLDER = ` @@ -35,21 +35,21 @@ cJV+wRTs/Szp6LXAgMmTkKMJ+9XXErUIUgwbl27Y3Rv/9ox1p5VRg+A= @autobind class AsapAuth extends React.PureComponent { _handleDisable() { - const { authentication } = this.props; - authentication.disabled = !authentication.disabled; - this.props.onChange(authentication); + const { request, onChange } = this.props; + onChange(request, { + ...request.authentication, + disabled: !request.authentication.disabled, + }); } _handleChangeProperty(property: string, value: string | boolean): void { - const { authentication } = this.props; - authentication[property] = value; - this.props.onChange(authentication); + const { request, onChange } = this.props; + onChange(request, { ...request.authentication, [property]: value }); } _handleChangePrivateKey(value: string): void { - const { authentication } = this.props; - authentication.privateKey = value; - this.props.onChange(authentication); + const { request, onChange } = this.props; + onChange(request, { ...request.authentication, privateKey: value }); } renderAsapAuthenticationFields(): React.Node { @@ -90,11 +90,14 @@ class AsapAuth extends React.PureComponent { const { handleRender, handleGetRenderContext, - authentication, + request, nunjucksPowerUserMode, isVariableUncovered, } = this.props; + + const { authentication } = request; const id = label.replace(/ /g, '-'); + return ( @@ -124,7 +127,14 @@ class AsapAuth extends React.PureComponent { } _handleEditPrivateKey() { - const { handleRender, handleGetRenderContext, authentication } = this.props; + const { + handleRender, + handleGetRenderContext, + request, + } = this.props; + + const { authentication } = request; + showModal(CodePromptModal, { submitName: 'Done', title: `Edit Private Key`, @@ -138,8 +148,9 @@ class AsapAuth extends React.PureComponent { } renderPrivateKeyInput(label: string): React.Element<*> { + const { authentication } = this.props.request; const id = label.replace(/ /g, '-'); - const { authentication } = this.props; + return ( @@ -160,7 +171,7 @@ class AsapAuth extends React.PureComponent { }, )}> @@ -170,37 +181,37 @@ class AsapAuth extends React.PureComponent { } render() { + const { authentication } = this.props.request; const fields = this.renderAsapAuthenticationFields(); - const { authentication } = this.props; return (
- {fields} - - - - + {fields} + + + +
- - -
- -
-
+ + +
+ +
+
diff --git a/packages/insomnia-app/app/ui/components/editors/auth/auth-wrapper.js b/packages/insomnia-app/app/ui/components/editors/auth/auth-wrapper.js index f84dc268fb..b8c2b22c30 100644 --- a/packages/insomnia-app/app/ui/components/editors/auth/auth-wrapper.js +++ b/packages/insomnia-app/app/ui/components/editors/auth/auth-wrapper.js @@ -1,5 +1,6 @@ -import React, { PureComponent } from 'react'; -import PropTypes from 'prop-types'; +// @flow + +import * as React from 'react'; import { AUTH_BASIC, AUTH_DIGEST, @@ -23,9 +24,26 @@ import AWSAuth from './aws-auth'; import NetrcAuth from './netrc-auth'; import AsapAuth from './asap-auth'; import autobind from 'autobind-decorator'; +import type { Request, RequestAuthentication } from '../../../../models/request'; +import type { OAuth2Token } from '../../../../models/o-auth-2-token'; +import type { Settings } from '../../../../models/settings'; + +type Props = { + handleRender: Function, + handleGetRenderContext: Function, + handleUpdateSettingsShowPasswords: (showPasswords: boolean) => Promise, + nunjucksPowerUserMode: boolean, + onChange: (Request, RequestAuthentication) => Promise, + request: Request, + showPasswords: boolean, + isVariableUncovered: boolean, + + // Optional + oAuth2Token: ?OAuth2Token, +}; @autobind -class AuthWrapper extends PureComponent { +class AuthWrapper extends React.PureComponent { renderEditor() { const { oAuth2Token, @@ -44,7 +62,7 @@ class AuthWrapper extends PureComponent { if (authentication.type === AUTH_BASIC) { return ( ); } else if (authentication.type === AUTH_NETRC) { - return ; + return ; } else if (authentication.type === AUTH_ASAP) { return (

- -
-
+ +
+
Select an auth type from above

@@ -169,21 +187,12 @@ class AuthWrapper extends PureComponent { } render() { - return
{this.renderEditor()}
; + return ( +
+ {this.renderEditor()} +
+ ); } } -AuthWrapper.propTypes = { - handleRender: PropTypes.func.isRequired, - handleGetRenderContext: PropTypes.func.isRequired, - handleUpdateSettingsShowPasswords: PropTypes.func.isRequired, - nunjucksPowerUserMode: PropTypes.bool.isRequired, - onChange: PropTypes.func.isRequired, - request: PropTypes.object.isRequired, - showPasswords: PropTypes.bool.isRequired, - isVariableUncovered: PropTypes.bool.isRequired, - // Optional - oAuth2Token: PropTypes.object, -}; - export default AuthWrapper; diff --git a/packages/insomnia-app/app/ui/components/editors/auth/aws-auth.js b/packages/insomnia-app/app/ui/components/editors/auth/aws-auth.js index faceaec614..891f103fd2 100644 --- a/packages/insomnia-app/app/ui/components/editors/auth/aws-auth.js +++ b/packages/insomnia-app/app/ui/components/editors/auth/aws-auth.js @@ -5,60 +5,58 @@ import classnames from 'classnames'; import OneLineEditor from '../../codemirror/one-line-editor'; import Button from '../../base/button'; import HelpTooltip from '../../help-tooltip'; -import type { RequestAuthentication } from '../../../../models/request'; +import type { Request, RequestAuthentication } from '../../../../models/request'; +import type { Settings } from '../../../../models/settings'; type Props = { - authentication: RequestAuthentication, + request: Request, nunjucksPowerUserMode: boolean, showPasswords: boolean, isVariableUncovered: boolean, - onChange: RequestAuthentication => void, + onChange: (Request, RequestAuthentication) => Promise, handleRender: string => Promise, handleGetRenderContext: () => Promise, - handleUpdateSettingsShowPasswords: boolean => Promise, + handleUpdateSettingsShowPasswords: boolean => Promise, }; @autobind class AWSAuth extends React.PureComponent { _handleDisable() { - const { authentication } = this.props; - authentication.disabled = !authentication.disabled; - this.props.onChange(authentication); + const { request, onChange } = this.props; + onChange(request, { + ...request.authentication, + disabled: !request.authentication.disabled, + }); } _handleChangeAccessKeyId(value: string) { - const { authentication } = this.props; - authentication.accessKeyId = value; - this.props.onChange(authentication); + const { request, onChange } = this.props; + onChange(request, { ...request.authentication, accessKeyId: value }); } _handleChangeSecretAccessKey(value: string) { - const { authentication } = this.props; - authentication.secretAccessKey = value; - this.props.onChange(authentication); + const { request, onChange } = this.props; + onChange(request, { ...request.authentication, secretAccessKey: value }); } _handleChangeRegion(value: string) { - const { authentication } = this.props; - authentication.region = value; - this.props.onChange(authentication); + const { request, onChange } = this.props; + onChange(request, { ...request.authentication, region: value }); } _handleChangeService(value: string) { - const { authentication } = this.props; - authentication.service = value; - this.props.onChange(authentication); + const { request, onChange } = this.props; + onChange(request, { ...request.authentication, service: value }); } _handleChangeSessionToken(value: string) { - const { authentication } = this.props; - authentication.sessionToken = value; - this.props.onChange(authentication); + const { request, onChange } = this.props; + onChange(request, { ...request.authentication, sessionToken: value }); } renderRow(key: string, label: string, onChange: Function, help?: string) { const { - authentication, + request, nunjucksPowerUserMode, handleRender, handleGetRenderContext, @@ -76,12 +74,12 @@ class AWSAuth extends React.PureComponent {
{ } render() { - const { authentication } = this.props; + const { authentication } = this.props.request; return (
- {this.renderRow('accessKeyId', 'Access Key ID', this._handleChangeAccessKeyId)} - {this.renderRow( - 'secretAccessKey', - 'Secret Access Key', - this._handleChangeSecretAccessKey, - )} - {this.renderRow( - 'region', - 'Region', - this._handleChangeRegion, - "will be calculated from hostname or host or use 'us-east-1' if not given", - )} - {this.renderRow( - 'service', - 'Service', - this._handleChangeService, - 'will be calculated from hostname or host if not given', - )} - {this.renderRow( - 'sessionToken', - 'Session Token', - this._handleChangeSessionToken, - 'Optional token used for multi-factor authentication', - )} - - - - + {this.renderRow('accessKeyId', 'Access Key ID', this._handleChangeAccessKeyId)} + {this.renderRow( + 'secretAccessKey', + 'Secret Access Key', + this._handleChangeSecretAccessKey, + )} + {this.renderRow( + 'region', + 'Region', + this._handleChangeRegion, + 'will be calculated from hostname or host or use \'us-east-1\' if not given', + )} + {this.renderRow( + 'service', + 'Service', + this._handleChangeService, + 'will be calculated from hostname or host if not given', + )} + {this.renderRow( + 'sessionToken', + 'Session Token', + this._handleChangeSessionToken, + 'Optional token used for multi-factor authentication', + )} + + + +
- - -
- -
-
+ + +
+ +
+
diff --git a/packages/insomnia-app/app/ui/components/editors/auth/basic-auth.js b/packages/insomnia-app/app/ui/components/editors/auth/basic-auth.js index a4ef04e1fe..bd5cdb63a5 100644 --- a/packages/insomnia-app/app/ui/components/editors/auth/basic-auth.js +++ b/packages/insomnia-app/app/ui/components/editors/auth/basic-auth.js @@ -1,33 +1,46 @@ -import React, { PureComponent } from 'react'; +// @flow +import * as React from 'react'; import classnames from 'classnames'; -import PropTypes from 'prop-types'; import autobind from 'autobind-decorator'; import OneLineEditor from '../../codemirror/one-line-editor'; import Button from '../../base/button'; +import type { Settings } from '../../../../models/settings'; +import type { Request, RequestAuthentication } from '../../../../models/request'; + +type Props = { + handleRender: Function, + handleGetRenderContext: Function, + handleUpdateSettingsShowPasswords: (boolean) => Promise, + nunjucksPowerUserMode: boolean, + onChange: (Request, RequestAuthentication) => Promise, + request: Request, + showPasswords: boolean, + isVariableUncovered: boolean, +}; @autobind -class BasicAuth extends PureComponent { +class BasicAuth extends React.PureComponent { _handleDisable() { - const { authentication } = this.props; - authentication.disabled = !authentication.disabled; - this.props.onChange(authentication); + const { request, onChange } = this.props; + onChange(request, { + ...request.authentication, + disabled: !request.authentication.disabled, + }); } - _handleChangeUsername(value) { - const { authentication } = this.props; - authentication.username = value; - this.props.onChange(authentication); + _handleChangeUsername(value: string) { + const { request, onChange } = this.props; + onChange(request, { ...request.authentication, username: value }); } - _handleChangePassword(value) { - const { authentication } = this.props; - authentication.password = value; - this.props.onChange(authentication); + _handleChangePassword(value: string) { + const { request, onChange } = this.props; + onChange(request, { ...request.authentication, password: value }); } render() { const { - authentication, + request, showPasswords, handleRender, handleGetRenderContext, @@ -35,82 +48,84 @@ class BasicAuth extends PureComponent { isVariableUncovered, } = this.props; + const { authentication } = request; + return (
- - - - - - - - - - - - + + + + + + + + + + + +
- - -
- -
-
- - -
- -
-
- - -
- -
-
+ + +
+ +
+
+ + +
+ +
+
+ + +
+ +
+
@@ -118,15 +133,4 @@ class BasicAuth extends PureComponent { } } -BasicAuth.propTypes = { - handleRender: PropTypes.func.isRequired, - handleGetRenderContext: PropTypes.func.isRequired, - handleUpdateSettingsShowPasswords: PropTypes.func.isRequired, - nunjucksPowerUserMode: PropTypes.bool.isRequired, - onChange: PropTypes.func.isRequired, - authentication: PropTypes.object.isRequired, - showPasswords: PropTypes.bool.isRequired, - isVariableUncovered: PropTypes.bool.isRequired, -}; - export default BasicAuth; diff --git a/packages/insomnia-app/app/ui/components/editors/auth/bearer-auth.js b/packages/insomnia-app/app/ui/components/editors/auth/bearer-auth.js index b8711e5cda..ab9473f2fa 100644 --- a/packages/insomnia-app/app/ui/components/editors/auth/bearer-auth.js +++ b/packages/insomnia-app/app/ui/components/editors/auth/bearer-auth.js @@ -1,120 +1,132 @@ -import React, { PureComponent } from 'react'; +// @flow +import * as React from 'react'; import classnames from 'classnames'; -import PropTypes from 'prop-types'; import autobind from 'autobind-decorator'; import OneLineEditor from '../../codemirror/one-line-editor'; import Button from '../../base/button'; import HelpTooltip from '../../help-tooltip'; +import type { Request, RequestAuthentication } from '../../../../models/request'; + +type Props = { + handleRender: Function, + handleGetRenderContext: Function, + nunjucksPowerUserMode: boolean, + request: Request, + onChange: (Request, RequestAuthentication) => Promise, + isVariableUncovered: boolean, +}; @autobind -class BearerAuth extends PureComponent { +class BearerAuth extends React.PureComponent { _handleDisable() { - const { authentication } = this.props; - authentication.disabled = !authentication.disabled; - this.props.onChange(authentication); + const { request, onChange } = this.props; + onChange(request, { + ...request.authentication, + disabled: !request.authentication.disabled, + }); } - _handleChangeToken(token) { - const { authentication } = this.props; - authentication.token = token; - this.props.onChange(authentication); + _handleChangeToken(token: string) { + const { request, onChange } = this.props; + onChange(request, { ...request.authentication, token }); } - _handleChangePrefix(prefix) { - const { authentication } = this.props; - authentication.prefix = prefix; - this.props.onChange(authentication); + _handleChangePrefix(prefix: string) { + const { request, onChange } = this.props; + onChange(request, { ...request.authentication, prefix }); } render() { const { - authentication, + request, handleRender, handleGetRenderContext, nunjucksPowerUserMode, isVariableUncovered, } = this.props; + const { authentication } = request; + return (
- - - - - - - - - - - - + + + + + + + + + + + +
- - -
- -
-
- - -
- -
-
- - -
- -
-
+ + +
+ +
+
+ + +
+ +
+
+ + +
+ +
+
@@ -122,13 +134,4 @@ class BearerAuth extends PureComponent { } } -BearerAuth.propTypes = { - handleRender: PropTypes.func.isRequired, - handleGetRenderContext: PropTypes.func.isRequired, - nunjucksPowerUserMode: PropTypes.bool.isRequired, - authentication: PropTypes.object.isRequired, - onChange: PropTypes.func.isRequired, - isVariableUncovered: PropTypes.bool.isRequired, -}; - export default BearerAuth; diff --git a/packages/insomnia-app/app/ui/components/editors/auth/digest-auth.js b/packages/insomnia-app/app/ui/components/editors/auth/digest-auth.js index aa6067f297..f7a861acae 100644 --- a/packages/insomnia-app/app/ui/components/editors/auth/digest-auth.js +++ b/packages/insomnia-app/app/ui/components/editors/auth/digest-auth.js @@ -1,33 +1,47 @@ -import React, { PureComponent } from 'react'; +// @flow + +import * as React from 'react'; import classnames from 'classnames'; -import PropTypes from 'prop-types'; import autobind from 'autobind-decorator'; import OneLineEditor from '../../codemirror/one-line-editor'; import Button from '../../base/button'; +import type { Settings } from '../../../../models/settings'; +import type { Request, RequestAuthentication } from '../../../../models/request'; + +type Props = { + handleRender: Function, + handleGetRenderContext: Function, + handleUpdateSettingsShowPasswords: (boolean) => Promise, + nunjucksPowerUserMode: boolean, + onChange: (Request, RequestAuthentication) => Promise, + request: Request, + showPasswords: boolean, + isVariableUncovered: boolean, +}; @autobind -class DigestAuth extends PureComponent { +class DigestAuth extends React.PureComponent { _handleDisable() { - const { authentication } = this.props; - authentication.disabled = !authentication.disabled; - this.props.onChange(authentication); + const { request, onChange } = this.props; + onChange(request, { + ...request.authentication, + disabled: !request.authentication.disabled, + }); } - _handleChangeUsername(value) { - const { authentication } = this.props; - authentication.username = value; - this.props.onChange(authentication); + _handleChangeUsername(value: string) { + const { request, onChange } = this.props; + onChange(request, { ...request.authentication, username: value }); } - _handleChangePassword(value) { - const { authentication } = this.props; - authentication.password = value; - this.props.onChange(authentication); + _handleChangePassword(value: string) { + const { request, onChange } = this.props; + onChange(request, { ...request.authentication, password: value }); } render() { const { - authentication, + request, showPasswords, handleRender, nunjucksPowerUserMode, @@ -35,82 +49,84 @@ class DigestAuth extends PureComponent { isVariableUncovered, } = this.props; + const { authentication } = request; + return (
- - - - - - - - - - - - + + + + + + + + + + + +
- - -
- -
-
- - -
- -
-
- - -
- -
-
+ + +
+ +
+
+ + +
+ +
+
+ + +
+ +
+
@@ -118,15 +134,4 @@ class DigestAuth extends PureComponent { } } -DigestAuth.propTypes = { - handleRender: PropTypes.func.isRequired, - handleGetRenderContext: PropTypes.func.isRequired, - handleUpdateSettingsShowPasswords: PropTypes.func.isRequired, - nunjucksPowerUserMode: PropTypes.bool.isRequired, - onChange: PropTypes.func.isRequired, - authentication: PropTypes.object.isRequired, - showPasswords: PropTypes.bool.isRequired, - isVariableUncovered: PropTypes.bool.isRequired, -}; - export default DigestAuth; diff --git a/packages/insomnia-app/app/ui/components/editors/auth/hawk-auth.js b/packages/insomnia-app/app/ui/components/editors/auth/hawk-auth.js index c22e506682..5ac43a008c 100644 --- a/packages/insomnia-app/app/ui/components/editors/auth/hawk-auth.js +++ b/packages/insomnia-app/app/ui/components/editors/auth/hawk-auth.js @@ -1,5 +1,4 @@ // @flow -import type { RequestAuthentication } from '../../../../models/request'; import * as React from 'react'; import classnames from 'classnames'; @@ -8,28 +7,30 @@ import OneLineEditor from '../../codemirror/one-line-editor'; import { HAWK_ALGORITHM_SHA1, HAWK_ALGORITHM_SHA256 } from '../../../../common/constants'; import HelpTooltip from '../../help-tooltip'; import Button from '../../base/button'; +import type { Request, RequestAuthentication } from '../../../../models/request'; type Props = { - authentication: RequestAuthentication, + request: Request, handleRender: Function, handleGetRenderContext: Function, nunjucksPowerUserMode: boolean, isVariableUncovered: boolean, - onChange: Function, + onChange: (Request, RequestAuthentication) => Promise, }; @autobind class HawkAuth extends React.PureComponent { _handleDisable() { - const { authentication } = this.props; - authentication.disabled = !authentication.disabled; - this.props.onChange(authentication); + const { request, onChange } = this.props; + onChange(request, { + ...request.authentication, + disabled: !request.authentication.disabled, + }); } _handleChangeProperty(property: string, value: string | boolean): void { - const { authentication } = this.props; - authentication[property] = value; - this.props.onChange(authentication); + const { request, onChange } = this.props; + onChange(request, { ...request.authentication, [property]: value }); } _handleChangeHawkAuthId(value: string): void { @@ -75,7 +76,7 @@ class HawkAuth extends React.PureComponent { onChange: Function, help: string | null = null, ): React.Element<*> { - const { authentication } = this.props; + const { authentication } = this.props.request; const id = label.replace(/ /g, '-'); const value = authentication.hasOwnProperty(property) ? authentication[property] : options[0]; @@ -109,10 +110,13 @@ class HawkAuth extends React.PureComponent { const { handleRender, handleGetRenderContext, - authentication, + request, nunjucksPowerUserMode, isVariableUncovered, } = this.props; + + const {authentication} = request; + const id = label.replace(/ /g, '-'); return ( @@ -144,7 +148,7 @@ class HawkAuth extends React.PureComponent { render() { const fields = this.renderHawkAuthenticationFields(); - const { authentication } = this.props; + const { authentication } = this.props.request; return (
diff --git a/packages/insomnia-app/app/ui/components/editors/auth/ntlm-auth.js b/packages/insomnia-app/app/ui/components/editors/auth/ntlm-auth.js index 492774199c..32fbad752e 100644 --- a/packages/insomnia-app/app/ui/components/editors/auth/ntlm-auth.js +++ b/packages/insomnia-app/app/ui/components/editors/auth/ntlm-auth.js @@ -1,33 +1,46 @@ -import React, { PureComponent } from 'react'; +// @flow +import * as React from 'react'; import classnames from 'classnames'; -import PropTypes from 'prop-types'; import autobind from 'autobind-decorator'; import OneLineEditor from '../../codemirror/one-line-editor'; import Button from '../../base/button'; +import type { Request, RequestAuthentication } from '../../../../models/request'; +import type { Settings } from '../../../../models/settings'; + +type Props = { + handleRender: Function, + handleGetRenderContext: Function, + handleUpdateSettingsShowPasswords: (boolean) => Promise, + nunjucksPowerUserMode: boolean, + onChange: (Request, RequestAuthentication) => Promise, + request: Request, + showPasswords: boolean, + isVariableUncovered: boolean, +}; @autobind -class NTLMAuth extends PureComponent { +class NTLMAuth extends React.PureComponent { _handleDisable() { - const { authentication } = this.props; - authentication.disabled = !authentication.disabled; - this.props.onChange(authentication); + const { request, onChange } = this.props; + onChange(request, { + ...request.authentication, + disabled: !request.authentication.disabled, + }); } - _handleChangeUsername(value) { - const { authentication } = this.props; - authentication.username = value; - this.props.onChange(authentication); + _handleChangeUsername(value: string) { + const { request, onChange } = this.props; + onChange(request, { ...request.authentication, username: value }); } - _handleChangePassword(value) { - const { authentication } = this.props; - authentication.password = value; - this.props.onChange(authentication); + _handleChangePassword(value: string) { + const { request, onChange } = this.props; + onChange(request, { ...request.authentication, password: value }); } render() { const { - authentication, + request, showPasswords, handleRender, handleGetRenderContext, @@ -35,82 +48,84 @@ class NTLMAuth extends PureComponent { isVariableUncovered, } = this.props; + const { authentication } = request; + return (
- - - - - - - - - - - - + + + + + + + + + + + +
- - -
- -
-
- - -
- -
-
- - -
- -
-
+ + +
+ +
+
+ + +
+ +
+
+ + +
+ +
+
@@ -118,15 +133,4 @@ class NTLMAuth extends PureComponent { } } -NTLMAuth.propTypes = { - handleRender: PropTypes.func.isRequired, - handleGetRenderContext: PropTypes.func.isRequired, - handleUpdateSettingsShowPasswords: PropTypes.func.isRequired, - nunjucksPowerUserMode: PropTypes.bool.isRequired, - onChange: PropTypes.func.isRequired, - authentication: PropTypes.object.isRequired, - showPasswords: PropTypes.bool.isRequired, - isVariableUncovered: PropTypes.bool.isRequired, -}; - export default NTLMAuth; diff --git a/packages/insomnia-app/app/ui/components/editors/auth/o-auth-2-auth.js b/packages/insomnia-app/app/ui/components/editors/auth/o-auth-2-auth.js index 6e8df421a5..3e10c2ea5d 100644 --- a/packages/insomnia-app/app/ui/components/editors/auth/o-auth-2-auth.js +++ b/packages/insomnia-app/app/ui/components/editors/auth/o-auth-2-auth.js @@ -1,5 +1,5 @@ // @flow -import type { Request } from '../../../../models/request'; +import type { Request, RequestAuthentication } from '../../../../models/request'; import type { OAuth2Token } from '../../../../models/o-auth-2-token'; import * as React from 'react'; @@ -26,19 +26,20 @@ import TimeFromNow from '../../time-from-now'; import Button from '../../base/button'; import { showModal } from '../../modals'; import ResponseDebugModal from '../../modals/response-debug-modal'; +import type { Settings } from '../../../../models/settings'; type Props = { handleRender: Function, handleGetRenderContext: Function, - handleUpdateSettingsShowPasswords: Function, + handleUpdateSettingsShowPasswords: (boolean) => Promise, nunjucksPowerUserMode: boolean, - onChange: Function, + onChange: (Request, RequestAuthentication) => Promise, request: Request, showPasswords: boolean, isVariableUncovered: boolean, // Optional - oAuth2Token: OAuth2Token | null, + oAuth2Token: ?OAuth2Token, }; type State = { @@ -134,11 +135,12 @@ class OAuth2Auth extends React.PureComponent { } _handleChangeProperty(property: string, value: string | boolean): void { - const { request } = this.props; + const { onChange, request } = this.props; const authentication = Object.assign({}, request.authentication, { [property]: value, }); - this.props.onChange(authentication); + + onChange(request, authentication); } _handlerChangeResponseType(e: SyntheticEvent): void { @@ -443,7 +445,7 @@ class OAuth2Auth extends React.PureComponent { return { basic: basicFields, advanced: advancedFields }; } - static renderExpireAt(token: OAuth2Token | null): React.Element<*> | string | null { + static renderExpireAt(token: ?OAuth2Token): React.Element<*> | string | null { if (!token || !token.accessToken) { return null; } diff --git a/packages/insomnia-app/app/ui/components/editors/body/body-editor.js b/packages/insomnia-app/app/ui/components/editors/body/body-editor.js index 47a0cf6416..6791aa70fd 100644 --- a/packages/insomnia-app/app/ui/components/editors/body/body-editor.js +++ b/packages/insomnia-app/app/ui/components/editors/body/body-editor.js @@ -14,7 +14,7 @@ import { CONTENT_TYPE_GRAPHQL, getContentTypeFromHeaders, } from '../../../../common/constants'; -import type { Request, RequestBodyParameter } from '../../../../models/request'; +import type { Request, RequestBody, RequestBodyParameter, RequestHeader } from '../../../../models/request'; import { newBodyFile, newBodyForm, @@ -30,9 +30,9 @@ import AskModal from '../../modals/ask-modal'; type Props = { // Required - onChange: Function, - onChangeHeaders: Function, - handleUpdateRequestMimeType: Function, + onChange: (r: Request, body: RequestBody) => Promise, + onChangeHeaders: (r: Request, headers: Array) => Promise, + handleUpdateRequestMimeType: (r: Request, mimeType: string) => Promise, handleRender: Function, handleGetRenderContext: Function, request: Request, @@ -50,25 +50,25 @@ class BodyEditor extends React.PureComponent { const oldContentType = request.body.mimeType || ''; const newBody = newBodyRaw(rawValue, oldContentType); - onChange(newBody); + onChange(request, newBody); } _handleGraphQLChange(content: string) { - const { onChange } = this.props; + const { onChange, request } = this.props; const newBody = newBodyRaw(content, CONTENT_TYPE_GRAPHQL); - onChange(newBody); + onChange(request, newBody); } _handleFormUrlEncodedChange(parameters: Array) { - const { onChange } = this.props; + const { onChange, request } = this.props; const newBody = newBodyFormUrlEncoded(parameters); - onChange(newBody); + onChange(request, newBody); } _handleFormChange(parameters: Array) { - const { onChange } = this.props; + const { onChange, request } = this.props; const newBody = newBodyForm(parameters); - onChange(newBody); + onChange(request, newBody); } async _handleFileChange(path: string) { @@ -97,7 +97,7 @@ class BodyEditor extends React.PureComponent { ), onDone: saidYes => { if (saidYes) { - onChangeHeaders(headers); + onChangeHeaders(request, headers); } }, }); @@ -105,7 +105,7 @@ class BodyEditor extends React.PureComponent { const newBody = newBodyFile(path); - onChange(newBody); + onChange(request, newBody); } render() { diff --git a/packages/insomnia-app/app/ui/components/editors/request-headers-editor.js b/packages/insomnia-app/app/ui/components/editors/request-headers-editor.js index e19844cb46..9a06444711 100644 --- a/packages/insomnia-app/app/ui/components/editors/request-headers-editor.js +++ b/packages/insomnia-app/app/ui/components/editors/request-headers-editor.js @@ -1,5 +1,6 @@ -import React, { PureComponent } from 'react'; -import PropTypes from 'prop-types'; +// @flow + +import * as React from 'react'; import autobind from 'autobind-decorator'; import KeyValueEditor from '../key-value-editor/editor'; import CodeEditor from '../codemirror/code-editor'; @@ -7,14 +8,36 @@ import allHeaderNames from '../../../datasets/header-names'; import allCharsets from '../../../datasets/charsets'; import allMimeTypes from '../../../datasets/content-types'; import allEncodings from '../../../datasets/encodings'; +import type { Request, RequestHeader } from '../../../models/request'; + +type Props = { + onChange: (r: Request, headers: Array) => Promise, + bulk: boolean, + editorFontSize: number, + editorIndentSize: number, + editorLineWrapping: boolean, + nunjucksPowerUserMode: boolean, + isVariableUncovered: boolean, + handleRender: Function, + handleGetRenderContext: Function, + request: Request, +}; @autobind -class RequestHeadersEditor extends PureComponent { - _handleBulkUpdate(headersString) { - this.props.onChange(this._getHeadersFromString(headersString)); +class RequestHeadersEditor extends React.PureComponent { + _handleBulkUpdate(headersString: string) { + const { onChange, request } = this.props; + const headers = RequestHeadersEditor._getHeadersFromString(headersString); + + onChange(request, headers); } - _getHeadersFromString(headersString) { + _handleKeyValueUpdate(headers: Array) { + const { onChange, request } = this.props; + onChange(request, headers); + } + + static _getHeadersFromString(headersString: string) { const headers = []; const rows = headersString.split(/\n+/); @@ -35,7 +58,7 @@ class RequestHeadersEditor extends PureComponent { } _getHeadersString() { - const { headers } = this.props; + const { headers } = this.props.request; let headersString = ''; @@ -56,7 +79,7 @@ class RequestHeadersEditor extends PureComponent { return headersString; } - _getCommonHeaderValues(pair) { + static _getCommonHeaderValues(pair: RequestHeader) { switch (pair.name.toLowerCase()) { case 'content-type': case 'accept': @@ -70,18 +93,17 @@ class RequestHeadersEditor extends PureComponent { } } - _getCommonHeaderNames(pair) { + static _getCommonHeaderNames(pair: RequestHeader) { return allHeaderNames; } render() { const { bulk, - headers, + request, editorFontSize, editorIndentSize, editorLineWrapping, - onChange, handleRender, handleGetRenderContext, nunjucksPowerUserMode, @@ -124,14 +146,14 @@ class RequestHeadersEditor extends PureComponent { sortable namePlaceholder="Header" valuePlaceholder="Value" - pairs={headers} + pairs={request.headers} nunjucksPowerUserMode={nunjucksPowerUserMode} isVariableUncovered={isVariableUncovered} handleRender={handleRender} handleGetRenderContext={handleGetRenderContext} - handleGetAutocompleteNameConstants={this._getCommonHeaderNames} - handleGetAutocompleteValueConstants={this._getCommonHeaderValues} - onChange={onChange} + handleGetAutocompleteNameConstants={RequestHeadersEditor._getCommonHeaderNames} + handleGetAutocompleteValueConstants={RequestHeadersEditor._getCommonHeaderValues} + onChange={this._handleKeyValueUpdate} />
@@ -139,22 +161,4 @@ class RequestHeadersEditor extends PureComponent { } } -RequestHeadersEditor.propTypes = { - onChange: PropTypes.func.isRequired, - bulk: PropTypes.bool.isRequired, - editorFontSize: PropTypes.number.isRequired, - editorIndentSize: PropTypes.number.isRequired, - editorLineWrapping: PropTypes.bool.isRequired, - nunjucksPowerUserMode: PropTypes.bool.isRequired, - isVariableUncovered: PropTypes.bool.isRequired, - handleRender: PropTypes.func.isRequired, - handleGetRenderContext: PropTypes.func.isRequired, - headers: PropTypes.arrayOf( - PropTypes.shape({ - name: PropTypes.string.isRequired, - value: PropTypes.string.isRequired, - }), - ).isRequired, -}; - export default RequestHeadersEditor; diff --git a/packages/insomnia-app/app/ui/components/request-pane.js b/packages/insomnia-app/app/ui/components/request-pane.js index 287b55345f..94b99b5961 100644 --- a/packages/insomnia-app/app/ui/components/request-pane.js +++ b/packages/insomnia-app/app/ui/components/request-pane.js @@ -1,5 +1,11 @@ // @flow -import type { Request } from '../../models/request'; +import type { + Request, + RequestAuthentication, + RequestBody, + RequestHeader, + RequestParameter, +} from '../../models/request'; import type { Workspace } from '../../models/workspace'; import type { OAuth2Token } from '../../models/o-auth-2-token'; @@ -14,7 +20,7 @@ import RenderedQueryString from './rendered-query-string'; import BodyEditor from './editors/body/body-editor'; import AuthWrapper from './editors/auth/auth-wrapper'; import RequestUrlBar from './request-url-bar.js'; -import { DEBOUNCE_MILLIS, getAuthTypeName, getContentTypeName } from '../../common/constants'; +import { getAuthTypeName, getContentTypeName } from '../../common/constants'; import { deconstructQueryStringToParams, extractQueryStringFromUrl } from 'insomnia-url'; import * as db from '../../common/database'; import * as models from '../../models'; @@ -28,21 +34,21 @@ import ErrorBoundary from './error-boundary'; type Props = { // Functions - forceUpdateRequest: Function, - forceUpdateRequestHeaders: Function, - handleSend: Function, - handleSendAndDownload: Function, - handleCreateRequest: Function, + forceUpdateRequest: (r: Request, patch: Object) => Promise, + forceUpdateRequestHeaders: (r: Request, headers: Array) => Promise, + handleSend: () => void, + handleSendAndDownload: (filepath?: string) => Promise, + handleCreateRequest: () => Promise, handleGenerateCode: Function, handleRender: Function, handleGetRenderContext: Function, - updateRequestUrl: Function, - updateRequestMethod: Function, - updateRequestBody: Function, - updateRequestParameters: Function, - updateRequestAuthentication: Function, - updateRequestHeaders: Function, - updateRequestMimeType: Function, + updateRequestUrl: (r: Request, url: string) => Promise, + updateRequestMethod: (r: Request, method: string) => Promise, + updateRequestBody: (r: Request, body: RequestBody) => Promise, + updateRequestParameters: (r: Request, params: Array) => Promise, + updateRequestAuthentication: (r: Request, auth: RequestAuthentication) => Promise, + updateRequestHeaders: (r: Request, headers: Array) => Promise, + updateRequestMimeType: (r: Request, mimeType: string) => Promise, updateSettingsShowPasswords: Function, updateSettingsUseBulkHeaderEditor: Function, handleImport: Function, @@ -62,8 +68,6 @@ type Props = { @autobind class RequestPane extends React.PureComponent { - _handleUpdateRequestUrlTimeout: TimeoutID; - _handleEditDescriptionAdd() { this._handleEditDescription(true); } @@ -102,18 +106,18 @@ class RequestPane extends React.PureComponent { } _handleCreateRequest() { - this.props.handleCreateRequest(this.props.request); + this.props.handleCreateRequest(); } - _handleUpdateRequestUrl(url: string) { - clearTimeout(this._handleUpdateRequestUrlTimeout); - this._handleUpdateRequestUrlTimeout = setTimeout(() => { - this.props.updateRequestUrl(url); - }, DEBOUNCE_MILLIS); + _handleUpdateRequestParameters(parameters: Array) { + const { request, updateRequestParameters } = this.props; + if (request) { + updateRequestParameters(request, parameters); + } } _handleImportQueryFromUrl() { - const { request } = this.props; + const { request, forceUpdateRequest } = this.props; if (!request) { console.warn('Tried to import query when no request active'); @@ -134,7 +138,7 @@ class RequestPane extends React.PureComponent { // Only update if url changed if (url !== request.url) { - this.props.forceUpdateRequest({ url, parameters }); + forceUpdateRequest(request, { url, parameters }); } } @@ -157,10 +161,10 @@ class RequestPane extends React.PureComponent { updateRequestAuthentication, updateRequestBody, updateRequestHeaders, - updateRequestMethod, updateRequestMimeType, - updateRequestParameters, updateSettingsShowPasswords, + updateRequestMethod, + updateRequestUrl, } = this.props; const paneClasses = 'request-pane theme--pane pane'; @@ -170,35 +174,35 @@ class RequestPane extends React.PureComponent { if (!request) { return (
-
+
- - - - - - - - - - - - + + + + + + + + + + + +
New Request - - - -
Switch Requests - - - -
Edit Environments - - - -
New Request + + + +
Switch Requests + + + +
Edit Environments + + + +
@@ -235,9 +239,8 @@ class RequestPane extends React.PureComponent { { nunjucksPowerUserMode={settings.nunjucksPowerUserMode} isVariableUncovered={isVariableUncovered} handleGetRenderContext={handleGetRenderContext} - url={request.url} - requestId={request._id} + request={request} />
@@ -264,16 +266,16 @@ class RequestPane extends React.PureComponent { ? getContentTypeName(request.body.mimeType) : 'Body'} {numBodyParams ? {numBodyParams} : null} - + {getAuthTypeName(request.authentication.type) || 'Auth'} - + @@ -293,7 +295,7 @@ class RequestPane extends React.PureComponent { Docs {request.description && ( - + )} @@ -339,7 +341,7 @@ class RequestPane extends React.PureComponent { - + @@ -357,7 +359,7 @@ class RequestPane extends React.PureComponent { handleGetRenderContext={handleGetRenderContext} nunjucksPowerUserMode={settings.nunjucksPowerUserMode} isVariableUncovered={isVariableUncovered} - onChange={updateRequestParameters} + onChange={this._handleUpdateRequestParameters} /> @@ -373,7 +375,6 @@ class RequestPane extends React.PureComponent { { editorIndentSize={settings.editorIndentSize} editorLineWrapping={settings.editorLineWrapping} onChange={updateRequestHeaders} + request={request} bulk={settings.useBulkHeaderEditor} /> @@ -417,10 +419,10 @@ class RequestPane extends React.PureComponent {

- + -
-
+
+