update
This commit is contained in:
15
packages/util/package.json
Normal file
15
packages/util/package.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "@repo/util",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"exports": {
|
||||
"./*": "./src/*.ts"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@repo/auth": "*",
|
||||
"@types/node": "^24.9.2",
|
||||
"@types/react": "^19.2.2",
|
||||
"@types/react-dom": "^19.2.2",
|
||||
"typescript": "^5.9.3"
|
||||
}
|
||||
}
|
||||
34
packages/util/src/meili.ts
Normal file
34
packages/util/src/meili.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { Meilisearch, type SearchResponse } from 'meilisearch'
|
||||
|
||||
const MAX_HITS_PER_PAGE = 100
|
||||
|
||||
export async function createSearch({
|
||||
query,
|
||||
filter = undefined,
|
||||
index,
|
||||
page,
|
||||
hitsPerPage = 25,
|
||||
sort,
|
||||
env
|
||||
}: {
|
||||
query?: string
|
||||
filter?: string
|
||||
index: string
|
||||
page?: number
|
||||
hitsPerPage?: number
|
||||
sort: string[]
|
||||
env: any
|
||||
}): Promise<SearchResponse> {
|
||||
const host = env.MEILI_HOST
|
||||
const apiKey = env.MEILI_API_KEY
|
||||
const client = new Meilisearch({ host, apiKey })
|
||||
const index_ = client.index(index)
|
||||
|
||||
return index_.search(query, {
|
||||
sort,
|
||||
filter,
|
||||
page,
|
||||
hitsPerPage:
|
||||
hitsPerPage > MAX_HITS_PER_PAGE ? MAX_HITS_PER_PAGE : hitsPerPage
|
||||
})
|
||||
}
|
||||
49
packages/util/src/request.ts
Normal file
49
packages/util/src/request.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
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({
|
||||
Authorization: `Bearer ${user.accessToken}`
|
||||
})
|
||||
|
||||
if (_headers instanceof Headers) {
|
||||
_headers.forEach((value, key) => headers.set(key, value))
|
||||
} else {
|
||||
Object.entries(_headers).forEach(([key, value]) => headers.set(key, value))
|
||||
}
|
||||
|
||||
console.log(
|
||||
`[${new Date().toISOString()}] [${requestId}] ${method} ${url_.toString()}`
|
||||
)
|
||||
|
||||
return fetch(url_.toString(), { method, headers, body, signal })
|
||||
}
|
||||
11
packages/util/tsconfig.json
Normal file
11
packages/util/tsconfig.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"moduleResolution": "bundler",
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"@/*": ["./src/*"]
|
||||
}
|
||||
},
|
||||
"include": ["src"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
Reference in New Issue
Block a user