Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling disableUnsafeCalls flag from options that would disable Run button for unsafe HTTP method calls #63

Merged
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
1 change: 1 addition & 0 deletions src/components/Operation/Operation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ export class Operation extends React.Component<OperationProps, OperationState> {
response={this.state.response}
pendingRequest={this.state.pendingRequest}
handleApiCall={this.handleApiCall}
disableUnsafeCalls={options.disableUnsafeCalls}
/>
</TabPanel>
<TabPanel key={'Examples panel'}>
Expand Down
2 changes: 1 addition & 1 deletion src/components/TryOut/Body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export const Body = ({
cursor={'pointer'}
onChange={({ target: { value } }) => setIsFormData(value === 'form-data')}
>
<option value="form-data">Form</option>
<option value="raw-json">JSON</option>
<option value="form-data">Form</option>
</Dropdown>
</SectionHeader>
<>
Expand Down
13 changes: 10 additions & 3 deletions src/components/TryOut/TryOut.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ enum RequestObjectType {
BODY = 'body',
}

const UNSAFE_HTTP_METHODS = ['put', 'patch', 'post', 'delete'];

interface TryOutProps {
operation: OperationModel;
response: any;
pendingRequest: boolean;
handleApiCall: (request: any) => void;
disableUnsafeCalls: boolean;
}

export type RequestBodyPayloadType = string | number | boolean | object | null;
Expand Down Expand Up @@ -54,15 +57,15 @@ const getInitialBodyByOperation = (operation: OperationModel): RequestBodyPayloa
};

export const TryOut = observer(
({ operation, response, pendingRequest, handleApiCall }: TryOutProps) => {
({ operation, response, pendingRequest, handleApiCall, disableUnsafeCalls }: TryOutProps) => {
const [request, setRequest] = React.useState({
queryParams: {},
pathParams: {},
cookieParams: {},
header: {},
body: getInitialBodyByOperation(operation),
});
const [isFormData, setIsFormData] = React.useState(true);
const [isFormData, setIsFormData] = React.useState(false);
const [error, setError] = React.useState(undefined);
const [showError, setShowError] = React.useState(false);
const [requiredFields, setRequiredFields] = React.useState<RequiredField[]>(
Expand Down Expand Up @@ -355,7 +358,11 @@ export const TryOut = observer(
showResponseSamples={false}
/>
<RunButton
disabled={pendingRequest || (isFormData && anyInvalidRequiredField(requiredFields))}
disabled={
(disableUnsafeCalls && UNSAFE_HTTP_METHODS.includes(operation.httpVerb)) ||
pendingRequest ||
(isFormData && anyInvalidRequiredField(requiredFields))
}
onClick={handleRunClick}
>{`Run`}</RunButton>
</>
Expand Down
3 changes: 3 additions & 0 deletions src/services/RedocNormalizedOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface RedocRawOptions {
ignoreNamedSchemas?: string[] | string;
hideSchemaPattern?: boolean;
generatedPayloadSamplesMaxDepth?: number;
disableUnsafeCalls?: boolean | string;
}

export function argValueToBoolean(val?: string | boolean, defaultValue?: boolean): boolean {
Expand Down Expand Up @@ -245,6 +246,7 @@ export class RedocNormalizedOptions {
ignoreNamedSchemas: Set<string>;
hideSchemaPattern: boolean;
generatedPayloadSamplesMaxDepth: number;
disableUnsafeCalls: boolean;

constructor(raw: RedocRawOptions, defaults: RedocRawOptions = {}) {
raw = { ...defaults, ...raw };
Expand Down Expand Up @@ -315,5 +317,6 @@ export class RedocNormalizedOptions {
RedocNormalizedOptions.normalizeGeneratedPayloadSamplesMaxDepth(
raw.generatedPayloadSamplesMaxDepth,
);
this.disableUnsafeCalls = argValueToBoolean(raw.disableUnsafeCalls);
}
}