-
-
Notifications
You must be signed in to change notification settings - Fork 344
feat: add support for ky v2 #3724 #3725
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| // This file is auto-generated by @hey-api/openapi-ts | ||
|
|
||
| import type { HTTPError, Options as KyOptions } from 'ky'; | ||
| import ky from 'ky'; | ||
| import type { Options as KyOptions } from 'ky'; | ||
| import ky, { isHTTPError } from 'ky'; | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was available in later version of ky v1, not required to use, but helps with tsc |
||
|
|
||
| import { createSseClient } from '../core/serverSentEvents.gen'; | ||
| import type { HttpMethod } from '../core/types.gen'; | ||
|
|
@@ -83,22 +83,44 @@ export const createClient = (config: Config = {}): Client => { | |
| request: Request, | ||
| opts: ResolvedRequestOptions, | ||
| interceptorsMiddleware: Middleware<Request, Response, unknown, ResolvedRequestOptions>, | ||
| kyHttpError?: { bodyConsumed: true; data: unknown }, | ||
| ) => { | ||
| const result = { | ||
| request, | ||
| response, | ||
| }; | ||
|
|
||
| const textError = await response.text(); | ||
| let jsonError: unknown; | ||
| let error: unknown; | ||
|
|
||
| try { | ||
| jsonError = JSON.parse(textError); | ||
| } catch { | ||
| jsonError = undefined; | ||
| if (kyHttpError) { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is due to:
|
||
| if (kyHttpError.data !== undefined) { | ||
| if (typeof kyHttpError.data === 'string') { | ||
| let jsonError: unknown; | ||
| try { | ||
| jsonError = JSON.parse(kyHttpError.data); | ||
| } catch { | ||
| jsonError = undefined; | ||
| } | ||
| error = jsonError ?? kyHttpError.data; | ||
| } else { | ||
| error = kyHttpError.data; | ||
| } | ||
| } else { | ||
| error = ''; | ||
| } | ||
| } else { | ||
| const textError = await response.text(); | ||
| let jsonError: unknown; | ||
|
|
||
| try { | ||
| jsonError = JSON.parse(textError); | ||
| } catch { | ||
| jsonError = undefined; | ||
| } | ||
|
|
||
| error = jsonError ?? textError; | ||
| } | ||
|
|
||
| const error = jsonError ?? textError; | ||
| let finalError = error; | ||
|
|
||
| for (const fn of interceptorsMiddleware.error.fns) { | ||
|
|
@@ -174,17 +196,30 @@ export const createClient = (config: Config = {}): Client => { | |
| try { | ||
| response = await kyInstance(request, kyOptions); | ||
| } catch (error) { | ||
| if (error && typeof error === 'object' && 'response' in error) { | ||
| const httpError = error as HTTPError; | ||
| response = httpError.response; | ||
| if (isHTTPError(error)) { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be safe to use: https://github.com/sindresorhus/ky/blob/7d23d68e2614777f240896060f31d77f74f1a0d2/source/utils/type-guards.ts#L8 |
||
| response = error.response; | ||
|
|
||
| for (const fn of interceptors.response.fns) { | ||
| if (fn) { | ||
| response = await fn(response, request, opts); | ||
| } | ||
| } | ||
|
|
||
| return parseErrorResponse(response, request, opts, interceptors); | ||
| if (error.data !== undefined) { | ||
| return parseErrorResponse(response, request, opts, interceptors, { | ||
| bodyConsumed: true, | ||
| data: error.data, | ||
| }); | ||
| } | ||
|
|
||
| if (!response.bodyUsed) { | ||
| return parseErrorResponse(response, request, opts, interceptors); | ||
| } | ||
|
|
||
| return parseErrorResponse(response, request, opts, interceptors, { | ||
| bodyConsumed: true, | ||
| data: undefined, | ||
| }); | ||
| } | ||
|
|
||
| throw error; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Verified the base url remains correct