Skip to content

Commit 5052c45

Browse files
feat: injectServerUrl and createFetch utils
1 parent e264afa commit 5052c45

File tree

1 file changed

+49
-13
lines changed

1 file changed

+49
-13
lines changed

src/e2e.ts

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { FetchOptions, FetchResponse, MappedResponseType, ResponseType } from 'ofetch'
22
import type { TestOptions } from './types'
33
import { ofetch } from 'ofetch'
4+
import { inject } from 'vitest'
45
import { clearTestContext, createTestContext, injectTestContext } from './context'
56
import { startServer, stopServer } from './server'
67

@@ -17,22 +18,42 @@ declare module 'vitest' {
1718
}
1819
}
1920

20-
export async function $fetch<T = any, R extends ResponseType = 'json'>(
21+
/**
22+
* Creates a custom `ofetch` instance with the Nitro server URL as the base URL.
23+
*
24+
* @remarks
25+
* The following additional fetch options have been set as defaults:
26+
* - `ignoreResponseError: true` to prevent throwing errors on non-2xx responses.
27+
* - `redirect: 'manual'` to prevent automatic redirects.
28+
* - `headers: { accept: 'application/json' }` to force a JSON error response when Nitro returns an error.
29+
*/
30+
export function createFetch() {
31+
const serverUrl = injectServerUrl()
32+
33+
return ofetch.create({
34+
baseURL: serverUrl,
35+
ignoreResponseError: true,
36+
redirect: 'manual',
37+
headers: {
38+
accept: 'application/json',
39+
},
40+
})
41+
}
42+
43+
/**
44+
* Fetches the raw response from the Nitro server for the given path. `FetchOptions` can be passed to customize the request.
45+
*
46+
* @remarks
47+
* The following additional fetch options have been set as defaults:
48+
* - `ignoreResponseError: true` to prevent throwing errors on non-2xx responses.
49+
* - `redirect: 'manual'` to prevent automatic redirects.
50+
* - `headers: { accept: 'application/json' }` to force a JSON error response when Nitro returns an error.
51+
*/
52+
export async function $fetchRaw<T = any, R extends ResponseType = 'json'>(
2153
path: string,
2254
options?: FetchOptions<R>,
2355
) {
24-
const ctx = injectTestContext()
25-
let serverUrl = ctx?.server?.url
26-
27-
if (!serverUrl) {
28-
const vitest = await import('vitest')
29-
serverUrl = vitest.inject('server')?.url
30-
}
31-
32-
if (!serverUrl) {
33-
throw new Error('Nitro server is not running.')
34-
}
35-
56+
const serverUrl = injectServerUrl()
3657
const localFetch = ofetch.create({
3758
baseURL: serverUrl,
3859
ignoreResponseError: true,
@@ -56,6 +77,21 @@ export async function $fetch<T = any, R extends ResponseType = 'json'>(
5677
return response as TestFetchResponse<MappedResponseType<R, T>>
5778
}
5879

80+
export function injectServerUrl() {
81+
const ctx = injectTestContext()
82+
let serverUrl = ctx?.server?.url
83+
84+
if (!serverUrl) {
85+
serverUrl = inject('server')?.url
86+
}
87+
88+
if (!serverUrl) {
89+
throw new Error('Nitro server is not running.')
90+
}
91+
92+
return serverUrl
93+
}
94+
5995
/**
6096
* Setup options for the Nitro test context.
6197
*

0 commit comments

Comments
 (0)