UNPKG

alinea

Version:

[![npm](https://img.shields.io/npm/v/alinea.svg)](https://npmjs.org/package/alinea) [![install size](https://packagephobia.com/badge?p=alinea)](https://packagephobia.com/result?p=alinea)

143 lines (141 loc) 3.67 kB
import "../../chunks/chunk-U5RRZUYZ.js"; // src/cloud/server/CloudHandler.ts import { Handler, JWTPreviews } from "alinea/backend"; import { HttpError } from "alinea/core"; import { Outcome } from "alinea/core/Outcome"; import { base64 } from "alinea/core/util/Encoding"; import { CloudAuthServer } from "./CloudAuthServer.js"; import { cloudConfig } from "./CloudConfig.js"; async function failOnHttpError(res) { if (res.status >= 400) throw new HttpError(res.status, await res.text()); return res; } function json(res) { return res.json(); } function withAuth(ctx, init = {}) { return { ...init, headers: { ...init.headers, authorization: `Bearer ${ctx.token}` } }; } function asJson(init = {}) { return { ...init, headers: { ...init.headers, "content-type": "application/json", accept: "application/json" } }; } var CloudApi = class { constructor(config) { this.config = config; } mutate(params, ctx) { return fetch( cloudConfig.mutate, withAuth( ctx, asJson({ method: "POST", body: JSON.stringify(params) }) ) ).then(failOnHttpError).then(json).then(Outcome.fromJSON).then(Outcome.unpack); } prepareUpload(file, ctx) { return fetch( cloudConfig.upload, withAuth( ctx, asJson({ method: "POST", body: JSON.stringify({ filename: file }) }) ) ).then(failOnHttpError).then(json).then(Outcome.fromJSON).then(Outcome.unpack); } revisions(file, ctx) { return fetch( cloudConfig.history + "?" + new URLSearchParams({ file }), withAuth(ctx) ).then(failOnHttpError).then(json).then(Outcome.fromJSON).then(Outcome.unpack); } revisionData(file, ref, ctx) { return fetch( cloudConfig.history + "?" + new URLSearchParams({ file, ref }), withAuth(ctx) ).then(failOnHttpError).then(json).then(Outcome.fromJSON).then(Outcome.unpack); } pendingSince(commitHash, ctx) { return fetch( cloudConfig.pending + "?" + new URLSearchParams({ since: commitHash }), withAuth(ctx) ).then(failOnHttpError).then( json ).then( Outcome.fromJSON ).then(Outcome.unpack).then((pending) => { if (pending.length === 0) return void 0; return { toCommitHash: pending[pending.length - 1].commitHashTo, mutations: pending.flatMap( (mutate) => mutate.mutations.flatMap((m) => m.meta) ) }; }); } storeDraft(draft, ctx) { const body = { fileHash: draft.fileHash, update: base64.stringify(draft.draft) }; return fetch( cloudConfig.drafts + "/" + draft.entryId, withAuth( ctx, asJson({ method: "PUT", body: JSON.stringify(body) }) ) ).then(failOnHttpError).then(() => void 0); } getDraft(entryId, ctx) { return fetch(cloudConfig.drafts + "/" + entryId, withAuth(ctx)).then(failOnHttpError).then(json).then( Outcome.fromJSON ).then(Outcome.unpack).then((data) => { return data ? { entryId, fileHash: data.fileHash, draft: base64.parse(data.update) } : void 0; }); } }; function createCloudHandler(config, db, apiKey) { const api = apiKey ? new CloudApi(config) : void 0; return new Handler({ auth: new CloudAuthServer({ config, apiKey }), db, config, target: api, media: api, history: api, pending: api, drafts: api, previews: new JWTPreviews(apiKey), previewAuthToken: apiKey }); } export { CloudApi, createCloudHandler };