(
path: string,
__LINEN_AUTH: LinenAuthClientConfig,
logger: LoggerInstance,
{ ctx, req = ctx?.req }: CtxOrReq = {}
)
| 48 | * pages *and* in _app.js. |
| 49 | */ |
| 50 | export async function fetchData<T = any>( |
| 51 | path: string, |
| 52 | __LINEN_AUTH: LinenAuthClientConfig, |
| 53 | logger: LoggerInstance, |
| 54 | { ctx, req = ctx?.req }: CtxOrReq = {} |
| 55 | ): Promise<T | null> { |
| 56 | const url = `${__LINEN_AUTH.basePath}/${path}`; |
| 57 | try { |
| 58 | const token = getJwtToken(); |
| 59 | const options: any = req?.headers.cookie |
| 60 | ? { headers: { cookie: req.headers.cookie } } |
| 61 | : {}; |
| 62 | |
| 63 | if (!!token) { |
| 64 | options.headers = { |
| 65 | ...(options.headers && { ...options.headers }), |
| 66 | Authorization: `Bearer ${token}`, |
| 67 | }; |
| 68 | } |
| 69 | |
| 70 | const res = await fetch(url, options); |
| 71 | const data = await res.json(); |
| 72 | if (!res.ok) throw data; |
| 73 | if (!!data.token) { |
| 74 | setJwtToken(data.token); |
| 75 | delete data.token; |
| 76 | } |
| 77 | return Object.keys(data).length > 0 ? data : null; // Return null if data empty |
| 78 | } catch (error: any) { |
| 79 | logger.error('CLIENT_FETCH_ERROR', { error: error as Error, url }); |
| 80 | return null; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** Returns the number of seconds elapsed since January 1, 1970 00:00:00 UTC. */ |
| 85 | export function now() { |
nothing calls this directly
no test coverage detected