32 lines
680 B
TypeScript
32 lines
680 B
TypeScript
|
|
export type DevtoolAction = "compile" | "extract"
|
||
|
|
|
||
|
|
interface ApiError {
|
||
|
|
error?: string
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function requestDevtoolJson<T>(
|
||
|
|
url: string,
|
||
|
|
init?: RequestInit
|
||
|
|
): Promise<T> {
|
||
|
|
const response = await fetch(url, init)
|
||
|
|
const body = (await response.json()) as T & ApiError
|
||
|
|
|
||
|
|
if (!response.ok) {
|
||
|
|
throw new Error(body.error ?? `Request failed with ${response.status}.`)
|
||
|
|
}
|
||
|
|
|
||
|
|
return body
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function runDevtoolAction(
|
||
|
|
action: DevtoolAction,
|
||
|
|
baseUrl = "/__i18n"
|
||
|
|
) {
|
||
|
|
const normalizedBaseUrl = baseUrl.replace(/\/+$/, "")
|
||
|
|
|
||
|
|
return requestDevtoolJson<{ output: string }>(
|
||
|
|
`${normalizedBaseUrl}/actions/${action}`,
|
||
|
|
{ method: "POST" }
|
||
|
|
)
|
||
|
|
}
|