Skip to content

Commit

Permalink
Merge pull request #34 from AcquisitionSimplicity/Add-getRaw-to-executer
Browse files Browse the repository at this point in the history
Add a way to get the unresolved Response for corner case support
  • Loading branch information
SaltyAom authored Dec 15, 2023
2 parents 56e35c6 + 8633493 commit aaa5499
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/treaty/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class EdenWS<Schema extends InputSchema<any> = 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,
Expand Down Expand Up @@ -175,6 +175,7 @@ const createProxy = (
$fetch?: RequestInit
$headers?: HeadersInit
$query?: Record<string, string>
getRaw?: boolean
},
{
fetch?: RequestInit
Expand All @@ -190,7 +191,7 @@ const createProxy = (
? initialBody
: undefined

const { $query, $fetch, $headers, $transform, ...restBody } =
const { $query, $fetch, $headers, $transform, getRaw, ...restBody } =
initialBody ?? {}

bodyObj ??= restBody
Expand Down Expand Up @@ -229,7 +230,7 @@ const createProxy = (
)
)

const execute = async () => {
const execute = async <T extends EdenTreaty.ExecuteOptions>(modifiers: T): Promise<EdenTreaty.ExecuteReturnType<T>> => {
let body: any

const headers = {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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<string, unknown>

Expand Down
14 changes: 14 additions & 0 deletions src/treaty/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Record<string, unknown>>,
Paths extends (string | number)[] = Split<keyof Schema & string>,
Expand Down Expand Up @@ -111,6 +119,7 @@ export namespace EdenTreaty {
: ((
params: {
$fetch?: RequestInit
getRaw?: boolean
} & (IsUnknown<Route['body']> extends false
? Replace<
Route['body'],
Expand Down Expand Up @@ -198,6 +207,11 @@ export namespace EdenTreaty {
rawData: MessageEvent['data']
}

export type ExecuteOptions = {
getRaw?: boolean
};
export type ExecuteReturnType<T extends ExecuteOptions> = T['getRaw'] extends true ? Response : DetailedResponse;

export type WSEvent<
K extends keyof WebSocketEventMap,
Data = unknown
Expand Down

0 comments on commit aaa5499

Please sign in to comment.