alinea
Version:
[](https://npmjs.org/package/alinea) [](https://packagephobia.com/result?p=alinea)
140 lines (138 loc) • 4.33 kB
JavaScript
import "../chunks/chunk-U5RRZUYZ.js";
// src/core/Client.ts
import { AbortController, fetch } from "@alinea/iso";
import {
Connection,
HttpError
} from "alinea/core";
import { base64 } from "./util/Encoding.js";
async function failOnHttpError(res, expectJson = true) {
if (res.ok)
return expectJson ? res.json() : void 0;
const text = await res.text();
throw new HttpError(res.status, text || res.statusText);
}
var Client = class _Client {
constructor(options) {
this.options = options;
}
previewToken() {
return this.requestJson(Connection.routes.previewToken()).then(
failOnHttpError
);
}
prepareUpload(file) {
return this.requestJson(Connection.routes.prepareUpload(), {
method: "POST",
body: JSON.stringify({ filename: file })
}).then(failOnHttpError);
}
resolve(params) {
const { resolveDefaults } = this.options;
const body = JSON.stringify({ ...resolveDefaults, ...params });
return this.requestJson(Connection.routes.resolve(), {
method: "POST",
body
}).then(failOnHttpError);
}
mutate(mutations) {
return this.requestJson(Connection.routes.mutate(), {
method: "POST",
body: JSON.stringify(mutations)
}).then(failOnHttpError);
}
authenticate(applyAuth, unauthorized) {
return new _Client({ ...this.options, applyAuth, unauthorized });
}
// History
revisions(file) {
const params = new URLSearchParams({ file });
return this.requestJson(
Connection.routes.revisions() + "?" + params.toString()
).then(failOnHttpError);
}
revisionData(file, revisionId) {
const params = new URLSearchParams({ file, revisionId });
return this.requestJson(
Connection.routes.revisions() + "?" + params.toString()
).then(failOnHttpError);
}
// Syncable
syncRequired(contentHash) {
const params = new URLSearchParams({ contentHash });
return this.requestJson(
Connection.routes.sync() + "?" + params.toString()
).then(failOnHttpError);
}
sync(contentHashes) {
return this.requestJson(Connection.routes.sync(), {
method: "POST",
body: JSON.stringify(contentHashes)
}).then(failOnHttpError);
}
// Drafts
getDraft(entryId) {
const params = new URLSearchParams({ entryId });
return this.requestJson(Connection.routes.draft() + "?" + params.toString()).then(failOnHttpError).then(
(draft) => draft ? { ...draft, draft: new Uint8Array(base64.parse(draft.draft)) } : void 0
);
}
storeDraft(draft) {
return this.requestJson(Connection.routes.draft(), {
method: "POST",
body: JSON.stringify({ ...draft, draft: base64.stringify(draft.draft) })
}).then((res) => failOnHttpError(res, false));
}
request(endpoint, init) {
const { url, applyAuth = (v) => v, unauthorized } = this.options;
const controller = new AbortController();
const signal = controller.signal;
const location = url.endsWith("/") && endpoint.startsWith("/") ? url + endpoint.slice(1) : url + endpoint;
const promise = fetch(location, {
...applyAuth(init),
signal
}).catch((err) => {
throw new HttpError(
500,
`\u274C ${init?.method || "GET"} "${endpoint}": ${err}`
);
}).then(async (res) => {
if (res.status === 401)
unauthorized?.();
if (!res.ok) {
const isJson = res.headers.get("content-type")?.includes("application/json");
const msg = isJson ? JSON.stringify(await res.json(), null, 2) : await res.text();
throw new HttpError(
res.status,
`\u274C ${init?.method || "GET"} "${endpoint}" (${res.status})
${msg}`
);
}
return res;
});
const cancel = () => controller.abort();
function cancelify(promise2) {
const t = promise2.then.bind(promise2);
const c = promise2.catch.bind(promise2);
return Object.assign(promise2, {
cancel,
then: (...args) => cancelify(t(...args)),
catch: (...args) => cancelify(c(...args))
});
}
return cancelify(promise);
}
requestJson(endpoint, init) {
return this.request(endpoint, {
...init,
headers: {
...init?.headers,
"content-type": "application/json",
accept: "application/json"
}
});
}
};
export {
Client
};