-
-
Notifications
You must be signed in to change notification settings - Fork 2
Open
Description
The FetchLike interface currently has json() and status as properties
| yield `}): Promise<{ json(): Promise<T>, status: number }>;`; |
https://developer.mozilla.org/en-US/docs/Web/API/Response#instance_properties
I don't nessesarily think that we need a fully featured interface but I do think that text() and ok would be valuable additions
typescript-http-client/src/http-client-generator.ts
Lines 601 to 637 in ea8c10c
| if (this.method.returns) | |
| yield `const res = await this.fetch${returnType}(path`; | |
| else { | |
| yield `await this.fetch(path`; | |
| } | |
| yield ` ,{`; | |
| if (this.httpMethod.verb.value.toUpperCase() !== 'GET') { | |
| yield ` method: '${this.httpMethod.verb.value.toUpperCase()}',`; | |
| } | |
| yield ` headers,`; | |
| if (this.hasBody()) yield ` body,`; | |
| yield ` }`; | |
| yield `)`; | |
| yield ``; | |
| yield ``; | |
| if (this.method.returns?.value.kind === 'ComplexValue') { | |
| const responseTypeName = getTypeByName( | |
| this.service, | |
| this.method.returns.value.typeName.value, | |
| )!; | |
| const mapperName = this.dtoBuilder.buildMapperName( | |
| responseTypeName.name.value, | |
| 'client-outbound', | |
| ); | |
| if (this.options?.httpClient?.validation === 'zod') { | |
| yield `return mappers.${mapperName}(await res.json());`; | |
| } else { | |
| const sanitizerName = camel( | |
| `sanitize_${snake(responseTypeName.name.value)}`, | |
| ); | |
| yield `return sanitizers.${sanitizerName}(mappers.${mapperName}(await res.json()));`; |
then I think it would be valuable to check ok before we use res as if fetch returns a 4xx with some error that doesn't match the expected returnType, then the details of that error are swallowed
if the returnType is expected to be
export type GetSampleResponse = {
errors: Error[];
data?: GetSampleResponseData;
};but we got an actual response of
{
"status": 418,
"name": "I'm a teapot"
}then the response from HttpSampleService.getSample() will be {}
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Type
Projects
Status
Todo