billfn 0.0.1 → 0.8.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/README.md ADDED
@@ -0,0 +1,117 @@
1
+ # billfn
2
+
3
+ BillFn is a self-hostable billing and entitlement layer for product builders. It gives you billing accounts, checkout
4
+ flows, subscription state, entitlement snapshots, usage metering and webhook ingestion behind one normalized interface,
5
+ so your product can ask a simpler question: what is this account allowed to do right now?
6
+
7
+ The core design is entitlement-first:
8
+
9
+ - providers are the source of truth for money movement
10
+ - `billfn` is the internal source of truth for entitlement state
11
+ - downstream consumers should read entitlements and quotas, not provider payloads
12
+
13
+ ## Install
14
+
15
+ ```bash
16
+ npm install billfn @billfn/provider-dodo @superfunctions/db
17
+ ```
18
+
19
+ `import ... from "billfn"` gives `@billfn/core` at the same version. Add a provider adapter (`@billfn/provider-dodo`,
20
+ `@billfn/provider-apple`) and, for the browser, `@billfn/client` or `@billfn/svelte`.
21
+
22
+ ## Quick start
23
+
24
+ ```ts
25
+ import { createBillFn, type BillFnCatalog } from "billfn";
26
+ import { createDodoProvider } from "@billfn/provider-dodo";
27
+ import { memoryAdapter } from "@superfunctions/db/adapters/memory";
28
+
29
+ const catalog: BillFnCatalog = {
30
+ plans: [
31
+ {
32
+ productKey: "nucleus",
33
+ planKey: "pro",
34
+ displayName: "Pro",
35
+ features: { sync: true, analytics: true },
36
+ limits: { storage: 1_000 },
37
+ prices: [
38
+ {
39
+ priceId: "price_pro_dodo_month",
40
+ provider: "dodo",
41
+ providerProductId: "pdt_pro_month",
42
+ currency: "USD",
43
+ amount: 12,
44
+ kind: "subscription",
45
+ interval: "month",
46
+ },
47
+ ],
48
+ },
49
+ ],
50
+ };
51
+
52
+ const billfn = createBillFn({
53
+ db: memoryAdapter({ debug: false }), // in-memory, for trying it out; use a durable adapter in production
54
+ catalog,
55
+ providers: {
56
+ dodo: createDodoProvider({
57
+ apiKey: process.env.DODO_API_KEY!,
58
+ webhookSecret: process.env.DODO_WEBHOOK_SECRET!,
59
+ }),
60
+ },
61
+ });
62
+ ```
63
+
64
+ This example targets Dodo's test mode, the adapter's default: use a test mode key (`dodo_test_...`). Production sets
65
+ `environment: "live_mode"` with a live key; see [server setup](https://github.com/21nCo/superfunctions/blob/dev/billfn/docs/content/docs/server/setup.mdx).
66
+
67
+ `createBillFn()` returns a service instance with:
68
+
69
+ - `router`, the HTTP routes under `basePath` (default `/billfn`): status, catalog, entitlements, usage, checkouts,
70
+ subscription cancel and sync, purchase restore, and `POST /billfn/webhooks/:provider`;
71
+ - `getSchema()`, the database schema for migrations;
72
+ - direct methods such as `createCheckout()` and `verifyCheckout()`;
73
+ - `subscriptionProvider` and `quotaProvider`, for the rest of your app to read entitlements and quotas.
74
+
75
+ ## Configuration
76
+
77
+ If you do not override them, `namespace` defaults to `"billfn"`, `basePath` to `"/billfn"`, ids come from the built-in
78
+ `defaultIdFactory()`, and billing accounts come from the default resolver: `organizationId` or `tenantId` becomes an
79
+ organization billing account, otherwise `principalId` or `actorId` becomes a user billing account, and a request with
80
+ none of them fails.
81
+
82
+ The router needs `auth.resolveSubject` to know who a request acts for; a `subject` in the request body or query string
83
+ is ignored when it is set. Without a resolver the account routes answer 401, unless you set
84
+ `auth.allowClientSubject: true` for trusted server-to-server callers. See
85
+ [getting started](https://github.com/21nCo/superfunctions/blob/dev/billfn/docs/content/docs/getting-started.mdx) and [routes](https://github.com/21nCo/superfunctions/blob/dev/billfn/docs/content/docs/server/routes.mdx).
86
+
87
+ ## Documentation
88
+
89
+ - [Overview](https://github.com/21nCo/superfunctions/blob/dev/billfn/docs/content/docs/index.mdx) and [getting started](https://github.com/21nCo/superfunctions/blob/dev/billfn/docs/content/docs/getting-started.mdx)
90
+ - [Architecture](https://github.com/21nCo/superfunctions/blob/dev/billfn/docs/content/docs/architecture.mdx)
91
+ - [Server setup](https://github.com/21nCo/superfunctions/blob/dev/billfn/docs/content/docs/server/setup.mdx), [routes](https://github.com/21nCo/superfunctions/blob/dev/billfn/docs/content/docs/server/routes.mdx),
92
+ [entitlements and quotas](https://github.com/21nCo/superfunctions/blob/dev/billfn/docs/content/docs/server/entitlements-and-quotas.mdx) and
93
+ [administration](https://github.com/21nCo/superfunctions/blob/dev/billfn/docs/content/docs/server/administration.mdx)
94
+ - Providers: [overview](https://github.com/21nCo/superfunctions/blob/dev/billfn/docs/content/docs/providers/overview.mdx), [Dodo](https://github.com/21nCo/superfunctions/blob/dev/billfn/docs/content/docs/providers/dodo.mdx),
95
+ [Apple](https://github.com/21nCo/superfunctions/blob/dev/billfn/docs/content/docs/providers/apple.mdx)
96
+ - Clients: [TypeScript](https://github.com/21nCo/superfunctions/blob/dev/billfn/docs/content/docs/clients/typescript.mdx), [Svelte](https://github.com/21nCo/superfunctions/blob/dev/billfn/docs/content/docs/clients/svelte.mdx),
97
+ [Swift](https://github.com/21nCo/superfunctions/blob/dev/billfn/docs/content/docs/clients/swift.mdx), [Python](https://github.com/21nCo/superfunctions/blob/dev/billfn/docs/content/docs/clients/python.mdx)
98
+ - [Production readiness](https://github.com/21nCo/superfunctions/blob/dev/billfn/docs/content/docs/production-readiness.mdx) and
99
+ [troubleshooting](https://github.com/21nCo/superfunctions/blob/dev/billfn/docs/content/docs/troubleshooting.mdx)
100
+
101
+ ## Packages
102
+
103
+ `billfn` is the front door; the scoped packages remain for installing one part.
104
+
105
+ | Package | What it is |
106
+ | --- | --- |
107
+ | `billfn` | The core as `import "billfn"` |
108
+ | `@billfn/core` | Billing domain, service, router, schema helpers, entitlement and quota providers; `billfn` re-exports it at the same version |
109
+ | `@billfn/client` | Typed HTTP client for browser or server consumers |
110
+ | `@billfn/svelte` | Svelte context and entitlement store helpers |
111
+ | `@billfn/admin` | Super Console administration capability backed by the configured BillFn instance |
112
+ | `@billfn/provider-dodo` | Dodo adapter: checkout, verification, sync, restore, refunds and webhooks |
113
+ | `@billfn/provider-apple` | Apple adapter: StoreKit verification, sync, restore and App Store Server Notifications |
114
+ | `@billfn/swift-bridge` | JavaScript bridge protocol for native-backed embedded web apps |
115
+
116
+ The Swift client (`BillFnSwift`) and the Python SDK (`billfn` on PyPI) live in [billfn/swift](https://github.com/21nCo/superfunctions/tree/dev/billfn/swift) and
117
+ [billfn/python](https://github.com/21nCo/superfunctions/tree/dev/billfn/python).
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from '@billfn/core';
package/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from '@billfn/core';
package/package.json CHANGED
@@ -1,42 +1,39 @@
1
1
  {
2
2
  "name": "billfn",
3
- "version": "0.0.1",
4
- "description": "Core billfn contracts, persistence, router, and entitlements",
3
+ "version": "0.8.1",
4
+ "description": "BillFn: import \"billfn\" for @billfn/core",
5
5
  "type": "module",
6
- "main": "./dist/index.js",
7
- "types": "./dist/index.d.ts",
8
6
  "exports": {
9
7
  ".": {
10
- "types": "./dist/index.d.ts",
11
- "default": "./dist/index.js"
8
+ "types": "./index.d.ts",
9
+ "import": "./index.js",
10
+ "default": "./index.js"
12
11
  }
13
12
  },
14
13
  "files": [
15
- "dist"
14
+ "index.js",
15
+ "index.d.ts",
16
+ "README.md"
16
17
  ],
17
18
  "scripts": {
18
- "build": "tsc",
19
- "test": "vitest run",
20
- "test:watch": "vitest --watch",
21
- "typecheck": "tsc --noEmit",
22
- "clean": "rm -rf dist"
19
+ "build": "node --check index.js",
20
+ "test": "node --test test/front-door.test.mjs"
21
+ },
22
+ "dependencies": {
23
+ "@billfn/core": "0.8.1"
23
24
  },
24
- "author": "21n",
25
25
  "license": "MIT",
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "git+https://github.com/21nCo/superfunctions.git",
29
+ "directory": "billfn/bare"
30
+ },
31
+ "bugs": {
32
+ "url": "https://github.com/21nCo/superfunctions/issues"
33
+ },
34
+ "sideEffects": false,
26
35
  "publishConfig": {
27
36
  "access": "public"
28
37
  },
29
- "dependencies": {
30
- "@superfunctions/db": "*",
31
- "@superfunctions/envelope": "*",
32
- "@superfunctions/errors": "*",
33
- "@superfunctions/http": "*",
34
- "@superfunctions/metrics": "*",
35
- "@superfunctions/queue": "*"
36
- },
37
- "devDependencies": {
38
- "@types/node": "^22.0.0",
39
- "typescript": "^5.6.0",
40
- "vitest": "^3.2.4"
41
- }
38
+ "gitHead": "3cdd0e0c51838497df52848e9d5f806c6865d93d"
42
39
  }
package/dist/errors.d.ts DELETED
@@ -1,58 +0,0 @@
1
- import { type ErrorRegistry, type SuperfunctionError } from '@superfunctions/errors';
2
- declare const definitions: readonly [{
3
- readonly code: "BILLFN_VALIDATION_ERROR";
4
- readonly httpStatus: 400;
5
- readonly retryable: false;
6
- }, {
7
- readonly code: "BILLFN_NOT_FOUND";
8
- readonly httpStatus: 404;
9
- readonly retryable: false;
10
- }, {
11
- readonly code: "BILLFN_CONFLICT";
12
- readonly httpStatus: 409;
13
- readonly retryable: false;
14
- }, {
15
- readonly code: "BILLFN_UNAUTHORIZED";
16
- readonly httpStatus: 403;
17
- readonly retryable: false;
18
- }, {
19
- readonly code: "BILLFN_PROVIDER_ERROR";
20
- readonly httpStatus: 502;
21
- readonly retryable: true;
22
- }, {
23
- readonly code: "BILLFN_PROVIDER_UNSUPPORTED";
24
- readonly httpStatus: 501;
25
- readonly retryable: false;
26
- }, {
27
- readonly code: "BILLFN_WEBHOOK_SIGNATURE_INVALID";
28
- readonly httpStatus: 401;
29
- readonly retryable: false;
30
- }, {
31
- readonly code: "BILLFN_SUBSCRIPTION_INACTIVE";
32
- readonly httpStatus: 402;
33
- readonly retryable: false;
34
- }, {
35
- readonly code: "BILLFN_QUOTA_EXCEEDED";
36
- readonly httpStatus: 403;
37
- readonly retryable: false;
38
- }, {
39
- readonly code: "BILLFN_FEATURE_UNAVAILABLE";
40
- readonly httpStatus: 403;
41
- readonly retryable: false;
42
- }, {
43
- readonly code: "BILLFN_CATALOG_PRICE_NOT_FOUND";
44
- readonly httpStatus: 404;
45
- readonly retryable: false;
46
- }, {
47
- readonly code: "BILLFN_INTERNAL_ERROR";
48
- readonly httpStatus: 500;
49
- readonly retryable: false;
50
- }];
51
- export declare const billFnErrorRegistry: ErrorRegistry;
52
- export declare function createBillFnError(input: {
53
- code: (typeof definitions)[number]['code'];
54
- message?: string;
55
- details?: Record<string, unknown>;
56
- }): SuperfunctionError;
57
- export {};
58
- //# sourceMappingURL=errors.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,aAAa,EAClB,KAAK,kBAAkB,EACxB,MAAM,wBAAwB,CAAC;AAEhC,QAAA,MAAM,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA6DP,CAAC;AAEX,eAAO,MAAM,mBAAmB,EAAE,aAAqD,CAAC;AAExF,wBAAgB,iBAAiB,CAAC,KAAK,EAAE;IACvC,IAAI,EAAE,CAAC,OAAO,WAAW,CAAC,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC;IAC3C,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACnC,GAAG,kBAAkB,CAOrB"}
package/dist/errors.js DELETED
@@ -1,73 +0,0 @@
1
- import { createErrorRegistry, createTypedError } from '@superfunctions/errors';
2
- const definitions = [
3
- {
4
- code: 'BILLFN_VALIDATION_ERROR',
5
- httpStatus: 400,
6
- retryable: false
7
- },
8
- {
9
- code: 'BILLFN_NOT_FOUND',
10
- httpStatus: 404,
11
- retryable: false
12
- },
13
- {
14
- code: 'BILLFN_CONFLICT',
15
- httpStatus: 409,
16
- retryable: false
17
- },
18
- {
19
- code: 'BILLFN_UNAUTHORIZED',
20
- httpStatus: 403,
21
- retryable: false
22
- },
23
- {
24
- code: 'BILLFN_PROVIDER_ERROR',
25
- httpStatus: 502,
26
- retryable: true
27
- },
28
- {
29
- code: 'BILLFN_PROVIDER_UNSUPPORTED',
30
- httpStatus: 501,
31
- retryable: false
32
- },
33
- {
34
- code: 'BILLFN_WEBHOOK_SIGNATURE_INVALID',
35
- httpStatus: 401,
36
- retryable: false
37
- },
38
- {
39
- code: 'BILLFN_SUBSCRIPTION_INACTIVE',
40
- httpStatus: 402,
41
- retryable: false
42
- },
43
- {
44
- code: 'BILLFN_QUOTA_EXCEEDED',
45
- httpStatus: 403,
46
- retryable: false
47
- },
48
- {
49
- code: 'BILLFN_FEATURE_UNAVAILABLE',
50
- httpStatus: 403,
51
- retryable: false
52
- },
53
- {
54
- code: 'BILLFN_CATALOG_PRICE_NOT_FOUND',
55
- httpStatus: 404,
56
- retryable: false
57
- },
58
- {
59
- code: 'BILLFN_INTERNAL_ERROR',
60
- httpStatus: 500,
61
- retryable: false
62
- }
63
- ];
64
- export const billFnErrorRegistry = createErrorRegistry([...definitions]);
65
- export function createBillFnError(input) {
66
- return createTypedError({
67
- code: input.code,
68
- message: input.message,
69
- details: input.details,
70
- registry: billFnErrorRegistry
71
- });
72
- }
73
- //# sourceMappingURL=errors.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,gBAAgB,EAGjB,MAAM,wBAAwB,CAAC;AAEhC,MAAM,WAAW,GAAG;IAClB;QACE,IAAI,EAAE,yBAAyB;QAC/B,UAAU,EAAE,GAAG;QACf,SAAS,EAAE,KAAK;KACjB;IACD;QACE,IAAI,EAAE,kBAAkB;QACxB,UAAU,EAAE,GAAG;QACf,SAAS,EAAE,KAAK;KACjB;IACD;QACE,IAAI,EAAE,iBAAiB;QACvB,UAAU,EAAE,GAAG;QACf,SAAS,EAAE,KAAK;KACjB;IACD;QACE,IAAI,EAAE,qBAAqB;QAC3B,UAAU,EAAE,GAAG;QACf,SAAS,EAAE,KAAK;KACjB;IACD;QACE,IAAI,EAAE,uBAAuB;QAC7B,UAAU,EAAE,GAAG;QACf,SAAS,EAAE,IAAI;KAChB;IACD;QACE,IAAI,EAAE,6BAA6B;QACnC,UAAU,EAAE,GAAG;QACf,SAAS,EAAE,KAAK;KACjB;IACD;QACE,IAAI,EAAE,kCAAkC;QACxC,UAAU,EAAE,GAAG;QACf,SAAS,EAAE,KAAK;KACjB;IACD;QACE,IAAI,EAAE,8BAA8B;QACpC,UAAU,EAAE,GAAG;QACf,SAAS,EAAE,KAAK;KACjB;IACD;QACE,IAAI,EAAE,uBAAuB;QAC7B,UAAU,EAAE,GAAG;QACf,SAAS,EAAE,KAAK;KACjB;IACD;QACE,IAAI,EAAE,4BAA4B;QAClC,UAAU,EAAE,GAAG;QACf,SAAS,EAAE,KAAK;KACjB;IACD;QACE,IAAI,EAAE,gCAAgC;QACtC,UAAU,EAAE,GAAG;QACf,SAAS,EAAE,KAAK;KACjB;IACD;QACE,IAAI,EAAE,uBAAuB;QAC7B,UAAU,EAAE,GAAG;QACf,SAAS,EAAE,KAAK;KACjB;CACO,CAAC;AAEX,MAAM,CAAC,MAAM,mBAAmB,GAAkB,mBAAmB,CAAC,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC;AAExF,MAAM,UAAU,iBAAiB,CAAC,KAIjC;IACC,OAAO,gBAAgB,CAAC;QACtB,IAAI,EAAE,KAAK,CAAC,IAAI;QAChB,OAAO,EAAE,KAAK,CAAC,OAAO;QACtB,OAAO,EAAE,KAAK,CAAC,OAAO;QACtB,QAAQ,EAAE,mBAAmB;KAC9B,CAAC,CAAC;AACL,CAAC"}
package/dist/helpers.d.ts DELETED
@@ -1,13 +0,0 @@
1
- import type { BillFnCatalog, BillFnPlanDefinition, BillFnPriceDefinition, BillableSubject, BillingAccountResolver, BillingOwnerType } from './types.js';
2
- export declare function normalizeBasePath(basePath: string | undefined, fallback: string): string;
3
- export declare function toIsoString(value: Date): string;
4
- export declare function defaultIdFactory(prefix: string): string;
5
- export declare function createDefaultBillingAccountResolver(): BillingAccountResolver;
6
- export declare function buildBillingAccountId(ownerType: BillingOwnerType, ownerId: string): string;
7
- export declare function resolvePlan(catalog: BillFnCatalog, planKey: string): BillFnPlanDefinition | undefined;
8
- export declare function resolvePrice(plan: BillFnPlanDefinition, provider: BillFnPriceDefinition['provider'], interval: BillFnPriceDefinition['interval'] | undefined): BillFnPriceDefinition | undefined;
9
- export declare function getSubjectFromQuery(url: URL): BillableSubject;
10
- export declare function resolveRequestSubject(request: Request, configResolver?: (request: Request) => Promise<BillableSubject | null> | BillableSubject | null, bodySubject?: BillableSubject): Promise<BillableSubject>;
11
- export declare function hasSomeSubjectField(subject: BillableSubject): boolean;
12
- export declare function decodeJwtPayload<T extends Record<string, unknown>>(token: string): T | null;
13
- //# sourceMappingURL=helpers.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,aAAa,EACb,oBAAoB,EACpB,qBAAqB,EACrB,eAAe,EACf,sBAAsB,EACtB,gBAAgB,EACjB,MAAM,YAAY,CAAC;AAEpB,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAMxF;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,IAAI,GAAG,MAAM,CAE/C;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAEvD;AAED,wBAAgB,mCAAmC,IAAI,sBAAsB,CAwB5E;AAED,wBAAgB,qBAAqB,CAAC,SAAS,EAAE,gBAAgB,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAE1F;AAED,wBAAgB,WAAW,CAAC,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,GAAG,oBAAoB,GAAG,SAAS,CAErG;AAED,wBAAgB,YAAY,CAC1B,IAAI,EAAE,oBAAoB,EAC1B,QAAQ,EAAE,qBAAqB,CAAC,UAAU,CAAC,EAC3C,QAAQ,EAAE,qBAAqB,CAAC,UAAU,CAAC,GAAG,SAAS,GACtD,qBAAqB,GAAG,SAAS,CAMnC;AAED,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,GAAG,GAAG,eAAe,CAc7D;AAED,wBAAsB,qBAAqB,CACzC,OAAO,EAAE,OAAO,EAChB,cAAc,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC,GAAG,eAAe,GAAG,IAAI,EAC/F,WAAW,CAAC,EAAE,eAAe,GAC5B,OAAO,CAAC,eAAe,CAAC,CAkB1B;AAED,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,eAAe,GAAG,OAAO,CAOrE;AAED,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,KAAK,EAAE,MAAM,GAAG,CAAC,GAAG,IAAI,CAgB3F"}
package/dist/helpers.js DELETED
@@ -1,102 +0,0 @@
1
- export function normalizeBasePath(basePath, fallback) {
2
- const raw = (basePath ?? fallback).trim();
3
- if (!raw) {
4
- return fallback;
5
- }
6
- return raw.startsWith('/') ? raw.replace(/\/$/, '') : `/${raw.replace(/\/$/, '')}`;
7
- }
8
- export function toIsoString(value) {
9
- return value.toISOString();
10
- }
11
- export function defaultIdFactory(prefix) {
12
- return `${prefix}_${Math.random().toString(36).slice(2, 10)}${Date.now().toString(36)}`;
13
- }
14
- export function createDefaultBillingAccountResolver() {
15
- return {
16
- async resolve(input) {
17
- const organizationId = input.organizationId ?? input.tenantId;
18
- if (organizationId) {
19
- return {
20
- billingAccountId: buildBillingAccountId('organization', organizationId),
21
- ownerType: 'organization',
22
- ownerId: organizationId
23
- };
24
- }
25
- const userId = input.principalId ?? input.actorId;
26
- if (!userId) {
27
- return null;
28
- }
29
- return {
30
- billingAccountId: buildBillingAccountId('user', userId),
31
- ownerType: 'user',
32
- ownerId: userId
33
- };
34
- }
35
- };
36
- }
37
- export function buildBillingAccountId(ownerType, ownerId) {
38
- return `ba_${ownerType}_${ownerId}`;
39
- }
40
- export function resolvePlan(catalog, planKey) {
41
- return catalog.plans.find((plan) => plan.planKey === planKey);
42
- }
43
- export function resolvePrice(plan, provider, interval) {
44
- if (interval) {
45
- return plan.prices.find((price) => price.provider === provider && price.interval === interval);
46
- }
47
- return plan.prices.find((price) => price.provider === provider);
48
- }
49
- export function getSubjectFromQuery(url) {
50
- const actorId = url.searchParams.get('actorId') ?? undefined;
51
- const principalId = url.searchParams.get('principalId') ?? undefined;
52
- const tenantId = url.searchParams.get('tenantId') ?? undefined;
53
- const organizationId = url.searchParams.get('organizationId') ?? undefined;
54
- const actorType = url.searchParams.get('actorType');
55
- return {
56
- actorId,
57
- actorType: actorType ?? undefined,
58
- principalId,
59
- tenantId,
60
- organizationId
61
- };
62
- }
63
- export async function resolveRequestSubject(request, configResolver, bodySubject) {
64
- if (configResolver) {
65
- const resolved = await configResolver(request);
66
- if (resolved && hasSomeSubjectField(resolved)) {
67
- return resolved;
68
- }
69
- }
70
- if (bodySubject && hasSomeSubjectField(bodySubject)) {
71
- return bodySubject;
72
- }
73
- const fromQuery = getSubjectFromQuery(new URL(request.url));
74
- if (hasSomeSubjectField(fromQuery)) {
75
- return fromQuery;
76
- }
77
- return {};
78
- }
79
- export function hasSomeSubjectField(subject) {
80
- return Boolean(subject.actorId ||
81
- subject.principalId ||
82
- subject.organizationId ||
83
- subject.tenantId);
84
- }
85
- export function decodeJwtPayload(token) {
86
- const parts = token.split('.');
87
- if (parts.length < 2) {
88
- return null;
89
- }
90
- try {
91
- const payload = parts[1]
92
- .replace(/-/g, '+')
93
- .replace(/_/g, '/');
94
- const padded = payload.padEnd(Math.ceil(payload.length / 4) * 4, '=');
95
- const decoded = Buffer.from(padded, 'base64').toString('utf8');
96
- return JSON.parse(decoded);
97
- }
98
- catch {
99
- return null;
100
- }
101
- }
102
- //# sourceMappingURL=helpers.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"helpers.js","sourceRo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CAAC;IAED,IAAI,CAAC;QACH,MAAM,OAAO,GAAG,KAAK,CAAC,CAAC,CAAC;aACrB,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC;aAClB,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;QACtB,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,CAAC;QACtE,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;QAC/D,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAM,CAAC;IAClC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
package/dist/http.d.ts DELETED
@@ -1,5 +0,0 @@
1
- import { type Envelope } from '@superfunctions/envelope';
2
- export declare function jsonResponse<T>(body: Envelope<T>, status?: number): Response;
3
- export declare function successResponse<T>(data: T, status?: number): Response;
4
- export declare function errorResponse(error: Error): Response;
5
- //# sourceMappingURL=http.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,KAAK,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AAGlE,wBAAgB,YAAY,CAAC,CAAC,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,MAAM,GAAE,MAAY,GAAG,QAAQ,CAOjF;AAED,wBAAgB,eAAe,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,MAAM,GAAE,MAAY,GAAG,QAAQ,CAE1E;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,KAAK,GAAG,QAAQ,CAuBpD"}
package/dist/http.js DELETED
@@ -1,33 +0,0 @@
1
- import { err, ok } from '@superfunctions/envelope';
2
- export function jsonResponse(body, status = 200) {
3
- return new Response(JSON.stringify(body), {
4
- status,
5
- headers: {
6
- 'content-type': 'application/json'
7
- }
8
- });
9
- }
10
- export function successResponse(data, status = 200) {
11
- return jsonResponse(ok(data), status);
12
- }
13
- export function errorResponse(error) {
14
- if (isSuperfunctionError(error)) {
15
- return jsonResponse(err({
16
- code: error.code,
17
- message: error.message,
18
- status: error.status,
19
- retryable: error.retryable,
20
- details: error.details
21
- }), error.status);
22
- }
23
- return jsonResponse(err({
24
- code: 'BILLFN_INTERNAL_ERROR',
25
- message: error.message || 'Unexpected billfn error',
26
- status: 500,
27
- retryable: false
28
- }), 500);
29
- }
30
- function isSuperfunctionError(value) {
31
- return 'code' in value && 'status' in value && 'retryable' in value;
32
- }
33
- //# sourceMappingURL=http.js.map
package/dist/http.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"http.js","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,EAAE,EAAiB,MAAM,0BAA0B,CAAC;AAGlE,MAAM,UAAU,YAAY,CAAI,IAAiB,EAAE,SAAiB,GAAG;IACrE,OAAO,IAAI,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE;QACxC,MAAM;QACN,OAAO,EAAE;YACP,cAAc,EAAE,kBAAkB;SACnC;KACF,CAAC,CAAC;AACL,CAAC;AAED,MAAM,UAAU,eAAe,CAAI,IAAO,EAAE,SAAiB,GAAG;IAC9D,OAAO,YAAY,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC,CAAC;AACxC,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,KAAY;IACxC,IAAI,oBAAoB,CAAC,KAAK,CAAC,EAAE,CAAC;QAChC,OAAO,YAAY,CACjB,GAAG,CAAC;YACF,IAAI,EAAE,KAAK,CAAC,IAAI;YAChB,OAAO,EAAE,KAAK,CAAC,OAAO;YACtB,MAAM,EAAE,KAAK,CAAC,MAAM;YACpB,SAAS,EAAE,KAAK,CAAC,SAAS;YAC1B,OAAO,EAAE,KAAK,CAAC,OAAO;SACvB,CAAC,EACF,KAAK,CAAC,MAAM,CACb,CAAC;IACJ,CAAC;IAED,OAAO,YAAY,CACjB,GAAG,CAAC;QACF,IAAI,EAAE,uBAAuB;QAC7B,OAAO,EAAE,KAAK,CAAC,OAAO,IAAI,yBAAyB;QACnD,MAAM,EAAE,GAAG;QACX,SAAS,EAAE,KAAK;KACjB,CAAC,EACF,GAAG,CACJ,CAAC;AACJ,CAAC;AAED,SAAS,oBAAoB,CAAC,KAAY;IACxC,OAAO,MAAM,IAAI,KAAK,IAAI,QAAQ,IAAI,KAAK,IAAI,WAAW,IAAI,KAAK,CAAC;AACtE,CAAC"}
package/dist/index.d.ts DELETED
@@ -1,7 +0,0 @@
1
- import type { BillFnConfig, BillFnInstance } from './types.js';
2
- export type * from './types.js';
3
- export { getSchema, getSchemaMap } from './schema.js';
4
- export { createBillFnError, billFnErrorRegistry } from './errors.js';
5
- export { createBillFnReconciliationWorker } from './service.js';
6
- export declare function createBillFn(config: BillFnConfig): BillFnInstance;
7
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAE/D,mBAAmB,YAAY,CAAC;AAChC,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AACrE,OAAO,EAAE,gCAAgC,EAAE,MAAM,cAAc,CAAC;AAEhE,wBAAgB,YAAY,CAAC,MAAM,EAAE,YAAY,GAAG,cAAc,CAyBjE"}
package/dist/index.js DELETED
@@ -1,32 +0,0 @@
1
- import { createBillFnRouter } from './router.js';
2
- import { getSchema } from './schema.js';
3
- import { createBillFnService } from './service.js';
4
- export { getSchema, getSchemaMap } from './schema.js';
5
- export { createBillFnError, billFnErrorRegistry } from './errors.js';
6
- export { createBillFnReconciliationWorker } from './service.js';
7
- export function createBillFn(config) {
8
- const service = createBillFnService(config);
9
- const router = createBillFnRouter(config);
10
- return {
11
- router,
12
- getSchema: () => getSchema({ namespace: config.namespace }),
13
- getCatalog: service.getCatalog,
14
- createCheckout: service.createCheckout,
15
- verifyCheckout: service.verifyCheckout,
16
- cancelSubscription: service.cancelSubscription,
17
- changeSubscription: service.changeSubscription,
18
- resumeSubscription: service.resumeSubscription,
19
- refundCharge: service.refundCharge,
20
- syncSubscription: service.syncSubscription,
21
- restorePurchases: service.restorePurchases,
22
- getEntitlements: service.getEntitlements,
23
- getUsage: service.getUsage,
24
- handleWebhook: service.handleWebhook,
25
- enqueueReconciliationJob: service.enqueueReconciliationJob,
26
- getReconciliationJob: service.getReconciliationJob,
27
- runReconciliationJob: service.runReconciliationJob,
28
- subscriptionProvider: service.subscriptionProvider,
29
- quotaProvider: service.quotaProvider
30
- };
31
- }
32
- //# sourceMappingURL=index.js.map
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAInD,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AACrE,OAAO,EAAE,gCAAgC,EAAE,MAAM,cAAc,CAAC;AAEhE,MAAM,UAAU,YAAY,CAAC,MAAoB;IAC/C,MAAM,OAAO,GAAG,mBAAmB,CAAC,MAAM,CAAC,CAAC;IAC5C,MAAM,MAAM,GAAG,kBAAkB,CAAC,MAAM,CAAC,CAAC;IAE1C,OAAO;QACL,MAAM;QACN,SAAS,EAAE,GAAG,EAAE,CAAC,SAAS,CAAC,EAAE,SAAS,EAAE,MAAM,CAAC,SAAS,EAAE,CAAC;QAC3D,UAAU,EAAE,OAAO,CAAC,UAAU;QAC9B,cAAc,EAAE,OAAO,CAAC,cAAc;QACtC,cAAc,EAAE,OAAO,CAAC,cAAc;QACtC,kBAAkB,EAAE,OAAO,CAAC,kBAAkB;QAC9C,kBAAkB,EAAE,OAAO,CAAC,kBAAkB;QAC9C,kBAAkB,EAAE,OAAO,CAAC,kBAAkB;QAC9C,YAAY,EAAE,OAAO,CAAC,YAAY;QAClC,gBAAgB,EAAE,OAAO,CAAC,gBAAgB;QAC1C,gBAAgB,EAAE,OAAO,CAAC,gBAAgB;QAC1C,eAAe,EAAE,OAAO,CAAC,eAAe;QACxC,QAAQ,EAAE,OAAO,CAAC,QAAQ;QAC1B,aAAa,EAAE,OAAO,CAAC,aAAa;QACpC,wBAAwB,EAAE,OAAO,CAAC,wBAAwB;QAC1D,oBAAoB,EAAE,OAAO,CAAC,oBAAoB;QAClD,oBAAoB,EAAE,OAAO,CAAC,oBAAoB;QAClD,oBAAoB,EAAE,OAAO,CAAC,oBAAoB;QAClD,aAAa,EAAE,OAAO,CAAC,aAAa;KACrC,CAAC;AACJ,CAAC"}
package/dist/router.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import type { BillFnConfig } from './types.js';
2
- export declare function createBillFnRouter(config: BillFnConfig): import("@superfunctions/http").Router<any>;
3
- //# sourceMappingURL=router.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../src/router.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,YAAY,EAAmB,MAAM,YAAY,CAAC;AAEhE,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,YAAY,8CAwVtD"}