Optional query parameter being sent as 'undefined'
ryancontent opened this issue · comments
Ryan commented
Hey Encore team! I’m using the typescript generated client and a route with an optional query parameter. It builds the optional type in TS correctly however it’s still being sent in the query when not included (being sent as “undefined”). Let me know if I might be missing something.
// backend
type GetDocumentsRequest struct {
FiscalYear int `query:"fiscal_year" encore:"optional"`
}
// frontend codegen client
export interface GetDocumentsRequest {
FiscalYear?: number
}
When it’s building the query it’s always including the parameter even though it could be undefined
public async GetDocuments(params: GetDocumentsRequest): Promise<GetDocumentsResponse> {
// Convert our params into the objects we need for the request
const query = makeRecord<string, string | string[]>({
"fiscal_year": String(params.FiscalYear),
})
// Now make the actual call to the API
const resp = await this.baseClient.callAPI("GET", `/documents`, undefined, {query})
return await resp.json() as GetDocumentsResponse
}
Since fiscal_year
is a number | undefined - using String()
returns 'undefined'
(as a string) so it’s not being caught by makeRecord