From 61ce1659cf2f0603bf9dab7c521cb192b6c2e5be Mon Sep 17 00:00:00 2001 From: Eduard Marbach Date: Sat, 12 Oct 2024 16:27:08 +0200 Subject: [PATCH] fix(ky): use json instead of body (fixes #84) --- src/__generated__/ky-client.ts | 34 ++++++++++++++++++++++++---------- src/custom-formats.ts | 2 +- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/__generated__/ky-client.ts b/src/__generated__/ky-client.ts index c371cb7..c291fc8 100644 --- a/src/__generated__/ky-client.ts +++ b/src/__generated__/ky-client.ts @@ -17,7 +17,7 @@ export type ResponsePromise = { export type ResponseFormat = keyof Omit; // Same as axios. Using provided SearchParamsOption by ky break some typings -export type QueryParamsType = Record; +export type QueryParamsType = Record; export interface FullRequestParams extends Omit { /** set parameter to `true` for call `securityWorker` for this request */ @@ -39,7 +39,7 @@ export type RequestParams = Omit extends Omit { securityWorker?: (securityData: SecurityDataType | null) => Promise | NormalizedOptions | void; secure?: boolean; - format?: ResponseType; + format?: ResponseFormat; } export enum ContentType { @@ -54,7 +54,7 @@ export class HttpClient { private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private secure?: boolean; - private format?: ResponseType; + private format?: ResponseFormat; constructor({ securityWorker, secure, format, ...options }: ApiConfig = {}) { this.ky = ky.create({ ...options, prefixUrl: options.prefixUrl || "" }); @@ -89,7 +89,7 @@ export class HttpClient { protected createFormData(input: Record): FormData { return Object.keys(input || {}).reduce((formData, key) => { const property = input[key]; - const propertyContent: any[] = property instanceof Array ? property : [property]; + const propertyContent: unknown[] = property instanceof Array ? property : [property]; // Changed `any[]` to `unknown[]` for (const formItem of propertyContent) { const isFileType = formItem instanceof Blob || formItem instanceof File; @@ -158,18 +158,32 @@ export class HttpClient { }; } - // workaround for query types - const searchParams = new URLSearchParams(query); + let searchParams: URLSearchParams | undefined; + + if (query != null) { + searchParams = new URLSearchParams(query as Record); + } + + // // Workaround for not working POSTs. Use JSON when json type else body + // const data: any = {}; + + // if (type == ContentType.Json) { + // data.json = body; + // } else { + // data.body = body as BodyInit; + // } - const request = this.ky(path.replace(/^\//, ""), { + const requestPromise: ResponsePromise = this.ky(path.replace(/^\//, ""), { ...options, headers, - searchParams: searchParams, - body: body as any, + searchParams, + //...data, + // Use always JSON + json: body as BodyInit, hooks, }); - return request; + return requestPromise; // Explicitly returning a typed promise }; } diff --git a/src/custom-formats.ts b/src/custom-formats.ts index 1b70dcf..4b5f9d4 100644 --- a/src/custom-formats.ts +++ b/src/custom-formats.ts @@ -89,7 +89,7 @@ export const manageCf = async ( createCFs++; } } catch (err: any) { - logger.error(err.response.data?.message, `Failed updating CF ${tr.requestConfig.name}`); + logger.error(err.response.data, `Failed updating CF ${tr.requestConfig.name}`); throw new Error(`Failed creating CF '${tr.requestConfig.name}'. Message: ${err.response.data?.message}`); } }