@moneymq/better-auth 0.2.1
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/dist/client.d.mts +171 -0
- package/dist/client.d.ts +171 -0
- package/dist/client.js +118 -0
- package/dist/client.js.map +1 -0
- package/dist/client.mjs +93 -0
- package/dist/client.mjs.map +1 -0
- package/dist/index.d.mts +421 -0
- package/dist/index.d.ts +421 -0
- package/dist/index.js +4557 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +4532 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +57 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
import * as _moneymq_sdk from '@moneymq/sdk';
|
|
2
|
+
import { MoneyMQ } from '@moneymq/sdk';
|
|
3
|
+
import * as better_auth from 'better-auth';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Usage record for billing
|
|
8
|
+
*/
|
|
9
|
+
interface UsageRecord {
|
|
10
|
+
id: string;
|
|
11
|
+
userId: string;
|
|
12
|
+
customerId: string;
|
|
13
|
+
metric: string;
|
|
14
|
+
quantity: number;
|
|
15
|
+
timestamp: Date;
|
|
16
|
+
metadata?: Record<string, string>;
|
|
17
|
+
billed: boolean;
|
|
18
|
+
billedAt?: Date;
|
|
19
|
+
paymentId?: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Usage metric configuration
|
|
23
|
+
*/
|
|
24
|
+
interface UsageMetric {
|
|
25
|
+
/**
|
|
26
|
+
* Unique identifier for this metric (e.g., "api_calls", "storage_gb")
|
|
27
|
+
*/
|
|
28
|
+
name: string;
|
|
29
|
+
/**
|
|
30
|
+
* MoneyMQ price ID for this metric
|
|
31
|
+
*/
|
|
32
|
+
priceId: string;
|
|
33
|
+
/**
|
|
34
|
+
* Human-readable display name
|
|
35
|
+
*/
|
|
36
|
+
displayName?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Unit of measurement (e.g., "requests", "GB", "minutes")
|
|
39
|
+
*/
|
|
40
|
+
unit?: string;
|
|
41
|
+
/**
|
|
42
|
+
* Aggregation method for billing period
|
|
43
|
+
* - 'sum': Total of all records (default)
|
|
44
|
+
* - 'max': Maximum value in period
|
|
45
|
+
* - 'last': Last recorded value
|
|
46
|
+
*/
|
|
47
|
+
aggregation?: 'sum' | 'max' | 'last';
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Usage billing configuration
|
|
51
|
+
*/
|
|
52
|
+
interface UsageBillingConfig {
|
|
53
|
+
/**
|
|
54
|
+
* Enable usage-based billing
|
|
55
|
+
*/
|
|
56
|
+
enabled: boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Available usage metrics
|
|
59
|
+
*/
|
|
60
|
+
metrics: UsageMetric[];
|
|
61
|
+
/**
|
|
62
|
+
* Callback after usage is recorded
|
|
63
|
+
*/
|
|
64
|
+
onUsageRecorded?: (record: UsageRecord) => void | Promise<void>;
|
|
65
|
+
/**
|
|
66
|
+
* Callback after usage is billed
|
|
67
|
+
*/
|
|
68
|
+
onUsageBilled?: (params: {
|
|
69
|
+
records: UsageRecord[];
|
|
70
|
+
paymentId: string;
|
|
71
|
+
amount: number;
|
|
72
|
+
}) => void | Promise<void>;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* MoneyMQ Better Auth plugin options
|
|
76
|
+
*/
|
|
77
|
+
interface MoneyMQPluginOptions {
|
|
78
|
+
/**
|
|
79
|
+
* MoneyMQ SDK client instance
|
|
80
|
+
*/
|
|
81
|
+
client: MoneyMQ;
|
|
82
|
+
/**
|
|
83
|
+
* Webhook secret for signature verification
|
|
84
|
+
*/
|
|
85
|
+
webhookSecret?: string;
|
|
86
|
+
/**
|
|
87
|
+
* Automatically create MoneyMQ customer on user signup
|
|
88
|
+
* @default true
|
|
89
|
+
*/
|
|
90
|
+
createCustomerOnSignUp?: boolean;
|
|
91
|
+
/**
|
|
92
|
+
* Callback after customer is created
|
|
93
|
+
*/
|
|
94
|
+
onCustomerCreate?: (params: {
|
|
95
|
+
customer: {
|
|
96
|
+
id: string;
|
|
97
|
+
email: string;
|
|
98
|
+
};
|
|
99
|
+
user: {
|
|
100
|
+
id: string;
|
|
101
|
+
email: string;
|
|
102
|
+
};
|
|
103
|
+
}) => void | Promise<void>;
|
|
104
|
+
/**
|
|
105
|
+
* Customize customer creation parameters
|
|
106
|
+
*/
|
|
107
|
+
getCustomerCreateParams?: (user: {
|
|
108
|
+
id: string;
|
|
109
|
+
email: string;
|
|
110
|
+
name?: string;
|
|
111
|
+
}) => {
|
|
112
|
+
email: string;
|
|
113
|
+
name?: string;
|
|
114
|
+
metadata?: Record<string, string>;
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* Handle custom webhook events
|
|
118
|
+
*/
|
|
119
|
+
onEvent?: (event: WebhookEvent) => void | Promise<void>;
|
|
120
|
+
/**
|
|
121
|
+
* Usage-based billing configuration
|
|
122
|
+
*/
|
|
123
|
+
usage?: UsageBillingConfig;
|
|
124
|
+
/**
|
|
125
|
+
* Schema customization
|
|
126
|
+
*/
|
|
127
|
+
schema?: {
|
|
128
|
+
usage?: {
|
|
129
|
+
modelName?: string;
|
|
130
|
+
fields?: Partial<Record<keyof UsageRecord, string>>;
|
|
131
|
+
};
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Webhook event from MoneyMQ
|
|
136
|
+
*/
|
|
137
|
+
interface WebhookEvent {
|
|
138
|
+
id: string;
|
|
139
|
+
type: WebhookEventType;
|
|
140
|
+
data: Record<string, unknown>;
|
|
141
|
+
created: number;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Supported webhook event types
|
|
145
|
+
*/
|
|
146
|
+
type WebhookEventType = 'payment.completed' | 'payment.failed' | 'checkout.completed' | 'checkout.expired' | 'customer.created' | 'customer.updated' | 'payout.completed' | 'payout.failed';
|
|
147
|
+
/**
|
|
148
|
+
* Client plugin options
|
|
149
|
+
*/
|
|
150
|
+
interface MoneyMQClientPluginOptions {
|
|
151
|
+
/**
|
|
152
|
+
* Enable usage tracking on client
|
|
153
|
+
*/
|
|
154
|
+
usage?: boolean;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Record usage parameters
|
|
158
|
+
*/
|
|
159
|
+
interface RecordUsageParams {
|
|
160
|
+
/**
|
|
161
|
+
* Metric name to record usage for
|
|
162
|
+
*/
|
|
163
|
+
metric: string;
|
|
164
|
+
/**
|
|
165
|
+
* Quantity to record
|
|
166
|
+
*/
|
|
167
|
+
quantity: number;
|
|
168
|
+
/**
|
|
169
|
+
* Optional metadata
|
|
170
|
+
*/
|
|
171
|
+
metadata?: Record<string, string>;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Get usage parameters
|
|
175
|
+
*/
|
|
176
|
+
interface GetUsageParams {
|
|
177
|
+
/**
|
|
178
|
+
* Metric name to get usage for (optional, returns all if not specified)
|
|
179
|
+
*/
|
|
180
|
+
metric?: string;
|
|
181
|
+
/**
|
|
182
|
+
* Start date for usage period
|
|
183
|
+
*/
|
|
184
|
+
startDate?: Date;
|
|
185
|
+
/**
|
|
186
|
+
* End date for usage period
|
|
187
|
+
*/
|
|
188
|
+
endDate?: Date;
|
|
189
|
+
/**
|
|
190
|
+
* Include billed usage records
|
|
191
|
+
* @default false
|
|
192
|
+
*/
|
|
193
|
+
includeBilled?: boolean;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Usage summary response
|
|
197
|
+
*/
|
|
198
|
+
interface UsageSummary {
|
|
199
|
+
metric: string;
|
|
200
|
+
displayName?: string;
|
|
201
|
+
unit?: string;
|
|
202
|
+
total: number;
|
|
203
|
+
unbilledTotal: number;
|
|
204
|
+
records: UsageRecord[];
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Create checkout parameters
|
|
208
|
+
*/
|
|
209
|
+
interface CreateCheckoutParams {
|
|
210
|
+
/**
|
|
211
|
+
* Line items for the checkout
|
|
212
|
+
*/
|
|
213
|
+
lineItems: Array<{
|
|
214
|
+
price: string;
|
|
215
|
+
quantity: number;
|
|
216
|
+
}>;
|
|
217
|
+
/**
|
|
218
|
+
* URL to redirect to on success
|
|
219
|
+
*/
|
|
220
|
+
successUrl: string;
|
|
221
|
+
/**
|
|
222
|
+
* URL to redirect to on cancel
|
|
223
|
+
*/
|
|
224
|
+
cancelUrl: string;
|
|
225
|
+
/**
|
|
226
|
+
* Optional metadata
|
|
227
|
+
*/
|
|
228
|
+
metadata?: Record<string, string>;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* MoneyMQ plugin for Better Auth
|
|
233
|
+
*
|
|
234
|
+
* Integrates MoneyMQ stablecoin payments with Better Auth for
|
|
235
|
+
* customer creation and usage-based billing.
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* ```typescript
|
|
239
|
+
* import { betterAuth } from 'better-auth';
|
|
240
|
+
* import { moneymq } from '@moneymq/better-auth';
|
|
241
|
+
* import { MoneyMQ } from '@moneymq/sdk';
|
|
242
|
+
*
|
|
243
|
+
* const moneymqClient = new MoneyMQ({
|
|
244
|
+
* endpoint: process.env.MONEYMQ_ENDPOINT!,
|
|
245
|
+
* secret: process.env.MONEYMQ_SECRET,
|
|
246
|
+
* });
|
|
247
|
+
*
|
|
248
|
+
* export const auth = betterAuth({
|
|
249
|
+
* plugins: [
|
|
250
|
+
* moneymq({
|
|
251
|
+
* client: moneymqClient,
|
|
252
|
+
* webhookSecret: process.env.MONEYMQ_WEBHOOK_SECRET,
|
|
253
|
+
* createCustomerOnSignUp: true,
|
|
254
|
+
* usage: {
|
|
255
|
+
* enabled: true,
|
|
256
|
+
* metrics: [
|
|
257
|
+
* { name: 'api_calls', priceId: 'price_xxx', unit: 'requests' },
|
|
258
|
+
* { name: 'storage', priceId: 'price_yyy', unit: 'GB' },
|
|
259
|
+
* ],
|
|
260
|
+
* },
|
|
261
|
+
* }),
|
|
262
|
+
* ],
|
|
263
|
+
* });
|
|
264
|
+
* ```
|
|
265
|
+
*/
|
|
266
|
+
declare const moneymq: (options: MoneyMQPluginOptions) => {
|
|
267
|
+
id: "moneymq";
|
|
268
|
+
endpoints: {
|
|
269
|
+
moneymqWebhook: better_auth.StrictEndpoint<"/moneymq/webhook", {
|
|
270
|
+
method: "POST";
|
|
271
|
+
metadata: {
|
|
272
|
+
isAction: false;
|
|
273
|
+
};
|
|
274
|
+
}, {
|
|
275
|
+
received: boolean;
|
|
276
|
+
}>;
|
|
277
|
+
recordUsage: better_auth.StrictEndpoint<"/moneymq/usage/record", {
|
|
278
|
+
method: "POST";
|
|
279
|
+
body: z.ZodObject<{
|
|
280
|
+
metric: z.ZodString;
|
|
281
|
+
quantity: z.ZodNumber;
|
|
282
|
+
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
283
|
+
}, "strip", z.ZodTypeAny, {
|
|
284
|
+
metric: string;
|
|
285
|
+
quantity: number;
|
|
286
|
+
metadata?: Record<string, string> | undefined;
|
|
287
|
+
}, {
|
|
288
|
+
metric: string;
|
|
289
|
+
quantity: number;
|
|
290
|
+
metadata?: Record<string, string> | undefined;
|
|
291
|
+
}>;
|
|
292
|
+
}, {
|
|
293
|
+
success: boolean;
|
|
294
|
+
record: UsageRecord;
|
|
295
|
+
}>;
|
|
296
|
+
getUsage: better_auth.StrictEndpoint<"/moneymq/usage", {
|
|
297
|
+
method: "GET";
|
|
298
|
+
query: z.ZodObject<{
|
|
299
|
+
metric: z.ZodOptional<z.ZodString>;
|
|
300
|
+
startDate: z.ZodOptional<z.ZodString>;
|
|
301
|
+
endDate: z.ZodOptional<z.ZodString>;
|
|
302
|
+
includeBilled: z.ZodOptional<z.ZodString>;
|
|
303
|
+
}, "strip", z.ZodTypeAny, {
|
|
304
|
+
metric?: string | undefined;
|
|
305
|
+
startDate?: string | undefined;
|
|
306
|
+
endDate?: string | undefined;
|
|
307
|
+
includeBilled?: string | undefined;
|
|
308
|
+
}, {
|
|
309
|
+
metric?: string | undefined;
|
|
310
|
+
startDate?: string | undefined;
|
|
311
|
+
endDate?: string | undefined;
|
|
312
|
+
includeBilled?: string | undefined;
|
|
313
|
+
}>;
|
|
314
|
+
}, {
|
|
315
|
+
usage: {
|
|
316
|
+
metric: string;
|
|
317
|
+
displayName: string | undefined;
|
|
318
|
+
unit: string | undefined;
|
|
319
|
+
total: number;
|
|
320
|
+
unbilledTotal: number;
|
|
321
|
+
records: UsageRecord[];
|
|
322
|
+
}[];
|
|
323
|
+
}>;
|
|
324
|
+
createUsageCheckout: better_auth.StrictEndpoint<"/moneymq/usage/checkout", {
|
|
325
|
+
method: "POST";
|
|
326
|
+
body: z.ZodObject<{
|
|
327
|
+
metrics: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
328
|
+
successUrl: z.ZodString;
|
|
329
|
+
cancelUrl: z.ZodString;
|
|
330
|
+
}, "strip", z.ZodTypeAny, {
|
|
331
|
+
successUrl: string;
|
|
332
|
+
cancelUrl: string;
|
|
333
|
+
metrics?: string[] | undefined;
|
|
334
|
+
}, {
|
|
335
|
+
successUrl: string;
|
|
336
|
+
cancelUrl: string;
|
|
337
|
+
metrics?: string[] | undefined;
|
|
338
|
+
}>;
|
|
339
|
+
}, {
|
|
340
|
+
url: string;
|
|
341
|
+
sessionId: string;
|
|
342
|
+
lineItems: {
|
|
343
|
+
price: string;
|
|
344
|
+
quantity: number;
|
|
345
|
+
}[];
|
|
346
|
+
totalRecords: number;
|
|
347
|
+
}>;
|
|
348
|
+
createCheckout: better_auth.StrictEndpoint<"/moneymq/checkout", {
|
|
349
|
+
method: "POST";
|
|
350
|
+
body: z.ZodObject<{
|
|
351
|
+
lineItems: z.ZodArray<z.ZodObject<{
|
|
352
|
+
price: z.ZodString;
|
|
353
|
+
quantity: z.ZodNumber;
|
|
354
|
+
}, "strip", z.ZodTypeAny, {
|
|
355
|
+
quantity: number;
|
|
356
|
+
price: string;
|
|
357
|
+
}, {
|
|
358
|
+
quantity: number;
|
|
359
|
+
price: string;
|
|
360
|
+
}>, "many">;
|
|
361
|
+
successUrl: z.ZodString;
|
|
362
|
+
cancelUrl: z.ZodString;
|
|
363
|
+
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
364
|
+
}, "strip", z.ZodTypeAny, {
|
|
365
|
+
successUrl: string;
|
|
366
|
+
cancelUrl: string;
|
|
367
|
+
lineItems: {
|
|
368
|
+
quantity: number;
|
|
369
|
+
price: string;
|
|
370
|
+
}[];
|
|
371
|
+
metadata?: Record<string, string> | undefined;
|
|
372
|
+
}, {
|
|
373
|
+
successUrl: string;
|
|
374
|
+
cancelUrl: string;
|
|
375
|
+
lineItems: {
|
|
376
|
+
quantity: number;
|
|
377
|
+
price: string;
|
|
378
|
+
}[];
|
|
379
|
+
metadata?: Record<string, string> | undefined;
|
|
380
|
+
}>;
|
|
381
|
+
}, {
|
|
382
|
+
url: string;
|
|
383
|
+
sessionId: string;
|
|
384
|
+
}>;
|
|
385
|
+
getCustomer: better_auth.StrictEndpoint<"/moneymq/customer", {
|
|
386
|
+
method: "GET";
|
|
387
|
+
}, {
|
|
388
|
+
customer: null;
|
|
389
|
+
} | {
|
|
390
|
+
customer: _moneymq_sdk.Customer;
|
|
391
|
+
}>;
|
|
392
|
+
getMetrics: better_auth.StrictEndpoint<"/moneymq/metrics", {
|
|
393
|
+
method: "GET";
|
|
394
|
+
}, {
|
|
395
|
+
metrics: {
|
|
396
|
+
name: string;
|
|
397
|
+
displayName: string | undefined;
|
|
398
|
+
unit: string | undefined;
|
|
399
|
+
aggregation: "sum" | "max" | "last";
|
|
400
|
+
}[];
|
|
401
|
+
}>;
|
|
402
|
+
};
|
|
403
|
+
schema: {
|
|
404
|
+
user: {
|
|
405
|
+
fields: {
|
|
406
|
+
moneymqCustomerId: {
|
|
407
|
+
type: "string";
|
|
408
|
+
required: false;
|
|
409
|
+
};
|
|
410
|
+
};
|
|
411
|
+
};
|
|
412
|
+
};
|
|
413
|
+
hooks: {
|
|
414
|
+
after: {
|
|
415
|
+
matcher: (context: better_auth.HookEndpointContext) => boolean;
|
|
416
|
+
handler: (ctx: better_auth.MiddlewareInputContext<better_auth.MiddlewareOptions>) => Promise<void>;
|
|
417
|
+
}[];
|
|
418
|
+
};
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
export { type CreateCheckoutParams, type GetUsageParams, type MoneyMQClientPluginOptions, type MoneyMQPluginOptions, type RecordUsageParams, type UsageBillingConfig, type UsageMetric, type UsageRecord, type UsageSummary, type WebhookEvent, type WebhookEventType, moneymq as default, moneymq };
|