(
path: string,
__NEXTAUTH: NextAuthClientConfig,
logger: LoggerInstance,
{ ctx, req = ctx?.req }: CtxOrReq = {}
)
| 30 | * pages *and* in _app.js. |
| 31 | */ |
| 32 | export 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 | |
| 56 | export function apiBaseUrl(__NEXTAUTH: NextAuthClientConfig) { |
| 57 | if (typeof window === "undefined") { |
nothing calls this directly
no test coverage detected
searching dependent graphs…