@baukit/purchases-core 0.0.0-stage → 0.12.2
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/README.md +36 -2
- package/dist/entitlement.d.ts +26 -0
- package/dist/entitlement.d.ts.map +1 -0
- package/dist/entitlement.js +12 -0
- package/dist/entitlement.js.map +1 -0
- package/dist/errors.d.ts +9 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +12 -0
- package/dist/errors.js.map +1 -0
- package/dist/flow.d.ts +55 -0
- package/dist/flow.d.ts.map +1 -0
- package/dist/flow.js +190 -0
- package/dist/flow.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/noop.d.ts +4 -0
- package/dist/noop.d.ts.map +1 -0
- package/dist/noop.js +14 -0
- package/dist/noop.js.map +1 -0
- package/dist/port.d.ts +30 -0
- package/dist/port.d.ts.map +1 -0
- package/dist/port.js +7 -0
- package/dist/port.js.map +1 -0
- package/dist/react.d.ts +14 -0
- package/dist/react.d.ts.map +1 -0
- package/dist/react.js +24 -0
- package/dist/react.js.map +1 -0
- package/package.json +64 -4
- package/src/entitlement.ts +39 -0
- package/src/errors.ts +26 -0
- package/src/flow.test.ts +532 -0
- package/src/flow.ts +252 -0
- package/src/index.ts +5 -0
- package/src/noop.ts +16 -0
- package/src/port.ts +40 -0
- package/src/react.test.ts +78 -0
- package/src/react.ts +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Patrick Koss
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,37 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@baukit/purchases-core`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A store-neutral purchase flow for Baukit mobile apps. It defines the `PurchasesPort` that a store adapter implements, a no-op port for builds without store keys, and a `PurchaseFlow` that gates features on the entitlement the product backend verified. It never reads store entitlements. Schlüsselbund owns entitlements, so a store receipt grants nothing until the refreshed ticket says so.
|
|
4
|
+
|
|
5
|
+
The package has no Expo, React Native or RevenueCat imports. `@baukit/purchases-revenuecat` is the RevenueCat adapter.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import {
|
|
9
|
+
PurchaseFlow,
|
|
10
|
+
createNoopPurchasesPort,
|
|
11
|
+
entitlementFromTicket,
|
|
12
|
+
} from '@baukit/purchases-core';
|
|
13
|
+
|
|
14
|
+
const flow = new PurchaseFlow({
|
|
15
|
+
port: createNoopPurchasesPort(),
|
|
16
|
+
backend: {
|
|
17
|
+
refreshEntitlement: async () => entitlementFromTicket(await api.refreshEntitlementTicket()),
|
|
18
|
+
creditBalance: () => api.creditBalance(),
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
await flow.identify(suiteSubject);
|
|
23
|
+
const { current } = await flow.offerings();
|
|
24
|
+
const outcome = await flow.purchase(current!.packages[0]!);
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
- `identify(suiteSubject)` must finish before `purchase`, `restore` or `purchaseCreditPack`. Without it they reject with `not_identified`.
|
|
28
|
+
- `signOut()` clears the state and calls the port's `reset`.
|
|
29
|
+
- `purchase` returns `granted` once the backend serves an entitlement with a newer revision, or `pending` after `refreshAttempts` reads (default 5, 2 seconds apart).
|
|
30
|
+
- `restore` returns `granted` or `unchanged`.
|
|
31
|
+
- `purchaseCreditPack` accepts only `one_time` packages and returns `credited` once the credit balance revision advances, or `pending`.
|
|
32
|
+
- Only one purchase runs at a time. A second one rejects with `busy`.
|
|
33
|
+
- Failures are `PurchaseError` with `code` `cancelled`, `pending`, `network`, `store`, `unavailable`, `not_identified`, `identity_changed`, `subject_mismatch`, `invalid_subject`, `not_credit_pack` or `busy`.
|
|
34
|
+
|
|
35
|
+
`@baukit/purchases-core/react` has `PurchasesProvider`, `usePurchaseFlow`, `usePurchaseState`, `useEntitlement` and `useFeature(key)`.
|
|
36
|
+
|
|
37
|
+
Read Baukit's `docs/platform/mobile-purchases.md` for the full wiring.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** Entitlement the product backend verified from the hub ticket. */
|
|
2
|
+
export interface VerifiedEntitlement {
|
|
3
|
+
readonly suiteSubject: string;
|
|
4
|
+
readonly tier: string;
|
|
5
|
+
readonly features: readonly string[];
|
|
6
|
+
readonly revision: number;
|
|
7
|
+
}
|
|
8
|
+
export interface CreditBalanceSnapshot {
|
|
9
|
+
readonly suiteSubject: string;
|
|
10
|
+
readonly available: number;
|
|
11
|
+
readonly revision: number;
|
|
12
|
+
}
|
|
13
|
+
/** Product backend calls. The backend refreshes the hub ticket with its service credentials. */
|
|
14
|
+
export interface EntitlementBackend {
|
|
15
|
+
refreshEntitlement(): Promise<VerifiedEntitlement | null>;
|
|
16
|
+
creditBalance?(): Promise<CreditBalanceSnapshot | null>;
|
|
17
|
+
}
|
|
18
|
+
export interface TicketClaimsLike {
|
|
19
|
+
readonly sub: string;
|
|
20
|
+
readonly tier: string;
|
|
21
|
+
readonly features: readonly string[];
|
|
22
|
+
readonly rev: number;
|
|
23
|
+
}
|
|
24
|
+
export declare function entitlementFromTicket(claims: TicketClaimsLike): VerifiedEntitlement;
|
|
25
|
+
export declare function hasFeature(entitlement: VerifiedEntitlement | null, key: string): boolean;
|
|
26
|
+
//# sourceMappingURL=entitlement.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entitlement.d.ts","sourceRoot":"","sources":["../src/entitlement.ts"],"names":[],"mappings":"AAAA,oEAAoE;AACpE,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAC;IACrC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC3B;AAED,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC3B;AAED,gGAAgG;AAChG,MAAM,WAAW,kBAAkB;IACjC,kBAAkB,IAAI,OAAO,CAAC,mBAAmB,GAAG,IAAI,CAAC,CAAC;IAC1D,aAAa,CAAC,IAAI,OAAO,CAAC,qBAAqB,GAAG,IAAI,CAAC,CAAC;CACzD;AAED,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAC;IACrC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;CACtB;AAED,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,gBAAgB,GAAG,mBAAmB,CAOnF;AAED,wBAAgB,UAAU,CAAC,WAAW,EAAE,mBAAmB,GAAG,IAAI,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAExF"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export function entitlementFromTicket(claims) {
|
|
2
|
+
return {
|
|
3
|
+
suiteSubject: claims.sub,
|
|
4
|
+
tier: claims.tier,
|
|
5
|
+
features: claims.features,
|
|
6
|
+
revision: claims.rev,
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
export function hasFeature(entitlement, key) {
|
|
10
|
+
return entitlement?.features.includes(key) ?? false;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=entitlement.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entitlement.js","sourceRoot":"","sources":["../src/entitlement.ts"],"names":[],"mappings":"AA2BA,MAAM,UAAU,qBAAqB,CAAC,MAAwB;IAC5D,OAAO;QACL,YAAY,EAAE,MAAM,CAAC,GAAG;QACxB,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,QAAQ,EAAE,MAAM,CAAC,QAAQ;QACzB,QAAQ,EAAE,MAAM,CAAC,GAAG;KACrB,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,WAAuC,EAAE,GAAW;IAC7E,OAAO,WAAW,EAAE,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,KAAK,CAAC;AACtD,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type PurchaseErrorCode = 'cancelled' | 'pending' | 'network' | 'store' | 'unavailable' | 'not_identified' | 'identity_changed' | 'subject_mismatch' | 'invalid_subject' | 'not_credit_pack' | 'busy';
|
|
2
|
+
export declare class PurchaseError extends Error {
|
|
3
|
+
readonly code: PurchaseErrorCode;
|
|
4
|
+
constructor(code: PurchaseErrorCode, options?: {
|
|
5
|
+
readonly cause?: unknown;
|
|
6
|
+
});
|
|
7
|
+
}
|
|
8
|
+
export declare function isPurchaseError(error: unknown, code?: PurchaseErrorCode): error is PurchaseError;
|
|
9
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,iBAAiB,GACzB,WAAW,GACX,SAAS,GACT,SAAS,GACT,OAAO,GACP,aAAa,GACb,gBAAgB,GAChB,kBAAkB,GAClB,kBAAkB,GAClB,iBAAiB,GACjB,iBAAiB,GACjB,MAAM,CAAC;AAEX,qBAAa,aAAc,SAAQ,KAAK;IAEpC,QAAQ,CAAC,IAAI,EAAE,iBAAiB;gBAAvB,IAAI,EAAE,iBAAiB,EAChC,OAAO,CAAC,EAAE;QAAE,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE;CAKzC;AAED,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,iBAAiB,GAAG,KAAK,IAAI,aAAa,CAEhG"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export class PurchaseError extends Error {
|
|
2
|
+
code;
|
|
3
|
+
constructor(code, options) {
|
|
4
|
+
super(code, options);
|
|
5
|
+
this.code = code;
|
|
6
|
+
this.name = 'PurchaseError';
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export function isPurchaseError(error, code) {
|
|
10
|
+
return error instanceof PurchaseError && (code === undefined || error.code === code);
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAaA,MAAM,OAAO,aAAc,SAAQ,KAAK;IAE3B;IADX,YACW,IAAuB,EAChC,OAAsC;QAEtC,KAAK,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QAHZ,SAAI,GAAJ,IAAI,CAAmB;QAIhC,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;IAC9B,CAAC;CACF;AAED,MAAM,UAAU,eAAe,CAAC,KAAc,EAAE,IAAwB;IACtE,OAAO,KAAK,YAAY,aAAa,IAAI,CAAC,IAAI,KAAK,SAAS,IAAI,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC;AACvF,CAAC"}
|
package/dist/flow.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { CreditBalanceSnapshot, EntitlementBackend, VerifiedEntitlement } from './entitlement.js';
|
|
2
|
+
import type { PurchaseOfferings, PurchasePackage, PurchasesPort } from './port.js';
|
|
3
|
+
export declare const DEFAULT_REFRESH_ATTEMPTS = 5;
|
|
4
|
+
export declare const DEFAULT_REFRESH_INTERVAL_MS = 2000;
|
|
5
|
+
export interface PurchaseState {
|
|
6
|
+
readonly suiteSubject: string | null;
|
|
7
|
+
readonly entitlement: VerifiedEntitlement | null;
|
|
8
|
+
readonly creditBalance: CreditBalanceSnapshot | null;
|
|
9
|
+
}
|
|
10
|
+
export type PurchaseOutcome = {
|
|
11
|
+
readonly status: 'granted';
|
|
12
|
+
readonly entitlement: VerifiedEntitlement;
|
|
13
|
+
} | {
|
|
14
|
+
readonly status: 'pending';
|
|
15
|
+
readonly entitlement: VerifiedEntitlement | null;
|
|
16
|
+
};
|
|
17
|
+
export type RestoreOutcome = {
|
|
18
|
+
readonly status: 'granted';
|
|
19
|
+
readonly entitlement: VerifiedEntitlement;
|
|
20
|
+
} | {
|
|
21
|
+
readonly status: 'unchanged';
|
|
22
|
+
readonly entitlement: VerifiedEntitlement | null;
|
|
23
|
+
};
|
|
24
|
+
export type CreditPackOutcome = {
|
|
25
|
+
readonly status: 'credited';
|
|
26
|
+
readonly balance: CreditBalanceSnapshot;
|
|
27
|
+
} | {
|
|
28
|
+
readonly status: 'pending';
|
|
29
|
+
readonly balance: CreditBalanceSnapshot | null;
|
|
30
|
+
};
|
|
31
|
+
export interface PurchaseFlowOptions {
|
|
32
|
+
readonly port: PurchasesPort;
|
|
33
|
+
readonly backend: EntitlementBackend;
|
|
34
|
+
/** Backend reads after a purchase before the outcome is reported as pending. */
|
|
35
|
+
readonly refreshAttempts?: number;
|
|
36
|
+
readonly refreshIntervalMs?: number;
|
|
37
|
+
readonly sleep?: (milliseconds: number) => Promise<void>;
|
|
38
|
+
}
|
|
39
|
+
export declare class PurchaseFlow {
|
|
40
|
+
#private;
|
|
41
|
+
constructor(options: PurchaseFlowOptions);
|
|
42
|
+
readonly getState: () => PurchaseState;
|
|
43
|
+
readonly subscribe: (listener: () => void) => (() => void);
|
|
44
|
+
/** Purchases stay refused until the store has finished identifying this suite subject. */
|
|
45
|
+
identify(suiteSubject: string): Promise<void>;
|
|
46
|
+
signOut(): Promise<void>;
|
|
47
|
+
offerings(): Promise<PurchaseOfferings>;
|
|
48
|
+
refreshEntitlement(): Promise<VerifiedEntitlement | null>;
|
|
49
|
+
refreshCreditBalance(): Promise<CreditBalanceSnapshot | null>;
|
|
50
|
+
purchase(item: PurchasePackage): Promise<PurchaseOutcome>;
|
|
51
|
+
restore(): Promise<RestoreOutcome>;
|
|
52
|
+
purchaseCreditPack(item: PurchasePackage): Promise<CreditPackOutcome>;
|
|
53
|
+
dispose(): void;
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=flow.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"flow.d.ts","sourceRoot":"","sources":["../src/flow.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,qBAAqB,EACrB,kB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,CAAC,OAAO,EAAE,kBAAkB,CAAC;IACrC,gFAAgF;IAChF,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAC;IAClC,QAAQ,CAAC,iBAAiB,CAAC,EAAE,MAAM,CAAC;IACpC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC,YAAY,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1D;AAqBD,qBAAa,YAAY;;gBAYX,OAAO,EAAE,mBAAmB;IAWxC,QAAQ,CAAC,QAAQ,QAAO,aAAa,CAAgB;IAErD,QAAQ,CAAC,SAAS,GAAI,UAAU,MAAM,IAAI,KAAG,CAAC,MAAM,IAAI,CAAC,CAKvD;IAEF,0FAA0F;IACpF,QAAQ,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAQ7C,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAK9B,SAAS,IAAI,OAAO,CAAC,iBAAiB,CAAC;IAIjC,kBAAkB,IAAI,OAAO,CAAC,mBAAmB,GAAG,IAAI,CAAC;IAOzD,oBAAoB,IAAI,OAAO,CAAC,qBAAqB,GAAG,IAAI,CAAC;IASnE,QAAQ,CAAC,IAAI,EAAE,eAAe,GAAG,OAAO,CAAC,eAAe,CAAC;IAWzD,OAAO,IAAI,OAAO,CAAC,cAAc,CAAC;IAWlC,kBAAkB,CAAC,IAAI,EAAE,eAAe,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAcrE,OAAO,IAAI,IAAI;CA2FhB"}
|
package/dist/flow.js
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { isPurchaseError, PurchaseError } from './errors.js';
|
|
2
|
+
export const DEFAULT_REFRESH_ATTEMPTS = 5;
|
|
3
|
+
export const DEFAULT_REFRESH_INTERVAL_MS = 2_000;
|
|
4
|
+
const NO_REVISION = -1;
|
|
5
|
+
const SIGNED_OUT = { suiteSubject: null, entitlement: null, creditBalance: null };
|
|
6
|
+
function wait(milliseconds) {
|
|
7
|
+
return new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
8
|
+
}
|
|
9
|
+
function storeError(error) {
|
|
10
|
+
return isPurchaseError(error) ? error : new PurchaseError('store', { cause: error });
|
|
11
|
+
}
|
|
12
|
+
export class PurchaseFlow {
|
|
13
|
+
#port;
|
|
14
|
+
#backend;
|
|
15
|
+
#attempts;
|
|
16
|
+
#interval;
|
|
17
|
+
#sleep;
|
|
18
|
+
#listeners = new Set();
|
|
19
|
+
#stopCustomerUpdates;
|
|
20
|
+
#state = SIGNED_OUT;
|
|
21
|
+
#generation = 0;
|
|
22
|
+
#busy = false;
|
|
23
|
+
constructor(options) {
|
|
24
|
+
this.#port = options.port;
|
|
25
|
+
this.#backend = options.backend;
|
|
26
|
+
this.#attempts = Math.max(1, options.refreshAttempts ?? DEFAULT_REFRESH_ATTEMPTS);
|
|
27
|
+
this.#interval = options.refreshIntervalMs ?? DEFAULT_REFRESH_INTERVAL_MS;
|
|
28
|
+
this.#sleep = options.sleep ?? wait;
|
|
29
|
+
this.#stopCustomerUpdates = this.#port.onCustomerUpdate(() => {
|
|
30
|
+
this.#refreshQuietly();
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
getState = () => this.#state;
|
|
34
|
+
subscribe = (listener) => {
|
|
35
|
+
this.#listeners.add(listener);
|
|
36
|
+
return () => {
|
|
37
|
+
this.#listeners.delete(listener);
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
/** Purchases stay refused until the store has finished identifying this suite subject. */
|
|
41
|
+
async identify(suiteSubject) {
|
|
42
|
+
if (suiteSubject.trim() === '')
|
|
43
|
+
throw new PurchaseError('invalid_subject');
|
|
44
|
+
const generation = this.#restart();
|
|
45
|
+
await this.#port.identify(suiteSubject);
|
|
46
|
+
this.#assertGeneration(generation);
|
|
47
|
+
this.#setState({ ...SIGNED_OUT, suiteSubject });
|
|
48
|
+
}
|
|
49
|
+
async signOut() {
|
|
50
|
+
this.#restart();
|
|
51
|
+
await this.#port.reset();
|
|
52
|
+
}
|
|
53
|
+
offerings() {
|
|
54
|
+
return this.#port.offerings();
|
|
55
|
+
}
|
|
56
|
+
async refreshEntitlement() {
|
|
57
|
+
const identity = this.#identity();
|
|
58
|
+
const entitlement = await this.#backend.refreshEntitlement();
|
|
59
|
+
this.#accept(identity, entitlement, 'entitlement');
|
|
60
|
+
return this.#state.entitlement;
|
|
61
|
+
}
|
|
62
|
+
async refreshCreditBalance() {
|
|
63
|
+
const identity = this.#identity();
|
|
64
|
+
const read = this.#backend.creditBalance?.bind(this.#backend);
|
|
65
|
+
if (read === undefined)
|
|
66
|
+
throw new PurchaseError('unavailable');
|
|
67
|
+
const balance = await read();
|
|
68
|
+
this.#accept(identity, balance, 'creditBalance');
|
|
69
|
+
return this.#state.creditBalance;
|
|
70
|
+
}
|
|
71
|
+
purchase(item) {
|
|
72
|
+
return this.#exclusive(async (identity) => {
|
|
73
|
+
const baseline = await this.#entitlementBaseline();
|
|
74
|
+
await this.#storeCall(identity, () => this.#port.purchase(item));
|
|
75
|
+
const entitlement = await this.#poll(identity, baseline, () => this.refreshEntitlement());
|
|
76
|
+
return entitlement
|
|
77
|
+
? { status: 'granted', entitlement }
|
|
78
|
+
: { status: 'pending', entitlement: this.#state.entitlement };
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
restore() {
|
|
82
|
+
return this.#exclusive(async (identity) => {
|
|
83
|
+
const baseline = await this.#entitlementBaseline();
|
|
84
|
+
await this.#storeCall(identity, () => this.#port.restore());
|
|
85
|
+
const entitlement = await this.#poll(identity, baseline, () => this.refreshEntitlement());
|
|
86
|
+
return entitlement
|
|
87
|
+
? { status: 'granted', entitlement }
|
|
88
|
+
: { status: 'unchanged', entitlement: this.#state.entitlement };
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
purchaseCreditPack(item) {
|
|
92
|
+
if (item.kind !== 'one_time')
|
|
93
|
+
return Promise.reject(new PurchaseError('not_credit_pack'));
|
|
94
|
+
return this.#exclusive(async (identity) => {
|
|
95
|
+
const baseline = (await this.refreshCreditBalance())?.revision;
|
|
96
|
+
await this.#storeCall(identity, () => this.#port.purchase(item));
|
|
97
|
+
const balance = await this.#poll(identity, baseline ?? NO_REVISION, () => this.refreshCreditBalance());
|
|
98
|
+
return balance
|
|
99
|
+
? { status: 'credited', balance }
|
|
100
|
+
: { status: 'pending', balance: this.#state.creditBalance };
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
dispose() {
|
|
104
|
+
this.#stopCustomerUpdates();
|
|
105
|
+
this.#listeners.clear();
|
|
106
|
+
}
|
|
107
|
+
async #entitlementBaseline() {
|
|
108
|
+
const entitlement = await this.refreshEntitlement();
|
|
109
|
+
return entitlement?.revision ?? NO_REVISION;
|
|
110
|
+
}
|
|
111
|
+
async #storeCall(identity, call) {
|
|
112
|
+
try {
|
|
113
|
+
await call();
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
throw storeError(error);
|
|
117
|
+
}
|
|
118
|
+
this.#assertGeneration(identity.generation);
|
|
119
|
+
}
|
|
120
|
+
async #poll(identity, baseline, read) {
|
|
121
|
+
for (let attempt = 0; attempt < this.#attempts; attempt += 1) {
|
|
122
|
+
if (attempt > 0)
|
|
123
|
+
await this.#sleep(this.#interval);
|
|
124
|
+
this.#assertGeneration(identity.generation);
|
|
125
|
+
const value = await this.#readTolerantly(read);
|
|
126
|
+
if (value !== null && value.revision > baseline)
|
|
127
|
+
return value;
|
|
128
|
+
}
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
async #readTolerantly(read) {
|
|
132
|
+
try {
|
|
133
|
+
return await read();
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
if (isPurchaseError(error))
|
|
137
|
+
throw error;
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
async #exclusive(operation) {
|
|
142
|
+
const identity = this.#identity();
|
|
143
|
+
if (this.#busy)
|
|
144
|
+
throw new PurchaseError('busy');
|
|
145
|
+
this.#busy = true;
|
|
146
|
+
try {
|
|
147
|
+
return await operation(identity);
|
|
148
|
+
}
|
|
149
|
+
finally {
|
|
150
|
+
this.#busy = false;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
#accept(identity, value, key) {
|
|
154
|
+
this.#assertGeneration(identity.generation);
|
|
155
|
+
if (value === null)
|
|
156
|
+
return;
|
|
157
|
+
if (value.suiteSubject !== identity.suiteSubject)
|
|
158
|
+
throw new PurchaseError('subject_mismatch');
|
|
159
|
+
const current = this.#state[key];
|
|
160
|
+
if (current !== null && value.revision < current.revision)
|
|
161
|
+
return;
|
|
162
|
+
this.#setState({ ...this.#state, [key]: value });
|
|
163
|
+
}
|
|
164
|
+
#identity() {
|
|
165
|
+
const { suiteSubject } = this.#state;
|
|
166
|
+
if (suiteSubject === null)
|
|
167
|
+
throw new PurchaseError('not_identified');
|
|
168
|
+
return { generation: this.#generation, suiteSubject };
|
|
169
|
+
}
|
|
170
|
+
#restart() {
|
|
171
|
+
this.#generation += 1;
|
|
172
|
+
this.#setState(SIGNED_OUT);
|
|
173
|
+
return this.#generation;
|
|
174
|
+
}
|
|
175
|
+
#assertGeneration(generation) {
|
|
176
|
+
if (generation !== this.#generation)
|
|
177
|
+
throw new PurchaseError('identity_changed');
|
|
178
|
+
}
|
|
179
|
+
#refreshQuietly() {
|
|
180
|
+
if (this.#state.suiteSubject === null)
|
|
181
|
+
return;
|
|
182
|
+
this.refreshEntitlement().catch(() => undefined);
|
|
183
|
+
}
|
|
184
|
+
#setState(state) {
|
|
185
|
+
this.#state = state;
|
|
186
|
+
for (const listener of this.#listeners)
|
|
187
|
+
listener();
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
//# sourceMappingURL=flow.js.map
|
package/dist/flow.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"flow.js","sourceRoot":"","sources":["../src/flow.ts"],"names":[],"mappings":"AAK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AAC;IACnD,CAAC;IAED,SAAS;QACP,MAAM,EAAE,YAAY,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC;QACrC,IAAI,YAAY,KAAK,IAAI;YAAE,MAAM,IAAI,aAAa,CAAC,gBAAgB,CAAC,CAAC;QACrE,OAAO,EAAE,UAAU,EAAE,IAAI,CAAC,WAAW,EAAE,YAAY,EAAE,CAAC;IACxD,CAAC;IAED,QAAQ;QACN,IAAI,CAAC,WAAW,IAAI,CAAC,CAAC;QACtB,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;QAC3B,OAAO,IAAI,CAAC,WAAW,CAAC;IAC1B,CAAC;IAED,iBAAiB,CAAC,UAAkB;QAClC,IAAI,UAAU,KAAK,IAAI,CAAC,WAAW;YAAE,MAAM,IAAI,aAAa,CAAC,kBAAkB,CAAC,CAAC;IACnF,CAAC;IAED,eAAe;QACb,IAAI,IAAI,CAAC,MAAM,CAAC,YAAY,KAAK,IAAI;YAAE,OAAO;QAC9C,IAAI,CAAC,kBAAkB,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;IACnD,CAAC;IAED,SAAS,CAAC,KAAoB;QAC5B,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,KAAK,MAAM,QAAQ,IAAI,IAAI,CAAC,UAAU;YAAE,QAAQ,EAAE,CAAC;IACrD,CAAC;CACF"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC"}
|
package/dist/noop.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"noop.d.ts","sourceRoot":"","sources":["../src/noop.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAqB,aAAa,EAAE,MAAM,WAAW,CAAC;AAIlE,kFAAkF;AAClF,wBAAgB,uBAAuB,IAAI,aAAa,CASvD"}
|
package/dist/noop.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { PurchaseError } from './errors.js';
|
|
2
|
+
const EMPTY_OFFERINGS = { current: null, all: {} };
|
|
3
|
+
/** Port for builds without store keys: identity succeeds, nothing is for sale. */
|
|
4
|
+
export function createNoopPurchasesPort() {
|
|
5
|
+
return {
|
|
6
|
+
identify: () => Promise.resolve(),
|
|
7
|
+
reset: () => Promise.resolve(),
|
|
8
|
+
offerings: () => Promise.resolve(EMPTY_OFFERINGS),
|
|
9
|
+
purchase: () => Promise.reject(new PurchaseError('unavailable')),
|
|
10
|
+
restore: () => Promise.reject(new PurchaseError('unavailable')),
|
|
11
|
+
onCustomerUpdate: () => () => undefined,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=noop.js.map
|
package/dist/noop.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"noop.js","sourceRoot":"","sources":["../src/noop.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAG5C,MAAM,eAAe,GAAsB,EAAE,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,EAAE,EAAE,CAAC;AAEtE,kFAAkF;AAClF,MAAM,UAAU,uBAAuB;IACrC,OAAO;QACL,QAAQ,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE;QACjC,KAAK,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE;QAC9B,SAAS,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,eAAe,CAAC;QACjD,QAAQ,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,aAAa,CAAC,aAAa,CAAC,CAAC;QAChE,OAAO,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,aAAa,CAAC,aAAa,CAAC,CAAC;QAC/D,gBAAgB,EAAE,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,SAAS;KACxC,CAAC;AACJ,CAAC"}
|
package/dist/port.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export type PurchaseProductKind = 'subscription' | 'one_time';
|
|
2
|
+
export interface PurchasePackage {
|
|
3
|
+
readonly id: string;
|
|
4
|
+
readonly offeringId: string;
|
|
5
|
+
readonly productId: string;
|
|
6
|
+
readonly kind: PurchaseProductKind;
|
|
7
|
+
readonly title: string;
|
|
8
|
+
readonly description: string;
|
|
9
|
+
readonly priceLabel: string;
|
|
10
|
+
}
|
|
11
|
+
export interface PurchaseOffering {
|
|
12
|
+
readonly id: string;
|
|
13
|
+
readonly packages: readonly PurchasePackage[];
|
|
14
|
+
}
|
|
15
|
+
export interface PurchaseOfferings {
|
|
16
|
+
readonly current: PurchaseOffering | null;
|
|
17
|
+
readonly all: Readonly<Record<string, PurchaseOffering>>;
|
|
18
|
+
}
|
|
19
|
+
/** Store boundary. Implementations throw `PurchaseError` with a store-neutral code. */
|
|
20
|
+
export interface PurchasesPort {
|
|
21
|
+
identify(suiteSubject: string): Promise<void>;
|
|
22
|
+
reset(): Promise<void>;
|
|
23
|
+
offerings(): Promise<PurchaseOfferings>;
|
|
24
|
+
purchase(item: PurchasePackage): Promise<void>;
|
|
25
|
+
restore(): Promise<void>;
|
|
26
|
+
onCustomerUpdate(listener: () => void): () => void;
|
|
27
|
+
}
|
|
28
|
+
/** Returns the configured offering when it exists, otherwise the store's current offering. */
|
|
29
|
+
export declare function selectOffering(offerings: PurchaseOfferings, configuredId?: string): PurchaseOffering | null;
|
|
30
|
+
//# sourceMappingURL=port.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"port.d.ts","sourceRoot":"","sources":["../src/port.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,mBAAmB,GAAG,cAAc,GAAG,UAAU,CAAC;AAE9D,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,IAAI,EAAE,mBAAmB,CAAC;IACnC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC7B;AAED,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,QAAQ,EAAE,SAAS,eAAe,EAAE,CAAC;CAC/C;AAED,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,OAAO,EAAE,gBAAgB,GAAG,IAAI,CAAC;IAC1C,QAAQ,CAAC,GAAG,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC,CAAC;CAC1D;AAED,uFAAuF;AACvF,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC9C,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACvB,SAAS,IAAI,OAAO,CAAC,iBAAiB,CAAC,CAAC;IACxC,QAAQ,CAAC,IAAI,EAAE,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC/C,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACzB,gBAAgB,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC;CACpD;AAED,8FAA8F;AAC9F,wBAAgB,cAAc,CAC5B,SAAS,EAAE,iBAAiB,EAC5B,YAAY,CAAC,EAAE,MAAM,GACpB,gBAAgB,GAAG,IAAI,CAGzB"}
|
package/dist/port.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Returns the configured offering when it exists, otherwise the store's current offering. */
|
|
2
|
+
export function selectOffering(offerings, configuredId) {
|
|
3
|
+
if (configuredId)
|
|
4
|
+
return offerings.all[configuredId] ?? null;
|
|
5
|
+
return offerings.current;
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=port.js.map
|
package/dist/port.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"port.js","sourceRoot":"","sources":["../src/port.ts"],"names":[],"mappings":"AAgCA,8FAA8F;AAC9F,MAAM,UAAU,cAAc,CAC5B,SAA4B,EAC5B,YAAqB;IAErB,IAAI,YAAY;QAAE,OAAO,SAAS,CAAC,GAAG,CAAC,YAAY,CAAC,IAAI,IAAI,CAAC;IAC7D,OAAO,SAAS,CAAC,OAAO,CAAC;AAC3B,CAAC"}
|
package/dist/react.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import { type VerifiedEntitlement } from './entitlement.js';
|
|
3
|
+
import type { PurchaseFlow, PurchaseState } from './flow.js';
|
|
4
|
+
export interface PurchasesProviderProps {
|
|
5
|
+
readonly flow: PurchaseFlow;
|
|
6
|
+
readonly children?: ReactNode;
|
|
7
|
+
}
|
|
8
|
+
export declare function PurchasesProvider({ flow, children }: PurchasesProviderProps): ReactNode;
|
|
9
|
+
export declare function usePurchaseFlow(): PurchaseFlow;
|
|
10
|
+
export declare function usePurchaseState(): PurchaseState;
|
|
11
|
+
export declare function useEntitlement(): VerifiedEntitlement | null;
|
|
12
|
+
/** True only when the backend-verified entitlement grants the feature key. */
|
|
13
|
+
export declare function useFeature(key: string): boolean;
|
|
14
|
+
//# sourceMappingURL=react.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../src/react.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAEvC,OAAO,EAAc,KAAK,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AACxE,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAI7D,MAAM,WAAW,sBAAsB;IACrC,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAC5B,QAAQ,CAAC,QAAQ,CAAC,EAAE,SAAS,CAAC;CAC/B;AAED,wBAAgB,iBAAiB,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,sBAAsB,GAAG,SAAS,CAEvF;AAED,wBAAgB,eAAe,IAAI,YAAY,CAI9C;AAED,wBAAgB,gBAAgB,IAAI,aAAa,CAGhD;AAED,wBAAgB,cAAc,IAAI,mBAAmB,GAAG,IAAI,CAE3D;AAED,8EAA8E;AAC9E,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAE/C"}
|
package/dist/react.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { createContext, createElement, useContext, useSyncExternalStore } from 'react';
|
|
2
|
+
import { hasFeature } from './entitlement.js';
|
|
3
|
+
const PurchaseFlowContext = createContext(null);
|
|
4
|
+
export function PurchasesProvider({ flow, children }) {
|
|
5
|
+
return createElement(PurchaseFlowContext.Provider, { value: flow }, children);
|
|
6
|
+
}
|
|
7
|
+
export function usePurchaseFlow() {
|
|
8
|
+
const flow = useContext(PurchaseFlowContext);
|
|
9
|
+
if (flow === null)
|
|
10
|
+
throw new Error('usePurchaseFlow requires a PurchasesProvider');
|
|
11
|
+
return flow;
|
|
12
|
+
}
|
|
13
|
+
export function usePurchaseState() {
|
|
14
|
+
const flow = usePurchaseFlow();
|
|
15
|
+
return useSyncExternalStore(flow.subscribe, flow.getState, flow.getState);
|
|
16
|
+
}
|
|
17
|
+
export function useEntitlement() {
|
|
18
|
+
return usePurchaseState().entitlement;
|
|
19
|
+
}
|
|
20
|
+
/** True only when the backend-verified entitlement grants the feature key. */
|
|
21
|
+
export function useFeature(key) {
|
|
22
|
+
return hasFeature(useEntitlement(), key);
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=react.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.js","sourceRoot":"","sources":["../src/react.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,UAAU,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AAGvF,OAAO,EAAE,UAAU,EAA4B,MAAM,kBAAkB,CAAC;AAGxE,MAAM,mBAAmB,GAAG,aAAa,CAAsB,IAAI,CAAC,CAAC;AAOrE,MAAM,UAAU,iBAAiB,CAAC,EAAE,IAAI,EAAE,QAAQ,EAA0B;IAC1E,OAAO,aAAa,CAAC,mBAAmB,CAAC,QAAQ,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE,QAAQ,CAAC,CAAC;AAChF,CAAC;AAED,MAAM,UAAU,eAAe;IAC7B,MAAM,IAAI,GAAG,UAAU,CAAC,mBAAmB,CAAC,CAAC;IAC7C,IAAI,IAAI,KAAK,IAAI;QAAE,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAC;IACnF,OAAO,IAAI,CAAC;AACd,CAAC;AAED,MAAM,UAAU,gBAAgB;IAC9B,MAAM,IAAI,GAAG,eAAe,EAAE,CAAC;IAC/B,OAAO,oBAAoB,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;AAC5E,CAAC;AAED,MAAM,UAAU,cAAc;IAC5B,OAAO,gBAAgB,EAAE,CAAC,WAAW,CAAC;AACxC,CAAC;AAED,8EAA8E;AAC9E,MAAM,UAAU,UAAU,CAAC,GAAW;IACpC,OAAO,UAAU,CAAC,cAAc,EAAE,EAAE,GAAG,CAAC,CAAC;AAC3C,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,66 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@baukit/purchases-core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.12.2",
|
|
4
|
+
"description": "Store-neutral mobile purchases: a purchases port, a no-op client, a purchase flow that waits for suite identity and the refreshed entitlement, and feature-key gating hooks.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Patrick Koss",
|
|
7
|
+
"homepage": "https://github.com/PatrickKoss/baukit#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/PatrickKoss/baukit.git",
|
|
11
|
+
"directory": "typescript/packages/purchases-core"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/PatrickKoss/baukit/issues"
|
|
15
|
+
},
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"type": "module",
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=24.0.0"
|
|
22
|
+
},
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"import": "./dist/index.js",
|
|
28
|
+
"default": "./dist/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./react": {
|
|
31
|
+
"types": "./dist/react.d.ts",
|
|
32
|
+
"import": "./dist/react.js",
|
|
33
|
+
"default": "./dist/react.js"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist",
|
|
38
|
+
"src",
|
|
39
|
+
"README.md",
|
|
40
|
+
"LICENSE"
|
|
41
|
+
],
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsc -p tsconfig.json",
|
|
44
|
+
"prepare": "pnpm run build",
|
|
45
|
+
"test": "tsc -p tsconfig.test.json && vitest run --config ../../vitest.config.ts && node ../../scripts/test-packed-exports.mjs",
|
|
46
|
+
"lint": "eslint src --max-warnings=0",
|
|
47
|
+
"format:check": "prettier --check src package.json tsconfig.json tsconfig.test.json"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@testing-library/react": "16.3.3",
|
|
51
|
+
"@types/react": "19.3.0",
|
|
52
|
+
"jsdom": "30.1.1",
|
|
53
|
+
"react": "19.3.0",
|
|
54
|
+
"react-dom": "19.3.0",
|
|
55
|
+
"typescript": "6.0.3",
|
|
56
|
+
"vitest": "5.0.3"
|
|
57
|
+
},
|
|
58
|
+
"peerDependencies": {
|
|
59
|
+
"react": "^19.2.0"
|
|
60
|
+
},
|
|
61
|
+
"peerDependenciesMeta": {
|
|
62
|
+
"react": {
|
|
63
|
+
"optional": true
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|