36 lines
1.4 KiB
JavaScript
36 lines
1.4 KiB
JavaScript
// Single source of truth for talking to the Postiz public API. One factory,
|
|
// built from the plugin's saved { baseUrl, apiKey }. Self-hosted vs cloud is
|
|
// purely the baseUrl -- no other code in the plugin branches on deployment.
|
|
|
|
const { PATH_BASE, ENDPOINTS, AUTH_HEADER } = require("./constants");
|
|
|
|
|
|
const makePostizClient = (pluginCfg) => {
|
|
const baseUrl = String((pluginCfg && pluginCfg.baseUrl) || "").replace(/\/+$/, "");
|
|
const apiKey = String((pluginCfg && pluginCfg.apiKey) || "");
|
|
|
|
const request = async (method, endpoint, body) => {
|
|
const url = `${baseUrl}${PATH_BASE}${endpoint}`;
|
|
const headers = { [AUTH_HEADER]: apiKey };
|
|
const init = { method, headers };
|
|
if (body !== undefined) {
|
|
headers["Content-Type"] = "application/json";
|
|
init.body = JSON.stringify(body);
|
|
}
|
|
const res = await fetch(url, init);
|
|
if (!res.ok) {
|
|
const text = await res.text();
|
|
throw new Error(`Postiz ${method} ${endpoint} -> ${res.status} ${text}`);
|
|
}
|
|
return res.status === 204 ? null : res.json();
|
|
};
|
|
|
|
return {
|
|
createPost: (payload) => request("POST", ENDPOINTS.POSTS, payload),
|
|
listIntegrations: () => request("GET", ENDPOINTS.INTEGRATIONS),
|
|
uploadMedia: (payload) => request("POST", ENDPOINTS.UPLOAD, payload)
|
|
};
|
|
};
|
|
|
|
|
|
module.exports = { makePostizClient };
|