@farm.js/autumn 0.1.31 → 0.1.35
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.d.ts +12 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +99 -22
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -139,6 +139,12 @@ export interface AutumnIntegrationInput extends AutumnClientPathOptions {
|
|
|
139
139
|
secretKey?: string;
|
|
140
140
|
serverURL?: string;
|
|
141
141
|
appBaseUrl?: string;
|
|
142
|
+
/**
|
|
143
|
+
* Origins besides the app's own that may call the billing routes, in
|
|
144
|
+
* `serverActions.allowedOrigins` pattern syntax. The routes act for the signed-in caller from its
|
|
145
|
+
* cookies, so a request from any other origin is refused.
|
|
146
|
+
*/
|
|
147
|
+
allowedOrigins?: readonly string[];
|
|
142
148
|
webhooks?: AutumnWebhookConfig;
|
|
143
149
|
billing: AutumnBillingOptions;
|
|
144
150
|
log?: FarmIntegrationLogger;
|
|
@@ -175,6 +181,12 @@ type AutumnDefaultPathInput = {
|
|
|
175
181
|
type AutumnIntegrationResult<TApi> = Omit<FarmIntegration, "api"> & {
|
|
176
182
|
api: TApi;
|
|
177
183
|
};
|
|
184
|
+
/**
|
|
185
|
+
* The Autumn customer id Farm derived before ids it could not keep as is were encoded. Owners
|
|
186
|
+
* with such ids (emails, handles) share customers under it; return it from
|
|
187
|
+
* `billing.resolveExternalCustomerId` only to keep existing customers while migrating them.
|
|
188
|
+
*/
|
|
189
|
+
export declare function legacyAutumnExternalCustomerId(owner: AutumnBillingOwner): string;
|
|
178
190
|
export declare function normalizeAutumnStatus(customer: Customer | null, subscription: Customer["subscriptions"][number] | null): AutumnBillingStatus;
|
|
179
191
|
/**
|
|
180
192
|
* Whether the subscription is scheduled to cancel but has not yet ended: a
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src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G;AACH,wBAAgB,yBAAyB,CACvC,YAAY,EAAE,QAAQ,CAAC,eAAe,CAAC,CAAC,MAAM,CAAC,GAAG,IAAI,GAAG,SAAS,GACjE,OAAO,CAMT;AA+SD,wBAAgB,MAAM,CAAC,KAAK,CAAC,MAAM,SAAS,sBAAsB,GAAG,sBAAsB,EACzF,KAAK,EAAE,MAAM,GACZ,uBAAuB,CAAC,sBAAsB,CAAC,CAAC;AACnD,wBAAgB,MAAM,CAAC,KAAK,CAAC,MAAM,SAAS,sBAAsB,EAChE,KAAK,EAAE,MAAM,GACZ,uBAAuB,CAAC,eAAe,CAAC,sBAAsB,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Autumn as AutumnSDK } from "autumn-js";
|
|
2
2
|
import { Webhook as StandardWebhook, WebhookVerificationError as StandardWebhookVerificationError, } from "standardwebhooks";
|
|
3
|
-
import { defineIntegration, integrationRoute, } from "@farm.js/core";
|
|
3
|
+
import { describeIntegrationOriginRejection, resolveIntegrationAllowedOrigins, validateIntegrationRequestOrigin, defineIntegration, integrationRoute, } from "@farm.js/core";
|
|
4
4
|
import { integrationConfig, normalizeWebhookConfig, resolveAppPath, toAbsoluteUrl, } from "@farm.js/integration-utils";
|
|
5
5
|
import { createAutumnClientApi, } from "./client.js";
|
|
6
6
|
export { autumnClient } from "./client.js";
|
|
@@ -296,11 +296,29 @@ function getOwnerExternalCustomerId(owner, billing, tools) {
|
|
|
296
296
|
if (billing.resolveExternalCustomerId) {
|
|
297
297
|
return billing.resolveExternalCustomerId(owner, tools);
|
|
298
298
|
}
|
|
299
|
-
const normalizedId = owner.id
|
|
299
|
+
const normalizedId = normalizeLegacyAutumnCustomerId(owner.id);
|
|
300
|
+
// An id the normalization keeps as is maps as it always has. Any other id (`a.b`, an email, a
|
|
301
|
+
// non-ASCII handle, an empty id) used to collapse onto another owner's customer (`a.b` and `a-b`
|
|
302
|
+
// both became `user_a-b`), so it is encoded instead. A normalized id never starts with `-`, so
|
|
303
|
+
// `kind_-<base64url>` cannot collide with it.
|
|
304
|
+
if (normalizedId && normalizedId === owner.id) {
|
|
305
|
+
return `${owner.kind}_${normalizedId}`;
|
|
306
|
+
}
|
|
307
|
+
return `${owner.kind}_-${Buffer.from(owner.id, "utf8").toString("base64url")}`;
|
|
308
|
+
}
|
|
309
|
+
function normalizeLegacyAutumnCustomerId(id) {
|
|
310
|
+
return id
|
|
300
311
|
.trim()
|
|
301
312
|
.replace(/[^a-zA-Z0-9_-]+/g, "-")
|
|
302
313
|
.replace(/^-+|-+$/g, "");
|
|
303
|
-
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* The Autumn customer id Farm derived before ids it could not keep as is were encoded. Owners
|
|
317
|
+
* with such ids (emails, handles) share customers under it; return it from
|
|
318
|
+
* `billing.resolveExternalCustomerId` only to keep existing customers while migrating them.
|
|
319
|
+
*/
|
|
320
|
+
export function legacyAutumnExternalCustomerId(owner) {
|
|
321
|
+
return `${owner.kind}_${normalizeLegacyAutumnCustomerId(owner.id) || "unknown"}`;
|
|
304
322
|
}
|
|
305
323
|
function resolveAutumnPlanLimitReference(value, defaultKey) {
|
|
306
324
|
if (!value || value === "plan_limit" || typeof value === "number") {
|
|
@@ -617,6 +635,7 @@ function createStandardWebhook(secret) {
|
|
|
617
635
|
return new StandardWebhook(Buffer.from(secret, "utf-8").toString("base64"));
|
|
618
636
|
}
|
|
619
637
|
export function autumn(input) {
|
|
638
|
+
const allowedOrigins = resolveIntegrationAllowedOrigins(input.allowedOrigins, "autumn().allowedOrigins");
|
|
620
639
|
const secretKey = input.secretKey ?? process.env.AUTUMN_SECRET_KEY ?? "";
|
|
621
640
|
const appBaseUrl = input.appBaseUrl ?? process.env.APP_BASE_URL ?? undefined;
|
|
622
641
|
const webhookSecret = process.env.AUTUMN_WEBHOOK_SECRET ?? undefined;
|
|
@@ -1001,6 +1020,9 @@ export function autumn(input) {
|
|
|
1001
1020
|
integrationRoute.post(usagePath, {
|
|
1002
1021
|
responseFormat: "json",
|
|
1003
1022
|
async handler(request, ctx) {
|
|
1023
|
+
const rejected = rejectForeignOrigin(request, allowedOrigins);
|
|
1024
|
+
if (rejected)
|
|
1025
|
+
return rejected;
|
|
1004
1026
|
const body = (await request.json().catch(() => ({})));
|
|
1005
1027
|
if (!body.key) {
|
|
1006
1028
|
return new Response("A billing usage key is required.", { status: 400 });
|
|
@@ -1036,6 +1058,9 @@ export function autumn(input) {
|
|
|
1036
1058
|
integrationRoute.post(meterUsagePath, {
|
|
1037
1059
|
responseFormat: "json",
|
|
1038
1060
|
async handler(request, ctx) {
|
|
1061
|
+
const rejected = rejectForeignOrigin(request, allowedOrigins);
|
|
1062
|
+
if (rejected)
|
|
1063
|
+
return rejected;
|
|
1039
1064
|
const body = (await request.json().catch(() => ({})));
|
|
1040
1065
|
if (!body.key) {
|
|
1041
1066
|
return new Response("An Autumn meter key is required.", { status: 400 });
|
|
@@ -1128,6 +1153,9 @@ export function autumn(input) {
|
|
|
1128
1153
|
integrationRoute.post(reportUsagePath, {
|
|
1129
1154
|
responseFormat: "json",
|
|
1130
1155
|
async handler(request, ctx) {
|
|
1156
|
+
const rejected = rejectForeignOrigin(request, allowedOrigins);
|
|
1157
|
+
if (rejected)
|
|
1158
|
+
return rejected;
|
|
1131
1159
|
const body = (await request.json().catch(() => ({})));
|
|
1132
1160
|
if (!body.key) {
|
|
1133
1161
|
return new Response("An Autumn meter key is required.", { status: 400 });
|
|
@@ -1193,22 +1221,24 @@ export function autumn(input) {
|
|
|
1193
1221
|
currentUsed,
|
|
1194
1222
|
quantity: body.quantity,
|
|
1195
1223
|
});
|
|
1224
|
+
const hardLimitReached = (used, projected) => Response.json({
|
|
1225
|
+
key: body.key,
|
|
1226
|
+
quantity: body.quantity,
|
|
1227
|
+
customerId: resolved.customer.id,
|
|
1228
|
+
eventName: meter.eventName ?? featureId,
|
|
1229
|
+
eventIdentifier: body.idempotencyKey,
|
|
1230
|
+
occurredAt: body.occurredAt ?? new Date().toISOString(),
|
|
1231
|
+
currentPeriodUsed: used,
|
|
1232
|
+
projectedCurrentPeriodUsed: projected,
|
|
1233
|
+
softLimit,
|
|
1234
|
+
hardLimit,
|
|
1235
|
+
state: "hard_limit_reached",
|
|
1236
|
+
warning: "The configured metered hard cap has been reached for the current billing period. Reported usage is blocked until the next cycle or a plan change.",
|
|
1237
|
+
});
|
|
1196
1238
|
if (typeof hardLimit === "number" && projectedCurrentPeriodUsed > hardLimit) {
|
|
1197
|
-
return
|
|
1198
|
-
key: body.key,
|
|
1199
|
-
quantity: body.quantity,
|
|
1200
|
-
customerId: resolved.customer.id,
|
|
1201
|
-
eventName: meter.eventName ?? featureId,
|
|
1202
|
-
eventIdentifier: body.idempotencyKey,
|
|
1203
|
-
occurredAt: body.occurredAt ?? new Date().toISOString(),
|
|
1204
|
-
currentPeriodUsed: currentUsed,
|
|
1205
|
-
projectedCurrentPeriodUsed,
|
|
1206
|
-
softLimit,
|
|
1207
|
-
hardLimit,
|
|
1208
|
-
state: "hard_limit_reached",
|
|
1209
|
-
warning: "The configured metered hard cap has been reached for the current billing period. Reported usage is blocked until the next cycle or a plan change.",
|
|
1210
|
-
});
|
|
1239
|
+
return hardLimitReached(currentUsed, projectedCurrentPeriodUsed);
|
|
1211
1240
|
}
|
|
1241
|
+
let nextUsed = projectedCurrentPeriodUsed;
|
|
1212
1242
|
if (isPrepaidFeatureQuantity({ livePlan, product: active.product, featureId })) {
|
|
1213
1243
|
if (!active.product?.autumnPlanId) {
|
|
1214
1244
|
return new Response("Autumn prepaid quantity updates require an active subscription product.", { status: 409 });
|
|
@@ -1232,17 +1262,39 @@ export function autumn(input) {
|
|
|
1232
1262
|
redirectMode: "never",
|
|
1233
1263
|
});
|
|
1234
1264
|
}
|
|
1235
|
-
else {
|
|
1236
|
-
//
|
|
1237
|
-
// types/docs and the actual endpoint behavior. Update the balance usage directly so
|
|
1238
|
-
// the example remains functional and type-safe for metered demos.
|
|
1265
|
+
else if (meter.aggregation === "last") {
|
|
1266
|
+
// A last-value meter reports the period's absolute usage, so the write sets it.
|
|
1239
1267
|
await sdk.balances.update({
|
|
1240
1268
|
customerId: resolved.customer.id ?? resolved.externalCustomerId,
|
|
1241
1269
|
featureId,
|
|
1242
1270
|
usage: projectedCurrentPeriodUsed,
|
|
1243
1271
|
});
|
|
1244
1272
|
}
|
|
1245
|
-
|
|
1273
|
+
else {
|
|
1274
|
+
// Track adds to Autumn's stored usage atomically. Writing back the total read above
|
|
1275
|
+
// let concurrent reports overwrite each other, so usage and the cap both lost them.
|
|
1276
|
+
const customerId = resolved.customer.id ?? resolved.externalCustomerId;
|
|
1277
|
+
const increment = meter.aggregation === "count" ? 1 : body.quantity;
|
|
1278
|
+
const properties = { idempotencyKey: body.idempotencyKey };
|
|
1279
|
+
const tracked = await sdk.track({
|
|
1280
|
+
customerId,
|
|
1281
|
+
featureId,
|
|
1282
|
+
value: increment,
|
|
1283
|
+
properties,
|
|
1284
|
+
});
|
|
1285
|
+
nextUsed = tracked.balance?.usage ?? projectedCurrentPeriodUsed;
|
|
1286
|
+
// Reports that raced past the cap between the read and the increment are taken back
|
|
1287
|
+
// out, so the cap holds; the accepted reports are never reversed.
|
|
1288
|
+
if (typeof hardLimit === "number" && nextUsed > hardLimit) {
|
|
1289
|
+
await sdk.track({
|
|
1290
|
+
customerId,
|
|
1291
|
+
featureId,
|
|
1292
|
+
value: -increment,
|
|
1293
|
+
properties: { ...properties, reversal: true },
|
|
1294
|
+
});
|
|
1295
|
+
return hardLimitReached(nextUsed - increment, nextUsed);
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1246
1298
|
setProjectedUsage({
|
|
1247
1299
|
externalCustomerId: resolved.externalCustomerId,
|
|
1248
1300
|
featureId,
|
|
@@ -1298,6 +1350,9 @@ export function autumn(input) {
|
|
|
1298
1350
|
integrationRoute.post(checkPath, {
|
|
1299
1351
|
responseFormat: "json",
|
|
1300
1352
|
async handler(request, ctx) {
|
|
1353
|
+
const rejected = rejectForeignOrigin(request, allowedOrigins);
|
|
1354
|
+
if (rejected)
|
|
1355
|
+
return rejected;
|
|
1301
1356
|
const body = (await request.json().catch(() => ({})));
|
|
1302
1357
|
if (!body.key) {
|
|
1303
1358
|
return new Response("An Autumn meter key is required.", { status: 400 });
|
|
@@ -1341,6 +1396,9 @@ export function autumn(input) {
|
|
|
1341
1396
|
integrationRoute.post(checkoutPath, {
|
|
1342
1397
|
responseFormat: "json",
|
|
1343
1398
|
async handler(request, ctx) {
|
|
1399
|
+
const rejected = rejectForeignOrigin(request, allowedOrigins);
|
|
1400
|
+
if (rejected)
|
|
1401
|
+
return rejected;
|
|
1344
1402
|
const body = (await request.json().catch(() => ({})));
|
|
1345
1403
|
if (!body.productId) {
|
|
1346
1404
|
return new Response("A billing productId is required.", { status: 400 });
|
|
@@ -1386,6 +1444,9 @@ export function autumn(input) {
|
|
|
1386
1444
|
integrationRoute.post(portalPath, {
|
|
1387
1445
|
responseFormat: "json",
|
|
1388
1446
|
async handler(request, ctx) {
|
|
1447
|
+
const rejected = rejectForeignOrigin(request, allowedOrigins);
|
|
1448
|
+
if (rejected)
|
|
1449
|
+
return rejected;
|
|
1389
1450
|
const body = (await request.json().catch(() => ({})));
|
|
1390
1451
|
const resolved = await requireOwner(ctx, billing, sdk);
|
|
1391
1452
|
const returnPath = resolveAppPath(body.returnTo, "Autumn portal returnTo");
|
|
@@ -1406,3 +1467,19 @@ export function autumn(input) {
|
|
|
1406
1467
|
],
|
|
1407
1468
|
});
|
|
1408
1469
|
}
|
|
1470
|
+
/**
|
|
1471
|
+
* The billing routes act for the signed-in caller from its session cookie, and a page on another
|
|
1472
|
+
* site can send them a simple POST (a text/plain form, a no-cors fetch) with no preflight. A browser
|
|
1473
|
+
* sends such a POST with its page's Origin, so a foreign origin, or one the browser marks
|
|
1474
|
+
* cross-site, is refused before the body is read. A caller with no Origin at all (a server, a
|
|
1475
|
+
* script) is not a browser on another site and keeps working, as with the email routes.
|
|
1476
|
+
*/
|
|
1477
|
+
function rejectForeignOrigin(request, allowedOrigins) {
|
|
1478
|
+
const origin = validateIntegrationRequestOrigin(request, {
|
|
1479
|
+
allowedOrigins,
|
|
1480
|
+
requireOriginMetadata: false,
|
|
1481
|
+
});
|
|
1482
|
+
return origin.ok
|
|
1483
|
+
? null
|
|
1484
|
+
: Response.json({ error: describeIntegrationOriginRejection(origin.reason) }, { status: 403 });
|
|
1485
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@farm.js/autumn",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.35",
|
|
4
4
|
"description": "Autumn billing integration for Farm.js",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"autumn-js": "^1.2.7",
|
|
33
33
|
"standardwebhooks": "^1.0.0",
|
|
34
|
-
"@farm.js/
|
|
35
|
-
"@farm.js/
|
|
34
|
+
"@farm.js/core": "0.1.35",
|
|
35
|
+
"@farm.js/integration-utils": "0.1.35"
|
|
36
36
|
},
|
|
37
37
|
"scripts": {
|
|
38
38
|
"build": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\" && tsc",
|