- Built primarily for tRPC
- Zod validated schemas and resource, all Typescript inferred.
- Axios handled API calls
- All server side requests
- Also works on NextJS
bun i @re-up/paymongo-fn
import { Paymongo } from "@re-up/paymongo-fn";
const p = Paymongo(process.env.PAYMONGO_SK);
const onCheckout = async (params) => await p.checkout.create(params);
// trpc/router.ts
const paymongo = createRouter({
checkout: checkoutProcedure.mutation(async({input}) => await p.checkout.create(input)
})
// trpc/caller.ts
const checkout = async (params) => await api.paymongo.checkout(params)
interface Input<T> {
input: T;
}
const asyncR =
<TParams, TReturn>(fn: (params: TParams) => Promise<TReturn>) =>
async ({ input }: Input<TParams>) =>
await fn(input);
This helper is for mutations and expects input as key. It makes your code a bit concise and readable.
const paymongo = createRouter({
// checkout params is inferred by what paymongo.checkout.create accepts
checkout: checkoutProcedure.mutation(asyncR(p.checkout.create)),
// retrieve params is inferred by what paymongo.checkout.retrieve accepts
retrieve: retrieveProcedure.mutation(asyncR(p.checkout.retrieve)),
});
const asyncFn =
<TParams, TReturn>(fn: (params: TParams) => Promise<TReturn>) =>
async (params: TParams) =>
await fn(params);
This one is a plane async/await wrapper, also type inferred based on the callers params.
const checkout = asyncFn(api.paymongo.checkout);
re-up.ph