Skip to content

Fix form data with show curl before try #1085

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
30 changes: 15 additions & 15 deletions dist/rapidoc-min.js

Large diffs are not rendered by default.

Binary file modified dist/rapidoc-min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/rapidoc-min.js.map

Large diffs are not rendered by default.

Binary file modified dist/rapidoc-min.js.map.gz
Binary file not shown.
56 changes: 48 additions & 8 deletions dist/rapidoc.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 15 additions & 15 deletions docs/rapidoc-min.js

Large diffs are not rendered by default.

56 changes: 48 additions & 8 deletions docs/rapidoc.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions docs/specs/form-data-test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"openapi": "3.0.3",
"paths": {
"/api/form-data-test": {
"post": {
"requestBody": {
"required": true,
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"allOf": [
{ "$ref": "#/components/schemas/FormDataTestRequest" }
]
}
}
}
}
}
}
},
"components": {
"schemas": {
"FormDataTestRequest": {
"properties": {
"items": { "type": "array", "items": { "type": "string" } },
"itemType": {
"type": "string",
"enum": [
"typeA",
"typeB",
"typeC",
"typeD",
"typeE"
]
}
},
"type": "object"
}
}
}
}
33 changes: 23 additions & 10 deletions src/components/api-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ export default class ApiRequest extends LitElement {
data-example = "${Array.isArray(fieldExamples) ? fieldExamples.join('~|~') : fieldExamples}"
data-array = "true"
placeholder = "add-multiple ↩"
.value = "${Array.isArray(fieldExamples) ? Array.isArray(fieldExamples[0]) ? fieldExamples[0] : fieldExamples : []}"
.initialValue = "${Array.isArray(fieldExamples) ? Array.isArray(fieldExamples[0]) ? fieldExamples[0] : fieldExamples : []}"
>
</tag-input>
`
Expand Down Expand Up @@ -975,6 +975,8 @@ export default class ApiRequest extends LitElement {
inputEl.value = e.target.dataset.enum;
}
}
const event = new Event('input');
inputEl.dispatchEvent(event);
}}"
>
${v}
Expand Down Expand Up @@ -1428,7 +1430,9 @@ export default class ApiRequest extends LitElement {
formDataParams.append(el.dataset.pname, el.value);
}
} else if (el.value && Array.isArray(el.value)) {
formDataParams.append(el.dataset.pname, el.value.join(','));
for (const value of el.value) {
formDataParams.append(`${el.dataset.pname}[]`, value);
}
}
});
fetchOptions.body = formDataParams;
Expand Down Expand Up @@ -1693,14 +1697,6 @@ export default class ApiRequest extends LitElement {
return [...aggregator, ` -F "${key}=@${value.name}"`];
}

const multiple = value.match(/([^,],)/gm);

if (multiple) {
const multipleResults = multiple.map((one) => `-F "${key}[]=${one}"`);

return [...aggregator, ...multipleResults];
}

return [...aggregator, ` -F "${key}=${value}"`];
}, []).join('\\\n');
} else if (requestBodyContainerEl && requestBodyContainerEl.dataset.selectedRequestBodyType) {
Expand Down Expand Up @@ -1778,6 +1774,23 @@ export default class ApiRequest extends LitElement {
}
}

firstUpdated() {
if (this.showCurlBeforeTry === 'true') {
this.shadowRoot.querySelectorAll('tag-input').forEach((el) => {
el.addEventListener('contentChanged', (/* event */) => {
this.applyCURLSyntax(this.shadowRoot);
});
});

this.shadowRoot.querySelectorAll('input').forEach((el) => {
window.console.log('registerEvent', el);
el.addEventListener('input', (/* event */) => {
this.applyCURLSyntax(this.shadowRoot);
});
});
}
}

disconnectedCallback() {
this.curlSyntax = '';
// Cleanup ObjectURL for the blob data if this component created one
Expand Down
23 changes: 23 additions & 0 deletions src/components/tag-input.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { LitElement, html, css } from 'lit';

export default class TagInput extends LitElement {
connectedCallback() {
this.value = this.initialValue ?? [];
super.connectedCallback();
}

/* eslint-disable indent */
render() {
let tagItemTmpl = '';
Expand All @@ -19,13 +24,31 @@ export default class TagInput extends LitElement {
}
/* eslint-enable indent */

get value() {
/* eslint-disable-next-line no-underscore-dangle */
return this._value;
}

set value(newValue) {
/* eslint-disable no-underscore-dangle */
const oldValue = this._value;
this._value = newValue;
/* eslint-enable no-underscore-dangle */
this.requestUpdate('value', oldValue);
this.sendContentChanged();
}

static get properties() {
return {
placeholder: { type: String },
value: { type: Array, attribute: 'value' },
};
}

sendContentChanged() {
this.dispatchEvent(new CustomEvent('contentChanged', { bubbles: true, composed: true }));
}

attributeChangedCallback(name, oldVal, newVal) {
if (name === 'value') {
if (newVal && oldVal !== newVal) {
Expand Down