42 lines
1.0 KiB
TypeScript
42 lines
1.0 KiB
TypeScript
import type { User } from '@repo/auth/auth'
|
|
import { requestIdContext, userContext } from '@repo/auth/context'
|
|
|
|
import type { LoaderFunctionArgs } from 'react-router'
|
|
|
|
export enum HttpMethod {
|
|
GET = 'GET',
|
|
POST = 'POST',
|
|
PUT = 'PUT',
|
|
PATCH = 'PATCH',
|
|
DELETE = 'DELETE'
|
|
}
|
|
|
|
type RequestArgs = {
|
|
url: string
|
|
method?: HttpMethod
|
|
headers?: HeadersInit
|
|
body?: BodyInit | null
|
|
request: LoaderFunctionArgs['request']
|
|
context: LoaderFunctionArgs['context']
|
|
}
|
|
|
|
export function request({
|
|
url,
|
|
method = HttpMethod.GET,
|
|
body = null,
|
|
headers: _headers = {},
|
|
request: { signal },
|
|
context
|
|
}: RequestArgs): Promise<Response> {
|
|
const requestId = context.get(requestIdContext) as string
|
|
const user = context.get(userContext) as User
|
|
const url_ = new URL(url, context.cloudflare.env.API_URL)
|
|
const headers = new Headers(
|
|
Object.assign({ Authorization: `Bearer ${user.accessToken}` }, _headers)
|
|
)
|
|
|
|
console.log(`[${requestId}] ${method} ${url_.toString()}`)
|
|
|
|
return fetch(url_.toString(), { method, headers, body, signal })
|
|
}
|