decent-sdk
Version:
SDK for the Decent API
357 lines (344 loc) • 11.9 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/react/index.ts
var exports_react = {};
__export(exports_react, {
useFetchProposals: () => useFetchProposals,
useFetchProposal: () => useFetchProposal,
useFetchMe: () => useFetchMe,
useFetchDaos: () => useFetchDaos,
useFetchDaoPermissions: () => useFetchDaoPermissions,
useFetchDao: () => useFetchDao,
useApiInfo: () => useApiInfo,
useApiHealth: () => useApiHealth,
useApiChains: () => useApiChains,
DecentApiProvider: () => DecentApiProvider,
DecentApiContext: () => DecentApiContext
});
module.exports = __toCommonJS(exports_react);
// src/react/contexts/DecentApiContext.tsx
var import_react = __toESM(require("react"));
// src/core/fetch/common/constants.ts
var DEFAULT_API_URL = "https://api.decent.build";
// src/react/contexts/DecentApiContext.tsx
var DecentApiContext = import_react.createContext({
apiUrl: DEFAULT_API_URL
});
function DecentApiProvider({ apiUrl = DEFAULT_API_URL, children }) {
return /* @__PURE__ */ import_react.default.createElement(DecentApiContext.Provider, {
value: { apiUrl }
}, children);
}
// src/react/hooks/imports.ts
var import_react_query = require("@tanstack/react-query");
var import_react2 = require("react");
// 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/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/react/hooks/useFetchDao.ts
var import_react_query2 = require("@tanstack/react-query");
var useFetchDao = (params) => {
const { chainId, address, enabled } = params;
const { apiUrl } = import_react2.useContext(DecentApiContext);
const shouldFetch = !!(chainId && address) && enabled;
const { data, error, isLoading } = import_react_query.useQuery({
queryKey: ["dao", chainId, address, apiUrl],
queryFn: shouldFetch ? () => getDao({ chainId, address, apiUrl }) : import_react_query2.skipToken,
initialData: {}
});
return { data, isLoading, error };
};
// src/react/hooks/useFetchDaos.ts
var import_react_query3 = require("@tanstack/react-query");
var useFetchDaos = (params) => {
const { chainId, enabled } = params ?? {};
const { apiUrl } = import_react2.useContext(DecentApiContext);
const shouldFetch = !!chainId && enabled;
const { data, error, isLoading } = import_react_query.useQuery({
queryKey: ["daos", chainId, apiUrl],
queryFn: shouldFetch ? () => getAllDaos({ chainId, apiUrl }) : import_react_query3.skipToken,
initialData: []
});
return { data, isLoading, error };
};
// 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;
};
// src/react/hooks/useFetchProposal.ts
var import_react_query4 = require("@tanstack/react-query");
var useFetchProposal = (params) => {
const { chainId, address, slug, enabled } = params;
const { apiUrl } = import_react2.useContext(DecentApiContext);
const shouldFetch = !!(chainId && address && slug) && enabled;
const { data, error, isLoading } = import_react_query.useQuery({
queryKey: ["proposal", chainId, address, slug, apiUrl],
queryFn: shouldFetch ? () => getProposal({ chainId, address, slug, apiUrl }) : import_react_query4.skipToken,
initialData: {}
});
return { data, isLoading, error };
};
// src/react/hooks/useFetchProposals.ts
var import_react_query5 = require("@tanstack/react-query");
var useFetchProposals = (params) => {
const { chainId, address, enabled } = params;
const { apiUrl } = import_react2.useContext(DecentApiContext);
const shouldFetch = !!(chainId && address) && enabled;
const { data, error, isLoading } = import_react_query.useQuery({
queryKey: ["proposals", chainId, address, apiUrl],
queryFn: shouldFetch ? () => getAllProposals({ chainId, address, apiUrl }) : import_react_query5.skipToken,
initialData: []
});
return { data, isLoading, error };
};
// 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/react/hooks/useFetchMe.ts
var import_react_query6 = require("@tanstack/react-query");
var useFetchMe = (params) => {
const { apiUrl } = import_react2.useContext(DecentApiContext);
const shouldFetch = !!apiUrl && params.enabled;
const { data, error, isLoading, refetch: queryRefetch } = import_react_query.useQuery({
queryKey: ["me", apiUrl],
queryFn: shouldFetch ? () => me({ apiUrl }) : import_react_query6.skipToken,
initialData: {}
});
const refetch = async () => {
if (shouldFetch) {
const result = await queryRefetch();
return result.data;
}
return Promise.resolve(undefined);
};
return {
data,
isLoading,
error,
refetch
};
};
// src/react/hooks/useFetchDaoPermissions.ts
var import_react_query7 = require("@tanstack/react-query");
var useFetchDaoPermissions = (params) => {
const { chainId, address, enabled } = params;
const { apiUrl } = import_react2.useContext(DecentApiContext);
const shouldFetch = !!(chainId && address) && enabled;
const { data, error, isLoading } = import_react_query.useQuery({
queryKey: ["daoPermissions", chainId, address, apiUrl],
queryFn: shouldFetch ? async () => {
const user = await getDaoPermissions({ chainId, address, apiUrl });
return user.permissions;
} : import_react_query7.skipToken,
initialData: {}
});
return { data, isLoading, error };
};
// 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/react/hooks/useApiChains.ts
var import_react_query8 = require("@tanstack/react-query");
var useApiChains = (params) => {
const { apiUrl } = import_react2.useContext(DecentApiContext);
const shouldFetch = params.enabled;
const { data, error, isLoading } = import_react_query.useQuery({
queryKey: ["chains", apiUrl],
queryFn: shouldFetch ? () => apiChains({ apiUrl }) : import_react_query8.skipToken,
initialData: []
});
return { data, isLoading, error };
};
// src/react/hooks/useApiHealth.ts
var import_react_query9 = require("@tanstack/react-query");
var useApiHealth = (params) => {
const { apiUrl } = import_react2.useContext(DecentApiContext);
const shouldFetch = params.enabled;
const { data, error, isLoading } = import_react_query.useQuery({
queryKey: ["health", apiUrl],
queryFn: shouldFetch ? () => apiHealth({ apiUrl }) : import_react_query9.skipToken,
initialData: ""
});
return { data, isLoading, error };
};
// src/react/hooks/useApiInfo.ts
var import_react_query10 = require("@tanstack/react-query");
var useApiInfo = (params) => {
const { apiUrl } = import_react2.useContext(DecentApiContext);
const shouldFetch = params.enabled;
const { data, error, isLoading } = import_react_query.useQuery({
queryKey: ["meta", apiUrl],
queryFn: shouldFetch ? () => apiInfo({ apiUrl }) : import_react_query10.skipToken,
initialData: { name: "", version: "" }
});
return { data, isLoading, error };
};
//# debugId=22706B8ACE24DC8064756E2164756E21