Skip to content
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

Add a way to get the unresolved Response for corner case support #34

Merged
merged 3 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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