add other projects
This commit is contained in:
40
apps/admin.saladeaula.digital/app/lib/request.ts
Normal file
40
apps/admin.saladeaula.digital/app/lib/request.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { requestIdContext, userContext } from '@/context'
|
||||
import type { User } from '@/lib/auth'
|
||||
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 })
|
||||
}
|
||||
Reference in New Issue
Block a user