UNPKG

autumn-js

Version:
71 lines (70 loc) 1.64 kB
"use server"; import { Autumn } from "../../sdk"; import { withAuth } from "./auth/withNextAuth"; import { toServerResponse } from "./utils"; import { toSnakeCase } from "@utils/toSnakeCase"; const createAutumnClient = (publishableKey) => { return new Autumn({ publishableKey }); }; const createCusAction = withAuth({ fn: async ({ customerId, customerData, ...params }) => { const autumn = createAutumnClient(); const result = await autumn.customers.create({ id: customerId, ...customerData, ...params }); return toServerResponse(result); }, withCustomerData: true }); const getEntityAction = withAuth({ fn: async ({ customerId, entityId, ...params }) => { const autumn = createAutumnClient(); let snakeParams = toSnakeCase({ obj: params }); const result = await autumn.entities.get( customerId, entityId, snakeParams ); return toServerResponse(result); } }); const createEntityAction = withAuth({ fn: async ({ customerId, entity }) => { const autumn = createAutumnClient(); let snakeEntity = toSnakeCase({ obj: entity }); const result = await autumn.entities.create(customerId, snakeEntity); return toServerResponse(result); } }); const deleteEntityAction = withAuth({ fn: async ({ customerId, entityId }) => { const autumn = createAutumnClient(); const result = await autumn.entities.delete(customerId, entityId); return toServerResponse(result); } }); export { createAutumnClient, createCusAction, createEntityAction, deleteEntityAction, getEntityAction };