Files
wehub-resource-sync 070959e133
landing-page-staging / Deploy landing page to staging (push) Has been skipped
landing-page-ci / Validate landing page (push) Failing after 4s
visual-baseline / Capture visual baselines (push) Has been cancelled
bake-plugin-previews / Bake plugin previews (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:00:47 +08:00

35 lines
1.2 KiB
TypeScript

export type JsonRequestOptions = {
body?: unknown;
headers?: Record<string, string>;
method?: string;
};
export async function requestJson<T>(baseUrl: string, path: string, options: JsonRequestOptions = {}): Promise<T> {
const response = await fetch(new URL(path, ensureTrailingSlash(baseUrl)), {
headers: {
...(options.body === undefined ? {} : { 'content-type': 'application/json' }),
...options.headers,
},
method: options.method ?? (options.body === undefined ? 'GET' : 'POST'),
...(options.body === undefined ? {} : { body: JSON.stringify(options.body) }),
});
const text = await response.text();
if (!response.ok) {
throw new Error(`HTTP ${response.status} ${path}: ${text.slice(0, 500)}`);
}
return (text ? JSON.parse(text) : null) as T;
}
export async function requestText(baseUrl: string, path: string): Promise<string> {
const response = await fetch(new URL(path, ensureTrailingSlash(baseUrl)));
const text = await response.text();
if (!response.ok) {
throw new Error(`HTTP ${response.status} ${path}: ${text.slice(0, 500)}`);
}
return text;
}
function ensureTrailingSlash(value: string): string {
return value.endsWith('/') ? value : `${value}/`;
}