feat: add support for ky v2 #3724#3725
Conversation
|
|
|
Reviewed PR #3725. Found a redundant/dead code path in the error-handling logic at Task list (6/6 completed)
|
|
@atomicpages is attempting to deploy a commit to the Hey API Team on Vercel. A member of the Team first needs to authorize it. |
|
|
TL;DR — Upgrades the Key changes
Summary | 35 files | 2 commits | base: Ky 2 error handling with
|
| import type { HTTPError, Options as KyOptions } from 'ky'; | ||
| import ky from 'ky'; | ||
| import type { Options as KyOptions } from 'ky'; | ||
| import ky, { isHTTPError } from 'ky'; |
There was a problem hiding this comment.
this was available in later version of ky v1, not required to use, but helps with tsc
There was a problem hiding this comment.
Important
Two issues to address: a dead/redundant code path in the error-handling logic, and a personal editor setting that leaked into the diff.
| jsonError = JSON.parse(textError); | ||
| } catch { | ||
| jsonError = undefined; | ||
| if (kyHttpError) { |
There was a problem hiding this comment.
This is due to:
- Make .json() throw on empty bodies and 204 responses instead of returning an empty string (Make
json()throw on empty responses sindresorhus/ky#854) sindresorhus/ky@1b8e1ff - Make beforeError hook receive all errors, not just HTTPError (Make
beforeErrorhook receive all errors sindresorhus/ky#829) sindresorhus/ky@101c74b
| if (error && typeof error === 'object' && 'response' in error) { | ||
| const httpError = error as HTTPError; | ||
| response = httpError.response; | ||
| if (isHTTPError(error)) { |
There was a problem hiding this comment.
This should be safe to use: https://github.com/sindresorhus/ky/blob/7d23d68e2614777f240896060f31d77f74f1a0d2/source/utils/type-guards.ts#L8
There was a problem hiding this comment.
This has already landed in #3806 , you may wanna pull/rebase from the latest branch.
| export const client = createClient( | ||
| createConfig<ClientOptions2>({ baseUrl: 'https://petstore3.swagger.io/api/v3' }), | ||
| ); | ||
| export const client = createClient(createConfig<ClientOptions2>({ baseUrl: '/api/v3' })); |
There was a problem hiding this comment.
Instead of altering the example, maybe you can add a test case?
In my own PR, I added an unit test case with mocked fetch to validate that fetch receives proper credential: include
You can use the similar technique to validate mocked fetch received a proper URL.
|
Acknowledged the author's correction on the truthiness thread — Task list (2/3 completed) |
|
Fixed the bug in the final error branch of Task list (4/4 completed)
|
|
@atomicpages Hello, would you like to resolve the conflict? Some of your changes got landed already in my merged PR #3806 (e.g., prefer There are also other changes as well. I will add a few review comments. |
|
|
||
| Hey API types omit several Ky options that the wrapper sets itself (for example `method`, `body`, and `prefix`). For any other Ky settings, pass them in **`kyOptions`** or on the top-level config where the generated types allow it. | ||
|
|
||
| If you are upgrading from Ky 1, rename **`prefixUrl` to `prefix`** anywhere you pass Ky configuration (including inside `kyOptions`). Ky 2 also adds a standard **`baseUrl`** option for URL resolution; see the [Ky v2.0.0 release notes](https://github.com/sindresorhus/ky/releases/tag/v2.0.0) for hooks, `HTTPError.data`, and other breaking changes. |
There was a problem hiding this comment.
Technically, this is not true.
AFAIK, the current version of client-ky only respects baseUrl from Hey API's own options, and Hey API always uses the buildUrl function to join the full URL, so the Ky instance will always receive a full URL instead of a relative URL.
In short, Ky's prefixUrl option never worked in the current version of client-ky.
| export interface Config<T extends ClientOptions = ClientOptions> | ||
| extends | ||
| Omit<KyOptions, 'body' | 'headers' | 'method' | 'prefixUrl' | 'retry' | 'throwHttpErrors'>, | ||
| Omit<KyOptions, 'body' | 'headers' | 'method' | 'prefix' | 'retry' | 'throwHttpErrors'>, |
There was a problem hiding this comment.
Ky v2 also has baseUrl, isn't it?
https://github.com/sindresorhus/ky#baseurl
Also, it would be nice to still include prefixUrl in the list as well, it would be nice to support both Ky v1 and v2 when possible.
| return parseErrorResponse(response, request, opts, interceptors, { | ||
| bodyConsumed: true, | ||
| data: undefined, | ||
| }); |
There was a problem hiding this comment.
bodyConsumed issue may (or may not) be resolved by my other yet-to-be-merged PR #3814
In that PR, I use a huge try catch block, so any thrown error may not have its body consumed (remains to be seen, though).


Config/kyOptionstypes: omitprefixinstead of removedprefixUrl.HTTPErrorwithisHTTPError, useerror.datawhen Ky has consumed the body, and fall back toresponse.text()when the body is still readable (e.g. tests).dataand a failingresponse.text().openapi-ts-testssnapshots for@hey-api/client-ky; bumpkyto 2.x in the Ky example and test package; regenerate example client output.docs/openapi-ts/clients/ky.mdfor Ky 2,prefixUrl→prefix, andkyOptionsexample.Note: the tests I delegated to cursor. I'm happy to either revert and/or amend any changes. LMK