UNPKG

azion

Version:

Azion Packages for Edge Computing.

380 lines (379 loc) 11 kB
const o = (a) => a ?? process.env.AZION_TOKEN ?? "", l = (a) => { const t = process.env.AZION_DEBUG && process.env.AZION_DEBUG === "true"; return typeof a > "u" || a === null ? !!t : !!a; }; async function d(a, t, r, e = !0) { const i = await fetch(a, t); if (!i.ok) { const s = `HTTP error! Status: ${i.status} - ${i.statusText}`; throw r && console.log(`Error in fetch: ${s}`), new Error(s); } if (e) { const s = i.headers.get("content-type"); if (!s || !s.includes("application/json")) { const _ = `Expected JSON response, but got: ${await i.text()}`; throw r && console.log(`Error in fetch: ${_}`), new Error(_); } return await i.json(); } else return await i.text(); } const u = process.env.AZION_ENV === "stage" ? "https://stage-api.azion.net/domains" : "https://api.azionapi.net/domains", m = (a, t) => { let r; return a.forEach((e) => { t[e] && (r = Array.isArray(t[e]) ? t[e].join(", ") : t[e]); }), r ?? JSON.stringify(t); }, g = (a) => ({ Authorization: `Token ${a}`, Accept: "application/json; version=3", "Content-Type": "application/json" }), h = async (a, t, { debug: r }) => { try { let e = { name: t.name, cnames: t?.cnames ?? [], cname_access_only: t?.cnameAccessOnly ?? !1, digital_certificate_id: t?.digitalCertificateId ?? void 0, edge_application_id: t.edgeApplicationId, edge_firewall_id: t?.edgeFirewallId ?? void 0, is_active: !0 }; if (t?.mtls) { if (t.mtls.verification !== "enforce" && t.mtls.verification !== "permissive") throw new Error("mtls.verification must be enforce or permissive"); e = { ...e, is_mtls_enabled: !0, mtls_verification: t.mtls.verification, mtls_trusted_ca_certificate_id: t.mtls.trustedCaCertificateId, crl_list: t.mtls.crlList }; } const i = await d( u, { method: "POST", headers: g(a), body: JSON.stringify(e) }, r ); return i.results ? i : { error: { message: m(["detail", "edge_application_id"], i), operation: "create domain" } }; } catch (e) { return r && console.error("Error creating Domain:", e), { error: { message: e.toString(), operation: "create domain" } }; } }, b = async (a, t, r) => { try { const { page_size: e = 10, page: i = 1 } = r || {}, s = new URLSearchParams({ page_size: String(e), page: String(i) }), c = await d( `${u}?${s.toString()}`, { method: "GET", headers: g(a) }, t?.debug ); return c.results ? { ...c, results: c.results.map((n) => ({ id: n.id, name: n.name, url: n.domain_name, environment: n.environment, active: n.is_active, edgeApplicationId: n.edge_application_id, cnameAccessOnly: n.cname_access_only, digitalCertificateId: n.digital_certificate_id, edgeFirewallId: n?.edge_firewall_id, cnames: n.cnames, mtls: n.is_mtls_enabled ? { verification: n.mtls_verification, trustedCaCertificateId: n.mtls_trusted_ca_certificate_id, crlList: n.crl_list } : void 0 })) } : { error: { message: m(["detail", "edge_application_id"], c), operation: "list domains" } }; } catch (e) { return t?.debug && console.error("Error listing Domains:", e), { error: { message: e.toString(), operation: "list domains" } }; } }, w = async (a, t, r) => { try { const e = await d( `${u}/${t}`, { method: "GET", headers: g(a) }, r?.debug ); return e.results ? e : { error: { message: m(["detail", "edge_application_id"], e), operation: "get domain" } }; } catch (e) { return r?.debug && console.error("Error getting Domain:", e), { error: { message: e.toString(), operation: "get domain" } }; } }, I = async (a, t, r, e) => { try { let i = { name: r.name, cnames: r?.cnames ?? [], cname_access_only: r?.cnameAccessOnly ?? !1, digital_certificate_id: r?.digitalCertificateId ?? void 0, edge_application_id: r.edgeApplicationId, edge_firewall_id: r?.edgeFirewallId ?? void 0, is_active: r.active ?? !0 }; if (r?.mtls) { if (r.mtls.verification !== "enforce" && r.mtls.verification !== "permissive") throw new Error("mtls.verification must be enforce or permissive"); i = { ...i, is_mtls_enabled: !0, mtls_verification: r.mtls.verification, mtls_trusted_ca_certificate_id: r.mtls.trustedCaCertificateId, crl_list: r.mtls.crlList }; } const s = await d( `${u}/${t}`, { method: "PUT", headers: g(a), body: JSON.stringify(i) }, e?.debug ); return s.results ? s : { error: { message: m(["detail", "edge_application_id"], s), operation: "update domain" } }; } catch (i) { return e?.debug && console.error("Error updating Domain:", i), { error: { message: i.toString(), operation: "update domain" } }; } }, E = async (a, t, r) => { try { return await d( `${u}/${t}`, { method: "DELETE", headers: g(a) }, r?.debug, !1 ), { results: void 0 }; } catch (e) { return r?.debug && console.error("Error deleting Domain:", e), { error: { message: e.toString(), operation: "delete domain" } }; } }, p = async (a, t, r) => { if (t.name === void 0 || t.edgeApplicationId === void 0) return { error: { message: "Domain name and Edge Application ID are required", operation: "create domain" } }; const { results: e, error: i } = await h(o(a), t, { ...r, debug: l(r?.debug) }); return e && e.id ? { data: { state: "executed", id: e.id, name: e?.name, url: e?.domain_name, environment: e?.environment, active: e?.is_active } } : { error: i }; }, f = async (a, t, r) => { const e = await b(o(a), r, t); return e.results ? { data: { count: e.count ?? e.results.length, state: "executed", results: e.results, pages: e.total_pages ?? 1 } } : { error: e.error }; }, v = async (a, t, r) => { const { results: e, error: i } = await w(o(a), t, { ...r, debug: l(r?.debug) }); return e && e.id ? { data: { state: "executed", id: e.id, name: e?.name, url: e?.domain_name, environment: e?.environment, active: e?.is_active, cnameAccessOnly: e?.cname_access_only, digitalCertificateId: e?.digital_certificate_id, cnames: e?.cnames, edgeApplicationId: e?.edge_application_id, edgeFirewallId: e?.edge_firewall_id, mtls: e?.is_mtls_enabled ? { verification: e.mtls_verification, trustedCaCertificateId: e.mtls_trusted_ca_certificate_id, crlList: e.crl_list } : void 0 } } : { error: i }; }, y = async (a, t, r, e) => { if (r?.edgeApplicationId === void 0) return { error: { message: "Edge Application ID is required", operation: "update domain" } }; const i = await I(o(a), t, r, { ...e, debug: l(e?.debug) }); return i?.results?.id ? { data: { state: "executed", name: i?.results?.name, id: i?.results?.id, environment: i?.results?.environment, cnames: i?.results?.cnames, url: i?.results?.domain_name, active: i?.results?.is_active, cnameAccessOnly: i?.results?.cname_access_only, digitalCertificateId: i?.results?.digital_certificate_id, edgeApplicationId: i?.results?.edge_application_id, edgeFirewallId: i?.results?.edge_firewall_id, mtls: i?.results?.is_mtls_enabled ? { verification: i?.results.mtls_verification, trustedCaCertificateId: i?.results.mtls_trusted_ca_certificate_id, crlList: i?.results.crl_list } : void 0 } } : { error: i?.error }; }, D = async (a, t, r) => { const { error: e } = await E(o(a), t, { ...r, debug: l(r?.debug) }); return e ? { error: e } : { data: { state: "executed", id: t } }; }, A = async (a, t) => p(o(), a, t), C = async (a, t) => f(o(), t, a), S = async (a, t) => v(o(), a, t), O = async (a, t, r) => y(o(), a, t, r), $ = async (a, t) => D(o(), a, t), T = (a) => { const t = o(a?.token), r = l(a?.options?.debug); return { /** * Create a new domain * @param domain Domain to create * @param options Options to create the domain * @returns Domain created with data or error * * @example * const domain = { name: 'example.com', edgeApplicationId: 123 }; * const { data, error } = await client.createDomain(domain); * if(data) { * console.log(data); * } else { * console.error(error); * } */ createDomain: (i, s) => p(t, i, { ...s, debug: r }), /** * List domains * @param options Options to list the domains * @param queryParams Query parameters to list the domains * @returns List of domains with data or error * * @example * const { data, error } = await client.getDomains(); * if(data) { * console.log(data.results); * } else { * console.error(error); * } */ getDomains: (i, s) => f(t, s, { ...i, debug: r }), /** * Get a domain by ID * @param domainId Domain ID * @param options Options to get the domain * @returns Domain created with data or error * * @example * const { data, error } = await client.getDomain(123); * if(data) { * console.log(data); * } else { * console.error(error); * } */ getDomain: (i, s) => v(t, i, { ...s, debug: r }), /** * Update a domain * @param domainId Domain ID * @param domain Domain to update * @param options Options to update the domain * @returns Domain updated with data or error * * @example * const domain = { name: 'example.com', edgeApplicationId: 123 }; * const { data, error } = await client.updateDomain(123, domain); * if(data) { * console.log(data); * } else { * console.error(error); * } */ updateDomain: (i, s, c) => y(t, i, s, { ...c, debug: r }), /** * Delete a domain * @param domainId Domain ID * @param options Options to delete the domain * @returns Domain deleted with data or error * @example * const { data, error } = await client.deleteDomain(123); * if(data) { * console.log(data.id); * } else { * console.error(error); * } */ deleteDomain: (i, s) => D(t, i, { ...s, debug: r }) }; }; export { T as createClient, A as createDomain, T as default, $ as deleteDomain, S as getDomain, C as getDomains, O as updateDomain };