-
Notifications
You must be signed in to change notification settings - Fork 3
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
feat: enable header parameters #18
Conversation
examples/schema-with-header-param.ts
Outdated
const query = toQuery({ startDate, endDate, limit, cursor }) | ||
const headers = toHeaders({ 'Accept-Version': acceptVersion }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's worth adding functionality to only include specific headers in the request factories as I think most of the time, headers will not be something we want to have to pass in with each request. In this example the acceptVersion
param is something you'd specify in the proxy class rather than specify on each request.
Perhaps we could add something like:
// Include all headers in request factories
openapi-tsrf generate ... --include-headers *
// Include specific headers in request factories
openapi-tsrf generate ... --include-headers header1 header2 header3
It'd also be worth including a specific example of this behaviour via a new spec in the examples
dir and adding a matching test in tests/index.spec.ts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
commander docs for variadic options
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that makes sense. I'll add that in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think headers come in two flavours. Ones you supply with every request and ones that are defined by the spec per operation.
In the case of the one defined by the spec, I don't know if you want to have the option to disable it.
Take this spec, for example:
@@ -0,0 +1,1227 @@
{
"openapi": "3.0.0",
"paths": {
"/receipts": {
"post": {
"operationId": "receipts",
"parameters": [
{
"name": "some-value",
"in": "path",
"required": true,
"schema": {
"type": "string",
}
},
{
"name": "X-Platform",
"in": "header",
"required": true,
"schema": {
"type": "string",
}
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"app_user_id",
],
"properties": {
"app_user_id": {
"type": "string",
},
}
}
}
}
},
"responses": {
"200": {
"content": {
"application/json": { }
}
}
},
"deprecated": false
}
}
}
}
The spec advertises that you can only call the endpoint if you supply the required header. So, I think roughly the call would be
const response = await API.postReceipts({ someValue: 'hi', xPlatform: 'pleb os' })
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not a choice of whether or not to include the header - it's whether we force the consumer to provide it at the call site (as per your example) or whether we implicitly supply it when constructing the request in our api proxy factory.
We can't determine which option is better from the spec alone, so we should make it configurable so the user can decide.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed, I've added a --include-headers
flag to the cli, allowing you to specify *
or a whitelist of header names like --include-headers header1 header 2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
default behaviour is to not include any headers
Would it be cleaner if headers worked a little more like?
|
@pleb I've just updated how headers work so you can now use the spread operator in the proxy, rather than appending new headers to a header object. |
src/runtime/request-helpers.ts
Outdated
@@ -18,3 +18,14 @@ export const toFormData = (o: Record<string, any>): FormData => { | |||
Object.entries(o).forEach(([key, data]) => fd.append(key, data)) | |||
return fd | |||
} | |||
|
|||
export const toHeaders = (o: { [key: string]: any }): Record<string, any> => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be able to use Record<string,string>
for typing I think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I'm not sure this is even needed. We only filter undefined options for query params so we don't end up with ?blah=undefined
. It should be fine to just specify headers in the factory as headers: { header1, header2 }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So rather than calling toHeaders({'x-header-name': xHeaderName})
we just return {'x-header-name': xHeaderName}
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we have Record<string, string>
on the Request interfaces, we can't just return the header values as they are, we need to convert them as strings. The other option is to have any header parameters as string variables in the client to convert them to string before calling any generated methods. This is a little less type safe though because it means people can pass any string in as a value rather than the type that's specified in the swagger doc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just resolved our usage with @pleb
Co-authored-by: Tristan Menzel <tristan.menzel@gmail.com>
Co-authored-by: Wade Baglin <wade@baglin.au>
No description provided.