Skip to content
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(fetch): split responses in separate types based on status #1885

Merged

Conversation

AllieJonsson
Copy link
Contributor

Status

READY

Description

Fix #1881
Splits response types into multiple types, to be able to check against status and get correctly typed data.

Test with specifications with multiple responses, as well as a default response

Todos

  • Tests
  • Documentation
  • Changelog Entry (unreleased)

Sorry, something went wrong.

melloware
melloware previously approved these changes Feb 7, 2025
@soartec-lab soartec-lab self-assigned this Feb 8, 2025
@soartec-lab
Copy link
Member

Thank you for this improvement. I'm checking other PRs, so I'll check this one in turn.

.map(
(r) => `export type ${responseTypeName}${pascal(r.key)} = {
${isContentTypeNdJson(r.contentType) ? 'stream: Response' : `data: ${r.value || 'unknown'}`}
status: ${r.key === 'default' ? `Exclude<HTTPStatusCodes, ${nonDefaultStatuses.join(' | ')}>` : r.key}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized this probably fails if the spec contains only a default status and no other statuses. Will need to change this, so dont merge yet

@melloware melloware marked this pull request as draft February 9, 2025 16:00
@melloware
Copy link
Collaborator

I converted it to a Draft for now @AllieJonsson so no one merges it.

@AllieJonsson AllieJonsson marked this pull request as ready for review February 9, 2025 20:20
Copy link
Member

@soartec-lab soartec-lab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AllieJonsson
It's not important to me, but it makes the code redundant, so I made a suggestion.

@@ -25,3 +25,12 @@ type NonReadonly<T> = [T] extends [UnionToIntersection<T>] ? {
: T[P];
} : DistributeReadOnlyOverUnions<T>;
`;

export const getHTTPStatusCodes = () => `
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm starting to notice that having this boiler template for each file causes noise.
I think this is a definition for the default, but how about making the default status definition a number instead of a strict enum?

Copy link
Contributor Author

@AllieJonsson AllieJonsson Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, that would not work. Image this scenario:

type response200 = {
  data: myData;
  status: 200;
}
type responseDefault = {
  data: myError;
  status: number;
}
type responseComposite = response200 | responseDefault;
type response = responseComposite & {
  headers: Headers;
}

Now checking if status is 200 is not enough:

if (resp.status === 200) {
  // resp.data is still typed to myData | myError
}

And you cannot exclude certain numbers from number:

type Ex = Exclude<number, 200> is resolved to type Ex = number

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, you are right. Please wait a moment as I confirm this.

Copy link
Member

@soartec-lab soartec-lab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AllieJonsson
I just finished checking everything. Thank you for the great changes 🙌
could you regenerate sample apps?

@AllieJonsson AllieJonsson force-pushed the feat/fetch-split-status-response branch from 929bc8b to ad498ae Compare February 14, 2025 08:00
Copy link
Member

@soartec-lab soartec-lab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AllieJonsson

thank you. Please check my comment about refactoring the swr and query packages

? `type SecondParameter<T extends (...args: any) => any> = Parameters<T>[1];\n\n`
: ''
}`;
}
${params.output.httpClient === OutputHttpClient.FETCH ? generateFetchHeader(params) : ''}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The functions when processing is divided by httpClient are consolidated into client.ts, so please move them 👍

@soartec-lab
Copy link
Member

CI is failing 👀

@soartec-lab
Copy link
Member

Hi @AllieJonsson Could you check this?

@AllieJonsson
Copy link
Contributor Author

Yes, sorry, I've had the flu 🤒 I'll fix this later today

@AllieJonsson AllieJonsson force-pushed the feat/fetch-split-status-response branch from 4bc00a9 to f98a3f5 Compare February 21, 2025 10:36
@AllieJonsson
Copy link
Contributor Author

@soartec-lab Issue fixed, but there seems to be some zod errors in the samples?

@soartec-lab
Copy link
Member

@AllieJonsson

Oh, please take care of yourself.
The zod error is probably due to #1907. Please wait to merge as I confirm this.

@soartec-lab
Copy link
Member

Hi @AllieJonsson, The issue has been fixed and merged. Could you please update the sample apps again?

@AllieJonsson AllieJonsson force-pushed the feat/fetch-split-status-response branch from f98a3f5 to 6da9e66 Compare February 22, 2025 12:56
@AllieJonsson AllieJonsson force-pushed the feat/fetch-split-status-response branch from 6da9e66 to 32c1782 Compare February 22, 2025 13:02
@AllieJonsson
Copy link
Contributor Author

Hi @AllieJonsson, The issue has been fixed and merged. Could you please update the sample apps again?

Thanks! It looks good now

Copy link
Member

@soartec-lab soartec-lab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a bunch!

@soartec-lab soartec-lab merged commit cba57d1 into orval-labs:master Feb 22, 2025
2 checks passed
@soartec-lab soartec-lab added enhancement New feature or request fetch Fetch client related issue labels Feb 22, 2025
@hypnoboutique
Copy link

Thank you so much for this fix! 🙏

Do you have any idea when it is likely to be released?

@melloware
Copy link
Collaborator

hopefully today @hypnoboutique

@AllieJonsson AllieJonsson deleted the feat/fetch-split-status-response branch February 27, 2025 18:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fetch Fetch client related issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Response type is not matched with status when using Fetch client
4 participants