vibezcheck 0.1.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/LICENSE +21 -0
- package/README.md +219 -0
- package/dist/ai-sdk/index.d.mts +34 -0
- package/dist/ai-sdk/index.d.ts +34 -0
- package/dist/ai-sdk/index.js +982 -0
- package/dist/ai-sdk/index.js.map +1 -0
- package/dist/ai-sdk/index.mjs +944 -0
- package/dist/ai-sdk/index.mjs.map +1 -0
- package/dist/auth/index.d.mts +59 -0
- package/dist/auth/index.d.ts +59 -0
- package/dist/auth/index.js +129 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/auth/index.mjs +90 -0
- package/dist/auth/index.mjs.map +1 -0
- package/dist/billing/index.d.mts +48 -0
- package/dist/billing/index.d.ts +48 -0
- package/dist/billing/index.js +128 -0
- package/dist/billing/index.js.map +1 -0
- package/dist/billing/index.mjs +90 -0
- package/dist/billing/index.mjs.map +1 -0
- package/dist/client-MJ3tl7bz.d.mts +44 -0
- package/dist/client-txrE0D_D.d.ts +44 -0
- package/dist/customers/index.d.mts +49 -0
- package/dist/customers/index.d.ts +49 -0
- package/dist/customers/index.js +158 -0
- package/dist/customers/index.js.map +1 -0
- package/dist/customers/index.mjs +119 -0
- package/dist/customers/index.mjs.map +1 -0
- package/dist/index.d.mts +92 -0
- package/dist/index.d.ts +92 -0
- package/dist/index.js +1458 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1386 -0
- package/dist/index.mjs.map +1 -0
- package/dist/meter/index.d.mts +131 -0
- package/dist/meter/index.d.ts +131 -0
- package/dist/meter/index.js +895 -0
- package/dist/meter/index.js.map +1 -0
- package/dist/meter/index.mjs +843 -0
- package/dist/meter/index.mjs.map +1 -0
- package/dist/pricing/index.d.mts +40 -0
- package/dist/pricing/index.d.ts +40 -0
- package/dist/pricing/index.js +178 -0
- package/dist/pricing/index.js.map +1 -0
- package/dist/pricing/index.mjs +146 -0
- package/dist/pricing/index.mjs.map +1 -0
- package/dist/types-CSrSmsd1.d.mts +159 -0
- package/dist/types-CSrSmsd1.d.ts +159 -0
- package/package.json +131 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/billing/index.ts
|
|
31
|
+
var billing_exports = {};
|
|
32
|
+
__export(billing_exports, {
|
|
33
|
+
BillingHelper: () => BillingHelper,
|
|
34
|
+
createBillingHelper: () => createBillingHelper
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(billing_exports);
|
|
37
|
+
|
|
38
|
+
// src/billing/sessions.ts
|
|
39
|
+
var import_stripe = __toESM(require("stripe"));
|
|
40
|
+
var BillingHelper = class {
|
|
41
|
+
stripe;
|
|
42
|
+
constructor(options = {}) {
|
|
43
|
+
if (options.stripe) {
|
|
44
|
+
this.stripe = options.stripe;
|
|
45
|
+
} else {
|
|
46
|
+
const apiKey = options.apiKey || process.env.STRIPE_SECRET_KEY;
|
|
47
|
+
if (!apiKey) {
|
|
48
|
+
throw new Error("[vibezcheck] Stripe API key required for billing operations.");
|
|
49
|
+
}
|
|
50
|
+
this.stripe = new import_stripe.default(apiKey);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Creates a Stripe Customer Portal session URL where users can manage cards, view usage & invoices
|
|
55
|
+
*/
|
|
56
|
+
async createPortalSession(params) {
|
|
57
|
+
const session = await this.stripe.billingPortal.sessions.create({
|
|
58
|
+
customer: params.customerId,
|
|
59
|
+
return_url: params.returnUrl
|
|
60
|
+
});
|
|
61
|
+
return session.url;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Creates a Stripe Checkout session to subscribe a customer to a metered pricing tier
|
|
65
|
+
*/
|
|
66
|
+
async createCheckoutSession(params) {
|
|
67
|
+
const session = await this.stripe.checkout.sessions.create({
|
|
68
|
+
customer: params.customerId,
|
|
69
|
+
customer_email: !params.customerId ? params.customerEmail : void 0,
|
|
70
|
+
line_items: [
|
|
71
|
+
{
|
|
72
|
+
price: params.priceId,
|
|
73
|
+
quantity: 1
|
|
74
|
+
}
|
|
75
|
+
],
|
|
76
|
+
mode: params.mode || "subscription",
|
|
77
|
+
success_url: `${params.returnUrl}?session_id={CHECKOUT_SESSION_ID}&status=success`,
|
|
78
|
+
cancel_url: `${params.returnUrl}?status=cancelled`,
|
|
79
|
+
metadata: params.metadata
|
|
80
|
+
});
|
|
81
|
+
if (!session.url) {
|
|
82
|
+
throw new Error("[vibezcheck] Failed to generate checkout session URL.");
|
|
83
|
+
}
|
|
84
|
+
return session.url;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Creates a Checkout session for topping up prepaid credit wallet balance
|
|
88
|
+
*/
|
|
89
|
+
async createTopUpSession(params) {
|
|
90
|
+
const session = await this.stripe.checkout.sessions.create({
|
|
91
|
+
customer: params.customerId,
|
|
92
|
+
line_items: [
|
|
93
|
+
{
|
|
94
|
+
price_data: {
|
|
95
|
+
currency: params.currency || "usd",
|
|
96
|
+
unit_amount: params.amountCents,
|
|
97
|
+
product_data: {
|
|
98
|
+
name: "AI Token Credits Top-Up",
|
|
99
|
+
description: `Add $${(params.amountCents / 100).toFixed(2)} in AI inference credits`
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
quantity: 1
|
|
103
|
+
}
|
|
104
|
+
],
|
|
105
|
+
mode: "payment",
|
|
106
|
+
success_url: `${params.returnUrl}?status=success&amount=${params.amountCents}`,
|
|
107
|
+
cancel_url: `${params.returnUrl}?status=cancelled`,
|
|
108
|
+
metadata: {
|
|
109
|
+
type: "vibezcheck_topup",
|
|
110
|
+
customerId: params.customerId,
|
|
111
|
+
amountCents: params.amountCents.toString()
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
if (!session.url) {
|
|
115
|
+
throw new Error("[vibezcheck] Failed to generate top-up checkout URL.");
|
|
116
|
+
}
|
|
117
|
+
return session.url;
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
function createBillingHelper(options = {}) {
|
|
121
|
+
return new BillingHelper(options);
|
|
122
|
+
}
|
|
123
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
124
|
+
0 && (module.exports = {
|
|
125
|
+
BillingHelper,
|
|
126
|
+
createBillingHelper
|
|
127
|
+
});
|
|
128
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/billing/index.ts","../../src/billing/sessions.ts"],"sourcesContent":["export * from './sessions';\n","import Stripe from 'stripe';\n\nexport interface CreatePortalSessionParams {\n customerId: string;\n returnUrl: string;\n}\n\nexport interface CreateCheckoutSessionParams {\n customerId?: string;\n customerEmail?: string;\n priceId: string;\n returnUrl: string;\n mode?: 'subscription' | 'payment';\n metadata?: Record<string, string>;\n}\n\nexport interface CreateTopUpSessionParams {\n customerId: string;\n amountCents: number;\n returnUrl: string;\n currency?: string;\n}\n\nexport class BillingHelper {\n private stripe: Stripe;\n\n constructor(options: { apiKey?: string; stripe?: Stripe } = {}) {\n if (options.stripe) {\n this.stripe = options.stripe;\n } else {\n const apiKey = options.apiKey || process.env.STRIPE_SECRET_KEY;\n if (!apiKey) {\n throw new Error('[vibezcheck] Stripe API key required for billing operations.');\n }\n this.stripe = new Stripe(apiKey);\n }\n }\n\n /**\n * Creates a Stripe Customer Portal session URL where users can manage cards, view usage & invoices\n */\n public async createPortalSession(params: CreatePortalSessionParams): Promise<string> {\n const session = await this.stripe.billingPortal.sessions.create({\n customer: params.customerId,\n return_url: params.returnUrl,\n });\n return session.url;\n }\n\n /**\n * Creates a Stripe Checkout session to subscribe a customer to a metered pricing tier\n */\n public async createCheckoutSession(params: CreateCheckoutSessionParams): Promise<string> {\n const session = await this.stripe.checkout.sessions.create({\n customer: params.customerId,\n customer_email: !params.customerId ? params.customerEmail : undefined,\n line_items: [\n {\n price: params.priceId,\n quantity: 1,\n },\n ],\n mode: params.mode || 'subscription',\n success_url: `${params.returnUrl}?session_id={CHECKOUT_SESSION_ID}&status=success`,\n cancel_url: `${params.returnUrl}?status=cancelled`,\n metadata: params.metadata,\n });\n\n if (!session.url) {\n throw new Error('[vibezcheck] Failed to generate checkout session URL.');\n }\n\n return session.url;\n }\n\n /**\n * Creates a Checkout session for topping up prepaid credit wallet balance\n */\n public async createTopUpSession(params: CreateTopUpSessionParams): Promise<string> {\n const session = await this.stripe.checkout.sessions.create({\n customer: params.customerId,\n line_items: [\n {\n price_data: {\n currency: params.currency || 'usd',\n unit_amount: params.amountCents,\n product_data: {\n name: 'AI Token Credits Top-Up',\n description: `Add $${(params.amountCents / 100).toFixed(2)} in AI inference credits`,\n },\n },\n quantity: 1,\n },\n ],\n mode: 'payment',\n success_url: `${params.returnUrl}?status=success&amount=${params.amountCents}`,\n cancel_url: `${params.returnUrl}?status=cancelled`,\n metadata: {\n type: 'vibezcheck_topup',\n customerId: params.customerId,\n amountCents: params.amountCents.toString(),\n },\n });\n\n if (!session.url) {\n throw new Error('[vibezcheck] Failed to generate top-up checkout URL.');\n }\n\n return session.url;\n }\n}\n\n/**\n * Factory to create BillingHelper\n */\nexport function createBillingHelper(options: { apiKey?: string; stripe?: Stripe } = {}): BillingHelper {\n return new BillingHelper(options);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAmB;AAuBZ,IAAM,gBAAN,MAAoB;AAAA,EACjB;AAAA,EAER,YAAY,UAAgD,CAAC,GAAG;AAC9D,QAAI,QAAQ,QAAQ;AAClB,WAAK,SAAS,QAAQ;AAAA,IACxB,OAAO;AACL,YAAM,SAAS,QAAQ,UAAU,QAAQ,IAAI;AAC7C,UAAI,CAAC,QAAQ;AACX,cAAM,IAAI,MAAM,8DAA8D;AAAA,MAChF;AACA,WAAK,SAAS,IAAI,cAAAA,QAAO,MAAM;AAAA,IACjC;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,MAAa,oBAAoB,QAAoD;AACnF,UAAM,UAAU,MAAM,KAAK,OAAO,cAAc,SAAS,OAAO;AAAA,MAC9D,UAAU,OAAO;AAAA,MACjB,YAAY,OAAO;AAAA,IACrB,CAAC;AACD,WAAO,QAAQ;AAAA,EACjB;AAAA;AAAA;AAAA;AAAA,EAKA,MAAa,sBAAsB,QAAsD;AACvF,UAAM,UAAU,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;AACrG,SAAO,IAAI,cAAc,OAAO;AAClC;","names":["Stripe"]}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// src/billing/sessions.ts
|
|
2
|
+
import Stripe from "stripe";
|
|
3
|
+
var BillingHelper = class {
|
|
4
|
+
stripe;
|
|
5
|
+
constructor(options = {}) {
|
|
6
|
+
if (options.stripe) {
|
|
7
|
+
this.stripe = options.stripe;
|
|
8
|
+
} else {
|
|
9
|
+
const apiKey = options.apiKey || process.env.STRIPE_SECRET_KEY;
|
|
10
|
+
if (!apiKey) {
|
|
11
|
+
throw new Error("[vibezcheck] Stripe API key required for billing operations.");
|
|
12
|
+
}
|
|
13
|
+
this.stripe = new Stripe(apiKey);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Creates a Stripe Customer Portal session URL where users can manage cards, view usage & invoices
|
|
18
|
+
*/
|
|
19
|
+
async createPortalSession(params) {
|
|
20
|
+
const session = await this.stripe.billingPortal.sessions.create({
|
|
21
|
+
customer: params.customerId,
|
|
22
|
+
return_url: params.returnUrl
|
|
23
|
+
});
|
|
24
|
+
return session.url;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Creates a Stripe Checkout session to subscribe a customer to a metered pricing tier
|
|
28
|
+
*/
|
|
29
|
+
async createCheckoutSession(params) {
|
|
30
|
+
const session = await this.stripe.checkout.sessions.create({
|
|
31
|
+
customer: params.customerId,
|
|
32
|
+
customer_email: !params.customerId ? params.customerEmail : void 0,
|
|
33
|
+
line_items: [
|
|
34
|
+
{
|
|
35
|
+
price: params.priceId,
|
|
36
|
+
quantity: 1
|
|
37
|
+
}
|
|
38
|
+
],
|
|
39
|
+
mode: params.mode || "subscription",
|
|
40
|
+
success_url: `${params.returnUrl}?session_id={CHECKOUT_SESSION_ID}&status=success`,
|
|
41
|
+
cancel_url: `${params.returnUrl}?status=cancelled`,
|
|
42
|
+
metadata: params.metadata
|
|
43
|
+
});
|
|
44
|
+
if (!session.url) {
|
|
45
|
+
throw new Error("[vibezcheck] Failed to generate checkout session URL.");
|
|
46
|
+
}
|
|
47
|
+
return session.url;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Creates a Checkout session for topping up prepaid credit wallet balance
|
|
51
|
+
*/
|
|
52
|
+
async createTopUpSession(params) {
|
|
53
|
+
const session = await this.stripe.checkout.sessions.create({
|
|
54
|
+
customer: params.customerId,
|
|
55
|
+
line_items: [
|
|
56
|
+
{
|
|
57
|
+
price_data: {
|
|
58
|
+
currency: params.currency || "usd",
|
|
59
|
+
unit_amount: params.amountCents,
|
|
60
|
+
product_data: {
|
|
61
|
+
name: "AI Token Credits Top-Up",
|
|
62
|
+
description: `Add $${(params.amountCents / 100).toFixed(2)} in AI inference credits`
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
quantity: 1
|
|
66
|
+
}
|
|
67
|
+
],
|
|
68
|
+
mode: "payment",
|
|
69
|
+
success_url: `${params.returnUrl}?status=success&amount=${params.amountCents}`,
|
|
70
|
+
cancel_url: `${params.returnUrl}?status=cancelled`,
|
|
71
|
+
metadata: {
|
|
72
|
+
type: "vibezcheck_topup",
|
|
73
|
+
customerId: params.customerId,
|
|
74
|
+
amountCents: params.amountCents.toString()
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
if (!session.url) {
|
|
78
|
+
throw new Error("[vibezcheck] Failed to generate top-up checkout URL.");
|
|
79
|
+
}
|
|
80
|
+
return session.url;
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
function createBillingHelper(options = {}) {
|
|
84
|
+
return new BillingHelper(options);
|
|
85
|
+
}
|
|
86
|
+
export {
|
|
87
|
+
BillingHelper,
|
|
88
|
+
createBillingHelper
|
|
89
|
+
};
|
|
90
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/billing/sessions.ts"],"sourcesContent":["import Stripe from 'stripe';\n\nexport interface CreatePortalSessionParams {\n customerId: string;\n returnUrl: string;\n}\n\nexport interface CreateCheckoutSessionParams {\n customerId?: string;\n customerEmail?: string;\n priceId: string;\n returnUrl: string;\n mode?: 'subscription' | 'payment';\n metadata?: Record<string, string>;\n}\n\nexport interface CreateTopUpSessionParams {\n customerId: string;\n amountCents: number;\n returnUrl: string;\n currency?: string;\n}\n\nexport class BillingHelper {\n private stripe: Stripe;\n\n constructor(options: { apiKey?: string; stripe?: Stripe } = {}) {\n if (options.stripe) {\n this.stripe = options.stripe;\n } else {\n const apiKey = options.apiKey || process.env.STRIPE_SECRET_KEY;\n if (!apiKey) {\n throw new Error('[vibezcheck] Stripe API key required for billing operations.');\n }\n this.stripe = new Stripe(apiKey);\n }\n }\n\n /**\n * Creates a Stripe Customer Portal session URL where users can manage cards, view usage & invoices\n */\n public async createPortalSession(params: CreatePortalSessionParams): Promise<string> {\n const session = await this.stripe.billingPortal.sessions.create({\n customer: params.customerId,\n return_url: params.returnUrl,\n });\n return session.url;\n }\n\n /**\n * Creates a Stripe Checkout session to subscribe a customer to a metered pricing tier\n */\n public async createCheckoutSession(params: CreateCheckoutSessionParams): Promise<string> {\n const session = await this.stripe.checkout.sessions.create({\n customer: params.customerId,\n customer_email: !params.customerId ? params.customerEmail : undefined,\n line_items: [\n {\n price: params.priceId,\n quantity: 1,\n },\n ],\n mode: params.mode || 'subscription',\n success_url: `${params.returnUrl}?session_id={CHECKOUT_SESSION_ID}&status=success`,\n cancel_url: `${params.returnUrl}?status=cancelled`,\n metadata: params.metadata,\n });\n\n if (!session.url) {\n throw new Error('[vibezcheck] Failed to generate checkout session URL.');\n }\n\n return session.url;\n }\n\n /**\n * Creates a Checkout session for topping up prepaid credit wallet balance\n */\n public async createTopUpSession(params: CreateTopUpSessionParams): Promise<string> {\n const session = await this.stripe.checkout.sessions.create({\n customer: params.customerId,\n line_items: [\n {\n price_data: {\n currency: params.currency || 'usd',\n unit_amount: params.amountCents,\n product_data: {\n name: 'AI Token Credits Top-Up',\n description: `Add $${(params.amountCents / 100).toFixed(2)} in AI inference credits`,\n },\n },\n quantity: 1,\n },\n ],\n mode: 'payment',\n success_url: `${params.returnUrl}?status=success&amount=${params.amountCents}`,\n cancel_url: `${params.returnUrl}?status=cancelled`,\n metadata: {\n type: 'vibezcheck_topup',\n customerId: params.customerId,\n amountCents: params.amountCents.toString(),\n },\n });\n\n if (!session.url) {\n throw new Error('[vibezcheck] Failed to generate top-up checkout URL.');\n }\n\n return session.url;\n }\n}\n\n/**\n * Factory to create BillingHelper\n */\nexport function createBillingHelper(options: { apiKey?: string; stripe?: Stripe } = {}): BillingHelper {\n return new BillingHelper(options);\n}\n"],"mappings":";AAAA,OAAO,YAAY;AAuBZ,IAAM,gBAAN,MAAoB;AAAA,EACjB;AAAA,EAER,YAAY,UAAgD,CAAC,GAAG;AAC9D,QAAI,QAAQ,QAAQ;AAClB,WAAK,SAAS,QAAQ;AAAA,IACxB,OAAO;AACL,YAAM,SAAS,QAAQ,UAAU,QAAQ,IAAI;AAC7C,UAAI,CAAC,QAAQ;AACX,cAAM,IAAI,MAAM,8DAA8D;AAAA,MAChF;AACA,WAAK,SAAS,IAAI,OAAO,MAAM;AAAA,IACjC;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,MAAa,oBAAoB,QAAoD;AACnF,UAAM,UAAU,MAAM,KAAK,OAAO,cAAc,SAAS,OAAO;AAAA,MAC9D,UAAU,OAAO;AAAA,MACjB,YAAY,OAAO;AAAA,IACrB,CAAC;AACD,WAAO,QAAQ;AAAA,EACjB;AAAA;AAAA;AAAA;AAAA,EAKA,MAAa,sBAAsB,QAAsD;AACvF,UAAM,UAAU,MAAM,KAAK,OAAO,SAAS,SAAS,OAAO;AAAA,MACzD,UAAU,OAAO;AAAA,MACjB,gBAAgB,CAAC,OAAO,aAAa,OAAO,gBAAgB;AAAA,MAC5D,YAAY;AAAA,QACV;AAAA,UACE,OAAO,OAAO;AAAA,UACd,UAAU;AAAA,QACZ;AAAA,MACF;AAAA,MACA,MAAM,OAAO,QAAQ;AAAA,MACrB,aAAa,GAAG,OAAO,SAAS;AAAA,MAChC,YAAY,GAAG,OAAO,SAAS;AAAA,MAC/B,UAAU,OAAO;AAAA,IACnB,CAAC;AAED,QAAI,CAAC,QAAQ,KAAK;AAChB,YAAM,IAAI,MAAM,uDAAuD;AAAA,IACzE;AAEA,WAAO,QAAQ;AAAA,EACjB;AAAA;AAAA;AAAA;AAAA,EAKA,MAAa,mBAAmB,QAAmD;AACjF,UAAM,UAAU,MAAM,KAAK,OAAO,SAAS,SAAS,OAAO;AAAA,MACzD,UAAU,OAAO;AAAA,MACjB,YAAY;AAAA,QACV;AAAA,UACE,YAAY;AAAA,YACV,UAAU,OAAO,YAAY;AAAA,YAC7B,aAAa,OAAO;AAAA,YACpB,cAAc;AAAA,cACZ,MAAM;AAAA,cACN,aAAa,SAAS,OAAO,cAAc,KAAK,QAAQ,CAAC,CAAC;AAAA,YAC5D;AAAA,UACF;AAAA,UACA,UAAU;AAAA,QACZ;AAAA,MACF;AAAA,MACA,MAAM;AAAA,MACN,aAAa,GAAG,OAAO,SAAS,0BAA0B,OAAO,WAAW;AAAA,MAC5E,YAAY,GAAG,OAAO,SAAS;AAAA,MAC/B,UAAU;AAAA,QACR,MAAM;AAAA,QACN,YAAY,OAAO;AAAA,QACnB,aAAa,OAAO,YAAY,SAAS;AAAA,MAC3C;AAAA,IACF,CAAC;AAED,QAAI,CAAC,QAAQ,KAAK;AAChB,YAAM,IAAI,MAAM,sDAAsD;AAAA,IACxE;AAEA,WAAO,QAAQ;AAAA,EACjB;AACF;AAKO,SAAS,oBAAoB,UAAgD,CAAC,GAAkB;AACrG,SAAO,IAAI,cAAc,OAAO;AAClC;","names":[]}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { M as MeterOptions, C as CustomerParam, U as UsageEvent, S as StreamWrapOptions, R as RecordUsageOptions, a as UsageSummary } from './types-CSrSmsd1.mjs';
|
|
2
|
+
|
|
3
|
+
declare class VibezMeter {
|
|
4
|
+
private batcher;
|
|
5
|
+
private stripeClient?;
|
|
6
|
+
private markupMultiplier?;
|
|
7
|
+
constructor(options?: MeterOptions);
|
|
8
|
+
/**
|
|
9
|
+
* Track token usage from a non-streaming response object (OpenAI, Anthropic, Gemini, etc.)
|
|
10
|
+
*/
|
|
11
|
+
trackUsage(response: any, options?: {
|
|
12
|
+
customer?: CustomerParam;
|
|
13
|
+
customerId?: string;
|
|
14
|
+
model?: string;
|
|
15
|
+
provider?: string;
|
|
16
|
+
metadata?: Record<string, string | number | boolean>;
|
|
17
|
+
}): UsageEvent | null;
|
|
18
|
+
/**
|
|
19
|
+
* Wrap any LLM stream (OpenAI, Anthropic, Gemini) with zero added latency
|
|
20
|
+
*/
|
|
21
|
+
wrapStream<T>(stream: T, options?: StreamWrapOptions): T;
|
|
22
|
+
/**
|
|
23
|
+
* Directly record token usage manually
|
|
24
|
+
*/
|
|
25
|
+
recordUsage(options: RecordUsageOptions): UsageEvent;
|
|
26
|
+
/**
|
|
27
|
+
* Flush pending events to Stripe (vital for Serverless & Edge environments)
|
|
28
|
+
*/
|
|
29
|
+
flush(): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Get in-memory aggregated usage statistics
|
|
32
|
+
*/
|
|
33
|
+
getUsageSummary(): UsageSummary;
|
|
34
|
+
/**
|
|
35
|
+
* Reset in-memory ledger
|
|
36
|
+
*/
|
|
37
|
+
resetSummary(): void;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Factory function to create a VibezMeter instance
|
|
41
|
+
*/
|
|
42
|
+
declare function createMeter(options?: MeterOptions): VibezMeter;
|
|
43
|
+
|
|
44
|
+
export { VibezMeter as V, createMeter as c };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { M as MeterOptions, C as CustomerParam, U as UsageEvent, S as StreamWrapOptions, R as RecordUsageOptions, a as UsageSummary } from './types-CSrSmsd1.js';
|
|
2
|
+
|
|
3
|
+
declare class VibezMeter {
|
|
4
|
+
private batcher;
|
|
5
|
+
private stripeClient?;
|
|
6
|
+
private markupMultiplier?;
|
|
7
|
+
constructor(options?: MeterOptions);
|
|
8
|
+
/**
|
|
9
|
+
* Track token usage from a non-streaming response object (OpenAI, Anthropic, Gemini, etc.)
|
|
10
|
+
*/
|
|
11
|
+
trackUsage(response: any, options?: {
|
|
12
|
+
customer?: CustomerParam;
|
|
13
|
+
customerId?: string;
|
|
14
|
+
model?: string;
|
|
15
|
+
provider?: string;
|
|
16
|
+
metadata?: Record<string, string | number | boolean>;
|
|
17
|
+
}): UsageEvent | null;
|
|
18
|
+
/**
|
|
19
|
+
* Wrap any LLM stream (OpenAI, Anthropic, Gemini) with zero added latency
|
|
20
|
+
*/
|
|
21
|
+
wrapStream<T>(stream: T, options?: StreamWrapOptions): T;
|
|
22
|
+
/**
|
|
23
|
+
* Directly record token usage manually
|
|
24
|
+
*/
|
|
25
|
+
recordUsage(options: RecordUsageOptions): UsageEvent;
|
|
26
|
+
/**
|
|
27
|
+
* Flush pending events to Stripe (vital for Serverless & Edge environments)
|
|
28
|
+
*/
|
|
29
|
+
flush(): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Get in-memory aggregated usage statistics
|
|
32
|
+
*/
|
|
33
|
+
getUsageSummary(): UsageSummary;
|
|
34
|
+
/**
|
|
35
|
+
* Reset in-memory ledger
|
|
36
|
+
*/
|
|
37
|
+
resetSummary(): void;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Factory function to create a VibezMeter instance
|
|
41
|
+
*/
|
|
42
|
+
declare function createMeter(options?: MeterOptions): VibezMeter;
|
|
43
|
+
|
|
44
|
+
export { VibezMeter as V, createMeter as c };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import Stripe from 'stripe';
|
|
2
|
+
|
|
3
|
+
interface GetOrCreateCustomerParams {
|
|
4
|
+
userId: string;
|
|
5
|
+
email?: string;
|
|
6
|
+
name?: string;
|
|
7
|
+
metadata?: Record<string, string>;
|
|
8
|
+
}
|
|
9
|
+
interface CustomerManagerOptions {
|
|
10
|
+
apiKey?: string;
|
|
11
|
+
stripe?: Stripe;
|
|
12
|
+
cacheTtlMs?: number;
|
|
13
|
+
}
|
|
14
|
+
declare class CustomerManager {
|
|
15
|
+
private stripe;
|
|
16
|
+
private cache;
|
|
17
|
+
constructor(options?: CustomerManagerOptions);
|
|
18
|
+
/**
|
|
19
|
+
* Retrieves existing Stripe Customer or automatically provisions a new one
|
|
20
|
+
*/
|
|
21
|
+
getOrCreate(params: GetOrCreateCustomerParams): Promise<{
|
|
22
|
+
id: string;
|
|
23
|
+
isNew: boolean;
|
|
24
|
+
customer: Stripe.Customer;
|
|
25
|
+
}>;
|
|
26
|
+
/**
|
|
27
|
+
* Clears in-memory resolution cache
|
|
28
|
+
*/
|
|
29
|
+
clearCache(): void;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Factory to create CustomerManager
|
|
33
|
+
*/
|
|
34
|
+
declare function createCustomerManager(options?: CustomerManagerOptions): CustomerManager;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* In-memory Customer Resolution Cache with TTL
|
|
38
|
+
*/
|
|
39
|
+
declare class CustomerCache {
|
|
40
|
+
private cache;
|
|
41
|
+
private readonly defaultTtlMs;
|
|
42
|
+
constructor(defaultTtlMs?: number);
|
|
43
|
+
get(key: string): string | null;
|
|
44
|
+
set(key: string, customerId: string, ttlMs?: number): void;
|
|
45
|
+
delete(key: string): void;
|
|
46
|
+
clear(): void;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export { CustomerCache, CustomerManager, type CustomerManagerOptions, type GetOrCreateCustomerParams, createCustomerManager };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import Stripe from 'stripe';
|
|
2
|
+
|
|
3
|
+
interface GetOrCreateCustomerParams {
|
|
4
|
+
userId: string;
|
|
5
|
+
email?: string;
|
|
6
|
+
name?: string;
|
|
7
|
+
metadata?: Record<string, string>;
|
|
8
|
+
}
|
|
9
|
+
interface CustomerManagerOptions {
|
|
10
|
+
apiKey?: string;
|
|
11
|
+
stripe?: Stripe;
|
|
12
|
+
cacheTtlMs?: number;
|
|
13
|
+
}
|
|
14
|
+
declare class CustomerManager {
|
|
15
|
+
private stripe;
|
|
16
|
+
private cache;
|
|
17
|
+
constructor(options?: CustomerManagerOptions);
|
|
18
|
+
/**
|
|
19
|
+
* Retrieves existing Stripe Customer or automatically provisions a new one
|
|
20
|
+
*/
|
|
21
|
+
getOrCreate(params: GetOrCreateCustomerParams): Promise<{
|
|
22
|
+
id: string;
|
|
23
|
+
isNew: boolean;
|
|
24
|
+
customer: Stripe.Customer;
|
|
25
|
+
}>;
|
|
26
|
+
/**
|
|
27
|
+
* Clears in-memory resolution cache
|
|
28
|
+
*/
|
|
29
|
+
clearCache(): void;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Factory to create CustomerManager
|
|
33
|
+
*/
|
|
34
|
+
declare function createCustomerManager(options?: CustomerManagerOptions): CustomerManager;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* In-memory Customer Resolution Cache with TTL
|
|
38
|
+
*/
|
|
39
|
+
declare class CustomerCache {
|
|
40
|
+
private cache;
|
|
41
|
+
private readonly defaultTtlMs;
|
|
42
|
+
constructor(defaultTtlMs?: number);
|
|
43
|
+
get(key: string): string | null;
|
|
44
|
+
set(key: string, customerId: string, ttlMs?: number): void;
|
|
45
|
+
delete(key: string): void;
|
|
46
|
+
clear(): void;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export { CustomerCache, CustomerManager, type CustomerManagerOptions, type GetOrCreateCustomerParams, createCustomerManager };
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/customers/index.ts
|
|
31
|
+
var customers_exports = {};
|
|
32
|
+
__export(customers_exports, {
|
|
33
|
+
CustomerCache: () => CustomerCache,
|
|
34
|
+
CustomerManager: () => CustomerManager,
|
|
35
|
+
createCustomerManager: () => createCustomerManager
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(customers_exports);
|
|
38
|
+
|
|
39
|
+
// src/customers/manager.ts
|
|
40
|
+
var import_stripe = __toESM(require("stripe"));
|
|
41
|
+
|
|
42
|
+
// src/customers/cache.ts
|
|
43
|
+
var CustomerCache = class {
|
|
44
|
+
cache = /* @__PURE__ */ new Map();
|
|
45
|
+
defaultTtlMs;
|
|
46
|
+
constructor(defaultTtlMs = 1e3 * 60 * 60) {
|
|
47
|
+
this.defaultTtlMs = defaultTtlMs;
|
|
48
|
+
}
|
|
49
|
+
get(key) {
|
|
50
|
+
const entry = this.cache.get(key);
|
|
51
|
+
if (!entry) return null;
|
|
52
|
+
if (Date.now() > entry.expiresAt) {
|
|
53
|
+
this.cache.delete(key);
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
return entry.customerId;
|
|
57
|
+
}
|
|
58
|
+
set(key, customerId, ttlMs) {
|
|
59
|
+
const expiresAt = Date.now() + (ttlMs ?? this.defaultTtlMs);
|
|
60
|
+
this.cache.set(key, { customerId, expiresAt });
|
|
61
|
+
}
|
|
62
|
+
delete(key) {
|
|
63
|
+
this.cache.delete(key);
|
|
64
|
+
}
|
|
65
|
+
clear() {
|
|
66
|
+
this.cache.clear();
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// src/customers/manager.ts
|
|
71
|
+
var CustomerManager = class {
|
|
72
|
+
stripe;
|
|
73
|
+
cache;
|
|
74
|
+
constructor(options = {}) {
|
|
75
|
+
if (options.stripe) {
|
|
76
|
+
this.stripe = options.stripe;
|
|
77
|
+
} else {
|
|
78
|
+
const apiKey = options.apiKey || process.env.STRIPE_SECRET_KEY;
|
|
79
|
+
if (!apiKey) {
|
|
80
|
+
throw new Error("[vibezcheck] Stripe API key required for customer management.");
|
|
81
|
+
}
|
|
82
|
+
this.stripe = new import_stripe.default(apiKey);
|
|
83
|
+
}
|
|
84
|
+
this.cache = new CustomerCache(options.cacheTtlMs);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Retrieves existing Stripe Customer or automatically provisions a new one
|
|
88
|
+
*/
|
|
89
|
+
async getOrCreate(params) {
|
|
90
|
+
const cacheKey = params.userId || params.email;
|
|
91
|
+
if (cacheKey) {
|
|
92
|
+
const cachedId = this.cache.get(cacheKey);
|
|
93
|
+
if (cachedId) {
|
|
94
|
+
return {
|
|
95
|
+
id: cachedId,
|
|
96
|
+
isNew: false,
|
|
97
|
+
customer: { id: cachedId }
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (params.userId) {
|
|
102
|
+
try {
|
|
103
|
+
const searchResult = await this.stripe.customers.search({
|
|
104
|
+
query: `metadata['vibez_user_id']:'${params.userId}'`,
|
|
105
|
+
limit: 1
|
|
106
|
+
});
|
|
107
|
+
if (searchResult.data.length > 0) {
|
|
108
|
+
const customer = searchResult.data[0];
|
|
109
|
+
if (cacheKey) this.cache.set(cacheKey, customer.id);
|
|
110
|
+
if (params.email) this.cache.set(params.email, customer.id);
|
|
111
|
+
return { id: customer.id, isNew: false, customer };
|
|
112
|
+
}
|
|
113
|
+
} catch {
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (params.email) {
|
|
117
|
+
const listResult = await this.stripe.customers.list({
|
|
118
|
+
email: params.email,
|
|
119
|
+
limit: 1
|
|
120
|
+
});
|
|
121
|
+
if (listResult.data.length > 0) {
|
|
122
|
+
const customer = listResult.data[0];
|
|
123
|
+
if (cacheKey) this.cache.set(cacheKey, customer.id);
|
|
124
|
+
if (params.userId) this.cache.set(params.userId, customer.id);
|
|
125
|
+
return { id: customer.id, isNew: false, customer };
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
const newCustomer = await this.stripe.customers.create({
|
|
129
|
+
email: params.email,
|
|
130
|
+
name: params.name,
|
|
131
|
+
metadata: {
|
|
132
|
+
vibez_user_id: params.userId,
|
|
133
|
+
created_by: "vibezcheck",
|
|
134
|
+
...params.metadata || {}
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
if (cacheKey) this.cache.set(cacheKey, newCustomer.id);
|
|
138
|
+
if (params.userId) this.cache.set(params.userId, newCustomer.id);
|
|
139
|
+
if (params.email) this.cache.set(params.email, newCustomer.id);
|
|
140
|
+
return { id: newCustomer.id, isNew: true, customer: newCustomer };
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Clears in-memory resolution cache
|
|
144
|
+
*/
|
|
145
|
+
clearCache() {
|
|
146
|
+
this.cache.clear();
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
function createCustomerManager(options = {}) {
|
|
150
|
+
return new CustomerManager(options);
|
|
151
|
+
}
|
|
152
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
153
|
+
0 && (module.exports = {
|
|
154
|
+
CustomerCache,
|
|
155
|
+
CustomerManager,
|
|
156
|
+
createCustomerManager
|
|
157
|
+
});
|
|
158
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/customers/index.ts","../../src/customers/manager.ts","../../src/customers/cache.ts"],"sourcesContent":["export * from './manager';\nexport * from './cache';\n","import Stripe from 'stripe';\nimport { CustomerCache } from './cache';\n\nexport interface GetOrCreateCustomerParams {\n userId: string;\n email?: string;\n name?: string;\n metadata?: Record<string, string>;\n}\n\nexport interface CustomerManagerOptions {\n apiKey?: string;\n stripe?: Stripe;\n cacheTtlMs?: number;\n}\n\nexport class CustomerManager {\n private stripe: Stripe;\n private cache: CustomerCache;\n\n constructor(options: CustomerManagerOptions = {}) {\n if (options.stripe) {\n this.stripe = options.stripe;\n } else {\n const apiKey = options.apiKey || process.env.STRIPE_SECRET_KEY;\n if (!apiKey) {\n throw new Error('[vibezcheck] Stripe API key required for customer management.');\n }\n this.stripe = new Stripe(apiKey);\n }\n\n this.cache = new CustomerCache(options.cacheTtlMs);\n }\n\n /**\n * Retrieves existing Stripe Customer or automatically provisions a new one\n */\n public async getOrCreate(\n params: GetOrCreateCustomerParams\n ): Promise<{ id: string; isNew: boolean; customer: Stripe.Customer }> {\n const cacheKey = params.userId || params.email;\n if (cacheKey) {\n const cachedId = this.cache.get(cacheKey);\n if (cachedId) {\n return {\n id: cachedId,\n isNew: false,\n customer: { id: cachedId } as Stripe.Customer,\n };\n }\n }\n\n // 1. Search by userId in Stripe metadata\n if (params.userId) {\n try {\n const searchResult = await this.stripe.customers.search({\n query: `metadata['vibez_user_id']:'${params.userId}'`,\n limit: 1,\n });\n\n if (searchResult.data.length > 0) {\n const customer = searchResult.data[0];\n if (cacheKey) this.cache.set(cacheKey, customer.id);\n if (params.email) this.cache.set(params.email, customer.id);\n return { id: customer.id, isNew: false, customer };\n }\n } catch {\n // Fallback to email search if search query is unsupported or errors\n }\n }\n\n // 2. Search by email if provided\n if (params.email) {\n const listResult = await this.stripe.customers.list({\n email: params.email,\n limit: 1,\n });\n\n if (listResult.data.length > 0) {\n const customer = listResult.data[0];\n if (cacheKey) this.cache.set(cacheKey, customer.id);\n if (params.userId) this.cache.set(params.userId, customer.id);\n return { id: customer.id, isNew: false, customer };\n }\n }\n\n // 3. Create new Customer in Stripe\n const newCustomer = await this.stripe.customers.create({\n email: params.email,\n name: params.name,\n metadata: {\n vibez_user_id: params.userId,\n created_by: 'vibezcheck',\n ...(params.metadata || {}),\n },\n });\n\n if (cacheKey) this.cache.set(cacheKey, newCustomer.id);\n if (params.userId) this.cache.set(params.userId, newCustomer.id);\n if (params.email) this.cache.set(params.email, newCustomer.id);\n\n return { id: newCustomer.id, isNew: true, customer: newCustomer };\n }\n\n /**\n * Clears in-memory resolution cache\n */\n public clearCache(): void {\n this.cache.clear();\n }\n}\n\n/**\n * Factory to create CustomerManager\n */\nexport function createCustomerManager(options: CustomerManagerOptions = {}): CustomerManager {\n return new CustomerManager(options);\n}\n","interface CacheEntry {\n customerId: string;\n expiresAt: number;\n}\n\n/**\n * In-memory Customer Resolution Cache with TTL\n */\nexport class CustomerCache {\n private cache = new Map<string, CacheEntry>();\n private readonly defaultTtlMs: number;\n\n constructor(defaultTtlMs: number = 1000 * 60 * 60) {\n // Default TTL: 1 hour\n this.defaultTtlMs = defaultTtlMs;\n }\n\n public get(key: string): string | null {\n const entry = this.cache.get(key);\n if (!entry) return null;\n\n if (Date.now() > entry.expiresAt) {\n this.cache.delete(key);\n return null;\n }\n\n return entry.customerId;\n }\n\n public set(key: string, customerId: string, ttlMs?: number): void {\n const expiresAt = Date.now() + (ttlMs ?? this.defaultTtlMs);\n this.cache.set(key, { customerId, expiresAt });\n }\n\n public delete(key: string): void {\n this.cache.delete(key);\n }\n\n public clear(): void {\n this.cache.clear();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAmB;;;ACQZ,IAAM,gBAAN,MAAoB;AAAA,EACjB,QAAQ,oBAAI,IAAwB;AAAA,EAC3B;AAAA,EAEjB,YAAY,eAAuB,MAAO,KAAK,IAAI;AAEjD,SAAK,eAAe;AAAA,EACtB;AAAA,EAEO,IAAI,KAA4B;AACrC,UAAM,QAAQ,KAAK,MAAM,IAAI,GAAG;AAChC,QAAI,CAAC,MAAO,QAAO;AAEnB,QAAI,KAAK,IAAI,IAAI,MAAM,WAAW;AAChC,WAAK,MAAM,OAAO,GAAG;AACrB,aAAO;AAAA,IACT;AAEA,WAAO,MAAM;AAAA,EACf;AAAA,EAEO,IAAI,KAAa,YAAoB,OAAsB;AAChE,UAAM,YAAY,KAAK,IAAI,KAAK,SAAS,KAAK;AAC9C,SAAK,MAAM,IAAI,KAAK,EAAE,YAAY,UAAU,CAAC;AAAA,EAC/C;AAAA,EAEO,OAAO,KAAmB;AAC/B,SAAK,MAAM,OAAO,GAAG;AAAA,EACvB;AAAA,EAEO,QAAc;AACnB,SAAK,MAAM,MAAM;AAAA,EACnB;AACF;;;ADzBO,IAAM,kBAAN,MAAsB;AAAA,EACnB;AAAA,EACA;AAAA,EAER,YAAY,UAAkC,CAAC,GAAG;AAChD,QAAI,QAAQ,QAAQ;AAClB,WAAK,SAAS,QAAQ;AAAA,IACxB,OAAO;AACL,YAAM,SAAS,QAAQ,UAAU,QAAQ,IAAI;AAC7C,UAAI,C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