Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/purple-kids-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@softnetics/hono-react-query": minor
---

Add Raw Response to the response from the lib.
14 changes: 13 additions & 1 deletion src/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import { createReactQueryClient, HonoResponseError } from '.'

const basicHonoApp = new Hono()

Check warning on line 12 in src/index.spec.tsx

View workflow job for this annotation

GitHub Actions / ci

'basicHonoApp' is assigned a value but only used as a type. Allowed unused vars must match /^_/u
.get('/users', (c) => {
return c.json({ users: [{ id: 'id', name: 'John Doe' }] }, 200)
})
Expand Down Expand Up @@ -51,7 +51,7 @@
baseUrl: 'http://localhost:3000',
})

const queryOptions = client.queryOptions('/users/:id', '$get', {

Check warning on line 54 in src/index.spec.tsx

View workflow job for this annotation

GitHub Actions / ci

'queryOptions' is assigned a value but only used as a type. Allowed unused vars must match /^_/u
input: {
param: { id: 'none' },
},
Expand All @@ -71,19 +71,21 @@
}
status: 200
format: 'json'
raw: Response
}
| {
data: {
error: string
}
status: 400
format: 'json'
raw: Response
},
Error
>
>()

const queryFn = () =>

Check warning on line 88 in src/index.spec.tsx

View workflow job for this annotation

GitHub Actions / ci

'queryFn' is assigned a value but only used as a type. Allowed unused vars must match /^_/u
client.useQuery('/users/:id', '$get', {
input: { param: { id: 'none' } },
options: { throwOnError: false },
Expand All @@ -100,13 +102,15 @@
}
status: 200
format: 'json'
raw: Response
}
| {
data: {
error: string
}
status: 400
format: 'json'
raw: Response
},
Error
>
Expand All @@ -118,7 +122,7 @@
baseUrl: 'http://localhost:3000',
})

const queryOptions = client.queryOptions('/users/:id', '$get', {

Check warning on line 125 in src/index.spec.tsx

View workflow job for this annotation

GitHub Actions / ci

'queryOptions' is assigned a value but only used as a type. Allowed unused vars must match /^_/u
input: {
param: { id: 'none' },
},
Expand All @@ -135,12 +139,13 @@
}
status: 200
format: 'json'
raw: Response
},
Error | HonoResponseError<{ error: string }, 400, 'json'>
>
>()

const queryFn = () =>

Check warning on line 148 in src/index.spec.tsx

View workflow job for this annotation

GitHub Actions / ci

'queryFn' is assigned a value but only used as a type. Allowed unused vars must match /^_/u
client.useQuery('/users/:id', '$get', {
input: { param: { id: 'none' } },
options: { throwOnError: true },
Expand All @@ -157,6 +162,7 @@
}
status: 200
format: 'json'
raw: Response
},
| Error
| HonoResponseError<
Expand All @@ -171,7 +177,7 @@
})

it('should create the query client with the correct types', () => {
const client = createReactQueryClient<BasicHonoApp>({

Check warning on line 180 in src/index.spec.tsx

View workflow job for this annotation

GitHub Actions / ci

'client' is assigned a value but only used as a type. Allowed unused vars must match /^_/u
baseUrl: 'http://localhost:3000',
})

Expand All @@ -196,10 +202,16 @@
>()

expectTypeOf<ReturnType<typeof client.useSetQueryData<'/users', '$post'>>>().toMatchTypeOf<
(data: { data: { user: { id: string; name: string } }; status: 201; format: 'json' }) => {
(data: {
data: { user: { id: string; name: string } }
status: 201
format: 'json'
raw: Response
}) => {
data: { user: { id: string; name: string } }
status: 201
format: 'json'
raw: Response
}
>()

Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ function getter(obj: object, paths: string[]): any {
}

async function responseParser(response: Response, throwOnError?: boolean): Promise<any> {
const raw = response.clone()

let data: any
const contentType = response.headers.get('content-type')
if (contentType?.includes('application/json')) {
Expand All @@ -74,6 +76,7 @@ async function responseParser(response: Response, throwOnError?: boolean): Promi
: contentType?.includes('text/plain')
? 'text'
: 'body',
raw,
}

if (response.ok || throwOnError === false) {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type InferFunctionReturn<T> = T extends (...rest: any[]) => infer TReturn ? Awai

type ClientResponseParser<T> =
T extends ClientResponse<infer TData, infer TStatus, infer TFormat>
? { data: TData; status: TStatus; format: TFormat }
? { data: TData; status: TStatus; format: TFormat; raw: Response }
: never

type SuccessResponse<T> =
Expand Down
Loading