You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
1.1 KiB
JavaScript
36 lines
1.1 KiB
JavaScript
export function createAdminInstanceTransport({
|
|
taskUrl,
|
|
fetchImpl = globalThis.fetch?.bind(globalThis),
|
|
locationImpl = globalThis.location,
|
|
csrfToken = () => globalThis.ADMIN_CSRF_TOKEN || '',
|
|
timeoutMs = 30000,
|
|
runtimeFactory = globalThis.VenixAdminRequestRuntime?.create,
|
|
} = {}) {
|
|
if (typeof taskUrl !== 'function') throw new TypeError('taskUrl is required');
|
|
if (typeof runtimeFactory !== 'function') {
|
|
throw new TypeError('administrator request runtime is unavailable');
|
|
}
|
|
|
|
const runtime = runtimeFactory({
|
|
fetchImpl, locationImpl, csrfToken, taskReceiptValidator: taskUrl,
|
|
});
|
|
|
|
function read(path, controller) {
|
|
return runtime.read(path, { signal: controller.signal, timeoutMs });
|
|
}
|
|
|
|
function mutate(path, body, controller, method = 'PATCH') {
|
|
return runtime.mutate(path, {
|
|
method, body, signal: controller.signal, timeoutMs, response: 'task',
|
|
});
|
|
}
|
|
|
|
function mutateJson(path, body, controller, method = 'POST') {
|
|
return runtime.mutate(path, {
|
|
method, body, signal: controller.signal, timeoutMs, response: 'json',
|
|
});
|
|
}
|
|
|
|
return Object.freeze({ read, mutate, mutateJson });
|
|
}
|