From a20a5b2e2fcb8f2221e56030d868b37637988605 Mon Sep 17 00:00:00 2001 From: Matthew Ary Date: Wed, 6 Dec 2023 13:30:58 -0800 Subject: [PATCH 1/3] Refactor createProxy function to support getRaw option --- src/treaty/index.ts | 10 ++++++---- src/treaty/types.ts | 13 +++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/treaty/index.ts b/src/treaty/index.ts index 4c33bb4..8b7bbe5 100644 --- a/src/treaty/index.ts +++ b/src/treaty/index.ts @@ -175,6 +175,7 @@ const createProxy = ( $fetch?: RequestInit $headers?: HeadersInit $query?: Record + getRaw?: boolean }, { fetch?: RequestInit @@ -190,7 +191,7 @@ const createProxy = ( ? initialBody : undefined - const { $query, $fetch, $headers, $transform, ...restBody } = + const { $query, $fetch, $headers, $transform, getRaw, ...restBody } = initialBody ?? {} bodyObj ??= restBody @@ -229,7 +230,7 @@ const createProxy = ( ) ) - const execute = async () => { + const execute = async (modifiers: T): Promise> => { let body: any const headers = { @@ -311,6 +312,7 @@ const createProxy = ( let data + if (modifiers.getRaw) return response as any switch (response.headers.get('Content-Type')?.split(';')[0]) { case 'application/json': data = await response.json() @@ -351,10 +353,10 @@ const createProxy = ( response: response, status: response.status, headers: response.headers - } + } as any } - return execute() + return execute({ getRaw }) } }) as unknown as Record diff --git a/src/treaty/types.ts b/src/treaty/types.ts index a5d978f..becb578 100644 --- a/src/treaty/types.ts +++ b/src/treaty/types.ts @@ -83,6 +83,14 @@ export namespace EdenTreaty { transform?: Transform } + export type DetailedResponse = { + data: any + error: any + response: Response + status: number + headers: Headers + }; + export type Sign< Schema extends Record>, Paths extends (string | number)[] = Split, @@ -198,6 +206,11 @@ export namespace EdenTreaty { rawData: MessageEvent['data'] } + export type ExecuteOptions = { + getRaw?: boolean + }; + export type ExecuteReturnType = T['getRaw'] extends true ? Response : DetailedResponse; + export type WSEvent< K extends keyof WebSocketEventMap, Data = unknown From 1a6871ec07e96a3b5b3ba79bf7562aeb9482007a Mon Sep 17 00:00:00 2001 From: Matthew Ary Date: Wed, 6 Dec 2023 13:39:01 -0800 Subject: [PATCH 2/3] Add getRaw parameter to treaty API request --- src/treaty/types.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/treaty/types.ts b/src/treaty/types.ts index becb578..044bf96 100644 --- a/src/treaty/types.ts +++ b/src/treaty/types.ts @@ -119,6 +119,7 @@ export namespace EdenTreaty { : (( params: { $fetch?: RequestInit + getRaw?: boolean } & (IsUnknown extends false ? Replace< Route['body'], From 8633493c46d301133c8d2671d0ae417873ebd453 Mon Sep 17 00:00:00 2001 From: Matthew Ary Date: Wed, 6 Dec 2023 13:54:38 -0800 Subject: [PATCH 3/3] Fix typo in boolean value assignment --- src/treaty/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/treaty/index.ts b/src/treaty/index.ts index 8b7bbe5..0d54bf2 100644 --- a/src/treaty/index.ts +++ b/src/treaty/index.ts @@ -125,7 +125,7 @@ export class EdenWS = InputSchema> { } else if (!Number.isNaN(+data)) data = +data else if (data === 'true') data = true - else if (data === 'fase') data = false + else if (data === 'false') data = false listener({ ...ws,