UNPKG

decent-sdk

Version:

SDK for the Decent API

174 lines (169 loc) 4.36 kB
// src/core/types/Chains.ts var SUPPORTED_CHAIN_IDS = [ 1, 10, 137, 8453, 11155111 ]; // src/core/fetch/common/constants.ts var DEFAULT_API_URL = "https://api.decent.build"; // src/core/fetch/common/generic.ts var SessionIdKeyInLocalStorage = "decent-sessionId"; var genericFetchAndThrowIfError = async (params) => { const { route, options = {}, apiUrl = DEFAULT_API_URL } = params; const sessionId = localStorage.getItem(SessionIdKeyInLocalStorage); const defaultOptions = { credentials: "include", ...options, headers: { ...sessionId ? { Authorization: `Bearer ${sessionId}` } : {}, ...options.headers } }; const url = `${apiUrl}${route ? `/${route}` : ""}`; const response = await fetch(url, defaultOptions); const json = await response.json(); if (!json.success) { throw new Error(json.error?.message || "Unknown API error"); } return json.data; }; // src/core/fetch/common/routes.ts var routes = { meta: "", health: "health", chains: "chains", auth: "auth", dao: "d", proposal: (chainId, address) => { return `${routes.dao}/${chainId}/${address}/proposals`; }, comment: (chainId, address, slug) => { return `${routes.dao}/${chainId}/${address}/proposals/${slug}/comments`; } }; // src/core/fetch/auth.ts var getNonce = async (params) => { const { apiUrl } = params ?? {}; const response = await genericFetchAndThrowIfError({ route: `${routes.auth}/nonce`, apiUrl }); localStorage.setItem(SessionIdKeyInLocalStorage, response.sessionId); return response.nonce; }; var verify = async (params) => { const { message, signature, apiUrl } = params; const response = await genericFetchAndThrowIfError({ route: `${routes.auth}/verify`, options: { method: "POST", body: JSON.stringify({ message, signature }) }, apiUrl }); return response; }; var me = async (params) => { const { apiUrl } = params ?? {}; const response = await genericFetchAndThrowIfError({ route: `${routes.auth}/me`, apiUrl }); return response; }; var logout = async (params) => { const { apiUrl } = params ?? {}; const response = await genericFetchAndThrowIfError({ route: `${routes.auth}/logout`, options: { method: "POST" }, apiUrl }); return response; }; // src/core/fetch/dao.ts var getDao = async (params) => { const { chainId, address, apiUrl } = params; const dao = await genericFetchAndThrowIfError({ route: `${routes.dao}/${chainId}/${address}`, apiUrl }); return dao; }; var getAllDaos = async (params) => { const { chainId, apiUrl } = params ?? {}; const daos = await genericFetchAndThrowIfError({ route: `${routes.dao}${chainId ? `${chainId}` : ""}`, apiUrl }); return daos; }; var getDaoPermissions = async (params) => { const { chainId, address, apiUrl } = params; const permissions = await genericFetchAndThrowIfError({ route: `${routes.dao}/${chainId}/${address}/me`, apiUrl }); return permissions; }; // src/core/fetch/meta.ts var apiInfo = async (params) => { const { apiUrl } = params ?? {}; const response = await genericFetchAndThrowIfError({ route: `${routes.meta}`, apiUrl }); return response; }; var apiHealth = async (params) => { const { apiUrl } = params ?? {}; const response = await genericFetchAndThrowIfError({ route: routes.health, apiUrl }); return response; }; var apiChains = async (params) => { const { apiUrl } = params ?? {}; const response = await genericFetchAndThrowIfError({ route: routes.chains, apiUrl }); return response; }; // src/core/fetch/proposal.ts var getAllProposals = async (params) => { const { chainId, address, apiUrl } = params; const proposals = await genericFetchAndThrowIfError({ route: routes.proposal(chainId, address), apiUrl }); return proposals; }; var getProposal = async (params) => { const { chainId, address, slug, apiUrl } = params; const proposal = await genericFetchAndThrowIfError({ route: `${routes.proposal(chainId, address)}/${slug}`, apiUrl }); return proposal; }; export { verify, me, logout, getProposal, getNonce, getDaoPermissions, getDao, getAllProposals, getAllDaos, apiInfo, apiHealth, apiChains, SUPPORTED_CHAIN_IDS }; //# debugId=0A6EB985EEF0CAA164756E2164756E21