UNPKG

autumn-js

Version:

Autumn JS Library

84 lines (80 loc) 1.92 kB
"use client"; import { handleAllowed } from "./chunk-3JJC2VU5.mjs"; import { useAutumnBase } from "./chunk-KJLHVBV4.mjs"; // src/libraries/react/hooks/useEntityBase.tsx import useSWR from "swr"; import { useContext } from "react"; var useEntityBase = ({ entityId, params, AutumnContext }) => { const { client } = useContext(AutumnContext); const queryKey = ["entity", entityId, params?.expand]; const fetchEntity = async () => { if (!entityId) { return null; } const { data: data2, error: error2 } = await client.entities.get(entityId, params); if (error2) { throw error2; } if (!data2) { return null; } return data2; }; const { data, error, isLoading, mutate } = useSWR(queryKey, fetchEntity, { fallbackData: null, onErrorRetry: (error2, key, config) => { if (error2.code == "entity_not_found") { return false; } return true; } }); const { check: checkAutumn, attach: attachAutumn, cancel: cancelAutumn, track: trackAutumn } = useAutumnBase({ AutumnContext }); const allowed = (params2) => handleAllowed({ customer: data, params: params2 }); const check = (params2) => checkAutumn({ ...params2, entityId: entityId || void 0 }); const attach = (params2) => attachAutumn({ ...params2, entityId: entityId || void 0 }); const cancel = (params2) => cancelAutumn({ ...params2, entityId: entityId || void 0 }); const track = (params2) => trackAutumn({ ...params2, entityId: entityId || void 0 }); if (!entityId) { return { entity: null, isLoading: false, error: null, refetch: mutate, allowed, check, attach, cancel, track }; } return { entity: error ? null : data, isLoading, error, refetch: mutate, allowed, check, attach, cancel, track }; }; export { useEntityBase };