-
-
Notifications
You must be signed in to change notification settings - Fork 529
Support more type of request body #1123
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
Comments
Any updates? |
I think this is easily-doable and a good idea. Thank you for being willing to open a PR! But I’d be happy to tackle this myself in a future release. The types are still changing rapidly and it may not be clear right this moment how to implement this. But the types will settle relatively soon, and contributions will be easier in the near future. |
So I’m actually starting to second-guess this feature request. While, yes, sometimes OpenAPI specs have endpoints that return non-JSON-y things like file downloads, it also is not too common. Further, there’s not really much benefit this library can provide when it comes to non-JSON objects. Put another way, all this library would be doing for you would be checking the URL. Which I personally would never install an npm package just to tell me my URLs are right or wrong. So if we do support this, it would be minimal support. Which would also make sense considering things like I’d probably lean towards this library simply returning |
I agree with you. However this library may do more than validating urls, bodies, params can also be checked. The minimal request is not to treat every response body as JSON. |
👋 Thanks for all the work you put into this package, @drwpow - this is an exceptionally useful library! I wanted to ask if I could clarify your point below:
I was thinking the original question was focused more on non-JSON request bodies? Your reply is more focused on response bodies... For example, I have a spec file that defines the following request body: requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
image_file:
type: string
format: binary The typescript interface generated from openapi-typescript turns that into: requestBody: {
content: {
'multipart/form-data': {
/** Format: binary */
image_file?: string;
};
};
}; But ultimately at build-time, The API endpoint I'm trying to hit requires me to send a FormData object. Unfortunately, casting FormData as Personally, I'm a huge fan of "Improve type check, pass these objects as is" in regard to passing other types of data (not just JSON) to request bodies, like @KotoriK originally mentioned. Maybe I'm totally misunderstanding the thread... but regardless, if you're open to allowing request bodies of types other than just JSON, I'd be more than happy to contribute to help get that done! |
We're in the same boat as @matthewvolk - the example given is actually great, thank you! We have a very similar scenario: Multipart endpoints which contain a file property in the request body. From what I can see, this would require two additions:
|
Also see the PR notes for more context on why this approach was taken. This should take care of this issue, but please let me know if any details are missing still. |
@drwpow Thank you for that - as far as I can see from the release notes this covers only the response direction though, right? Another common scenario seems to be request bodies with non-JSON content, like in @matthewvolk's example, such as a request which contains a file Blob to be sent within a multipart request. |
Ah thank you—I did have another item to track more flexible handling of requests. But I don’t have another issue for that, so I can use this one. |
`openapi-fetch` does not handle non-JSON `body`s, always stringifying them, and sets the `content-type` to `application/json`. The patch here does two things: - Do not stringify `body` if it is one of the types that should not be stringified (https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#body) - Do not add `content-type: application/json` unless it really is stringified JSON. Upstream issue: openapi-ts/openapi-typescript#1123 I'm not a bit lost on fixing the types and adding tests, so not raising a PR upstream.
I had a go at fixing this but the types and tests are a bit beyond me, so I'm not submitting a PR - just applied the fix via Thanks for your work on this library! |
`openapi-fetch` does not handle non-JSON `body`s, always stringifying them, and sets the `content-type` to `application/json`. The patch here does two things: - Do not stringify `body` if it is one of the types that should not be stringified (https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#body) - Do not add `content-type: application/json` unless it really is stringified JSON. Upstream issue: openapi-ts/openapi-typescript#1123 I'm not a bit lost on fixing the types and adding tests, so not raising a PR upstream.
`openapi-fetch` does not handle non-JSON `body`s, always stringifying them, and sets the `content-type` to `application/json`. The patch here does two things: - Do not stringify `body` if it is one of the types that should not be stringified (https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#body) - Do not add `content-type: application/json` unless it really is stringified JSON. Upstream issue: openapi-ts/openapi-typescript#1123 I'm not a bit lost on fixing the types and adding tests, so not raising a PR upstream.
I started to write a PR when I realised I can use the bodySerializer option as you can see in this sample test: https://github.com/drwpow/openapi-typescript/blob/main/packages/openapi-fetch/src/index.test.ts#L274-L291 |
@drwpow Thank you for this - looking forward to give it a try! |
The newly added |
The content type should not be manually set for formData - the browser sets it automatically. Wonder if that's the issue. |
I should have said that I don't set it (and maybe I should have posted some code) const { post } = createClient<paths>({
baseUrl: 'http://localhost:8000/',
mode: 'cors',
})
const foo: string
const upload_file: File
await post('/levy-upload', {
body: {
foo,
upload_file,
},
bodySerializer (body) {
const fd = new FormData()
fd.append('foo', body.foo)
fd.append('upload_file', body.upload_file)
return fd
},
}) The request that gets made is const { post } = createClient<paths>({
baseUrl: 'http://localhost:8000/',
mode: 'cors',
fetch: (url, init) => {
const { headers, ...newInit } = init
return fetch(url, newInit)
},
}) |
Yeah - I meant I think the library is manually setting it, not you |
Maybe should open an issue 🧐 |
Description
Currently request body that is not encoded as string will be serialize to JSON https://github.com/drwpow/openapi-fetch/blob/fabfad203c326c91d7a3107f43ab3e5be95bc4f1/src/index.ts#L130
However JSON is not the only citizen, binary data and FormData are also widely used in practice.
Proposal
Improve type check, pass these objects as is:
(list from https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#body)
Checklist
The text was updated successfully, but these errors were encountered: