UNPKG

@brizy/cloud-media-upload

Version:
122 lines (121 loc) 5.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ApiClient = void 0; exports.fetchCredentials = fetchCredentials; const constants_1 = require("../constants"); const utils_1 = require("../utils"); const validation_1 = require("../utils/validation"); class ApiClient { constructor({ token, projectId }) { this.getItems = async ({ fileName, name, type, page, count, orderBy, }) => { const rType = (0, utils_1.fromValToMediaGalleryParam)("type", type); const rPage = (0, utils_1.fromValToMediaGalleryParam)("page", page.toString()); const rCount = (0, utils_1.fromValToMediaGalleryParam)("count", count.toString()); const rFilename = (0, utils_1.fromValToMediaGalleryParam)("filename", fileName); const rName = (0, utils_1.fromValToMediaGalleryParam)("name", name); const orderByParam = orderBy.split("="); const rOrderBy = (0, utils_1.fromValToMediaGalleryParam)(orderByParam[0], orderByParam[1]); const queryParams = { ...rCount, ...rType, ...rFilename, ...rName, ...rPage, ...rOrderBy, }; try { const response = await this.request({ method: "GET", }, "", queryParams); if (response.ok) { return await response.json(); } } catch (e) { throw new Error(`Failed to getItems: ${e}`); } }; this.removeItems = async (elIds) => { try { const response = await this.request({ body: JSON.stringify({ id_list: elIds.join(",") }), method: "DELETE", }); if (response.ok) { return elIds; } throw new Error(response.statusText); } catch (e) { throw new Error(`Failed to remove media Gallery: ${e}`); } }; this.updateItem = async ({ id, fileNameWithExtension, title }) => { const filename = (0, utils_1.fromValToMediaGalleryParam)("filename", fileNameWithExtension); const alt_title = (0, utils_1.fromValToMediaGalleryParam)("alt_title", title); try { const response = await this.request({ body: JSON.stringify({ ...filename, ...alt_title }), method: "PATCH", }, `/${id}`); if (response.ok) { return await response.json(); } throw new Error(response.statusText); } catch (e) { throw new Error(`Failed to update: ${e}`); } }; this.uploadItem = async ({ file, altTitle, name }) => { const base64 = await (0, utils_1.getBase64)(file); const mime = file.type.split("/")[0]; const type = (0, utils_1.getTypeOrDocument)(mime); const attachment = base64.replace(/data:[a-zA-Z]*\/.+;base64,/, ""); const rAltTitle = (0, utils_1.fromValToMediaGalleryParam)("alt_title", altTitle); const rName = (0, utils_1.fromValToMediaGalleryParam)("name", name); try { const response = await this.request({ headers: { "Content-Type": "application/x-www-form-urlencoded", }, body: new URLSearchParams({ ...rAltTitle, ...rName, ...(type ? { type } : {}), attachment, filename: file.name, }), method: "POST", }); if (response.ok) { return await response.json(); } throw new Error(response.statusText); } catch (e) { throw new Error(`Failed to upload: ${e}`); } }; this.request = (parameters, url = "", queryParams = {}) => { const fetchUrl = (0, utils_1.makeUrl)(`${constants_1.API_URL}/${projectId}/media${url}`, queryParams); return fetch(fetchUrl, { ...parameters, headers: { "Content-Type": "application/json", "x-auth-user-token": token, ...parameters.headers, }, }); }; } } exports.ApiClient = ApiClient; async function fetchCredentials(url, clientId) { const response = await fetch(`${url}?client_id=${clientId}`); if (!response.ok) { throw new Error("Failed to fetch credentials..."); } const data = await response.json(); return validation_1.credentialSchema.parse(data); }