add user
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import type { LoaderFunctionArgs } from 'react-router'
|
||||
|
||||
import type { User } from '@repo/auth/auth'
|
||||
import { userContext } from '@repo/auth/context'
|
||||
import { requestIdContext, userContext } from '@repo/auth/context'
|
||||
|
||||
import type { LoaderFunctionArgs } from 'react-router'
|
||||
|
||||
export enum HttpMethod {
|
||||
GET = 'GET',
|
||||
@@ -23,23 +23,21 @@ type RequestArgs = {
|
||||
export function request({
|
||||
url,
|
||||
method = HttpMethod.GET,
|
||||
headers: _headers = {},
|
||||
body = null,
|
||||
headers: _headers = {},
|
||||
request: { signal },
|
||||
context
|
||||
}: RequestArgs): Promise<Response> {
|
||||
const requestId = context.get(requestIdContext) as string
|
||||
const user = context.get(userContext) as User
|
||||
// @ts-ignore
|
||||
const url_ = new URL(url, context.cloudflare.env.API_URL)
|
||||
const headers = new Headers(
|
||||
Object.assign(_headers, { Authorization: `Bearer ${user.accessToken}` })
|
||||
Object.assign({ Authorization: `Bearer ${user.accessToken}` }, _headers)
|
||||
)
|
||||
// @ts-ignore
|
||||
const endpoint = new URL(url, context.cloudflare.env.API_URL)
|
||||
|
||||
return fetch(endpoint.toString(), {
|
||||
method,
|
||||
headers,
|
||||
body,
|
||||
signal
|
||||
})
|
||||
console.log(
|
||||
`[${new Date().toISOString()}] [${requestId}] ${method} ${url_.toString()}`
|
||||
)
|
||||
|
||||
return fetch(url_.toString(), { method, headers, body, signal })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user