@daveyplate/supabase-swr-entities
Version:
An entity management library for Supabase and SWR
182 lines (181 loc) • 7.42 kB
JavaScript
import { useCallback } from "react";
import { useSWRConfig } from "swr";
import { useSession } from "@supabase/auth-helpers-react";
import { v4 } from "uuid";
import { apiPath } from "./client-utils";
import { useAPI } from "./api-methods";
// TODO look into "lang" param.... and how that needs to be factored in for mutations
export function useCreateEntity() {
const session = useSession();
const mutateEntity = useMutateEntity();
const { postAPI } = useAPI();
const swrConfig = useSWRConfig();
const { onError } = swrConfig;
const createEntity = useCallback(async (table, entity, params, optimisticFields) => {
if (!table)
return { error: new Error("Entity not loaded") };
const createParams = params && Object.assign({}, params);
const newEntity = Object.assign(Object.assign({ id: v4() }, entity), { locale: createParams === null || createParams === void 0 ? void 0 : createParams.lang });
createParams === null || createParams === void 0 ? true : delete createParams.limit;
createParams === null || createParams === void 0 ? true : delete createParams.offset;
createParams === null || createParams === void 0 ? true : delete createParams.order;
const mutateParams = createParams && Object.assign({}, createParams);
createParams === null || createParams === void 0 ? true : delete createParams.lang;
const path = apiPath(table, null, createParams);
try {
const entity = await mutateEntity(table, newEntity.id, async () => {
return postAPI(path, newEntity);
}, mutateParams, {
optimisticData: Object.assign(Object.assign({}, newEntity), optimisticFields),
revalidate: false
});
return { entity };
}
catch (error) {
onError(error, path, swrConfig);
return { error: error };
}
}, [session]);
return createEntity;
}
export function useUpdateEntity() {
const session = useSession();
const mutateEntity = useMutateEntity();
const { patchAPI } = useAPI();
const swrConfig = useSWRConfig();
const { onError } = swrConfig;
const updateEntity = useCallback(async (table, id, fields, params) => {
if (!table)
return { error: new Error("Entity not loaded") };
if (!fields)
return { error: new Error("No fields to update") };
const updateParams = params && Object.assign({}, params);
updateParams === null || updateParams === void 0 ? true : delete updateParams.offset;
const mutateParams = updateParams && Object.assign({}, updateParams);
if (updateParams === null || updateParams === void 0 ? void 0 : updateParams.lang) {
fields.locale = updateParams.lang;
delete updateParams.lang;
}
const path = apiPath(table, id, updateParams);
try {
const entity = await mutateEntity(table, id, async () => {
return patchAPI(path, fields);
}, mutateParams, {
optimisticData: (entity) => (Object.assign(Object.assign({ updated_at: new Date() }, entity), fields)),
revalidate: false
});
return { entity };
}
catch (error) {
onError(error, path, swrConfig);
return { error: error };
}
}, [session]);
return updateEntity;
}
export function useDeleteEntity() {
const session = useSession();
const mutateEntity = useMutateEntity();
const { deleteAPI } = useAPI();
const swrConfig = useSWRConfig();
const { onError } = swrConfig;
const deleteEntity = useCallback(async (table, id, params) => {
if (!table)
return { error: new Error("Entity not loaded") };
const deleteParams = params && Object.assign({}, params);
deleteParams === null || deleteParams === void 0 ? true : delete deleteParams.offset;
const mutateParams = deleteParams && Object.assign({}, deleteParams);
deleteParams === null || deleteParams === void 0 ? true : delete deleteParams.lang;
const path = apiPath(table, id, deleteParams);
try {
await mutateEntity(table, id, async () => {
await deleteAPI(path);
return null;
}, mutateParams, {
optimisticData: null,
revalidate: false
});
}
catch (error) {
onError(error, path, swrConfig);
return { error: error };
}
return { success: true };
}, [session]);
return deleteEntity;
}
export function useUpdateEntities() {
const session = useSession();
const { patchAPI } = useAPI();
const swrConfig = useSWRConfig();
const { onError } = swrConfig;
const updateEntities = useCallback(async (table, params, fields) => {
if (!table)
return { error: new Error("Entity not loaded") };
const path = apiPath(table, null, params);
// Patch the entities via API
try {
await patchAPI(path, fields);
}
catch (error) {
onError(error, path, swrConfig);
return { error: error };
}
return { success: true };
}, [session]);
return updateEntities;
}
export function useDeleteEntities() {
const session = useSession();
const { deleteAPI } = useAPI();
const swrConfig = useSWRConfig();
const { onError } = swrConfig;
const deleteEntities = useCallback(async (table, params) => {
if (!table)
return { error: new Error("Entity not loaded") };
const path = apiPath(table, null, params);
// Delete the entities via API
try {
await deleteAPI(path);
}
catch (error) {
onError(error, path, swrConfig);
return { error: error };
}
return { success: true };
}, [session]);
return deleteEntities;
}
export function useMutateEntities() {
const { mutate } = useSWRConfig();
const session = useSession();
const mutateEntities = useCallback(async (table, entities, params, opts = false) => {
if (!table)
return { error: new Error("Entity not loaded") };
const path = apiPath(table, null, params);
if (!entities) {
return mutate(path);
}
return mutate(path, {
data: entities,
count: entities === null || entities === void 0 ? void 0 : entities.length,
limit: (params === null || params === void 0 ? void 0 : params.limit) || 100,
offset: (params === null || params === void 0 ? void 0 : params.offset) || 0,
has_more: (params === null || params === void 0 ? void 0 : params.has_more) || false
}, opts);
}, [session]);
return mutateEntities;
}
export function useMutateEntity() {
const { mutate } = useSWRConfig();
const session = useSession();
const mutateEntity = useCallback((table, id, data, params, opts = false) => {
if (!table)
return { error: new Error("Entity not loaded") };
const path = apiPath(table, id, params);
if (data == undefined)
return mutate(path);
return mutate(path, data, opts);
}, [session]);
return mutateEntity;
}