decent-sdk
Version:
SDK for the Decent API
219 lines (212 loc) • 6.16 kB
JavaScript
var __create = Object.create;
var __getProtoOf = Object.getPrototypeOf;
var __defProp = Object.defineProperty;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __toESM = (mod, isNodeMode, target) => {
target = mod != null ? __create(__getProtoOf(mod)) : {};
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
for (let key of __getOwnPropNames(mod))
if (!__hasOwnProp.call(to, key))
__defProp(to, key, {
get: () => mod[key],
enumerable: true
});
return to;
};
var __moduleCache = /* @__PURE__ */ new WeakMap;
var __toCommonJS = (from) => {
var entry = __moduleCache.get(from), desc;
if (entry)
return entry;
entry = __defProp({}, "__esModule", { value: true });
if (from && typeof from === "object" || typeof from === "function")
__getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
get: () => from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
}));
__moduleCache.set(from, entry);
return entry;
};
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, {
get: all[name],
enumerable: true,
configurable: true,
set: (newValue) => all[name] = () => newValue
});
};
// src/core/index.ts
var exports_core = {};
__export(exports_core, {
verify: () => verify,
me: () => me,
logout: () => logout,
getProposal: () => getProposal,
getNonce: () => getNonce,
getDaoPermissions: () => getDaoPermissions,
getDao: () => getDao,
getAllProposals: () => getAllProposals,
getAllDaos: () => getAllDaos,
apiInfo: () => apiInfo,
apiHealth: () => apiHealth,
apiChains: () => apiChains,
SUPPORTED_CHAIN_IDS: () => SUPPORTED_CHAIN_IDS
});
module.exports = __toCommonJS(exports_core);
// 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;
};
//# debugId=8FC14BF04C645DF764756E2164756E21