Skip to content

Suggestion - Check res.ok #37

@ToyVo

Description

@ToyVo

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

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 {}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions