openapi-fetch: How to specify which content-type is returned in data? #1866
-
Hi, Considering the follow types generated from openapi-typescript (removed unrelevant fields): someCollection: {
responses: {
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["Some-List"][];
"application/vnd.api+json": components["schemas"]["Some-List"][];
"application/hal+json": components["schemas"]["Some-List"][];
"application/ld+json": {
"hydra:member": components["schemas"]["Some-List"][];
"hydra:totalItems"?: number;
"hydra:view"?: {
/** Format: iri-reference */
"@id"?: string;
"@type"?: string;
/** Format: iri-reference */
"hydra:first"?: string;
/** Format: iri-reference */
"hydra:last"?: string;
/** Format: iri-reference */
"hydra:next"?: string;
};
"hydra:search"?: {
"@type"?: string;
"hydra:template"?: string;
"hydra:variableRepresentation"?: string;
"hydra:mapping"?: {
"@type"?: string;
variable?: string;
property?: string | null;
required?: boolean;
}[];
};
};
"text/html": components["schemas"]["Some-List"][];
};
};
};
}; And the follow implementation: import createClient from 'openapi-fetch'
import type { paths } from './openapi.d.ts'
export const client = createClient<paths>({
baseUrl: "...",
headers: {
'Content-Type': 'application/json',
},
})
export const someFunction = async () => {
const { data, error } = await client.GET('/api/some-endpoint', {})
return data.map(item => item.name)
} Then typescript does not seem to be able to resolve the proper "content" type in the data variable. It is typed as string when you try to access properties. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Update. Eventually figured out that you also can pass a second type arg to
Now the second question is if there a way to change this per request basis or if I need to create separate clients for each response type? |
Beta Was this translation helpful? Give feedback.
Update. Eventually figured out that you also can pass a second type arg to
createClient
. Like so:Now the second question is if there a way to change this per request basis or if I need to create separate clients for each response type?