autumn-js
Version:
Autumn JS Library
234 lines (232 loc) • 6.1 kB
JavaScript
import {
secretKeyCheck
} from "./chunk-UNZHJTEY.mjs";
import {
createRouterWithOptions
} from "./chunk-2RZ2FZX2.mjs";
import "./chunk-5EKNB4IJ.mjs";
import "./chunk-3OLXYDCU.mjs";
import "./chunk-K7JGEYUX.mjs";
import "./chunk-HHMIVOXX.mjs";
import "./chunk-5H6HVCOP.mjs";
import {
AttachParamsSchema,
Autumn,
BillingPortalParamsSchema,
CancelParamsSchema,
CheckParamsSchema,
CreateReferralCodeParamsSchema,
CustomerExpandEnum,
RedeemReferralCodeParamsSchema,
TrackParamsSchema
} from "./chunk-IOSEAUDJ.mjs";
import "./chunk-45WVZY23.mjs";
import "./chunk-KSG3E4Q2.mjs";
import "./chunk-6DZX6EAA.mjs";
// src/libraries/backend/better-auth.ts
import { createAuthEndpoint } from "better-auth/plugins";
import { APIError, createEndpoint } from "better-call";
import { findRoute } from "rou3";
import { sessionMiddleware } from "better-auth/api";
import { z } from "zod";
var router = createRouterWithOptions();
var betterAuthPathMap = {
// "create-customer": "customers",
// "customers/get": "customers",
attach: "attach",
check: "check",
track: "track",
cancel: "cancel",
"referrals/redeem-code": "referrals/redeem",
"referrals/create-code": "referrals/code",
"open-billing-portal": "billing_portal"
// "products/list": "products",
};
var handleReq = async ({
ctx,
options,
method
}) => {
let { found, error: resError } = secretKeyCheck();
if (!found && !options?.secretKey) {
throw new APIError(resError.statusCode, {
message: resError.message,
code: resError.code
});
}
const client = new Autumn({
url: options?.url,
secretKey: options?.secretKey
});
let searchParams = {};
try {
const req = ctx.request;
const url = new URL(req.url);
searchParams = Object.fromEntries(url.searchParams);
} catch (error) {
}
const rest = ctx.path.split("/autumn/")[1];
const pathname = `/api/autumn/${betterAuthPathMap[rest] || rest}`;
const match = findRoute(router, method, pathname);
if (!match) {
return ctx.json({ error: "Not found" }, { status: 404 });
}
const { data, params: pathParams } = match;
const { handler } = data;
const body = ctx.body;
const session = ctx.context.session;
const identify = async () => {
if (!session) {
return;
}
return {
customerId: session.user.id,
customerData: {
email: session.user.email,
name: session.user.name
}
};
};
const result = await handler({
autumn: client,
body,
path: pathname,
getCustomer: identify,
pathParams,
searchParams
});
if (result.statusCode >= 400) {
throw new APIError(result.statusCode, {
message: result.body.message,
code: result.body.code
});
}
return ctx.json(result.body, { status: result.statusCode });
};
var autumn = (options) => {
let secretKey = options?.secretKey;
let url = options?.url;
return {
id: "autumn",
endpoints: {
createCustomer: createEndpoint(
"/autumn/customers",
{
method: "POST",
use: [sessionMiddleware],
body: z.object({
expand: z.array(CustomerExpandEnum).optional()
}),
metadata: {
isAction: false
}
},
async (ctx) => {
return await handleReq({ ctx, options, method: "POST" });
}
),
listProducts: createAuthEndpoint(
"/autumn/products",
{
method: "GET",
use: [sessionMiddleware]
},
async (ctx) => {
return await handleReq({ ctx, options, method: "GET" });
}
),
attach: createAuthEndpoint(
"/autumn/attach",
{
method: "POST",
body: AttachParamsSchema.omit({
customer_id: true
}),
use: [sessionMiddleware]
},
async (ctx) => handleReq({ ctx, options, method: "POST" })
),
check: createAuthEndpoint(
"/autumn/check",
{
method: "POST",
body: CheckParamsSchema.omit({
customer_id: true
}),
use: [sessionMiddleware]
},
async (ctx) => handleReq({ ctx, options, method: "POST" })
),
track: createAuthEndpoint(
"/autumn/track",
{
method: "POST",
body: TrackParamsSchema.omit({
customer_id: true
}),
use: [sessionMiddleware]
},
async (ctx) => handleReq({ ctx, options, method: "POST" })
),
cancel: createAuthEndpoint(
"/autumn/cancel",
{
method: "POST",
body: CancelParamsSchema.omit({
customer_id: true
}),
use: [sessionMiddleware]
},
async (ctx) => {
return await handleReq({ ctx, options, method: "POST" });
}
),
createReferralCode: createAuthEndpoint(
"/autumn/referrals/create-code",
{
method: "POST",
body: CreateReferralCodeParamsSchema.omit({
customer_id: true
}),
use: [sessionMiddleware]
},
async (ctx) => {
return await handleReq({ ctx, options, method: "POST" });
}
),
redeemReferralCode: createAuthEndpoint(
"/autumn/referrals/redeem-code",
{
method: "POST",
body: RedeemReferralCodeParamsSchema.omit({
customer_id: true
}),
use: [sessionMiddleware]
},
async (ctx) => {
return await handleReq({ ctx, options, method: "POST" });
}
),
billingPortal: createAuthEndpoint(
"/autumn/billing_portal",
{
method: "POST",
body: BillingPortalParamsSchema,
metadata: {
isAction: false
},
use: [sessionMiddleware]
},
async (ctx) => await handleReq({ ctx, options, method: "POST" })
)
}
};
};
var autumnClient = () => ({
id: "autumn",
$InferServerPlugin: {}
});
export {
autumn,
autumnClient
};