@drawbridge/drawbridge-stripe 0.1.14 → 0.1.16

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/index.js CHANGED
@@ -35,27 +35,29 @@ var require_billing = __commonJS({
35
35
  };
36
36
  };
37
37
  const billableItems = async () => {
38
- var _a, _b, _c, _d, _e, _f;
38
+ var _a;
39
39
  try {
40
- const items = [];
41
40
  const { data: components } = await axios.get(
42
41
  billing + "/pricing_plans/" + process.env.STRIPE_PRICING_AI_PLAN_ID + "/components",
43
42
  options
44
43
  );
45
- for (let index = 0; index < ((_a = components == null ? void 0 : components.data) == null ? void 0 : _a.length); index++) {
46
- const plan = (_b = components == null ? void 0 : components.data) == null ? void 0 : _b[index];
47
- if ((_c = plan == null ? void 0 : plan.rate_card) == null ? void 0 : _c.id) {
48
- const { data: rates } = await axios.get(
49
- billing + "/rate_cards/" + ((_d = plan.rate_card) == null ? void 0 : _d.id) + "/rates",
50
- options
51
- );
52
- for (let i = 0; i < (rates == null ? void 0 : rates.data.length); i++) {
53
- const element = (_e = rates == null ? void 0 : rates.data) == null ? void 0 : _e[i];
54
- if ((_f = element == null ? void 0 : element.metered_item) == null ? void 0 : _f.id) {
55
- items.push({
56
- id: element.metered_item.id
57
- });
58
- }
44
+ const rateCardIds = ((components == null ? void 0 : components.data) || []).map((plan) => {
45
+ var _a2;
46
+ return (_a2 = plan == null ? void 0 : plan.rate_card) == null ? void 0 : _a2.id;
47
+ }).filter(Boolean);
48
+ const rateResults = await Promise.all(
49
+ rateCardIds.map((id) => axios.get(
50
+ billing + "/rate_cards/" + id + "/rates",
51
+ options
52
+ ))
53
+ );
54
+ const items = [];
55
+ for (const { data: rates } of rateResults) {
56
+ for (const element of (rates == null ? void 0 : rates.data) || []) {
57
+ if ((_a = element == null ? void 0 : element.metered_item) == null ? void 0 : _a.id) {
58
+ items.push({
59
+ id: element.metered_item.id
60
+ });
59
61
  }
60
62
  ;
61
63
  }
@@ -262,28 +264,30 @@ var require_billing = __commonJS({
262
264
  const model = [rest == null ? void 0 : rest.provider, rest == null ? void 0 : rest.model].join("/");
263
265
  const input = Number(metadata == null ? void 0 : metadata.promptTokenCount);
264
266
  const output = parseInt(Number(metadata == null ? void 0 : metadata.totalTokenCount) - input);
265
- if (input) {
267
+ const sendMeterEvent = (token_type, value) => {
266
268
  stripe2.billing.meterEvents.create({
267
269
  event_name,
268
270
  payload: {
269
271
  model,
270
272
  stripe_customer_id: customer,
271
- token_type: "input",
272
- value: input
273
+ token_type,
274
+ value
273
275
  }
276
+ }).catch((error) => {
277
+ console.error("[billing.usage] meterEvents.create failed", {
278
+ customer,
279
+ token_type,
280
+ value,
281
+ error: error == null ? void 0 : error.message
282
+ });
274
283
  });
284
+ };
285
+ if (input) {
286
+ sendMeterEvent("input", input);
275
287
  }
276
288
  ;
277
289
  if (output) {
278
- stripe2.billing.meterEvents.create({
279
- event_name,
280
- payload: {
281
- model,
282
- stripe_customer_id: customer,
283
- token_type: "output",
284
- value: output
285
- }
286
- });
290
+ sendMeterEvent("output", output);
287
291
  }
288
292
  ;
289
293
  };
@@ -618,9 +622,7 @@ var require_subscription = __commonJS({
618
622
  }
619
623
  ],
620
624
  metadata,
621
- ...billingCycleAnchor && {
622
- billing_cycle_anchor: billingCycleAnchor
623
- }
625
+ billing_cycle_anchor: billingCycleAnchor || "now"
624
626
  },
625
627
  idem(idempotencyKey, "subscription.create")
626
628
  );
package/dist/index.mjs CHANGED
@@ -41,27 +41,29 @@ var require_billing = __commonJS({
41
41
  };
42
42
  };
43
43
  const billableItems = async () => {
44
- var _a, _b, _c, _d, _e, _f;
44
+ var _a;
45
45
  try {
46
- const items = [];
47
46
  const { data: components } = await axios.get(
48
47
  billing + "/pricing_plans/" + process.env.STRIPE_PRICING_AI_PLAN_ID + "/components",
49
48
  options
50
49
  );
51
- for (let index = 0; index < ((_a = components == null ? void 0 : components.data) == null ? void 0 : _a.length); index++) {
52
- const plan = (_b = components == null ? void 0 : components.data) == null ? void 0 : _b[index];
53
- if ((_c = plan == null ? void 0 : plan.rate_card) == null ? void 0 : _c.id) {
54
- const { data: rates } = await axios.get(
55
- billing + "/rate_cards/" + ((_d = plan.rate_card) == null ? void 0 : _d.id) + "/rates",
56
- options
57
- );
58
- for (let i = 0; i < (rates == null ? void 0 : rates.data.length); i++) {
59
- const element = (_e = rates == null ? void 0 : rates.data) == null ? void 0 : _e[i];
60
- if ((_f = element == null ? void 0 : element.metered_item) == null ? void 0 : _f.id) {
61
- items.push({
62
- id: element.metered_item.id
63
- });
64
- }
50
+ const rateCardIds = ((components == null ? void 0 : components.data) || []).map((plan) => {
51
+ var _a2;
52
+ return (_a2 = plan == null ? void 0 : plan.rate_card) == null ? void 0 : _a2.id;
53
+ }).filter(Boolean);
54
+ const rateResults = await Promise.all(
55
+ rateCardIds.map((id) => axios.get(
56
+ billing + "/rate_cards/" + id + "/rates",
57
+ options
58
+ ))
59
+ );
60
+ const items = [];
61
+ for (const { data: rates } of rateResults) {
62
+ for (const element of (rates == null ? void 0 : rates.data) || []) {
63
+ if ((_a = element == null ? void 0 : element.metered_item) == null ? void 0 : _a.id) {
64
+ items.push({
65
+ id: element.metered_item.id
66
+ });
65
67
  }
66
68
  ;
67
69
  }
@@ -268,28 +270,30 @@ var require_billing = __commonJS({
268
270
  const model = [rest == null ? void 0 : rest.provider, rest == null ? void 0 : rest.model].join("/");
269
271
  const input = Number(metadata == null ? void 0 : metadata.promptTokenCount);
270
272
  const output = parseInt(Number(metadata == null ? void 0 : metadata.totalTokenCount) - input);
271
- if (input) {
273
+ const sendMeterEvent = (token_type, value) => {
272
274
  stripe.billing.meterEvents.create({
273
275
  event_name,
274
276
  payload: {
275
277
  model,
276
278
  stripe_customer_id: customer,
277
- token_type: "input",
278
- value: input
279
+ token_type,
280
+ value
279
281
  }
282
+ }).catch((error) => {
283
+ console.error("[billing.usage] meterEvents.create failed", {
284
+ customer,
285
+ token_type,
286
+ value,
287
+ error: error == null ? void 0 : error.message
288
+ });
280
289
  });
290
+ };
291
+ if (input) {
292
+ sendMeterEvent("input", input);
281
293
  }
282
294
  ;
283
295
  if (output) {
284
- stripe.billing.meterEvents.create({
285
- event_name,
286
- payload: {
287
- model,
288
- stripe_customer_id: customer,
289
- token_type: "output",
290
- value: output
291
- }
292
- });
296
+ sendMeterEvent("output", output);
293
297
  }
294
298
  ;
295
299
  };
@@ -624,9 +628,7 @@ var require_subscription = __commonJS({
624
628
  }
625
629
  ],
626
630
  metadata,
627
- ...billingCycleAnchor && {
628
- billing_cycle_anchor: billingCycleAnchor
629
- }
631
+ billing_cycle_anchor: billingCycleAnchor || "now"
630
632
  },
631
633
  idem(idempotencyKey, "subscription.create")
632
634
  );
package/package.json CHANGED
@@ -24,5 +24,5 @@
24
24
  "build": "tsup ./index.js && npm publish"
25
25
  },
26
26
  "types": "dist/index.d.ts",
27
- "version": "0.1.14"
27
+ "version": "0.1.16"
28
28
  }