// @flow 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'; import autobind from 'autobind-decorator'; import { deconstructQueryStringToParams, extractQueryStringFromUrl } from 'insomnia-url'; import * as React from 'react'; import { Tab, TabList, TabPanel, Tabs } from 'react-tabs'; import { getAuthTypeName, getContentTypeName } from '../../common/constants'; import * as db from '../../common/database'; import { hotKeyRefs } from '../../common/hotkeys'; import * as models from '../../models'; import AuthDropdown from './dropdowns/auth-dropdown'; import ContentTypeDropdown from './dropdowns/content-type-dropdown'; import AuthWrapper from './editors/auth/auth-wrapper'; import BodyEditor from './editors/body/body-editor'; import RequestHeadersEditor from './editors/request-headers-editor'; import ErrorBoundary from './error-boundary'; import Hotkey from './hotkey'; import MarkdownPreview from './markdown-preview'; import { showModal } from './modals/index'; import RequestSettingsModal from './modals/request-settings-modal'; import RenderedQueryString from './rendered-query-string'; import RequestUrlBar from './request-url-bar.js'; import type { Settings } from '../../models/settings'; import RequestParametersEditor from './editors/request-parameters-editor'; type Props = { // Functions 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, handleUpdateDownloadPath: 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, updateSettingsUseBulkParametersEditor: Function, handleImport: Function, handleImportFile: Function, // Other workspace: Workspace, settings: Settings, isVariableUncovered: boolean, environmentId: string, forceRefreshCounter: number, headerEditorKey: string, // Optional request: ?Request, downloadPath: string | null, oAuth2Token: ?OAuth2Token, }; @autobind class RequestPane extends React.PureComponent { _handleEditDescriptionAdd() { this._handleEditDescription(true); } _handleEditDescription(addDescription: boolean) { showModal(RequestSettingsModal, { request: this.props.request, forceEditMode: addDescription, }); } async _autocompleteUrls(): Promise> { const docs = await db.withDescendants(this.props.workspace, models.request.type); const requestId = this.props.request ? this.props.request._id : 'n/a'; const urls = docs .filter( (d: any) => d.type === models.request.type && // Only requests d._id !== requestId && // Not current request (d.url || ''), // Only ones with non-empty URLs ) .map((r: any) => (r.url || '').trim()); return Array.from(new Set(urls)); } _handleUpdateSettingsUseBulkHeaderEditor() { const { settings, updateSettingsUseBulkHeaderEditor } = this.props; updateSettingsUseBulkHeaderEditor(!settings.useBulkHeaderEditor); } _handleUpdateSettingsUseBulkParametersEditor() { const { settings, updateSettingsUseBulkParametersEditor } = this.props; updateSettingsUseBulkParametersEditor(!settings.useBulkParametersEditor); } _handleImportFile() { this.props.handleImportFile(); } _handleCreateRequest() { this.props.handleCreateRequest(); } _handleImportQueryFromUrl() { const { request, forceUpdateRequest } = this.props; if (!request) { console.warn('Tried to import query when no request active'); return; } let query; try { query = extractQueryStringFromUrl(request.url); } catch (e) { console.warn('Failed to parse url to import querystring'); return; } // Remove the search string (?foo=bar&...) from the Url const url = request.url.replace(`?${query}`, ''); const parameters = [...request.parameters, ...deconstructQueryStringToParams(query)]; // Only update if url changed if (url !== request.url) { forceUpdateRequest(request, { url, parameters }); } } render() { const { forceRefreshCounter, forceUpdateRequestHeaders, handleGenerateCode, handleGetRenderContext, handleImport, handleRender, handleSend, handleSendAndDownload, handleUpdateDownloadPath, oAuth2Token, request, workspace, environmentId, settings, isVariableUncovered, updateRequestAuthentication, updateRequestBody, updateRequestHeaders, updateRequestMimeType, updateSettingsShowPasswords, updateRequestMethod, updateRequestParameters, updateRequestUrl, headerEditorKey, downloadPath, } = this.props; const paneClasses = 'request-pane theme--pane pane'; const paneHeaderClasses = 'pane__header theme--pane__header'; const paneBodyClasses = 'pane__body theme--pane__body'; const hotKeyRegistry = settings.hotKeyRegistry; if (!request) { return (
New Request
Switch Requests
Edit Environments
); } let numBodyParams = 0; if (request.body && request.body.params) { numBodyParams = request.body.params.filter(p => !p.disabled).length; } const numParameters = request.parameters.filter(p => !p.disabled).length; const numHeaders = request.headers.filter(h => !h.disabled).length; const urlHasQueryParameters = request.url.indexOf('?') >= 0; const uniqueKey = `${forceRefreshCounter}::${request._id}`; return (
{typeof request.body.mimeType === 'string' ? getContentTypeName(request.body.mimeType) : 'Body'} {numBodyParams ? {numBodyParams} : null} {getAuthTypeName(request.authentication.type) || 'Auth'}
{request.description ? (
) : (



)}
); } } export default RequestPane;