@farm.js/autumn 0.1.51-beta.3 → 0.1.61
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 +0 -12
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +43 -103
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -139,12 +139,6 @@ 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[];
|
|
148
142
|
webhooks?: AutumnWebhookConfig;
|
|
149
143
|
billing: AutumnBillingOptions;
|
|
150
144
|
log?: FarmIntegrationLogger;
|
|
@@ -181,12 +175,6 @@ type AutumnDefaultPathInput = {
|
|
|
181
175
|
type AutumnIntegrationResult<TApi> = Omit<FarmIntegration, "api"> & {
|
|
182
176
|
api: TApi;
|
|
183
177
|
};
|
|
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;
|
|
190
178
|
export declare function normalizeAutumnStatus(customer: Customer | null, subscription: Customer["subscriptions"][number] | null): AutumnBillingStatus;
|
|
191
179
|
/**
|
|
192
180
|
* 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/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,SAAS,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,KAAK,EAIV,QAAQ,EAIT,MAAM,WAAW,CAAC;AAKnB,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,SAAS,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,KAAK,EAIV,QAAQ,EAIT,MAAM,WAAW,CAAC;AAKnB,OAAO,EAGL,KAAK,eAAe,EAEpB,KAAK,6BAA6B,EAClC,KAAK,qBAAqB,EAC3B,MAAM,eAAe,CAAC;AAOvB,OAAO,KAAK,EAEV,iBAAiB,EACjB,gBAAgB,EACjB,MAAM,qCAAqC,CAAC;AAC7C,OAAO,EAcL,KAAK,wBAAwB,EAG7B,KAAK,mBAAmB,EAOxB,KAAK,eAAe,EACpB,KAAK,uBAAuB,EAC5B,KAAK,sBAAsB,EAK5B,MAAM,aAAa,CAAC;AAErB,YAAY,EACV,uBAAuB,EACvB,wBAAwB,EACxB,8BAA8B,EAC9B,iCAAiC,EACjC,2BAA2B,EAC3B,oBAAoB,EACpB,2BAA2B,EAC3B,yBAAyB,EACzB,uBAAuB,EACvB,4BAA4B,EAC5B,6BAA6B,EAC7B,6BAA6B,EAC7B,8BAA8B,EAC9B,yBAAyB,EACzB,uBAAuB,EACvB,4BAA4B,EAC5B,wBAAwB,EACxB,uBAAuB,EACvB,oBAAoB,EACpB,mBAAmB,EACnB,oBAAoB,EACpB,eAAe,EACf,sBAAsB,EACtB,iBAAiB,EACjB,kBAAkB,GACnB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,KAAK,oBAAoB,GAAG;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,CAAC,GAAG,EAAE,MAAM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B,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 {
|
|
3
|
+
import { 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,29 +296,11 @@ function getOwnerExternalCustomerId(owner, billing, tools) {
|
|
|
296
296
|
if (billing.resolveExternalCustomerId) {
|
|
297
297
|
return billing.resolveExternalCustomerId(owner, tools);
|
|
298
298
|
}
|
|
299
|
-
const normalizedId =
|
|
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
|
|
299
|
+
const normalizedId = owner.id
|
|
311
300
|
.trim()
|
|
312
301
|
.replace(/[^a-zA-Z0-9_-]+/g, "-")
|
|
313
302
|
.replace(/^-+|-+$/g, "");
|
|
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"}`;
|
|
303
|
+
return `${owner.kind}_${normalizedId || "unknown"}`;
|
|
322
304
|
}
|
|
323
305
|
function resolveAutumnPlanLimitReference(value, defaultKey) {
|
|
324
306
|
if (!value || value === "plan_limit" || typeof value === "number") {
|
|
@@ -635,7 +617,6 @@ function createStandardWebhook(secret) {
|
|
|
635
617
|
return new StandardWebhook(Buffer.from(secret, "utf-8").toString("base64"));
|
|
636
618
|
}
|
|
637
619
|
export function autumn(input) {
|
|
638
|
-
const allowedOrigins = resolveIntegrationAllowedOrigins(input.allowedOrigins, "autumn().allowedOrigins");
|
|
639
620
|
const secretKey = input.secretKey ?? process.env.AUTUMN_SECRET_KEY ?? "";
|
|
640
621
|
const appBaseUrl = input.appBaseUrl ?? process.env.APP_BASE_URL ?? undefined;
|
|
641
622
|
const webhookSecret = process.env.AUTUMN_WEBHOOK_SECRET ?? undefined;
|
|
@@ -1020,9 +1001,6 @@ export function autumn(input) {
|
|
|
1020
1001
|
integrationRoute.post(usagePath, {
|
|
1021
1002
|
responseFormat: "json",
|
|
1022
1003
|
async handler(request, ctx) {
|
|
1023
|
-
const rejected = rejectForeignOrigin(request, allowedOrigins);
|
|
1024
|
-
if (rejected)
|
|
1025
|
-
return rejected;
|
|
1026
1004
|
const body = (await request.json().catch(() => ({})));
|
|
1027
1005
|
if (!body.key) {
|
|
1028
1006
|
return new Response("A billing usage key is required.", { status: 400 });
|
|
@@ -1058,9 +1036,6 @@ export function autumn(input) {
|
|
|
1058
1036
|
integrationRoute.post(meterUsagePath, {
|
|
1059
1037
|
responseFormat: "json",
|
|
1060
1038
|
async handler(request, ctx) {
|
|
1061
|
-
const rejected = rejectForeignOrigin(request, allowedOrigins);
|
|
1062
|
-
if (rejected)
|
|
1063
|
-
return rejected;
|
|
1064
1039
|
const body = (await request.json().catch(() => ({})));
|
|
1065
1040
|
if (!body.key) {
|
|
1066
1041
|
return new Response("An Autumn meter key is required.", { status: 400 });
|
|
@@ -1153,9 +1128,6 @@ export function autumn(input) {
|
|
|
1153
1128
|
integrationRoute.post(reportUsagePath, {
|
|
1154
1129
|
responseFormat: "json",
|
|
1155
1130
|
async handler(request, ctx) {
|
|
1156
|
-
const rejected = rejectForeignOrigin(request, allowedOrigins);
|
|
1157
|
-
if (rejected)
|
|
1158
|
-
return rejected;
|
|
1159
1131
|
const body = (await request.json().catch(() => ({})));
|
|
1160
1132
|
if (!body.key) {
|
|
1161
1133
|
return new Response("An Autumn meter key is required.", { status: 400 });
|
|
@@ -1221,63 +1193,56 @@ export function autumn(input) {
|
|
|
1221
1193
|
currentUsed,
|
|
1222
1194
|
quantity: body.quantity,
|
|
1223
1195
|
});
|
|
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
|
-
});
|
|
1238
1196
|
if (typeof hardLimit === "number" && projectedCurrentPeriodUsed > hardLimit) {
|
|
1239
|
-
return
|
|
1197
|
+
return Response.json({
|
|
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
|
+
});
|
|
1240
1211
|
}
|
|
1241
|
-
let nextUsed = projectedCurrentPeriodUsed;
|
|
1242
1212
|
if (isPrepaidFeatureQuantity({ livePlan, product: active.product, featureId })) {
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
else if (meter.aggregation === "last") {
|
|
1249
|
-
// A last-value meter reports the period's absolute usage, so the write sets it.
|
|
1250
|
-
await sdk.balances.update({
|
|
1213
|
+
if (!active.product?.autumnPlanId) {
|
|
1214
|
+
return new Response("Autumn prepaid quantity updates require an active subscription product.", { status: 409 });
|
|
1215
|
+
}
|
|
1216
|
+
const customizedItems = mergeAutumnProductItems(livePlan, active.product);
|
|
1217
|
+
await sdk.billing.update({
|
|
1251
1218
|
customerId: resolved.customer.id ?? resolved.externalCustomerId,
|
|
1252
|
-
|
|
1253
|
-
|
|
1219
|
+
planId: active.product.autumnPlanId,
|
|
1220
|
+
featureQuantities: [
|
|
1221
|
+
{
|
|
1222
|
+
featureId,
|
|
1223
|
+
quantity: body.quantity,
|
|
1224
|
+
},
|
|
1225
|
+
],
|
|
1226
|
+
customize: customizedItems.length > 0
|
|
1227
|
+
? {
|
|
1228
|
+
items: customizedItems,
|
|
1229
|
+
}
|
|
1230
|
+
: undefined,
|
|
1231
|
+
prorationBehavior: "prorate_immediately",
|
|
1232
|
+
redirectMode: "never",
|
|
1254
1233
|
});
|
|
1255
1234
|
}
|
|
1256
1235
|
else {
|
|
1257
|
-
//
|
|
1258
|
-
//
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
const tracked = await sdk.track({
|
|
1263
|
-
customerId,
|
|
1236
|
+
// Autumn's current SDK/runtime track path is inconsistent across the published
|
|
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.
|
|
1239
|
+
await sdk.balances.update({
|
|
1240
|
+
customerId: resolved.customer.id ?? resolved.externalCustomerId,
|
|
1264
1241
|
featureId,
|
|
1265
|
-
|
|
1266
|
-
properties,
|
|
1242
|
+
usage: projectedCurrentPeriodUsed,
|
|
1267
1243
|
});
|
|
1268
|
-
nextUsed = tracked.balance?.usage ?? projectedCurrentPeriodUsed;
|
|
1269
|
-
// Reports that raced past the cap between the read and the increment are taken back
|
|
1270
|
-
// out, so the cap holds; the accepted reports are never reversed.
|
|
1271
|
-
if (typeof hardLimit === "number" && nextUsed > hardLimit) {
|
|
1272
|
-
await sdk.track({
|
|
1273
|
-
customerId,
|
|
1274
|
-
featureId,
|
|
1275
|
-
value: -increment,
|
|
1276
|
-
properties: { ...properties, reversal: true },
|
|
1277
|
-
});
|
|
1278
|
-
return hardLimitReached(nextUsed - increment, nextUsed);
|
|
1279
|
-
}
|
|
1280
1244
|
}
|
|
1245
|
+
const nextUsed = projectedCurrentPeriodUsed;
|
|
1281
1246
|
setProjectedUsage({
|
|
1282
1247
|
externalCustomerId: resolved.externalCustomerId,
|
|
1283
1248
|
featureId,
|
|
@@ -1333,9 +1298,6 @@ export function autumn(input) {
|
|
|
1333
1298
|
integrationRoute.post(checkPath, {
|
|
1334
1299
|
responseFormat: "json",
|
|
1335
1300
|
async handler(request, ctx) {
|
|
1336
|
-
const rejected = rejectForeignOrigin(request, allowedOrigins);
|
|
1337
|
-
if (rejected)
|
|
1338
|
-
return rejected;
|
|
1339
1301
|
const body = (await request.json().catch(() => ({})));
|
|
1340
1302
|
if (!body.key) {
|
|
1341
1303
|
return new Response("An Autumn meter key is required.", { status: 400 });
|
|
@@ -1379,9 +1341,6 @@ export function autumn(input) {
|
|
|
1379
1341
|
integrationRoute.post(checkoutPath, {
|
|
1380
1342
|
responseFormat: "json",
|
|
1381
1343
|
async handler(request, ctx) {
|
|
1382
|
-
const rejected = rejectForeignOrigin(request, allowedOrigins);
|
|
1383
|
-
if (rejected)
|
|
1384
|
-
return rejected;
|
|
1385
1344
|
const body = (await request.json().catch(() => ({})));
|
|
1386
1345
|
if (!body.productId) {
|
|
1387
1346
|
return new Response("A billing productId is required.", { status: 400 });
|
|
@@ -1427,9 +1386,6 @@ export function autumn(input) {
|
|
|
1427
1386
|
integrationRoute.post(portalPath, {
|
|
1428
1387
|
responseFormat: "json",
|
|
1429
1388
|
async handler(request, ctx) {
|
|
1430
|
-
const rejected = rejectForeignOrigin(request, allowedOrigins);
|
|
1431
|
-
if (rejected)
|
|
1432
|
-
return rejected;
|
|
1433
1389
|
const body = (await request.json().catch(() => ({})));
|
|
1434
1390
|
const resolved = await requireOwner(ctx, billing, sdk);
|
|
1435
1391
|
const returnPath = resolveAppPath(body.returnTo, "Autumn portal returnTo");
|
|
@@ -1450,19 +1406,3 @@ export function autumn(input) {
|
|
|
1450
1406
|
],
|
|
1451
1407
|
});
|
|
1452
1408
|
}
|
|
1453
|
-
/**
|
|
1454
|
-
* The billing routes act for the signed-in caller from its session cookie, and a page on another
|
|
1455
|
-
* site can send them a simple POST (a text/plain form, a no-cors fetch) with no preflight. A browser
|
|
1456
|
-
* sends such a POST with its page's Origin, so a foreign origin, or one the browser marks
|
|
1457
|
-
* cross-site, is refused before the body is read. A caller with no Origin at all (a server, a
|
|
1458
|
-
* script) is not a browser on another site and keeps working, as with the email routes.
|
|
1459
|
-
*/
|
|
1460
|
-
function rejectForeignOrigin(request, allowedOrigins) {
|
|
1461
|
-
const origin = validateIntegrationRequestOrigin(request, {
|
|
1462
|
-
allowedOrigins,
|
|
1463
|
-
requireOriginMetadata: false,
|
|
1464
|
-
});
|
|
1465
|
-
return origin.ok
|
|
1466
|
-
? null
|
|
1467
|
-
: Response.json({ error: describeIntegrationOriginRejection(origin.reason) }, { status: 403 });
|
|
1468
|
-
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@farm.js/autumn",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.61",
|
|
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/core": "0.1.
|
|
35
|
-
"@farm.js/integration-utils": "0.1.
|
|
34
|
+
"@farm.js/core": "0.1.61",
|
|
35
|
+
"@farm.js/integration-utils": "0.1.61"
|
|
36
36
|
},
|
|
37
37
|
"scripts": {
|
|
38
38
|
"build": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\" && tsc",
|