decent-sdk
Version:
SDK for the Decent API
312 lines (301 loc) • 9.6 kB
JavaScript
// src/react/contexts/DecentApiContext.tsx
import React, { createContext } from "react";
// src/core/fetch/common/constants.ts
var DEFAULT_API_URL = "https://api.decent.build";
// src/react/contexts/DecentApiContext.tsx
var DecentApiContext = createContext({
apiUrl: DEFAULT_API_URL
});
function DecentApiProvider({ apiUrl = DEFAULT_API_URL, children }) {
return /* @__PURE__ */ React.createElement(DecentApiContext.Provider, {
value: { apiUrl }
}, children);
}
// src/react/hooks/imports.ts
import { useQuery } from "@tanstack/react-query";
import { useContext } from "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
import { skipToken } from "@tanstack/react-query";
var useFetchDao = (params) => {
const { chainId, address, enabled } = params;
const { apiUrl } = useContext(DecentApiContext);
const shouldFetch = !!(chainId && address) && enabled;
const { data, error, isLoading } = useQuery({
queryKey: ["dao", chainId, address, apiUrl],
queryFn: shouldFetch ? () => getDao({ chainId, address, apiUrl }) : skipToken,
initialData: {}
});
return { data, isLoading, error };
};
// src/react/hooks/useFetchDaos.ts
import { skipToken as skipToken2 } from "@tanstack/react-query";
var useFetchDaos = (params) => {
const { chainId, enabled } = params ?? {};
const { apiUrl } = useContext(DecentApiContext);
const shouldFetch = !!chainId && enabled;
const { data, error, isLoading } = useQuery({
queryKey: ["daos", chainId, apiUrl],
queryFn: shouldFetch ? () => getAllDaos({ chainId, apiUrl }) : skipToken2,
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
import { skipToken as skipToken3 } from "@tanstack/react-query";
var useFetchProposal = (params) => {
const { chainId, address, slug, enabled } = params;
const { apiUrl } = useContext(DecentApiContext);
const shouldFetch = !!(chainId && address && slug) && enabled;
const { data, error, isLoading } = useQuery({
queryKey: ["proposal", chainId, address, slug, apiUrl],
queryFn: shouldFetch ? () => getProposal({ chainId, address, slug, apiUrl }) : skipToken3,
initialData: {}
});
return { data, isLoading, error };
};
// src/react/hooks/useFetchProposals.ts
import { skipToken as skipToken4 } from "@tanstack/react-query";
var useFetchProposals = (params) => {
const { chainId, address, enabled } = params;
const { apiUrl } = useContext(DecentApiContext);
const shouldFetch = !!(chainId && address) && enabled;
const { data, error, isLoading } = useQuery({
queryKey: ["proposals", chainId, address, apiUrl],
queryFn: shouldFetch ? () => getAllProposals({ chainId, address, apiUrl }) : skipToken4,
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
import { skipToken as skipToken5 } from "@tanstack/react-query";
var useFetchMe = (params) => {
const { apiUrl } = useContext(DecentApiContext);
const shouldFetch = !!apiUrl && params.enabled;
const { data, error, isLoading, refetch: queryRefetch } = useQuery({
queryKey: ["me", apiUrl],
queryFn: shouldFetch ? () => me({ apiUrl }) : skipToken5,
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
import { skipToken as skipToken6 } from "@tanstack/react-query";
var useFetchDaoPermissions = (params) => {
const { chainId, address, enabled } = params;
const { apiUrl } = useContext(DecentApiContext);
const shouldFetch = !!(chainId && address) && enabled;
const { data, error, isLoading } = useQuery({
queryKey: ["daoPermissions", chainId, address, apiUrl],
queryFn: shouldFetch ? async () => {
const user = await getDaoPermissions({ chainId, address, apiUrl });
return user.permissions;
} : skipToken6,
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
import { skipToken as skipToken7 } from "@tanstack/react-query";
var useApiChains = (params) => {
const { apiUrl } = useContext(DecentApiContext);
const shouldFetch = params.enabled;
const { data, error, isLoading } = useQuery({
queryKey: ["chains", apiUrl],
queryFn: shouldFetch ? () => apiChains({ apiUrl }) : skipToken7,
initialData: []
});
return { data, isLoading, error };
};
// src/react/hooks/useApiHealth.ts
import { skipToken as skipToken8 } from "@tanstack/react-query";
var useApiHealth = (params) => {
const { apiUrl } = useContext(DecentApiContext);
const shouldFetch = params.enabled;
const { data, error, isLoading } = useQuery({
queryKey: ["health", apiUrl],
queryFn: shouldFetch ? () => apiHealth({ apiUrl }) : skipToken8,
initialData: ""
});
return { data, isLoading, error };
};
// src/react/hooks/useApiInfo.ts
import { skipToken as skipToken9 } from "@tanstack/react-query";
var useApiInfo = (params) => {
const { apiUrl } = useContext(DecentApiContext);
const shouldFetch = params.enabled;
const { data, error, isLoading } = useQuery({
queryKey: ["meta", apiUrl],
queryFn: shouldFetch ? () => apiInfo({ apiUrl }) : skipToken9,
initialData: { name: "", version: "" }
});
return { data, isLoading, error };
};
export {
useFetchProposals,
useFetchProposal,
useFetchMe,
useFetchDaos,
useFetchDaoPermissions,
useFetchDao,
useApiInfo,
useApiHealth,
useApiChains,
DecentApiProvider,
DecentApiContext
};
//# debugId=162B5AACBFFBD2EF64756E2164756E21