MCPcopy
hub / github.com/nextauthjs/next-auth / fetchData

Function fetchData

src/client/_utils.ts:32–54  ·  view source on GitHub ↗
(
  path: string,
  __NEXTAUTH: NextAuthClientConfig,
  logger: LoggerInstance,
  { ctx, req = ctx?.req }: CtxOrReq = {}
)

Source from the content-addressed store, hash-verified

30 * pages *and* in _app.js.
31 */
32export async function fetchData<T = any>(
33 path: string,
34 __NEXTAUTH: NextAuthClientConfig,
35 logger: LoggerInstance,
36 { ctx, req = ctx?.req }: CtxOrReq = {}
37): Promise<T | null> {
38 try {
39 const options = req?.headers.cookie
40 ? { headers: { cookie: req.headers.cookie } }
41 : {}
42 const res = await fetch(`${apiBaseUrl(__NEXTAUTH)}/${path}`, options)
43 const data = await res.json()
44 if (!res.ok) throw data
45 return Object.keys(data).length > 0 ? data : null // Return null if data empty
46 } catch (error) {
47 logger.error("CLIENT_FETCH_ERROR", {
48 error: error as Error,
49 path,
50 ...(req ? { header: req.headers } : {}),
51 })
52 return null
53 }
54}
55
56export function apiBaseUrl(__NEXTAUTH: NextAuthClientConfig) {
57 if (typeof window === "undefined") {

Callers

nothing calls this directly

Calls 1

apiBaseUrlFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…