-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: File Uploads as Request Body (#2737)
* feat: Add support for application octet-stream * feat: Improve types and add unit tests * feat: fix lint errors and typo * feat: fix on change file value * feat: remove regex in convert request to sample * feat: bump version
- Loading branch information
Showing
10 changed files
with
240 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/elements-core/src/__fixtures__/operations/application-octet-stream-post.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { IHttpOperation } from '@stoplight/types'; | ||
|
||
export const httpOperation: IHttpOperation = { | ||
id: '?http-operation-id?', | ||
iid: 'POST_todos', | ||
method: 'post', | ||
path: '/todos', | ||
summary: 'Upload File', | ||
responses: [ | ||
{ | ||
id: '?http-response-200?', | ||
code: '200', | ||
}, | ||
], | ||
servers: [ | ||
{ | ||
id: '?http-server-todos.stoplight.io?', | ||
url: 'https://todos.stoplight.io', | ||
}, | ||
], | ||
request: { | ||
body: { | ||
id: '?http-request-body?', | ||
contents: [ | ||
{ | ||
id: '?http-media-0?', | ||
mediaType: 'application/octet-stream', | ||
schema: { | ||
type: 'string', | ||
format: 'binary', | ||
$schema: 'http://json-schema.org/draft-07/schema#', | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}; | ||
|
||
export default httpOperation; |
52 changes: 52 additions & 0 deletions
52
packages/elements-core/src/components/TryIt/Body/BinaryBody.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { SchemaTree } from '@stoplight/json-schema-tree'; | ||
import { Choice, useChoices } from '@stoplight/json-schema-viewer'; | ||
import { Panel } from '@stoplight/mosaic'; | ||
import { IMediaTypeContent } from '@stoplight/types'; | ||
import * as React from 'react'; | ||
|
||
import { FileUploadParameterEditor } from '../Parameters/FileUploadParameterEditors'; | ||
import { OneOfMenu } from './FormDataBody'; | ||
import { BodyParameterValues } from './request-body-utils'; | ||
|
||
export interface BinaryBodyProps { | ||
specification?: IMediaTypeContent; | ||
values: BodyParameterValues; | ||
onChangeValues: (newValues: BodyParameterValues) => void; | ||
} | ||
|
||
export const BinaryBody: React.FC<BinaryBodyProps> = ({ specification, values, onChangeValues }) => { | ||
const schema: any = React.useMemo(() => { | ||
const schema = specification?.schema ?? {}; | ||
const tree = new SchemaTree(schema, { mergeAllOf: true, refResolver: null }); | ||
tree.populate(); | ||
return tree.root.children[0]; | ||
}, [specification]); | ||
|
||
const { selectedChoice, choices, setSelectedChoice } = useChoices(schema); | ||
|
||
const onSchemaChange = (choice: Choice) => { | ||
// Erase existing values; the old and new schemas may have nothing in common. | ||
onChangeValues({}); | ||
setSelectedChoice(choice); | ||
}; | ||
|
||
return ( | ||
<Panel defaultIsOpen> | ||
<Panel.Titlebar | ||
rightComponent={<OneOfMenu choices={choices} choice={selectedChoice} onChange={onSchemaChange} />} | ||
> | ||
Body | ||
</Panel.Titlebar> | ||
<Panel.Content className="sl-overflow-y-auto ParameterGrid OperationParametersContent"> | ||
<FileUploadParameterEditor | ||
key={'file'} | ||
parameter={{ name: 'file' }} | ||
value={values.file instanceof File ? values.file : undefined} | ||
onChange={newValue => { | ||
newValue ? onChangeValues({ file: newValue }) : onChangeValues({}); | ||
}} | ||
/> | ||
</Panel.Content> | ||
</Panel> | ||
); | ||
}; |
29 changes: 29 additions & 0 deletions
29
packages/elements-core/src/components/TryIt/Body/__tests__/BinaryBody.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import '@testing-library/jest-dom'; | ||
|
||
import { render } from '@testing-library/react'; | ||
import React from 'react'; | ||
|
||
import { BinaryBody, BinaryBodyProps } from '../BinaryBody'; | ||
|
||
describe('BinaryBody', () => { | ||
it('renders file input when the form is application/octet-stream', () => { | ||
const props: BinaryBodyProps = { | ||
specification: { | ||
id: '493afac014fa8', | ||
mediaType: 'application/octet-stream', | ||
encodings: [], | ||
schema: { | ||
type: 'string', | ||
format: 'binary', | ||
$schema: 'http://json-schema.org/draft-07/schema#', | ||
}, | ||
}, | ||
values: {}, | ||
onChangeValues: () => {}, | ||
}; | ||
|
||
const { getByLabelText } = render(<BinaryBody {...props} />); | ||
|
||
expect(getByLabelText('file')).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.