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: Custom fetch per call #1373

Merged
merged 6 commits into from
Oct 8, 2023

Conversation

HugeLetters
Copy link
Contributor

Changes

Closes #1369

How to Review

Adds the option to pass in custom fetch as fetchOption on per API call basis. Added a simple test to see that custom fetch overrides the one specified during client initialization.

Checklist

  • Unit tests updated
  • docs/ updated (if necessary)

@changeset-bot
Copy link

changeset-bot bot commented Oct 6, 2023

🦋 Changeset detected

Latest commit: 1f23659

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
openapi-fetch Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@HugeLetters
Copy link
Contributor Author

HugeLetters commented Oct 7, 2023

@drwpow since this change was directly inspired by SvelteKit I would like to add some additional info to docs about making openapi-fetch work with SvelteKit. The issue is not directly related to a custom fetch so perhaps you would prefer a separate PR?

Since your library reads content-length header when parsing a response, if you fetch during SSR SvelteKit will error because it doesn't serialize headers by default.
The fix is rather simple and an error message tells you almost everything you need to know to fix it but I thought adding a small section with a code example in the docs would be nice.
image

The fix is this

// hooks.server.ts
import type { Handle } from '@sveltejs/kit';
export const handle: Handle = async ({ event, resolve }) => {
	return resolve(event, {
		filterSerializedResponseHeaders(name) {
			return name === 'content-length';
		}
	});
};

Can I add this example to docs in this same PR?

@drwpow
Copy link
Contributor

drwpow commented Oct 7, 2023

Docs improvements are always appreciated!

Maybe rather than having that snippet live just in docs, though, putting it in the examples/sveltekit example code itself would mean people don’t miss it when they’re setting up their project. Inline comments can be added if explanation is needed. Beyond that, I’d probably omit it in the docs unless there’s something critical for the reader to understand.

@drwpow
Copy link
Contributor

drwpow commented Oct 7, 2023

But re: not serializing headers in SSR, that’s interesting! I’d be open to revisiting that default behavior after understanding why SvelteKit does that better (couldn’t find the “why” in a quick search, just other libraries requiring similar workarounds)

@HugeLetters
Copy link
Contributor Author

@drwpow I've updated the examples

Copy link
Contributor

@drwpow drwpow left a comment

Choose a reason for hiding this comment

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

This looks great, thank you!

Before releasing this version, though, I want to do a little testing first to make sure the updated types generated from openapi-typescript@7 didn’t introduce any accidental breaking changes from openapi-fetch (it shouldn’t have, but I need to double-check).

@drwpow
Copy link
Contributor

drwpow commented Oct 8, 2023

One last thing—could you add a patch changeset to describe the feature? See the comment on this PR for instructions.

.changeset/wise-poems-do.md Outdated Show resolved Hide resolved
@drwpow drwpow merged commit fd3e96f into openapi-ts:main Oct 8, 2023
@drwpow
Copy link
Contributor

drwpow commented Oct 8, 2023

Thank you again!

@github-actions github-actions bot mentioned this pull request Oct 8, 2023
@HugeLetters HugeLetters deleted the custom-fetch-per-call branch October 28, 2024 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom fetch support per call
2 participants