pecunia-core 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/index.d.mts +2 -0
- package/dist/api/index.mjs +5 -0
- package/dist/api-Dnub-EIf.mjs +35 -0
- package/dist/async_hooks-D8vqDJIk.mjs +20 -0
- package/dist/context/index.d.mts +2 -0
- package/dist/context/index.mjs +4 -0
- package/dist/context-_erD9NsC.mjs +164 -0
- package/dist/db/index.d.mts +2 -0
- package/dist/db/index.mjs +6 -0
- package/dist/db-JBwMpkmd.mjs +146 -0
- package/dist/env/index.d.mts +2 -0
- package/dist/env/index.mjs +3 -0
- package/dist/env-C7DHqPmD.mjs +230 -0
- package/dist/errors/index.d.mts +2 -0
- package/dist/errors/index.mjs +3 -0
- package/dist/errors-vpD21Iku.mjs +566 -0
- package/dist/generate-id-D3ZvbbXn.mjs +9 -0
- package/dist/get-payment-tables-0FkEj3dw.mjs +3677 -0
- package/dist/hooks/index.d.mts +2 -0
- package/dist/hooks/index.mjs +4 -0
- package/dist/hooks-PXnNTy74.mjs +1 -0
- package/dist/index-BNj-TC2H.d.mts +10 -0
- package/dist/index-B_V7jOck.d.mts +77 -0
- package/dist/index-CtOgkCz5.d.mts +2021 -0
- package/dist/index-DOO_aiRf.d.mts +6 -0
- package/dist/index.d.mts +5 -0
- package/dist/index.mjs +13 -0
- package/dist/types/index.d.mts +2 -0
- package/dist/types/index.mjs +11 -0
- package/dist/types-B0U9sZ8x.mjs +320 -0
- package/dist/utils/index.d.mts +2 -0
- package/dist/utils/index.mjs +5 -0
- package/dist/utils-BNmlJWNJ.mjs +30 -0
- package/package.json +126 -0
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as useMiddleware, i as optionsMiddleware, n as createPaymentEndpoint, r as createPecuniaMiddleware, t as PaymentMiddleware } from "../index-CtOgkCz5.mjs";
|
|
2
|
+
export { PaymentMiddleware, createPaymentEndpoint, createPecuniaMiddleware, optionsMiddleware, useMiddleware };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import "../context-_erD9NsC.mjs";
|
|
2
|
+
import "../async_hooks-D8vqDJIk.mjs";
|
|
3
|
+
import { i as useMiddleware, n as createPecuniaMiddleware, r as optionsMiddleware, t as createPaymentEndpoint } from "../api-Dnub-EIf.mjs";
|
|
4
|
+
|
|
5
|
+
export { createPaymentEndpoint, createPecuniaMiddleware, optionsMiddleware, useMiddleware };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { f as runWithEndpointContext } from "./context-_erD9NsC.mjs";
|
|
2
|
+
import { createEndpoint, createMiddleware } from "better-call";
|
|
3
|
+
|
|
4
|
+
//#region src/api/middleware/index.ts
|
|
5
|
+
const optionsMiddleware = createMiddleware(async () => {
|
|
6
|
+
/**
|
|
7
|
+
* This will be passed on the instance of
|
|
8
|
+
* the context. Used to infer the type
|
|
9
|
+
* here.
|
|
10
|
+
*/
|
|
11
|
+
return {};
|
|
12
|
+
});
|
|
13
|
+
const createPecuniaMiddleware = createMiddleware.create({ use: [optionsMiddleware, createMiddleware(async () => {
|
|
14
|
+
return {};
|
|
15
|
+
})] });
|
|
16
|
+
const useMiddleware = [optionsMiddleware];
|
|
17
|
+
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/api/endpoint/index.ts
|
|
20
|
+
function createPaymentEndpoint(pathOrOptions, handlerOrOptions, handlerOrNever) {
|
|
21
|
+
const path = typeof pathOrOptions === "string" ? pathOrOptions : void 0;
|
|
22
|
+
const options = typeof handlerOrOptions === "object" ? handlerOrOptions : pathOrOptions;
|
|
23
|
+
const handler = typeof handlerOrOptions === "function" ? handlerOrOptions : handlerOrNever;
|
|
24
|
+
if (path) return createEndpoint(path, {
|
|
25
|
+
...options,
|
|
26
|
+
use: [...options?.use || [], ...useMiddleware]
|
|
27
|
+
}, async (ctx) => runWithEndpointContext(ctx, () => handler(ctx)));
|
|
28
|
+
return createEndpoint({
|
|
29
|
+
...options,
|
|
30
|
+
use: [...options?.use || [], ...useMiddleware]
|
|
31
|
+
}, async (ctx) => runWithEndpointContext(ctx, () => handler(ctx)));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
//#endregion
|
|
35
|
+
export { useMiddleware as i, createPecuniaMiddleware as n, optionsMiddleware as r, createPaymentEndpoint as t };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region src/hooks/async_hooks/index.ts
|
|
2
|
+
const AsyncLocalStoragePromise = import(
|
|
3
|
+
/* @vite-ignore */
|
|
4
|
+
/* webpackIgnore: true */
|
|
5
|
+
"node:async_hooks"
|
|
6
|
+
).then((mod) => mod.AsyncLocalStorage).catch((err) => {
|
|
7
|
+
if ("AsyncLocalStorage" in globalThis) return globalThis.AsyncLocalStorage;
|
|
8
|
+
if (typeof window !== "undefined") return null;
|
|
9
|
+
console.warn("[senly] Warning: AsyncLocalStorage is not available in this environment. Some features may not work as expected.");
|
|
10
|
+
console.warn("[senly] If you are using Cloudflare Workers, please see: https://developers.cloudflare.com/workers/configuration/compatibility-flags/#nodejs-compatibility-flag");
|
|
11
|
+
throw err;
|
|
12
|
+
});
|
|
13
|
+
async function getAsyncLocalStorage() {
|
|
14
|
+
const mod = await AsyncLocalStoragePromise;
|
|
15
|
+
if (mod === null) throw new Error("getAsyncLocalStorage is only available in server code");
|
|
16
|
+
else return mod;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
export { getAsyncLocalStorage as t };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { At as RequestState, Dt as getCurrentDBAdapterAsyncLocalStorage, Et as getCurrentAdapter, Ft as hasRequestState, It as runWithRequestState, Lt as getCurrentAuthContext, Mt as defineRequestState, Nt as getCurrentRequestState, Ot as runWithAdapter, Pt as getRequestStateAsyncLocalStorage, Rt as getCurrentPaymentContextAsyncLocalStorage, jt as RequestStateWeakMap, kt as runWithTransaction, zt as runWithEndpointContext } from "../index-CtOgkCz5.mjs";
|
|
2
|
+
export { RequestState, RequestStateWeakMap, defineRequestState, getCurrentAdapter, getCurrentAuthContext, getCurrentDBAdapterAsyncLocalStorage, getCurrentPaymentContextAsyncLocalStorage, getCurrentRequestState, getRequestStateAsyncLocalStorage, hasRequestState, runWithAdapter, runWithEndpointContext, runWithRequestState, runWithTransaction };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { a as defineRequestState, c as hasRequestState, d as getCurrentPaymentContextAsyncLocalStorage, f as runWithEndpointContext, i as runWithTransaction, l as runWithRequestState, n as getCurrentDBAdapterAsyncLocalStorage, o as getCurrentRequestState, r as runWithAdapter, s as getRequestStateAsyncLocalStorage, t as getCurrentAdapter, u as getCurrentAuthContext } from "../context-_erD9NsC.mjs";
|
|
2
|
+
import "../async_hooks-D8vqDJIk.mjs";
|
|
3
|
+
|
|
4
|
+
export { defineRequestState, getCurrentAdapter, getCurrentAuthContext, getCurrentDBAdapterAsyncLocalStorage, getCurrentPaymentContextAsyncLocalStorage, getCurrentRequestState, getRequestStateAsyncLocalStorage, hasRequestState, runWithAdapter, runWithEndpointContext, runWithRequestState, runWithTransaction };
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { t as getAsyncLocalStorage } from "./async_hooks-D8vqDJIk.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/async_hooks/pure.ts
|
|
4
|
+
/**
|
|
5
|
+
* Polyfill for serverless functions.
|
|
6
|
+
*/
|
|
7
|
+
var AsyncLocalStoragePolyfill = class {
|
|
8
|
+
#current = void 0;
|
|
9
|
+
run(store, fn) {
|
|
10
|
+
const prev = this.#current;
|
|
11
|
+
this.#current = store;
|
|
12
|
+
const result = fn();
|
|
13
|
+
if (result instanceof Promise) return result.finally(() => {
|
|
14
|
+
this.#current = prev;
|
|
15
|
+
});
|
|
16
|
+
this.#current = prev;
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
getStore() {
|
|
20
|
+
return this.#current;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
const AsyncLocalStoragePromise = Promise.resolve().then(() => {
|
|
24
|
+
if ("AsyncLocalStorage" in globalThis) return globalThis.AsyncLocalStorage;
|
|
25
|
+
return AsyncLocalStoragePolyfill;
|
|
26
|
+
});
|
|
27
|
+
async function getAsyncLocalStorage$1() {
|
|
28
|
+
const mod = await AsyncLocalStoragePromise;
|
|
29
|
+
if (mod === null) throw new Error("getAsyncLocalStorage is only available in server code");
|
|
30
|
+
else return mod;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region src/context/payment-context.ts
|
|
35
|
+
let currentContextAsyncStorage = null;
|
|
36
|
+
const ensureAsyncStorage$2 = async () => {
|
|
37
|
+
if (!currentContextAsyncStorage) currentContextAsyncStorage = new (await (getAsyncLocalStorage$1()))();
|
|
38
|
+
return currentContextAsyncStorage;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* This is for internal use only. Most users should use `getCurrentAuthContext` instead.
|
|
42
|
+
*
|
|
43
|
+
* It is exposed for advanced use cases where you need direct access to the AsyncLocalStorage instance.
|
|
44
|
+
*/
|
|
45
|
+
async function getCurrentPaymentContextAsyncLocalStorage() {
|
|
46
|
+
return ensureAsyncStorage$2();
|
|
47
|
+
}
|
|
48
|
+
async function getCurrentAuthContext() {
|
|
49
|
+
const context = (await ensureAsyncStorage$2()).getStore();
|
|
50
|
+
if (!context) throw new Error("No payment context found. Please make sure you are calling this function within a `runWithEndpointContext` callback.");
|
|
51
|
+
return context;
|
|
52
|
+
}
|
|
53
|
+
async function runWithEndpointContext(context, fn) {
|
|
54
|
+
return (await ensureAsyncStorage$2()).run(context, fn);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/context/request-state.ts
|
|
59
|
+
let requestStateAsyncStorage = null;
|
|
60
|
+
const ensureAsyncStorage$1 = async () => {
|
|
61
|
+
if (!requestStateAsyncStorage) requestStateAsyncStorage = new (await (getAsyncLocalStorage$1()))();
|
|
62
|
+
return requestStateAsyncStorage;
|
|
63
|
+
};
|
|
64
|
+
async function getRequestStateAsyncLocalStorage() {
|
|
65
|
+
return ensureAsyncStorage$1();
|
|
66
|
+
}
|
|
67
|
+
async function hasRequestState() {
|
|
68
|
+
return (await ensureAsyncStorage$1()).getStore() !== void 0;
|
|
69
|
+
}
|
|
70
|
+
async function getCurrentRequestState() {
|
|
71
|
+
const store = (await ensureAsyncStorage$1()).getStore();
|
|
72
|
+
if (!store) throw new Error("No request state found. Please make sure you are calling this function within a `runWithRequestState` callback.");
|
|
73
|
+
return store;
|
|
74
|
+
}
|
|
75
|
+
async function runWithRequestState(store, fn) {
|
|
76
|
+
return (await ensureAsyncStorage$1()).run(store, fn);
|
|
77
|
+
}
|
|
78
|
+
function defineRequestState(initFn) {
|
|
79
|
+
const ref = Object.freeze({});
|
|
80
|
+
return {
|
|
81
|
+
get ref() {
|
|
82
|
+
return ref;
|
|
83
|
+
},
|
|
84
|
+
async get() {
|
|
85
|
+
const store = await getCurrentRequestState();
|
|
86
|
+
if (!store.has(ref)) {
|
|
87
|
+
const initialValue = await initFn();
|
|
88
|
+
store.set(ref, initialValue);
|
|
89
|
+
return initialValue;
|
|
90
|
+
}
|
|
91
|
+
return store.get(ref);
|
|
92
|
+
},
|
|
93
|
+
async set(value) {
|
|
94
|
+
(await getCurrentRequestState()).set(ref, value);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
//#endregion
|
|
100
|
+
//#region src/context/transaction.ts
|
|
101
|
+
let currentAdapterAsyncStorage = null;
|
|
102
|
+
const ensureAsyncStorage = async () => {
|
|
103
|
+
if (!currentAdapterAsyncStorage) currentAdapterAsyncStorage = new (await (getAsyncLocalStorage()))();
|
|
104
|
+
return currentAdapterAsyncStorage;
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* This is for internal use only. Most users should use `getCurrentAdapter` instead.
|
|
108
|
+
*
|
|
109
|
+
* It is exposed for advanced use cases where you need direct access to the AsyncLocalStorage instance.
|
|
110
|
+
*/
|
|
111
|
+
const getCurrentDBAdapterAsyncLocalStorage = async () => {
|
|
112
|
+
return ensureAsyncStorage();
|
|
113
|
+
};
|
|
114
|
+
const getCurrentAdapter = async (fallback) => {
|
|
115
|
+
return ensureAsyncStorage().then((als) => {
|
|
116
|
+
return als.getStore() || fallback;
|
|
117
|
+
}).catch(() => {
|
|
118
|
+
return fallback;
|
|
119
|
+
});
|
|
120
|
+
};
|
|
121
|
+
const runWithAdapter = async (adapter, fn) => {
|
|
122
|
+
let called = true;
|
|
123
|
+
return ensureAsyncStorage().then((als) => {
|
|
124
|
+
called = true;
|
|
125
|
+
return als.run(adapter, fn);
|
|
126
|
+
}).catch((err) => {
|
|
127
|
+
if (!called) return fn();
|
|
128
|
+
throw err;
|
|
129
|
+
});
|
|
130
|
+
};
|
|
131
|
+
const runWithTransaction = async (adapter, fn) => {
|
|
132
|
+
let called = true;
|
|
133
|
+
return ensureAsyncStorage().then((als) => {
|
|
134
|
+
called = true;
|
|
135
|
+
return adapter.transaction(async (trx) => {
|
|
136
|
+
return als.run(trx, fn);
|
|
137
|
+
});
|
|
138
|
+
}).catch((err) => {
|
|
139
|
+
if (!called) return fn();
|
|
140
|
+
throw err;
|
|
141
|
+
});
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
//#endregion
|
|
145
|
+
//#region src/context/index.ts
|
|
146
|
+
const glo = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {};
|
|
147
|
+
const importIdentifier = "__ $PECUNIA$ __";
|
|
148
|
+
if (glo[importIdentifier] === true)
|
|
149
|
+
/**
|
|
150
|
+
* Dear reader of this message. Please take this seriously.
|
|
151
|
+
*
|
|
152
|
+
* If you see this message, make sure that you only import one version of Pecunia. In many cases,
|
|
153
|
+
* your package manager installs two versions of Pecunia that are used by different packages within your project.
|
|
154
|
+
*
|
|
155
|
+
* This often leads to issues that are hard to debug. We often need to ensure async local storage instance,
|
|
156
|
+
* If you imported different versions of Pecunia, it is impossible for us to
|
|
157
|
+
* do status synchronization per request anymore - which might break the states.
|
|
158
|
+
*
|
|
159
|
+
*/
|
|
160
|
+
console.error("Pecunia was already imported. This breaks async local storage instance and will lead to issues!");
|
|
161
|
+
glo[importIdentifier] = true;
|
|
162
|
+
|
|
163
|
+
//#endregion
|
|
164
|
+
export { defineRequestState as a, hasRequestState as c, getCurrentPaymentContextAsyncLocalStorage as d, runWithEndpointContext as f, runWithTransaction as i, runWithRequestState as l, getCurrentDBAdapterAsyncLocalStorage as n, getCurrentRequestState as o, runWithAdapter as r, getRequestStateAsyncLocalStorage as s, getCurrentAdapter as t, getCurrentAuthContext as u };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { $t as KyselyDatabaseDialectType, Ct as initGetFieldAttributes, St as initGetFieldName, Tt as initGetDefaultFieldName, _t as discountSchema, at as getPaymentTables, bt as initGetModelName, ct as Subscription, dt as sharedDeletableSchema, ft as Product, gt as Discount, ht as priceSchema, lt as subscriptionSchema, mt as Price, ot as deepmerge, pt as productSchema, st as withApplyDefault, ut as sharedCoreSchema, vt as Customer, wt as initGetDefaultModelName, xt as initGetIdField, yt as customerSchema } from "../index-CtOgkCz5.mjs";
|
|
2
|
+
export { Customer, Discount, KyselyDatabaseDialectType, Price, Product, Subscription, customerSchema, deepmerge, discountSchema, getPaymentTables, initGetDefaultFieldName, initGetDefaultModelName, initGetFieldAttributes, initGetFieldName, initGetIdField, initGetModelName, priceSchema, productSchema, sharedCoreSchema, sharedDeletableSchema, subscriptionSchema, withApplyDefault };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import "../errors-vpD21Iku.mjs";
|
|
2
|
+
import { a as initGetFieldAttributes, c as initGetDefaultModelName, i as initGetModelName, n as deepmerge, o as initGetIdField, r as withApplyDefault, s as initGetDefaultFieldName, t as getPaymentTables } from "../get-payment-tables-0FkEj3dw.mjs";
|
|
3
|
+
import "../generate-id-D3ZvbbXn.mjs";
|
|
4
|
+
import { a as customerSchema, c as initGetFieldName, i as discountSchema, n as productSchema, o as sharedCoreSchema, r as priceSchema, s as sharedDeletableSchema, t as subscriptionSchema } from "../db-JBwMpkmd.mjs";
|
|
5
|
+
|
|
6
|
+
export { customerSchema, deepmerge, discountSchema, getPaymentTables, initGetDefaultFieldName, initGetDefaultModelName, initGetFieldAttributes, initGetFieldName, initGetIdField, initGetModelName, priceSchema, productSchema, sharedCoreSchema, sharedDeletableSchema, subscriptionSchema, withApplyDefault };
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { A as UsageAggregation, E as SubscriptionStatus, S as ProrationBehavior, b as PricingModel, c as BillingInterval, d as CollectionMethod, f as Currency, p as DiscountType, x as ProductType, y as PriceType } from "./errors-vpD21Iku.mjs";
|
|
2
|
+
import { c as initGetDefaultModelName, s as initGetDefaultFieldName } from "./get-payment-tables-0FkEj3dw.mjs";
|
|
3
|
+
import z$1, { z } from "zod";
|
|
4
|
+
|
|
5
|
+
//#region src/db/attributes/get-field-name.ts
|
|
6
|
+
const initGetFieldName = ({ schema, usePlural }) => {
|
|
7
|
+
const getDefaultModelName = initGetDefaultModelName({
|
|
8
|
+
schema,
|
|
9
|
+
usePlural
|
|
10
|
+
});
|
|
11
|
+
const getDefaultFieldName = initGetDefaultFieldName({
|
|
12
|
+
schema,
|
|
13
|
+
usePlural
|
|
14
|
+
});
|
|
15
|
+
/**
|
|
16
|
+
* Get the field name which is expected to be saved in the database based on the user's schema.
|
|
17
|
+
*
|
|
18
|
+
* This function is useful if you need to save the field name to the database.
|
|
19
|
+
*
|
|
20
|
+
* For example, if the user has defined a custom field name for the `user` model, then you can use this function to get the actual field name from the schema.
|
|
21
|
+
*/
|
|
22
|
+
function getFieldName({ model: modelName, field: fieldName }) {
|
|
23
|
+
const model = getDefaultModelName(modelName);
|
|
24
|
+
const field = getDefaultFieldName({
|
|
25
|
+
model,
|
|
26
|
+
field: fieldName
|
|
27
|
+
});
|
|
28
|
+
return schema[model]?.fields[field]?.fieldName || field;
|
|
29
|
+
}
|
|
30
|
+
return getFieldName;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region src/db/schema/shared.ts
|
|
35
|
+
const sharedCoreSchema = z$1.object({
|
|
36
|
+
id: z$1.uuid("id"),
|
|
37
|
+
createdAt: z$1.date().default(() => /* @__PURE__ */ new Date()),
|
|
38
|
+
updatedAt: z$1.date().default(() => /* @__PURE__ */ new Date())
|
|
39
|
+
});
|
|
40
|
+
const sharedDeletableSchema = z$1.object({
|
|
41
|
+
deleted: z$1.boolean().default(false),
|
|
42
|
+
deletedAt: z$1.coerce.date().optional()
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region src/db/schema/customer.ts
|
|
47
|
+
const customerSchema = sharedCoreSchema.extend({
|
|
48
|
+
phoneNumber: z$1.string().max(15),
|
|
49
|
+
firstName: z$1.string().max(50),
|
|
50
|
+
paymentMethodId: z$1.string().optional(),
|
|
51
|
+
lastName: z$1.string().max(50),
|
|
52
|
+
email: z$1.email().max(255)
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region src/db/schema/discount.ts
|
|
57
|
+
const discountSchema = sharedCoreSchema.extend(sharedDeletableSchema.shape).extend({
|
|
58
|
+
code: z.string().max(50),
|
|
59
|
+
name: z.string().max(255).optional(),
|
|
60
|
+
description: z.string().optional(),
|
|
61
|
+
type: z.enum(DiscountType).default(DiscountType.PERCENTAGE),
|
|
62
|
+
value: z.number().positive().min(0),
|
|
63
|
+
currency: z.enum(Currency).optional(),
|
|
64
|
+
active: z.boolean().default(true),
|
|
65
|
+
startsAt: z.coerce.date().optional(),
|
|
66
|
+
endsAt: z.coerce.date().optional(),
|
|
67
|
+
maxUses: z.number().int().positive().optional(),
|
|
68
|
+
maxUsesPerCustomer: z.number().int().positive().optional(),
|
|
69
|
+
timesUsed: z.number().int().nonnegative().default(0),
|
|
70
|
+
minimumAmount: z.number().positive().optional(),
|
|
71
|
+
minimumAmountCurrency: z.enum(Currency).optional(),
|
|
72
|
+
maximumDiscountAmount: z.number().positive().optional(),
|
|
73
|
+
maximumDiscountAmountCurrency: z.enum(Currency).optional(),
|
|
74
|
+
durationInMonths: z.number().int().positive().optional(),
|
|
75
|
+
appliesToProductIds: z.array(z.uuid()).default([]),
|
|
76
|
+
appliesToPriceIds: z.array(z.uuid()).default([]),
|
|
77
|
+
appliesToCustomerIds: z.array(z.uuid()).default([]),
|
|
78
|
+
appliesToAllProducts: z.boolean().default(false),
|
|
79
|
+
appliesToAllPrices: z.boolean().default(false),
|
|
80
|
+
appliesToAllCustomers: z.boolean().default(true),
|
|
81
|
+
firstTimeCustomerOnly: z.boolean().default(false),
|
|
82
|
+
metadata: z.record(z.string(), z.union([z.string(), z.number()])).optional()
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
//#endregion
|
|
86
|
+
//#region src/db/schema/price.ts
|
|
87
|
+
const priceSchema = sharedCoreSchema.extend(sharedDeletableSchema.shape).extend({
|
|
88
|
+
productId: z.uuid().optional(),
|
|
89
|
+
unitAmount: z.number().default(0),
|
|
90
|
+
currency: z.enum(Currency).default(Currency.KES),
|
|
91
|
+
type: z.enum(PriceType).default(PriceType.ONE_TIME),
|
|
92
|
+
pricingModel: z.enum(PricingModel).default(PricingModel.FLAT),
|
|
93
|
+
billingInterval: z.enum(BillingInterval).default(BillingInterval.MONTH),
|
|
94
|
+
billingIntervalCount: z.number().min(1).default(1),
|
|
95
|
+
trialPeriodDays: z.number().min(0).default(0),
|
|
96
|
+
usageAggregation: z.enum(UsageAggregation).optional(),
|
|
97
|
+
active: z.boolean().default(true),
|
|
98
|
+
metadata: z.record(z.string(), z.union([z.string(), z.number()])).optional()
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
//#endregion
|
|
102
|
+
//#region src/db/schema/product.ts
|
|
103
|
+
const productSchema = sharedCoreSchema.extend(sharedDeletableSchema.shape).extend({
|
|
104
|
+
type: z$1.enum(ProductType),
|
|
105
|
+
name: z$1.string().max(255),
|
|
106
|
+
description: z$1.string().optional(),
|
|
107
|
+
images: z$1.array(z$1.string()).default([]),
|
|
108
|
+
defaultPriceId: z$1.uuid().optional(),
|
|
109
|
+
marketing_features: z$1.array(z$1.object({ name: z$1.string() })).optional(),
|
|
110
|
+
package_dimensions: z$1.object({
|
|
111
|
+
height: z$1.number(),
|
|
112
|
+
length: z$1.number(),
|
|
113
|
+
weight: z$1.number(),
|
|
114
|
+
width: z$1.number()
|
|
115
|
+
}).optional(),
|
|
116
|
+
shippable: z$1.boolean().optional(),
|
|
117
|
+
url: z$1.url().optional(),
|
|
118
|
+
active: z$1.boolean().default(true),
|
|
119
|
+
metadata: z$1.record(z$1.string(), z$1.union([z$1.string(), z$1.number()])).optional()
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
//#endregion
|
|
123
|
+
//#region src/db/schema/subscription.ts
|
|
124
|
+
const subscriptionSchema = sharedCoreSchema.extend(sharedDeletableSchema.shape).extend({
|
|
125
|
+
customerId: z.uuid(),
|
|
126
|
+
paymentMethodId: z.uuid().optional(),
|
|
127
|
+
discountId: z.uuid().optional(),
|
|
128
|
+
discountEndsAt: z.coerce.date().optional(),
|
|
129
|
+
status: z.enum(SubscriptionStatus).default(SubscriptionStatus.INCOMPLETE),
|
|
130
|
+
currentBillingPeriodStart: z.coerce.date(),
|
|
131
|
+
currentBillingPeriodEnd: z.coerce.date(),
|
|
132
|
+
currentTrialStart: z.coerce.date().optional(),
|
|
133
|
+
currentTrialEnd: z.coerce.date().optional(),
|
|
134
|
+
cancelAtPeriodEnd: z.boolean().default(false),
|
|
135
|
+
canceledAt: z.coerce.date().optional(),
|
|
136
|
+
cancelAt: z.coerce.date().optional(),
|
|
137
|
+
daysUntilDue: z.number().min(0).default(0),
|
|
138
|
+
proration: z.enum(ProrationBehavior).default(ProrationBehavior.CREATE_PRORATIONS),
|
|
139
|
+
collectionMethod: z.enum(CollectionMethod).default(CollectionMethod.SEND_INVOICE),
|
|
140
|
+
metadata: z.record(z.string(), z.union([z.string(), z.number()])).optional(),
|
|
141
|
+
subscriptionStartedAt: z.coerce.date(),
|
|
142
|
+
subscriptionEndedAt: z.coerce.date().optional()
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
//#endregion
|
|
146
|
+
export { customerSchema as a, initGetFieldName as c, discountSchema as i, productSchema as n, sharedCoreSchema as o, priceSchema as r, sharedDeletableSchema as s, subscriptionSchema as t };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { _ as isTest, a as TTY_COLORS, c as logger, d as EnvObject, f as env, g as isProduction, h as isDevelopment, i as Logger, l as shouldPublishLog, m as getEnvVar, n as LogHandlerParams, o as createLogger, p as getBooleanEnvVar, r as LogLevel, s as levels, t as InternalLogger, u as ENV, v as nodeENV, y as getColorDepth } from "../index-B_V7jOck.mjs";
|
|
2
|
+
export { ENV, EnvObject, InternalLogger, LogHandlerParams, LogLevel, Logger, TTY_COLORS, createLogger, env, getBooleanEnvVar, getColorDepth, getEnvVar, isDevelopment, isProduction, isTest, levels, logger, nodeENV, shouldPublishLog };
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { a as shouldPublishLog, c as env, d as isDevelopment, f as isProduction, i as logger, l as getBooleanEnvVar, m as nodeENV, n as createLogger, o as getColorDepth, p as isTest, r as levels, s as ENV, t as TTY_COLORS, u as getEnvVar } from "../env-C7DHqPmD.mjs";
|
|
2
|
+
|
|
3
|
+
export { ENV, TTY_COLORS, createLogger, env, getBooleanEnvVar, getColorDepth, getEnvVar, isDevelopment, isProduction, isTest, levels, logger, nodeENV, shouldPublishLog };
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
//#region src/env/env-impl.ts
|
|
2
|
+
const _envShim = Object.create(null);
|
|
3
|
+
const _getEnv = (useShim) => globalThis.process?.env || globalThis.Deno?.env.toObject() || globalThis.__env__ || (useShim ? _envShim : globalThis);
|
|
4
|
+
const env = new Proxy(_envShim, {
|
|
5
|
+
get(_, prop) {
|
|
6
|
+
return _getEnv()[prop] ?? _envShim[prop];
|
|
7
|
+
},
|
|
8
|
+
has(_, prop) {
|
|
9
|
+
return prop in _getEnv() || prop in _envShim;
|
|
10
|
+
},
|
|
11
|
+
set(_, prop, value) {
|
|
12
|
+
const env$1 = _getEnv(true);
|
|
13
|
+
env$1[prop] = value;
|
|
14
|
+
return true;
|
|
15
|
+
},
|
|
16
|
+
deleteProperty(_, prop) {
|
|
17
|
+
if (!prop) return false;
|
|
18
|
+
const env$1 = _getEnv(true);
|
|
19
|
+
delete env$1[prop];
|
|
20
|
+
return true;
|
|
21
|
+
},
|
|
22
|
+
ownKeys() {
|
|
23
|
+
const env$1 = _getEnv(true);
|
|
24
|
+
return Object.keys(env$1);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
function toBoolean(val) {
|
|
28
|
+
return val ? val !== "false" : false;
|
|
29
|
+
}
|
|
30
|
+
const nodeENV = typeof process !== "undefined" && process.env && process.env.NODE_ENV || "";
|
|
31
|
+
/** Detect if `NODE_ENV` environment variable is `production` */
|
|
32
|
+
const isProduction = nodeENV === "production";
|
|
33
|
+
/** Detect if `NODE_ENV` environment variable is `dev` or `development` */
|
|
34
|
+
const isDevelopment = () => nodeENV === "dev" || nodeENV === "development";
|
|
35
|
+
/** Detect if `NODE_ENV` environment variable is `test` */
|
|
36
|
+
const isTest = () => nodeENV === "test" || toBoolean(env.TEST);
|
|
37
|
+
/**
|
|
38
|
+
* Get environment variable with fallback
|
|
39
|
+
*/
|
|
40
|
+
function getEnvVar(key, fallback) {
|
|
41
|
+
if (typeof process !== "undefined" && process.env) return process.env[key] ?? fallback;
|
|
42
|
+
if (typeof Deno !== "undefined") return Deno.env.get(key) ?? fallback;
|
|
43
|
+
if (typeof Bun !== "undefined") return Bun.env[key] ?? fallback;
|
|
44
|
+
return fallback;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Get boolean environment variable
|
|
48
|
+
*/
|
|
49
|
+
function getBooleanEnvVar(key, fallback = true) {
|
|
50
|
+
const value = getEnvVar(key);
|
|
51
|
+
if (!value) return fallback;
|
|
52
|
+
return value !== "0" && value.toLowerCase() !== "false" && value !== "";
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Common environment variables used in Pecunia
|
|
56
|
+
*/
|
|
57
|
+
const ENV = Object.freeze({
|
|
58
|
+
get NODE_ENV() {
|
|
59
|
+
return getEnvVar("NODE_ENV", "development");
|
|
60
|
+
},
|
|
61
|
+
get PACKAGE_VERSION() {
|
|
62
|
+
return getEnvVar("PACKAGE_VERSION", "0.0.0");
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/env/color-depth.ts
|
|
68
|
+
const COLORS_2 = 1;
|
|
69
|
+
const COLORS_16 = 4;
|
|
70
|
+
const COLORS_256 = 8;
|
|
71
|
+
const COLORS_16m = 24;
|
|
72
|
+
const TERM_ENVS = {
|
|
73
|
+
eterm: COLORS_16,
|
|
74
|
+
cons25: COLORS_16,
|
|
75
|
+
console: COLORS_16,
|
|
76
|
+
cygwin: COLORS_16,
|
|
77
|
+
dtterm: COLORS_16,
|
|
78
|
+
gnome: COLORS_16,
|
|
79
|
+
hurd: COLORS_16,
|
|
80
|
+
jfbterm: COLORS_16,
|
|
81
|
+
konsole: COLORS_16,
|
|
82
|
+
kterm: COLORS_16,
|
|
83
|
+
mlterm: COLORS_16,
|
|
84
|
+
mosh: COLORS_16m,
|
|
85
|
+
putty: COLORS_16,
|
|
86
|
+
st: COLORS_16,
|
|
87
|
+
"rxvt-unicode-24bit": COLORS_16m,
|
|
88
|
+
terminator: COLORS_16m,
|
|
89
|
+
"xterm-kitty": COLORS_16m
|
|
90
|
+
};
|
|
91
|
+
const CI_ENVS_MAP = new Map(Object.entries({
|
|
92
|
+
APPVEYOR: COLORS_256,
|
|
93
|
+
BUILDKITE: COLORS_256,
|
|
94
|
+
CIRCLECI: COLORS_16m,
|
|
95
|
+
DRONE: COLORS_256,
|
|
96
|
+
GITEA_ACTIONS: COLORS_16m,
|
|
97
|
+
GITHUB_ACTIONS: COLORS_16m,
|
|
98
|
+
GITLAB_CI: COLORS_256,
|
|
99
|
+
TRAVIS: COLORS_256
|
|
100
|
+
}));
|
|
101
|
+
const TERM_ENVS_REG_EXP = [
|
|
102
|
+
/ansi/,
|
|
103
|
+
/color/,
|
|
104
|
+
/linux/,
|
|
105
|
+
/direct/,
|
|
106
|
+
/^con[0-9]*x[0-9]/,
|
|
107
|
+
/^rxvt/,
|
|
108
|
+
/^screen/,
|
|
109
|
+
/^xterm/,
|
|
110
|
+
/^vt100/,
|
|
111
|
+
/^vt220/
|
|
112
|
+
];
|
|
113
|
+
function getColorDepth() {
|
|
114
|
+
if (getEnvVar("FORCE_COLOR") !== void 0) switch (getEnvVar("FORCE_COLOR")) {
|
|
115
|
+
case "":
|
|
116
|
+
case "1":
|
|
117
|
+
case "true": return COLORS_16;
|
|
118
|
+
case "2": return COLORS_256;
|
|
119
|
+
case "3": return COLORS_16m;
|
|
120
|
+
default: return COLORS_2;
|
|
121
|
+
}
|
|
122
|
+
if (getEnvVar("NODE_DISABLE_COLORS") !== void 0 && getEnvVar("NODE_DISABLE_COLORS") !== "" || getEnvVar("NO_COLOR") !== void 0 && getEnvVar("NO_COLOR") !== "" || getEnvVar("TERM") === "dumb") return COLORS_2;
|
|
123
|
+
if (getEnvVar("TMUX")) return COLORS_16m;
|
|
124
|
+
if ("TF_BUILD" in env && "AGENT_NAME" in env) return COLORS_16;
|
|
125
|
+
if ("CI" in env) {
|
|
126
|
+
for (const { 0: envName, 1: colors } of CI_ENVS_MAP) if (envName in env) return colors;
|
|
127
|
+
if (getEnvVar("CI_NAME") === "codeship") return COLORS_256;
|
|
128
|
+
return COLORS_2;
|
|
129
|
+
}
|
|
130
|
+
if ("TEAMCITY_VERSION" in env) return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.exec(getEnvVar("TEAMCITY_VERSION")) !== null ? COLORS_16 : COLORS_2;
|
|
131
|
+
switch (getEnvVar("TERM_PROGRAM")) {
|
|
132
|
+
case "iTerm.app":
|
|
133
|
+
if (!getEnvVar("TERM_PROGRAM_VERSION") || /^[0-2]\./.exec(getEnvVar("TERM_PROGRAM_VERSION")) !== null) return COLORS_256;
|
|
134
|
+
return COLORS_16m;
|
|
135
|
+
case "HyperTerm":
|
|
136
|
+
case "MacTerm": return COLORS_16m;
|
|
137
|
+
case "Apple_Terminal": return COLORS_256;
|
|
138
|
+
}
|
|
139
|
+
if (getEnvVar("COLORTERM") === "truecolor" || getEnvVar("COLORTERM") === "24bit") return COLORS_16m;
|
|
140
|
+
if (getEnvVar("TERM")) {
|
|
141
|
+
if (/truecolor/.exec(getEnvVar("TERM")) !== null) return COLORS_16m;
|
|
142
|
+
if (/^xterm-256/.exec(getEnvVar("TERM")) !== null) return COLORS_256;
|
|
143
|
+
const termEnv = getEnvVar("TERM").toLowerCase();
|
|
144
|
+
if (TERM_ENVS[termEnv]) return TERM_ENVS[termEnv];
|
|
145
|
+
if (TERM_ENVS_REG_EXP.some((term) => term.exec(termEnv) !== null)) return COLORS_16;
|
|
146
|
+
}
|
|
147
|
+
if (getEnvVar("COLORTERM")) return COLORS_16;
|
|
148
|
+
return COLORS_2;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
//#endregion
|
|
152
|
+
//#region src/env/logger.ts
|
|
153
|
+
const TTY_COLORS = {
|
|
154
|
+
reset: "\x1B[0m",
|
|
155
|
+
bright: "\x1B[1m",
|
|
156
|
+
dim: "\x1B[2m",
|
|
157
|
+
undim: "\x1B[22m",
|
|
158
|
+
underscore: "\x1B[4m",
|
|
159
|
+
blink: "\x1B[5m",
|
|
160
|
+
reverse: "\x1B[7m",
|
|
161
|
+
hidden: "\x1B[8m",
|
|
162
|
+
fg: {
|
|
163
|
+
black: "\x1B[30m",
|
|
164
|
+
red: "\x1B[31m",
|
|
165
|
+
green: "\x1B[32m",
|
|
166
|
+
yellow: "\x1B[33m",
|
|
167
|
+
blue: "\x1B[34m",
|
|
168
|
+
magenta: "\x1B[35m",
|
|
169
|
+
cyan: "\x1B[36m",
|
|
170
|
+
white: "\x1B[37m"
|
|
171
|
+
},
|
|
172
|
+
bg: {
|
|
173
|
+
black: "\x1B[40m",
|
|
174
|
+
red: "\x1B[41m",
|
|
175
|
+
green: "\x1B[42m",
|
|
176
|
+
yellow: "\x1B[43m",
|
|
177
|
+
blue: "\x1B[44m",
|
|
178
|
+
magenta: "\x1B[45m",
|
|
179
|
+
cyan: "\x1B[46m",
|
|
180
|
+
white: "\x1B[47m"
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
const levels = [
|
|
184
|
+
"debug",
|
|
185
|
+
"info",
|
|
186
|
+
"success",
|
|
187
|
+
"warn",
|
|
188
|
+
"error"
|
|
189
|
+
];
|
|
190
|
+
function shouldPublishLog(currentLogLevel, logLevel) {
|
|
191
|
+
return levels.indexOf(logLevel) >= levels.indexOf(currentLogLevel);
|
|
192
|
+
}
|
|
193
|
+
const levelColors = {
|
|
194
|
+
info: TTY_COLORS.fg.blue,
|
|
195
|
+
success: TTY_COLORS.fg.green,
|
|
196
|
+
warn: TTY_COLORS.fg.yellow,
|
|
197
|
+
error: TTY_COLORS.fg.red,
|
|
198
|
+
debug: TTY_COLORS.fg.magenta
|
|
199
|
+
};
|
|
200
|
+
const formatMessage = (level, message, colorsEnabled) => {
|
|
201
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
202
|
+
if (colorsEnabled) return `${TTY_COLORS.dim}${timestamp}${TTY_COLORS.reset} ${levelColors[level]}${level.toUpperCase()}${TTY_COLORS.reset} ${TTY_COLORS.bright}[Better Auth]:${TTY_COLORS.reset} ${message}`;
|
|
203
|
+
return `${timestamp} ${level.toUpperCase()} [Better Auth]: ${message}`;
|
|
204
|
+
};
|
|
205
|
+
const createLogger = (options) => {
|
|
206
|
+
const enabled = options?.disabled !== true;
|
|
207
|
+
const logLevel = options?.level ?? "error";
|
|
208
|
+
const colorsEnabled = options?.disableColors !== void 0 ? !options.disableColors : getColorDepth() !== 1;
|
|
209
|
+
const LogFunc = (level, message, args = []) => {
|
|
210
|
+
if (!enabled || !shouldPublishLog(logLevel, level)) return;
|
|
211
|
+
const formattedMessage = formatMessage(level, message, colorsEnabled);
|
|
212
|
+
if (!options || typeof options.log !== "function") {
|
|
213
|
+
if (level === "error") console.error(formattedMessage, ...args);
|
|
214
|
+
else if (level === "warn") console.warn(formattedMessage, ...args);
|
|
215
|
+
else console.log(formattedMessage, ...args);
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
options.log(level === "success" ? "info" : level, message, ...args);
|
|
219
|
+
};
|
|
220
|
+
return {
|
|
221
|
+
...Object.fromEntries(levels.map((level) => [level, (...[message, ...args]) => LogFunc(level, message, args)])),
|
|
222
|
+
get level() {
|
|
223
|
+
return logLevel;
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
};
|
|
227
|
+
const logger = createLogger();
|
|
228
|
+
|
|
229
|
+
//#endregion
|
|
230
|
+
export { shouldPublishLog as a, env as c, isDevelopment as d, isProduction as f, logger as i, getBooleanEnvVar as l, nodeENV as m, createLogger as n, getColorDepth as o, isTest as p, levels as r, ENV as s, TTY_COLORS as t, getEnvVar as u };
|