@baukit/purchases-core 0.18.2 → 0.19.0
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/access.js +1 -1
- package/dist/access.js.map +1 -1
- package/dist/account-plan.d.ts.map +1 -1
- package/dist/account-plan.js +91 -38
- package/dist/account-plan.js.map +1 -1
- package/dist/paywall.d.ts.map +1 -1
- package/dist/paywall.js +23 -15
- package/dist/paywall.js.map +1 -1
- package/dist/plans-link.js +9 -4
- package/dist/plans-link.js.map +1 -1
- package/dist/port.js +1 -1
- package/dist/port.js.map +1 -1
- package/dist/react.d.ts +5 -1
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +18 -9
- package/dist/react.js.map +1 -1
- package/package.json +6 -3
- package/src/access.ts +1 -1
- package/src/account-plan.ts +104 -39
- package/src/organization.test.ts +20 -1
- package/src/paywall.ts +34 -17
- package/src/plans-link.ts +9 -3
- package/src/port.ts +1 -1
- package/src/react.ts +31 -10
package/dist/access.js
CHANGED
|
@@ -21,7 +21,7 @@ export function organizationFeatureAccess(organization, feature, context) {
|
|
|
21
21
|
}
|
|
22
22
|
export function featureAccess(state, feature, context) {
|
|
23
23
|
const { entitlement, entitlementStatus } = state;
|
|
24
|
-
if (entitlement?.features.includes(feature)) {
|
|
24
|
+
if (entitlement?.features.includes(feature) === true) {
|
|
25
25
|
return { status: 'granted', entitlement };
|
|
26
26
|
}
|
|
27
27
|
if (entitlement !== null) {
|
package/dist/access.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"access.js","sourceRoot":"","sources":["../src/access.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAqC,MAAM,cAAc,CAAC;AAwB7E,yGAAyG;AACzG,MAAM,UAAU,yBAAyB,CACvC,YAAmC,EACnC,OAAe,EACf,OAAwC;IAExC,QAAQ,YAAY,CAAC,MAAM,EAAE,CAAC;QAC5B,KAAK,SAAS,CAAC;QACf,KAAK,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M,EAAE,EAAE,CAAC;IAC9E,CAAC;AACH,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"account-plan.d.ts","sourceRoot":"","sources":["../src/account-plan.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"account-plan.d.ts","sourceRoot":"","sources":["../src/account-plan.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAIV,kBAAkB,EAGnB,MAAM,kBAAkB,CAAC;AAE1B,mDAAmD;AACnD,eAAO,MAAM,wBAAwB,0KAO3B,CAAC;AAEX,yEAAyE;AACzE,eAAO,MAAM,mBAAmB,sBAAsB,CAAC;AACvD,MAAM,MAAM,oBAAoB,GAAG,CAAC,OAAO,wBAAwB,CAAC,CAAC,MAAM,CAAC,CAAC;AAE7E,qBAAa,gBAAiB,SAAQ,KAAK;IAEvC,QAAQ,CAAC,MAAM,EAAE,MAAM;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM;IACrB,QAAQ,CAAC,iBAAiB,EAAE,MAAM,GAAG,IAAI;gBAFhC,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACZ,iBAAiB,GAAE,MAAM,GAAG,IAAW;CAKnD;AAED,MAAM,WAAW,yBAAyB;IACxC,mGAAmG;IACnG,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,CACd,KAAK,EAAE,MAAM,EACb,IAAI,EAAE;QAAE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;KAAE,KAC/C,OAAO,CAAC,QAAQ,CAAC,CAAC;IACvB,+DAA+D;IAC/D,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACnF;AAmID;;;GAGG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,yBAAyB,GAAG,kBAAkB,CAsB/F"}
|
package/dist/account-plan.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isNonNegativeSafeInteger, isRecord, isString, isStringArray, nullable, } from '@baukit/type-guards';
|
|
1
2
|
/** Error codes of Baukit's account plan routes. */
|
|
2
3
|
export const ACCOUNT_PLAN_ERROR_CODES = [
|
|
3
4
|
'suite_identity_missing',
|
|
@@ -21,60 +22,112 @@ export class AccountPlanError extends Error {
|
|
|
21
22
|
this.name = 'AccountPlanError';
|
|
22
23
|
}
|
|
23
24
|
}
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
const billingChannels = [
|
|
26
|
+
'web',
|
|
27
|
+
'app_store',
|
|
28
|
+
'play_store',
|
|
29
|
+
'family',
|
|
30
|
+
'organization',
|
|
31
|
+
'promo',
|
|
32
|
+
];
|
|
33
|
+
const isNullableString = nullable(isString);
|
|
34
|
+
const isNullableCount = nullable(isNonNegativeSafeInteger);
|
|
35
|
+
function isBillingChannel(value) {
|
|
36
|
+
return billingChannels.some((channel) => channel === value);
|
|
37
|
+
}
|
|
38
|
+
function invalidEntitlement() {
|
|
39
|
+
throw new TypeError('The API returned an invalid entitlement.');
|
|
26
40
|
}
|
|
27
|
-
function
|
|
28
|
-
|
|
41
|
+
function invalidBalance() {
|
|
42
|
+
throw new TypeError('The API returned an invalid credit balance.');
|
|
29
43
|
}
|
|
30
|
-
function
|
|
31
|
-
|
|
44
|
+
function parseBilling(value) {
|
|
45
|
+
if (value === null)
|
|
46
|
+
return null;
|
|
47
|
+
if (!isRecord(value) || !isBillingChannel(value['channel']))
|
|
48
|
+
return invalidEntitlement();
|
|
49
|
+
const { soldBy, manageUrl } = value;
|
|
50
|
+
if (![soldBy, manageUrl].every((field) => field === undefined || isString(field)))
|
|
51
|
+
return invalidEntitlement();
|
|
52
|
+
return {
|
|
53
|
+
channel: value['channel'],
|
|
54
|
+
...(isString(soldBy) ? { soldBy } : {}),
|
|
55
|
+
...(isString(manageUrl) ? { manageUrl } : {}),
|
|
56
|
+
};
|
|
32
57
|
}
|
|
33
|
-
function
|
|
34
|
-
|
|
58
|
+
function parseOrganization(value) {
|
|
59
|
+
if (value === null)
|
|
60
|
+
return null;
|
|
61
|
+
if (!isRecord(value) || !isString(value['organizationId']) || !isNullableString(value['name']))
|
|
62
|
+
return invalidEntitlement();
|
|
63
|
+
return { organizationId: value['organizationId'], name: value['name'] };
|
|
35
64
|
}
|
|
36
65
|
function parseEntitlement(value) {
|
|
37
|
-
if (!
|
|
38
|
-
(
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
!
|
|
42
|
-
!
|
|
43
|
-
!
|
|
44
|
-
!
|
|
45
|
-
!
|
|
46
|
-
|
|
47
|
-
throw new TypeError('The API returned an invalid entitlement.');
|
|
66
|
+
if (!isRecord(value))
|
|
67
|
+
return invalidEntitlement();
|
|
68
|
+
const { mode, suiteSubject, tier, features, revision, plansUrl } = value;
|
|
69
|
+
if ((mode !== 'hub' && mode !== 'standalone') ||
|
|
70
|
+
!isString(suiteSubject) ||
|
|
71
|
+
!isString(tier) ||
|
|
72
|
+
!isStringArray(features) ||
|
|
73
|
+
!isNonNegativeSafeInteger(revision) ||
|
|
74
|
+
!isNullableString(plansUrl)) {
|
|
75
|
+
return invalidEntitlement();
|
|
48
76
|
}
|
|
49
|
-
|
|
77
|
+
const entitlement = {
|
|
78
|
+
mode,
|
|
79
|
+
suiteSubject,
|
|
80
|
+
tier,
|
|
81
|
+
features,
|
|
82
|
+
revision,
|
|
83
|
+
plansUrl,
|
|
84
|
+
billing: parseBilling(value['billing']),
|
|
85
|
+
};
|
|
86
|
+
if (value['organization'] === undefined)
|
|
87
|
+
return entitlement;
|
|
88
|
+
return { ...entitlement, organization: parseOrganization(value['organization']) };
|
|
89
|
+
}
|
|
90
|
+
function parsePack(value) {
|
|
91
|
+
if (!isRecord(value) ||
|
|
92
|
+
!isNonNegativeSafeInteger(value['available']) ||
|
|
93
|
+
!isNonNegativeSafeInteger(value['expiresAt']))
|
|
94
|
+
return invalidBalance();
|
|
95
|
+
return { available: value['available'], expiresAt: value['expiresAt'] };
|
|
50
96
|
}
|
|
51
|
-
function
|
|
52
|
-
|
|
97
|
+
function parseGrant(value) {
|
|
98
|
+
if (!isRecord(value) ||
|
|
99
|
+
!isNonNegativeSafeInteger(value['available']) ||
|
|
100
|
+
!isNullableCount(value['expiresAt']))
|
|
101
|
+
return invalidBalance();
|
|
102
|
+
return { available: value['available'], expiresAt: value['expiresAt'] };
|
|
53
103
|
}
|
|
54
104
|
function parseBalance(value) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
!
|
|
60
|
-
!
|
|
61
|
-
!
|
|
62
|
-
!
|
|
63
|
-
|
|
64
|
-
!nullable(grant['expiresAt'], count) ||
|
|
65
|
-
!Array.isArray(packs) ||
|
|
66
|
-
!packs.every((pack) => record(pack) && count(pack['available']) && count(pack['expiresAt']))) {
|
|
67
|
-
throw new TypeError('The API returned an invalid credit balance.');
|
|
105
|
+
if (!isRecord(value))
|
|
106
|
+
return invalidBalance();
|
|
107
|
+
const { suiteSubject, wallet, revision, total, packs } = value;
|
|
108
|
+
if (!isString(suiteSubject) ||
|
|
109
|
+
!isString(wallet) ||
|
|
110
|
+
!isNonNegativeSafeInteger(revision) ||
|
|
111
|
+
!isNonNegativeSafeInteger(total) ||
|
|
112
|
+
!Array.isArray(packs)) {
|
|
113
|
+
return invalidBalance();
|
|
68
114
|
}
|
|
69
|
-
return
|
|
115
|
+
return {
|
|
116
|
+
suiteSubject,
|
|
117
|
+
wallet,
|
|
118
|
+
revision,
|
|
119
|
+
total,
|
|
120
|
+
grant: parseGrant(value['grant']),
|
|
121
|
+
packs: packs.map(parsePack),
|
|
122
|
+
};
|
|
70
123
|
}
|
|
71
124
|
async function failure(response) {
|
|
72
125
|
const retry = Number(response.headers.get('retry-after'));
|
|
73
126
|
let code = 'internal_error';
|
|
74
127
|
try {
|
|
75
128
|
const body = await response.json();
|
|
76
|
-
const error =
|
|
77
|
-
if (
|
|
129
|
+
const error = isRecord(body) ? body['error'] : undefined;
|
|
130
|
+
if (isRecord(error) && isString(error['code']))
|
|
78
131
|
code = error['code'];
|
|
79
132
|
}
|
|
80
133
|
catch {
|
package/dist/account-plan.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"account-plan.js","sourceRoot":"","sources":["../src/account-plan.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"account-plan.js","sourceRoot":"","sources":["../src/account-plan.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,wBAAwB,EACxB,QAAQ,EACR,QAAQ,EACR,aAAa,EACb,QAAQ,GACT,MAAM,qBAAqB,CAAC;AAU7B,mDAAmD;AACnD,MAAM,CAAC,MAAM,wBAAwB,GAAG;IACtC,wBAAwB;IACxB,0BAA0B;IAC1B,qBAAqB;IACrB,sBAAsB;IACtB,6BAA6B;IAC7B,yBAAyB;CACjB,CAAC;AAEX,yEAAyE;AACzE,MAAM,CAAC,MAAM,mBAAmB,GAAG,mBAAmB,CAAC;AAGvD,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IAE9B;IACA;IACA;IAHX,YACW,MAAc,EACd,IAAY,EACZ,oBAAmC,IAAI;QAEhD,KAAK,CAAC,IAAI,CAAC,CAAC;QAJH,WAAM,GAAN,MAAM,CAAQ;QACd,SAAI,GAAJ,IAAI,CAAQ;QACZ,sBAAiB,GAAjB,iBAAiB,CAAsB;QAGhD,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;IACjC,CAAC;CACF;AAaD,MAAM,eAAe,GAA8B;IACjD,KAAK;IACL,WAAW;IACX,YAAY;IACZ,QAAQ;IACR,cAAc;IACd,OAAO;CACR,CAAC;AAEF,MAAM,gBAAgB,GAAG,QAAQ,CAAC,QAAQ,CAAC,CAAC;AAC5C,MAAM,eAAe,GAAG,QAAQ,CAAC,wBAAwB,CAAC,CAAC;AAE3D,SAAS,gB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AC;QACpE,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;YACjB,MAAM,KAAK,GAAG,MAAM,OAAO,CAAC,QAAQ,CAAC,CAAC;YACtC,IAAI,KAAK,CAAC,IAAI,KAAK,wBAAwB;gBAAE,OAAO,IAAI,CAAC;YACzD,MAAM,KAAK,CAAC;QACd,CAAC;QACD,OAAO,KAAK,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;IACtC,CAAC;IACD,OAAO;QACL,kBAAkB,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,iBAAiB,EAAE,gBAAgB,CAAC;QACnE,aAAa,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,aAAa,EAAE,YAAY,CAAC;QACtD,uBAAuB,EAAE,CAAC,cAAc,EAAE,EAAE,CAC1C,IAAI,CAAC,iBAAiB,EAAE,gBAAgB,EAAE,EAAE,CAAC,mBAAmB,CAAC,EAAE,cAAc,EAAE,CAAC;KACvF,CAAC;AACJ,CAAC"}
|
package/dist/paywall.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"paywall.d.ts","sourceRoot":"","sources":["../src/paywall.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"paywall.d.ts","sourceRoot":"","sources":["../src/paywall.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,mBAAmB,EACpB,MAAM,kBAAkB,CAAC;AAG1B,uEAAuE;AACvE,MAAM,MAAM,OAAO,GACf;IAAE,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC;IAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,GAC/D;IACE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IACxB,QAAQ,CAAC,OAAO,EAAE,KAAK,GAAG,WAAW,GAAG,YAAY,CAAC;IACrD,QAAQ,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IAClC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;CAChC,GACD;IAAE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,QAAQ,GAAG,cAAc,CAAA;CAAE,GACzE;IACE,QAAQ,CAAC,IAAI,EAAE,sBAAsB,CAAC;IACtC,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAChC,QAAQ,CAAC,gBAAgB,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1C,GACD;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAE9B,MAAM,WAAW,cAAc;IAC7B,6DAA6D;IAC7D,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,WAAW,EAAE,mBAAmB,EAAE,OAAO,EAAE,cAAc,GAAG,OAAO,CAO7F"}
|
package/dist/paywall.js
CHANGED
|
@@ -10,21 +10,29 @@ export function paywallFor(entitlement, context) {
|
|
|
10
10
|
if (entitlement.mode === 'standalone')
|
|
11
11
|
return { kind: 'none' };
|
|
12
12
|
const organization = entitlement.organization;
|
|
13
|
-
if (organization)
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
:
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
13
|
+
if (organization !== undefined && organization !== null)
|
|
14
|
+
return organizationPaywall(entitlement, organization, context);
|
|
15
|
+
const link = paywallLink(entitlement, context);
|
|
16
|
+
return channelPaywall(entitlement, link);
|
|
17
|
+
}
|
|
18
|
+
function organizationPaywall(entitlement, organization, context) {
|
|
19
|
+
if (context.feature === undefined || entitlement.features.includes(context.feature))
|
|
20
|
+
return { kind: 'covered', channel: 'organization' };
|
|
21
|
+
return {
|
|
22
|
+
kind: 'organization_managed',
|
|
23
|
+
organizationId: organization.organizationId,
|
|
24
|
+
organizationName: organization.name,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function paywallLink(entitlement, context) {
|
|
28
|
+
if (entitlement.plansUrl === null)
|
|
29
|
+
return null;
|
|
30
|
+
return plansLink(entitlement.plansUrl, {
|
|
31
|
+
from: context.productId,
|
|
32
|
+
...(context.feature === undefined ? {} : { feature: context.feature }),
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
function channelPaywall(entitlement, link) {
|
|
28
36
|
const channel = entitlement.billing?.channel;
|
|
29
37
|
switch (channel) {
|
|
30
38
|
case 'web':
|
package/dist/paywall.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"paywall.js","sourceRoot":"","sources":["../src/paywall.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"paywall.js","sourceRoot":"","sources":["../src/paywall.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAyB5C;;;;;;GAMG;AACH,MAAM,UAAU,UAAU,CAAC,WAAgC,EAAE,OAAuB;IAClF,IAAI,WAAW,CAAC,IAAI,KAAK,YAAY;QAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;IAC/D,MAAM,YAAY,GAAG,WAAW,CAAC,YAAY,CAAC;IAC9C,IAAI,YAAY,KAAK,SAAS,IAAI,YAAY,KAAK,IAAI;QACrD,OAAO,mBAAmB,CAAC,WAAW,EAAE,YAAY,EAAE,OAAO,CAAC,CAAC;IACjE,MAAM,IAAI,GAAG,WAAW,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;IAC/C,OAAO,cAAc,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC;AAC3C,CAAC;AAED,SAAS,mBAAmB,CAC1B,WAAgC,EAChC,YAAqC,EACrC,OAAuB;I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B,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC;QACtC,KAAK,OAAO,CAAC;QACb,KAAK,SAAS;YACZ,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;IAChD,CAAC;AACH,CAAC"}
|
package/dist/plans-link.js
CHANGED
|
@@ -5,10 +5,15 @@ export function plansLink(plansUrl, context = {}) {
|
|
|
5
5
|
if (!httpUrl.test(plansUrl))
|
|
6
6
|
throw new TypeError('The plans URL must be an HTTP(S) URL.');
|
|
7
7
|
const parameters = [];
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
const { from, feature } = context;
|
|
9
|
+
if (from === undefined || from === '')
|
|
10
|
+
return plansUrlWith(plansUrl, parameters);
|
|
11
|
+
parameters.push(`from=${encodeURIComponent(from)}`);
|
|
12
|
+
if (feature !== undefined && feature !== '')
|
|
13
|
+
parameters.push(`feature=${encodeURIComponent(feature)}`);
|
|
14
|
+
return plansUrlWith(plansUrl, parameters);
|
|
15
|
+
}
|
|
16
|
+
function plansUrlWith(plansUrl, parameters) {
|
|
12
17
|
const query = parameters.join('&');
|
|
13
18
|
if (query === '')
|
|
14
19
|
return plansUrl;
|
package/dist/plans-link.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plans-link.js","sourceRoot":"","sources":["../src/plans-link.ts"],"names":[],"mappings":"AAOA,wFAAwF;AACxF,MAAM,OAAO,GAAG,sCAAsC,CAAC;AAEvD,qKAAqK;AACrK,MAAM,UAAU,SAAS,CAAC,QAAgB,EAAE,UAA4B,EAAE;IACxE,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC;QAAE,MAAM,IAAI,SAAS,CAAC,uCAAuC,CAAC,CAAC;IAC1F,MAAM,UAAU,GAAa,EAAE,CAAC;IAChC,IAAI,OAAO,CAAC,IAAI;QAAE,UAAU,CAAC,IAAI,CAAC,QAAQ,kBAAkB,CAAC,
|
|
1
|
+
{"version":3,"file":"plans-link.js","sourceRoot":"","sources":["../src/plans-link.ts"],"names":[],"mappings":"AAOA,wFAAwF;AACxF,MAAM,OAAO,GAAG,sCAAsC,CAAC;AAEvD,qKAAqK;AACrK,MAAM,UAAU,SAAS,CAAC,QAAgB,EAAE,UAA4B,EAAE;IACxE,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC;QAAE,MAAM,IAAI,SAAS,CAAC,uCAAuC,CAAC,CAAC;IAC1F,MAAM,UAAU,GAAa,EAAE,CAAC;IAChC,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;IAClC,IAAI,IAAI,KAAK,SAAS,IAAI,IAAI,KAAK,EAAE;QAAE,OAAO,YAAY,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC;IACjF,UAAU,CAAC,IAAI,CAAC,QAAQ,kBAAkB,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACpD,IAAI,OAAO,KAAK,SAAS,IAAI,OAAO,KAAK,EAAE;QACzC,UAAU,CAAC,IAAI,CAAC,WAAW,kBAAkB,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;IAC5D,OAAO,YAAY,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC;AAC5C,CAAC;AAED,SAAS,YAAY,CAAC,QAAgB,EAAE,UAA6B;IACnE,MAAM,KAAK,GAAG,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACnC,IAAI,KAAK,KAAK,EAAE;QAAE,OAAO,QAAQ,CAAC;IAClC,OAAO,GAAG,QAAQ,GAAG,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,KAAK,EAAE,CAAC;AACpE,CAAC"}
|
package/dist/port.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/** Returns the configured offering when it exists, otherwise the store's current offering. */
|
|
2
2
|
export function selectOffering(offerings, configuredId) {
|
|
3
|
-
if (configuredId)
|
|
3
|
+
if (configuredId !== undefined && configuredId !== '')
|
|
4
4
|
return offerings.all[configuredId] ?? null;
|
|
5
5
|
return offerings.current;
|
|
6
6
|
}
|
package/dist/port.js.map
CHANGED
|
@@ -1 +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;
|
|
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,KAAK,SAAS,IAAI,YAAY,KAAK,EAAE;QAAE,OAAO,SAAS,CAAC,GAAG,CAAC,YAAY,CAAC,IAAI,IAAI,CAAC;IAClG,OAAO,SAAS,CAAC,OAAO,CAAC;AAC3B,CAAC"}
|
package/dist/react.d.ts
CHANGED
|
@@ -19,8 +19,12 @@ export interface FeatureOptions {
|
|
|
19
19
|
}
|
|
20
20
|
/** Access to a feature key from the backend-verified entitlement. See `featureAccess`. */
|
|
21
21
|
export declare function useFeature(key: string, options?: FeatureOptions): FeatureAccess;
|
|
22
|
+
/** An organization's plan state with `retry`, which reads the plan again. */
|
|
23
|
+
export type OrganizationPlan = OrganizationPlanState & {
|
|
24
|
+
readonly retry: () => void;
|
|
25
|
+
};
|
|
22
26
|
/** Loads the plan of the organization the account acts for. */
|
|
23
|
-
export declare function useOrganizationPlan(backend: Pick<EntitlementBackend, 'organizationEntitlement'>, organizationId: string):
|
|
27
|
+
export declare function useOrganizationPlan(backend: Pick<EntitlementBackend, 'organizationEntitlement'>, organizationId: string): OrganizationPlan;
|
|
24
28
|
export interface LockedFeatureProps {
|
|
25
29
|
readonly feature: string;
|
|
26
30
|
readonly children?: ReactNode;
|
package/dist/react.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../src/react.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../src/react.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAEvC,OAAO,EAGL,KAAK,aAAa,EAClB,KAAK,qBAAqB,EAC3B,MAAM,aAAa,CAAC;AAErB,OAAO,KAAK,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAChF,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAC7D,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAS5C,MAAM,WAAW,sBAAsB;IACrC,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAC5B,0DAA0D;IAC1D,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,QAAQ,CAAC,EAAE,SAAS,CAAC;CAC/B;AAED,wBAAgB,iBAAiB,CAAC,EAChC,IAAI,EACJ,SAAS,EACT,QAAQ,GACT,EAAE,sBAAsB,GAAG,SAAS,CAEpC;AAQD,wBAAgB,eAAe,IAAI,YAAY,CAE9C;AAED,wBAAgB,gBAAgB,IAAI,aAAa,CAGhD;AAED,wBAAgB,cAAc,IAAI,mBAAmB,GAAG,IAAI,CAE3D;AAED,MAAM,WAAW,cAAc;IAC7B,yFAAyF;IACzF,QAAQ,CAAC,YAAY,CAAC,EAAE,qBAAqB,CAAC;CAC/C;AAED,0FAA0F;AAC1F,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,GAAE,cAAmB,GAAG,aAAa,CAMnF;AAeD,6EAA6E;AAC7E,MAAM,MAAM,gBAAgB,GAAG,qBAAqB,GAAG;IAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,IAAI,CAAA;CAAE,CAAC;AAEtF,+DAA+D;AAC/D,wBAAgB,mBAAmB,CACjC,OAAO,EAAE,IAAI,CAAC,kBAAkB,EAAE,yBAAyB,CAAC,EAC5D,cAAc,EAAE,MAAM,GACrB,gBAAgB,CAgClB;AAED,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,QAAQ,CAAC,EAAE,SAAS,CAAC;IAC9B,+EAA+E;IAC/E,QAAQ,CAAC,MAAM,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,SAAS,CAAC;IACjD,QAAQ,CAAC,OAAO,CAAC,EAAE,SAAS,CAAC;IAC7B,8EAA8E;IAC9E,QAAQ,CAAC,WAAW,CAAC,EAAE,SAAS,CAAC;IACjC,QAAQ,CAAC,YAAY,CAAC,EAAE,qBAAqB,CAAC;CAC/C;AAED,+EAA+E;AAC/E,wBAAgB,aAAa,CAAC,KAAK,EAAE,kBAAkB,GAAG,SAAS,CAelE"}
|
package/dist/react.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createContext, createElement, Fragment, useContext, useEffect, useState, useSyncExternalStore, } from 'react';
|
|
1
|
+
import { createContext, createElement, Fragment, useCallback, useContext, useEffect, useState, useSyncExternalStore, } from 'react';
|
|
2
2
|
import { featureAccess, organizationFeatureAccess, } from './access.js';
|
|
3
3
|
import { AccountPlanError } from './account-plan.js';
|
|
4
4
|
const PurchaseFlowContext = createContext(null);
|
|
@@ -36,23 +36,32 @@ function organizationFailure(error) {
|
|
|
36
36
|
}
|
|
37
37
|
/** Loads the plan of the organization the account acts for. */
|
|
38
38
|
export function useOrganizationPlan(backend, organizationId) {
|
|
39
|
-
const [
|
|
39
|
+
const [attempt, setAttempt] = useState(0);
|
|
40
|
+
const [settled, setSettled] = useState(null);
|
|
41
|
+
const retry = useCallback(() => {
|
|
42
|
+
setAttempt((previous) => previous + 1);
|
|
43
|
+
}, []);
|
|
40
44
|
useEffect(() => {
|
|
41
45
|
let active = true;
|
|
42
|
-
|
|
46
|
+
const settle = (state) => {
|
|
47
|
+
if (active)
|
|
48
|
+
setSettled({ backend, organizationId, attempt, state });
|
|
49
|
+
};
|
|
43
50
|
const load = backend.organizationEntitlement?.(organizationId) ?? Promise.resolve(null);
|
|
44
51
|
load.then((entitlement) => {
|
|
45
|
-
|
|
46
|
-
setState(entitlement === null ? { status: 'not_accessible' } : { status: 'ready', entitlement });
|
|
52
|
+
settle(entitlement === null ? { status: 'not_accessible' } : { status: 'ready', entitlement });
|
|
47
53
|
}, (error) => {
|
|
48
|
-
|
|
49
|
-
setState(organizationFailure(error));
|
|
54
|
+
settle(organizationFailure(error));
|
|
50
55
|
});
|
|
51
56
|
return () => {
|
|
52
57
|
active = false;
|
|
53
58
|
};
|
|
54
|
-
}, [backend, organizationId]);
|
|
55
|
-
|
|
59
|
+
}, [backend, organizationId, attempt]);
|
|
60
|
+
const current = settled?.backend === backend &&
|
|
61
|
+
settled.organizationId === organizationId &&
|
|
62
|
+
settled.attempt === attempt;
|
|
63
|
+
const state = current ? settled.state : { status: 'loading' };
|
|
64
|
+
return { ...state, retry };
|
|
56
65
|
}
|
|
57
66
|
/** Renders `children` only while the verified entitlement grants `feature`. */
|
|
58
67
|
export function LockedFeature(props) {
|
package/dist/react.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react.js","sourceRoot":"","sources":["../src/react.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,aAAa,EACb,QAAQ,EACR,UAAU,EACV,SAAS,EACT,QAAQ,EACR,oBAAoB,GACrB,MAAM,OAAO,CAAC;AAGf,OAAO,EACL,aAAa,EACb,yBAAyB,GAG1B,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,gBAAgB,EAAE,MAAM,m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,YAAY,EAAE,KAAK,CAAC,YAAY,EAAE,CAC7E,CAAC;IACF,QAAQ,MAAM,CAAC,MAAM,EAAE,CAAC;QACtB,KAAK,SAAS;YACZ,OAAO,aAAa,CAAC,QAAQ,EAAE,IAAI,EAAE,KAAK,CAAC,QAAQ,CAAC,CAAC;QACvD,KAAK,QAAQ;YACX,OAAO,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QACtC,KAAK,SAAS;YACZ,OAAO,KAAK,CAAC,OAAO,IAAI,IAAI,CAAC;QAC/B,KAAK,aAAa;YAChB,OAAO,KAAK,CAAC,WAAW,IAAI,IAAI,CAAC;IACrC,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@baukit/purchases-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.19.0",
|
|
4
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
5
|
"license": "MIT",
|
|
6
6
|
"author": "Patrick Koss",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"build": "tsc -p tsconfig.json",
|
|
44
44
|
"prepare": "pnpm run build",
|
|
45
45
|
"test": "tsc -p tsconfig.test.json && vitest run --config ../../vitest.config.ts && node ../../scripts/test-packed-exports.mjs",
|
|
46
|
-
"lint": "eslint
|
|
46
|
+
"lint": "eslint . --max-warnings=0",
|
|
47
47
|
"format:check": "prettier --check src package.json tsconfig.json tsconfig.test.json"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"react": "19.3.0",
|
|
54
54
|
"react-dom": "19.3.0",
|
|
55
55
|
"typescript": "6.0.3",
|
|
56
|
-
"vitest": "5.0.
|
|
56
|
+
"vitest": "5.0.5"
|
|
57
57
|
},
|
|
58
58
|
"peerDependencies": {
|
|
59
59
|
"react": "^19.2.0"
|
|
@@ -62,5 +62,8 @@
|
|
|
62
62
|
"react": {
|
|
63
63
|
"optional": true
|
|
64
64
|
}
|
|
65
|
+
},
|
|
66
|
+
"dependencies": {
|
|
67
|
+
"@baukit/type-guards": "^0.19.0"
|
|
65
68
|
}
|
|
66
69
|
}
|
package/src/access.ts
CHANGED
|
@@ -54,7 +54,7 @@ export function featureAccess(
|
|
|
54
54
|
context: Omit<PaywallContext, 'feature'>,
|
|
55
55
|
): FeatureAccess {
|
|
56
56
|
const { entitlement, entitlementStatus } = state;
|
|
57
|
-
if (entitlement?.features.includes(feature)) {
|
|
57
|
+
if (entitlement?.features.includes(feature) === true) {
|
|
58
58
|
return { status: 'granted', entitlement };
|
|
59
59
|
}
|
|
60
60
|
if (entitlement !== null) {
|
package/src/account-plan.ts
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isNonNegativeSafeInteger,
|
|
3
|
+
isRecord,
|
|
4
|
+
isString,
|
|
5
|
+
isStringArray,
|
|
6
|
+
nullable,
|
|
7
|
+
} from '@baukit/type-guards';
|
|
1
8
|
import type {
|
|
9
|
+
BillingChannel,
|
|
10
|
+
BillingClaim,
|
|
2
11
|
CreditBalanceSnapshot,
|
|
3
12
|
EntitlementBackend,
|
|
13
|
+
EntitlementOrganization,
|
|
4
14
|
VerifiedEntitlement,
|
|
5
15
|
} from './entitlement.js';
|
|
6
16
|
|
|
@@ -40,61 +50,116 @@ export interface AccountPlanBackendOptions {
|
|
|
40
50
|
readonly headers?: () => Promise<Record<string, string>> | Record<string, string>;
|
|
41
51
|
}
|
|
42
52
|
|
|
43
|
-
|
|
53
|
+
const billingChannels: readonly BillingChannel[] = [
|
|
54
|
+
'web',
|
|
55
|
+
'app_store',
|
|
56
|
+
'play_store',
|
|
57
|
+
'family',
|
|
58
|
+
'organization',
|
|
59
|
+
'promo',
|
|
60
|
+
];
|
|
44
61
|
|
|
45
|
-
|
|
46
|
-
|
|
62
|
+
const isNullableString = nullable(isString);
|
|
63
|
+
const isNullableCount = nullable(isNonNegativeSafeInteger);
|
|
64
|
+
|
|
65
|
+
function isBillingChannel(value: unknown): value is BillingChannel {
|
|
66
|
+
return billingChannels.some((channel) => channel === value);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function invalidEntitlement(): never {
|
|
70
|
+
throw new TypeError('The API returned an invalid entitlement.');
|
|
47
71
|
}
|
|
48
|
-
|
|
49
|
-
|
|
72
|
+
|
|
73
|
+
function invalidBalance(): never {
|
|
74
|
+
throw new TypeError('The API returned an invalid credit balance.');
|
|
50
75
|
}
|
|
51
|
-
|
|
52
|
-
|
|
76
|
+
|
|
77
|
+
function parseBilling(value: unknown): BillingClaim | null {
|
|
78
|
+
if (value === null) return null;
|
|
79
|
+
if (!isRecord(value) || !isBillingChannel(value['channel'])) return invalidEntitlement();
|
|
80
|
+
const { soldBy, manageUrl } = value;
|
|
81
|
+
if (![soldBy, manageUrl].every((field) => field === undefined || isString(field)))
|
|
82
|
+
return invalidEntitlement();
|
|
83
|
+
return {
|
|
84
|
+
channel: value['channel'],
|
|
85
|
+
...(isString(soldBy) ? { soldBy } : {}),
|
|
86
|
+
...(isString(manageUrl) ? { manageUrl } : {}),
|
|
87
|
+
};
|
|
53
88
|
}
|
|
54
|
-
|
|
55
|
-
|
|
89
|
+
|
|
90
|
+
function parseOrganization(value: unknown): EntitlementOrganization | null {
|
|
91
|
+
if (value === null) return null;
|
|
92
|
+
if (!isRecord(value) || !isString(value['organizationId']) || !isNullableString(value['name']))
|
|
93
|
+
return invalidEntitlement();
|
|
94
|
+
return { organizationId: value['organizationId'], name: value['name'] };
|
|
56
95
|
}
|
|
57
96
|
|
|
58
97
|
function parseEntitlement(value: unknown): VerifiedEntitlement {
|
|
98
|
+
if (!isRecord(value)) return invalidEntitlement();
|
|
99
|
+
const { mode, suiteSubject, tier, features, revision, plansUrl } = value;
|
|
59
100
|
if (
|
|
60
|
-
|
|
61
|
-
(
|
|
62
|
-
!
|
|
63
|
-
!
|
|
64
|
-
!
|
|
65
|
-
!
|
|
66
|
-
!count(value['revision']) ||
|
|
67
|
-
!nullable(value['plansUrl'], text) ||
|
|
68
|
-
!nullable(value['billing'], record) ||
|
|
69
|
-
!(value['organization'] === undefined || nullable(value['organization'], organization))
|
|
101
|
+
(mode !== 'hub' && mode !== 'standalone') ||
|
|
102
|
+
!isString(suiteSubject) ||
|
|
103
|
+
!isString(tier) ||
|
|
104
|
+
!isStringArray(features) ||
|
|
105
|
+
!isNonNegativeSafeInteger(revision) ||
|
|
106
|
+
!isNullableString(plansUrl)
|
|
70
107
|
) {
|
|
71
|
-
|
|
108
|
+
return invalidEntitlement();
|
|
72
109
|
}
|
|
73
|
-
|
|
110
|
+
const entitlement: VerifiedEntitlement = {
|
|
111
|
+
mode,
|
|
112
|
+
suiteSubject,
|
|
113
|
+
tier,
|
|
114
|
+
features,
|
|
115
|
+
revision,
|
|
116
|
+
plansUrl,
|
|
117
|
+
billing: parseBilling(value['billing']),
|
|
118
|
+
};
|
|
119
|
+
if (value['organization'] === undefined) return entitlement;
|
|
120
|
+
return { ...entitlement, organization: parseOrganization(value['organization']) };
|
|
74
121
|
}
|
|
75
122
|
|
|
76
|
-
function
|
|
77
|
-
|
|
123
|
+
function parsePack(value: unknown): CreditBalanceSnapshot['packs'][number] {
|
|
124
|
+
if (
|
|
125
|
+
!isRecord(value) ||
|
|
126
|
+
!isNonNegativeSafeInteger(value['available']) ||
|
|
127
|
+
!isNonNegativeSafeInteger(value['expiresAt'])
|
|
128
|
+
)
|
|
129
|
+
return invalidBalance();
|
|
130
|
+
return { available: value['available'], expiresAt: value['expiresAt'] };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function parseGrant(value: unknown): CreditBalanceSnapshot['grant'] {
|
|
134
|
+
if (
|
|
135
|
+
!isRecord(value) ||
|
|
136
|
+
!isNonNegativeSafeInteger(value['available']) ||
|
|
137
|
+
!isNullableCount(value['expiresAt'])
|
|
138
|
+
)
|
|
139
|
+
return invalidBalance();
|
|
140
|
+
return { available: value['available'], expiresAt: value['expiresAt'] };
|
|
78
141
|
}
|
|
79
142
|
|
|
80
143
|
function parseBalance(value: unknown): CreditBalanceSnapshot {
|
|
81
|
-
|
|
82
|
-
const
|
|
144
|
+
if (!isRecord(value)) return invalidBalance();
|
|
145
|
+
const { suiteSubject, wallet, revision, total, packs } = value;
|
|
83
146
|
if (
|
|
84
|
-
!
|
|
85
|
-
!
|
|
86
|
-
!
|
|
87
|
-
!
|
|
88
|
-
!
|
|
89
|
-
!record(grant) ||
|
|
90
|
-
!count(grant['available']) ||
|
|
91
|
-
!nullable(grant['expiresAt'], count) ||
|
|
92
|
-
!Array.isArray(packs) ||
|
|
93
|
-
!packs.every((pack) => record(pack) && count(pack['available']) && count(pack['expiresAt']))
|
|
147
|
+
!isString(suiteSubject) ||
|
|
148
|
+
!isString(wallet) ||
|
|
149
|
+
!isNonNegativeSafeInteger(revision) ||
|
|
150
|
+
!isNonNegativeSafeInteger(total) ||
|
|
151
|
+
!Array.isArray(packs)
|
|
94
152
|
) {
|
|
95
|
-
|
|
153
|
+
return invalidBalance();
|
|
96
154
|
}
|
|
97
|
-
return
|
|
155
|
+
return {
|
|
156
|
+
suiteSubject,
|
|
157
|
+
wallet,
|
|
158
|
+
revision,
|
|
159
|
+
total,
|
|
160
|
+
grant: parseGrant(value['grant']),
|
|
161
|
+
packs: packs.map(parsePack),
|
|
162
|
+
};
|
|
98
163
|
}
|
|
99
164
|
|
|
100
165
|
async function failure(response: Response): Promise<AccountPlanError> {
|
|
@@ -102,8 +167,8 @@ async function failure(response: Response): Promise<AccountPlanError> {
|
|
|
102
167
|
let code = 'internal_error';
|
|
103
168
|
try {
|
|
104
169
|
const body: unknown = await response.json();
|
|
105
|
-
const error =
|
|
106
|
-
if (
|
|
170
|
+
const error = isRecord(body) ? body['error'] : undefined;
|
|
171
|
+
if (isRecord(error) && isString(error['code'])) code = error['code'];
|
|
107
172
|
} catch {
|
|
108
173
|
code = 'internal_error';
|
|
109
174
|
}
|
package/src/organization.test.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// @vitest-environment jsdom
|
|
2
|
-
import { cleanup, renderHook, waitFor } from '@testing-library/react';
|
|
2
|
+
import { act, cleanup, renderHook, waitFor } from '@testing-library/react';
|
|
3
3
|
import { createElement, type ReactNode } from 'react';
|
|
4
4
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
5
5
|
|
|
@@ -211,4 +211,23 @@ describe('useFeature with an organization plan', () => {
|
|
|
211
211
|
expect(view.result.current).toEqual({ status: 'unavailable' });
|
|
212
212
|
});
|
|
213
213
|
});
|
|
214
|
+
|
|
215
|
+
it('reads the plan again on retry after the hub was unavailable', async () => {
|
|
216
|
+
const organizationEntitlement = vi
|
|
217
|
+
.fn<(id: string) => Promise<VerifiedEntitlement | null>>()
|
|
218
|
+
.mockRejectedValueOnce(new AccountPlanError(503, 'entitlements_unavailable', 5))
|
|
219
|
+
.mockResolvedValueOnce(studio);
|
|
220
|
+
const backend = { organizationEntitlement };
|
|
221
|
+
const view = renderHook(() => useOrganizationPlan(backend, STUDIO));
|
|
222
|
+
await waitFor(() => {
|
|
223
|
+
expect(view.result.current.status).toBe('unavailable');
|
|
224
|
+
});
|
|
225
|
+
act(() => {
|
|
226
|
+
view.result.current.retry();
|
|
227
|
+
});
|
|
228
|
+
await waitFor(() => {
|
|
229
|
+
expect(view.result.current).toMatchObject({ status: 'ready', entitlement: studio });
|
|
230
|
+
});
|
|
231
|
+
expect(organizationEntitlement).toHaveBeenCalledTimes(2);
|
|
232
|
+
});
|
|
214
233
|
});
|
package/src/paywall.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
BillingChannel,
|
|
3
|
+
EntitlementOrganization,
|
|
4
|
+
VerifiedEntitlement,
|
|
5
|
+
} from './entitlement.js';
|
|
2
6
|
import { plansLink } from './plans-link.js';
|
|
3
7
|
|
|
4
8
|
/** What a locked feature may offer. Only `purchase` sells anything. */
|
|
@@ -34,22 +38,35 @@ export interface PaywallContext {
|
|
|
34
38
|
export function paywallFor(entitlement: VerifiedEntitlement, context: PaywallContext): Paywall {
|
|
35
39
|
if (entitlement.mode === 'standalone') return { kind: 'none' };
|
|
36
40
|
const organization = entitlement.organization;
|
|
37
|
-
if (organization)
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
41
|
+
if (organization !== undefined && organization !== null)
|
|
42
|
+
return organizationPaywall(entitlement, organization, context);
|
|
43
|
+
const link = paywallLink(entitlement, context);
|
|
44
|
+
return channelPaywall(entitlement, link);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function organizationPaywall(
|
|
48
|
+
entitlement: VerifiedEntitlement,
|
|
49
|
+
organization: EntitlementOrganization,
|
|
50
|
+
context: PaywallContext,
|
|
51
|
+
): Paywall {
|
|
52
|
+
if (context.feature === undefined || entitlement.features.includes(context.feature))
|
|
53
|
+
return { kind: 'covered', channel: 'organization' };
|
|
54
|
+
return {
|
|
55
|
+
kind: 'organization_managed',
|
|
56
|
+
organizationId: organization.organizationId,
|
|
57
|
+
organizationName: organization.name,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function paywallLink(entitlement: VerifiedEntitlement, context: PaywallContext): string | null {
|
|
62
|
+
if (entitlement.plansUrl === null) return null;
|
|
63
|
+
return plansLink(entitlement.plansUrl, {
|
|
64
|
+
from: context.productId,
|
|
65
|
+
...(context.feature === undefined ? {} : { feature: context.feature }),
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function channelPaywall(entitlement: VerifiedEntitlement, link: string | null): Paywall {
|
|
53
70
|
const channel: BillingChannel | undefined = entitlement.billing?.channel;
|
|
54
71
|
switch (channel) {
|
|
55
72
|
case 'web':
|
package/src/plans-link.ts
CHANGED
|
@@ -12,9 +12,15 @@ const httpUrl = /^https?:\/\/[^\s/?#@]+(?:[/?]\S*)?$/u;
|
|
|
12
12
|
export function plansLink(plansUrl: string, context: PlansLinkContext = {}): string {
|
|
13
13
|
if (!httpUrl.test(plansUrl)) throw new TypeError('The plans URL must be an HTTP(S) URL.');
|
|
14
14
|
const parameters: string[] = [];
|
|
15
|
-
|
|
16
|
-
if (
|
|
17
|
-
|
|
15
|
+
const { from, feature } = context;
|
|
16
|
+
if (from === undefined || from === '') return plansUrlWith(plansUrl, parameters);
|
|
17
|
+
parameters.push(`from=${encodeURIComponent(from)}`);
|
|
18
|
+
if (feature !== undefined && feature !== '')
|
|
19
|
+
parameters.push(`feature=${encodeURIComponent(feature)}`);
|
|
20
|
+
return plansUrlWith(plansUrl, parameters);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function plansUrlWith(plansUrl: string, parameters: readonly string[]): string {
|
|
18
24
|
const query = parameters.join('&');
|
|
19
25
|
if (query === '') return plansUrl;
|
|
20
26
|
return `${plansUrl}${plansUrl.includes('?') ? '&' : '?'}${query}`;
|
package/src/port.ts
CHANGED
|
@@ -35,6 +35,6 @@ export function selectOffering(
|
|
|
35
35
|
offerings: PurchaseOfferings,
|
|
36
36
|
configuredId?: string,
|
|
37
37
|
): PurchaseOffering | null {
|
|
38
|
-
if (configuredId) return offerings.all[configuredId] ?? null;
|
|
38
|
+
if (configuredId !== undefined && configuredId !== '') return offerings.all[configuredId] ?? null;
|
|
39
39
|
return offerings.current;
|
|
40
40
|
}
|
package/src/react.ts
CHANGED
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
createContext,
|
|
3
3
|
createElement,
|
|
4
4
|
Fragment,
|
|
5
|
+
useCallback,
|
|
5
6
|
useContext,
|
|
6
7
|
useEffect,
|
|
7
8
|
useState,
|
|
@@ -81,32 +82,52 @@ function organizationFailure(error: unknown): OrganizationPlanState {
|
|
|
81
82
|
return { status: 'unavailable' };
|
|
82
83
|
}
|
|
83
84
|
|
|
85
|
+
interface SettledOrganizationPlan {
|
|
86
|
+
readonly backend: Pick<EntitlementBackend, 'organizationEntitlement'>;
|
|
87
|
+
readonly organizationId: string;
|
|
88
|
+
readonly attempt: number;
|
|
89
|
+
readonly state: OrganizationPlanState;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** An organization's plan state with `retry`, which reads the plan again. */
|
|
93
|
+
export type OrganizationPlan = OrganizationPlanState & { readonly retry: () => void };
|
|
94
|
+
|
|
84
95
|
/** Loads the plan of the organization the account acts for. */
|
|
85
96
|
export function useOrganizationPlan(
|
|
86
97
|
backend: Pick<EntitlementBackend, 'organizationEntitlement'>,
|
|
87
98
|
organizationId: string,
|
|
88
|
-
):
|
|
89
|
-
const [
|
|
99
|
+
): OrganizationPlan {
|
|
100
|
+
const [attempt, setAttempt] = useState(0);
|
|
101
|
+
const [settled, setSettled] = useState<SettledOrganizationPlan | null>(null);
|
|
102
|
+
const retry = useCallback(() => {
|
|
103
|
+
setAttempt((previous) => previous + 1);
|
|
104
|
+
}, []);
|
|
90
105
|
useEffect(() => {
|
|
91
106
|
let active = true;
|
|
92
|
-
|
|
107
|
+
const settle = (state: OrganizationPlanState): void => {
|
|
108
|
+
if (active) setSettled({ backend, organizationId, attempt, state });
|
|
109
|
+
};
|
|
93
110
|
const load = backend.organizationEntitlement?.(organizationId) ?? Promise.resolve(null);
|
|
94
111
|
load.then(
|
|
95
112
|
(entitlement) => {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
);
|
|
113
|
+
settle(
|
|
114
|
+
entitlement === null ? { status: 'not_accessible' } : { status: 'ready', entitlement },
|
|
115
|
+
);
|
|
100
116
|
},
|
|
101
117
|
(error: unknown) => {
|
|
102
|
-
|
|
118
|
+
settle(organizationFailure(error));
|
|
103
119
|
},
|
|
104
120
|
);
|
|
105
121
|
return () => {
|
|
106
122
|
active = false;
|
|
107
123
|
};
|
|
108
|
-
}, [backend, organizationId]);
|
|
109
|
-
|
|
124
|
+
}, [backend, organizationId, attempt]);
|
|
125
|
+
const current =
|
|
126
|
+
settled?.backend === backend &&
|
|
127
|
+
settled.organizationId === organizationId &&
|
|
128
|
+
settled.attempt === attempt;
|
|
129
|
+
const state: OrganizationPlanState = current ? settled.state : { status: 'loading' };
|
|
130
|
+
return { ...state, retry };
|
|
110
131
|
}
|
|
111
132
|
|
|
112
133
|
export interface LockedFeatureProps {
|