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

openapi-fetch improvements #1169

Merged
merged 2 commits into from
Jun 16, 2023
Merged

openapi-fetch improvements #1169

merged 2 commits into from
Jun 16, 2023

Conversation

drwpow
Copy link
Contributor

@drwpow drwpow commented Jun 16, 2023

Changes

Fixes:

✨ new parseAs option

To address the handling of non-JSON responses (#1123, #1159), you can now parse the response as something other than .json()

const { data } = await get('/my/url', { parseAs: 'arrayBuffer' });

parseAs takes any of the following options:

  • json
  • text
  • arrayBuffer
  • blob
  • stream (this skips parsing, and returns the Response.body as-is)

The reason for the manual detection is:

  • Your OpenAPI schema isn’t readable at runtime, so when the response comes in, this has no clue what your OpenAPI schema said it would be
  • Relying on the Content-Type header isn’t possible because it could be wrong or ambiguous (e.g. it’s not clear from Content-Type alone when someone may want a .blob() vs .arrayBuffer())
  • You may want more conditional control over response type (e.g. only return .arrayBuffer() in certain instances)

How to Review

Tests should pass

Checklist

  • Unit tests updated
  • README updated
  • examples/ directory updated (only applicable for openapi-typescript)

@changeset-bot
Copy link

changeset-bot bot commented Jun 16, 2023

🦋 Changeset detected

Latest commit: 3a61261

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 Minor

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

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jun 16, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 3a61261
Status: ✅  Deploy successful!
Preview URL: https://9b9a3004.openapi-ts.pages.dev
Branch Preview URL: https://improvements.openapi-ts.pages.dev

View logs

@@ -54,6 +56,31 @@ export type FetchResponse<T> =
| { data: T extends { responses: any } ? NonNullable<FilterKeys<Success<T["responses"]>, JSONLike>> : unknown; error?: never; response: Response }
| { data?: never; error: T extends { responses: any } ? NonNullable<FilterKeys<Error<T["responses"]>, JSONLike>> : unknown; response: Response };

/** Call URLSearchParams() on the object, but remove `undefined` and `null` params */
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Minor QoL improvement: yes, the library did use new URLSearchParams() as the default. But handling undefined / null params is annoying to have to implement one’s own querySerializer for.


/** Construct URL string from baseUrl and handle path and query params */
export function createFinalURL<O>(url: string, options: { baseUrl?: string; params: { query?: Record<string, unknown>; path?: Record<string, unknown> }; querySerializer: QuerySerializer<O> }): string {
let finalURL = `${options.baseUrl ? options.baseUrl.replace(TRAILING_SLASH_RE, "") : ""}${url as string}`;

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data

This [regular expression](1) that depends on [library input](2) may run slow on strings with many repetitions of '/'. This [regular expression](1) that depends on [library input](3) may run slow on strings with many repetitions of '/'.
}

/** Construct URL string from baseUrl and handle path and query params */
export function createFinalURL<O>(url: string, options: { baseUrl?: string; params: { query?: Record<string, unknown>; path?: Record<string, unknown> }; querySerializer: QuerySerializer<O> }): string {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

createFinalURL() is now exposed for testing (#1161)

@drwpow drwpow force-pushed the improvements branch 2 times, most recently from da2d8a5 to 9ddc881 Compare June 16, 2023 04:38
@drwpow drwpow merged commit 74bfc0d into main Jun 16, 2023
@drwpow drwpow deleted the improvements branch June 16, 2023 04:55
@github-actions github-actions bot mentioned this pull request Jun 16, 2023
@drwpow drwpow mentioned this pull request Jun 16, 2023
1 task
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.

1 participant