chore: scaffold turbo monorepo with hono api and react admin web
This commit is contained in:
36
apps/web/src/lib/api-client.test.ts
Normal file
36
apps/web/src/lib/api-client.test.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { ApiError, readProblem } from './api-client'
|
||||
|
||||
describe('readProblem', () => {
|
||||
it('parses a problem+json body', async () => {
|
||||
const response = new Response(
|
||||
JSON.stringify({ type: 'about:blank', title: 'Not Found', status: 404 }),
|
||||
{ status: 404, headers: { 'content-type': 'application/problem+json' } },
|
||||
)
|
||||
|
||||
await expect(readProblem(response)).resolves.toEqual({
|
||||
type: 'about:blank',
|
||||
title: 'Not Found',
|
||||
status: 404,
|
||||
})
|
||||
})
|
||||
|
||||
it('falls back to a generic problem for other content types', async () => {
|
||||
const response = new Response('<html>nope</html>', { status: 502 })
|
||||
|
||||
await expect(readProblem(response)).resolves.toMatchObject({
|
||||
type: 'about:blank',
|
||||
status: 502,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('ApiError', () => {
|
||||
it('exposes the problem payload', () => {
|
||||
const error = new ApiError({ type: 'about:blank', title: 'Boom', status: 500 })
|
||||
|
||||
expect(error.message).toBe('Boom')
|
||||
expect(error.problem.status).toBe(500)
|
||||
})
|
||||
})
|
||||
55
apps/web/src/lib/api-client.ts
Normal file
55
apps/web/src/lib/api-client.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { PROBLEM_CONTENT_TYPE, type ProblemDetails } from '@superlist/shared'
|
||||
|
||||
const baseUrl = import.meta.env.VITE_API_URL ?? 'http://localhost:3000'
|
||||
|
||||
/** Error thrown when the API answers with a RFC 9457 Problem Details payload. */
|
||||
export class ApiError extends Error {
|
||||
readonly problem: ProblemDetails
|
||||
|
||||
constructor(problem: ProblemDetails) {
|
||||
super(problem.title)
|
||||
this.name = 'ApiError'
|
||||
this.problem = problem
|
||||
}
|
||||
}
|
||||
|
||||
/** Reads a Problem Details body, falling back to a generic problem. */
|
||||
export async function readProblem(response: Response): Promise<ProblemDetails> {
|
||||
const contentType = response.headers.get('content-type') ?? ''
|
||||
|
||||
if (contentType.includes(PROBLEM_CONTENT_TYPE)) {
|
||||
try {
|
||||
return (await response.json()) as ProblemDetails
|
||||
} catch {
|
||||
// fall through to the generic problem below
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
type: 'about:blank',
|
||||
title: response.statusText || 'Request failed',
|
||||
status: response.status,
|
||||
}
|
||||
}
|
||||
|
||||
/** Thin `fetch` wrapper: JSON by default, sessions enabled, problems as errors. */
|
||||
export async function apiFetch<T>(path: string, init: RequestInit = {}): Promise<T> {
|
||||
const response = await fetch(`${baseUrl}${path}`, {
|
||||
credentials: 'include',
|
||||
...init,
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
...init.headers,
|
||||
},
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
throw new ApiError(await readProblem(response))
|
||||
}
|
||||
|
||||
if (response.status === 204) {
|
||||
return undefined as T
|
||||
}
|
||||
|
||||
return (await response.json()) as T
|
||||
}
|
||||
11
apps/web/src/lib/auth-client.ts
Normal file
11
apps/web/src/lib/auth-client.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { createAuthClient } from 'better-auth/react'
|
||||
|
||||
/**
|
||||
* BetterAuth browser client. `baseURL` points at the Hono API, which mounts
|
||||
* BetterAuth under `/api/auth/*`.
|
||||
*/
|
||||
export const authClient = createAuthClient({
|
||||
baseURL: import.meta.env.VITE_API_URL ?? 'http://localhost:3000',
|
||||
})
|
||||
|
||||
export const { signIn, signUp, signOut, useSession } = authClient
|
||||
18
apps/web/src/lib/query-client.ts
Normal file
18
apps/web/src/lib/query-client.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { QueryClient } from '@tanstack/react-query'
|
||||
|
||||
/** Context every route receives, so loaders can prefetch through the same cache. */
|
||||
export type RouterContext = {
|
||||
queryClient: QueryClient
|
||||
}
|
||||
|
||||
export function createQueryClient() {
|
||||
return new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: {
|
||||
staleTime: 30_000,
|
||||
retry: 1,
|
||||
refetchOnWindowFocus: false,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
1
apps/web/src/lib/utils.ts
Normal file
1
apps/web/src/lib/utils.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { cn } from "cn"
|
||||
Reference in New Issue
Block a user