Initial commit: Add project files
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
import axios from 'axios'
|
||||
import { ApiProblem, type ProblemDetails } from './problem'
|
||||
|
||||
export const api = axios.create({ baseURL: '/api', timeout: 15_000 })
|
||||
|
||||
api.interceptors.request.use(config => {
|
||||
const user = import.meta.env.VITE_DEV_AUTH_USER as string | undefined
|
||||
const role = import.meta.env.VITE_DEV_AUTH_ROLE as string | undefined
|
||||
if (import.meta.env.DEV && user && role) {
|
||||
config.headers['X-KArtSell-User'] = user
|
||||
config.headers['X-KArtSell-Role'] = role
|
||||
}
|
||||
return config
|
||||
})
|
||||
|
||||
api.interceptors.response.use(
|
||||
response => response,
|
||||
error => {
|
||||
const data = error.response?.data as ProblemDetails | undefined
|
||||
if (data?.status) throw new ApiProblem(data)
|
||||
throw error
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,16 @@
|
||||
export interface ProblemDetails {
|
||||
type?: string
|
||||
title: string
|
||||
status: number
|
||||
detail?: string
|
||||
traceId?: string
|
||||
errors?: Record<string, string[]>
|
||||
}
|
||||
|
||||
export class ApiProblem extends Error {
|
||||
constructor(public readonly problem: ProblemDetails) {
|
||||
super(problem.title)
|
||||
}
|
||||
|
||||
get status(): number { return this.problem.status }
|
||||
}
|
||||
Reference in New Issue
Block a user