@baukit/purchases-core 0.17.0 → 0.18.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.d.ts +13 -0
- package/dist/access.d.ts.map +1 -1
- package/dist/access.js +20 -0
- package/dist/access.js.map +1 -1
- package/dist/account-plan.d.ts +7 -2
- package/dist/account-plan.d.ts.map +1 -1
- package/dist/account-plan.js +16 -4
- package/dist/account-plan.js.map +1 -1
- package/dist/entitlement.d.ts +9 -0
- package/dist/entitlement.d.ts.map +1 -1
- package/dist/entitlement.js.map +1 -1
- package/dist/paywall.d.ts +7 -1
- package/dist/paywall.d.ts.map +1 -1
- package/dist/paywall.js +13 -1
- package/dist/paywall.js.map +1 -1
- package/dist/react.d.ts +10 -3
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +34 -5
- package/dist/react.js.map +1 -1
- package/package.json +1 -1
- package/src/access.ts +31 -0
- package/src/account-plan.ts +23 -4
- package/src/entitlement.ts +10 -0
- package/src/organization.test.ts +214 -0
- package/src/paywall.ts +18 -1
- package/src/react.ts +66 -6
package/dist/access.d.ts
CHANGED
|
@@ -18,5 +18,18 @@ export type FeatureAccess = {
|
|
|
18
18
|
readonly entitlement: VerifiedEntitlement | null;
|
|
19
19
|
readonly paywall: Paywall;
|
|
20
20
|
};
|
|
21
|
+
/** An organization's plan as a client loads it through `organizationEntitlement`. */
|
|
22
|
+
export type OrganizationPlanState = {
|
|
23
|
+
readonly status: 'loading';
|
|
24
|
+
} | {
|
|
25
|
+
readonly status: 'unavailable';
|
|
26
|
+
} | {
|
|
27
|
+
readonly status: 'not_accessible';
|
|
28
|
+
} | {
|
|
29
|
+
readonly status: 'ready';
|
|
30
|
+
readonly entitlement: VerifiedEntitlement;
|
|
31
|
+
};
|
|
32
|
+
/** Access to `feature` through an organization's plan. A non-member sees neither feature nor paywall. */
|
|
33
|
+
export declare function organizationFeatureAccess(organization: OrganizationPlanState, feature: string, context: Omit<PaywallContext, 'feature'>): FeatureAccess;
|
|
21
34
|
export declare function featureAccess(state: PurchaseState, feature: string, context: Omit<PaywallContext, 'feature'>): FeatureAccess;
|
|
22
35
|
//# sourceMappingURL=access.d.ts.map
|
package/dist/access.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"access.d.ts","sourceRoot":"","sources":["../src/access.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAC5D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,EAAc,KAAK,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,cAAc,CAAC;AAE7E;;;;GAIG;AACH,MAAM,MAAM,aAAa,GACrB;IAAE,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAA;CAAE,GAC9B;IAAE,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAA;CAAE,GAClC;IAAE,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC;IAAC,QAAQ,CAAC,WAAW,EAAE,mBAAmB,CAAA;CAAE,GACzE;IACE,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC;IAC1B,QAAQ,CAAC,WAAW,EAAE,mBAAmB,GAAG,IAAI,CAAC;IACjD,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;CAC3B,CAAC;AAEN,wBAAgB,aAAa,CAC3B,KAAK,EAAE,aAAa,EACpB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,IAAI,CAAC,cAAc,EAAE,SAAS,CAAC,GACvC,aAAa,CAqBf"}
|
|
1
|
+
{"version":3,"file":"access.d.ts","sourceRoot":"","sources":["../src/access.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAC5D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,EAAc,KAAK,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,cAAc,CAAC;AAE7E;;;;GAIG;AACH,MAAM,MAAM,aAAa,GACrB;IAAE,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAA;CAAE,GAC9B;IAAE,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAA;CAAE,GAClC;IAAE,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC;IAAC,QAAQ,CAAC,WAAW,EAAE,mBAAmB,CAAA;CAAE,GACzE;IACE,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC;IAC1B,QAAQ,CAAC,WAAW,EAAE,mBAAmB,GAAG,IAAI,CAAC;IACjD,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;CAC3B,CAAC;AAEN,qFAAqF;AACrF,MAAM,MAAM,qBAAqB,GAC7B;IAAE,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAA;CAAE,GAC9B;IAAE,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAA;CAAE,GAClC;IAAE,QAAQ,CAAC,MAAM,EAAE,gBAAgB,CAAA;CAAE,GACrC;IAAE,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IAAC,QAAQ,CAAC,WAAW,EAAE,mBAAmB,CAAA;CAAE,CAAC;AAE5E,yGAAyG;AACzG,wBAAgB,yBAAyB,CACvC,YAAY,EAAE,qBAAqB,EACnC,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,IAAI,CAAC,cAAc,EAAE,SAAS,CAAC,GACvC,aAAa,CAiBf;AAED,wBAAgB,aAAa,CAC3B,KAAK,EAAE,aAAa,EACpB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,IAAI,CAAC,cAAc,EAAE,SAAS,CAAC,GACvC,aAAa,CAqBf"}
|
package/dist/access.js
CHANGED
|
@@ -1,4 +1,24 @@
|
|
|
1
1
|
import { paywallFor } from './paywall.js';
|
|
2
|
+
/** Access to `feature` through an organization's plan. A non-member sees neither feature nor paywall. */
|
|
3
|
+
export function organizationFeatureAccess(organization, feature, context) {
|
|
4
|
+
switch (organization.status) {
|
|
5
|
+
case 'loading':
|
|
6
|
+
case 'unavailable':
|
|
7
|
+
return { status: organization.status };
|
|
8
|
+
case 'not_accessible':
|
|
9
|
+
return { status: 'locked', entitlement: null, paywall: { kind: 'none' } };
|
|
10
|
+
case 'ready': {
|
|
11
|
+
const { entitlement } = organization;
|
|
12
|
+
if (entitlement.features.includes(feature))
|
|
13
|
+
return { status: 'granted', entitlement };
|
|
14
|
+
return {
|
|
15
|
+
status: 'locked',
|
|
16
|
+
entitlement,
|
|
17
|
+
paywall: paywallFor(entitlement, { ...context, feature }),
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
2
22
|
export function featureAccess(state, feature, context) {
|
|
3
23
|
const { entitlement, entitlementStatus } = state;
|
|
4
24
|
if (entitlement?.features.includes(feature)) {
|
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;
|
|
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,aAAa;YAChB,OAAO,EAAE,MAAM,EAAE,YAAY,CAAC,MAAM,EAAE,CAAC;QACzC,KAAK,gBAAgB;YACnB,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,WAAW,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,CAAC;QAC5E,KAAK,OAAO,CAAC,CAAC,CAAC;YACb,MAA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|
import type { EntitlementBackend } from './entitlement.js';
|
|
2
2
|
/** Error codes of Baukit's account plan routes. */
|
|
3
|
-
export declare const ACCOUNT_PLAN_ERROR_CODES: readonly ["suite_identity_missing", "entitlements_unavailable", "credits_unavailable", "feature_not_entitled"];
|
|
3
|
+
export declare const ACCOUNT_PLAN_ERROR_CODES: readonly ["suite_identity_missing", "entitlements_unavailable", "credits_unavailable", "feature_not_entitled", "organization_not_accessible", "organization_id_invalid"];
|
|
4
|
+
/** The request header that names the organization a request acts for. */
|
|
5
|
+
export declare const ORGANIZATION_HEADER = "x-organization-id";
|
|
4
6
|
export type AccountPlanErrorCode = (typeof ACCOUNT_PLAN_ERROR_CODES)[number];
|
|
5
7
|
export declare class AccountPlanError extends Error {
|
|
6
8
|
readonly status: number;
|
|
@@ -17,6 +19,9 @@ export interface AccountPlanBackendOptions {
|
|
|
17
19
|
/** Called per request, for example to add the bearer token. */
|
|
18
20
|
readonly headers?: () => Promise<Record<string, string>> | Record<string, string>;
|
|
19
21
|
}
|
|
20
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* Reads Baukit's `GET /me/entitlement` and `GET /me/credits`. `suite_identity_missing` resolves
|
|
24
|
+
* to `null`. `organizationEntitlement` rejects a non-member with `organization_not_accessible`.
|
|
25
|
+
*/
|
|
21
26
|
export declare function createAccountPlanBackend(options: AccountPlanBackendOptions): EntitlementBackend;
|
|
22
27
|
//# sourceMappingURL=account-plan.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"account-plan.d.ts","sourceRoot":"","sources":["../src/account-plan.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,kBAAkB,EAEnB,MAAM,kBAAkB,CAAC;AAE1B,mDAAmD;AACnD,eAAO,MAAM,wBAAwB,
|
|
1
|
+
{"version":3,"file":"account-plan.d.ts","sourceRoot":"","sources":["../src/account-plan.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,kBAAkB,EAEnB,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;AA4ED;;;GAGG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,yBAAyB,GAAG,kBAAkB,CAsB/F"}
|
package/dist/account-plan.js
CHANGED
|
@@ -4,7 +4,11 @@ export const ACCOUNT_PLAN_ERROR_CODES = [
|
|
|
4
4
|
'entitlements_unavailable',
|
|
5
5
|
'credits_unavailable',
|
|
6
6
|
'feature_not_entitled',
|
|
7
|
+
'organization_not_accessible',
|
|
8
|
+
'organization_id_invalid',
|
|
7
9
|
];
|
|
10
|
+
/** The request header that names the organization a request acts for. */
|
|
11
|
+
export const ORGANIZATION_HEADER = 'x-organization-id';
|
|
8
12
|
export class AccountPlanError extends Error {
|
|
9
13
|
status;
|
|
10
14
|
code;
|
|
@@ -38,11 +42,15 @@ function parseEntitlement(value) {
|
|
|
38
42
|
!value['features'].every(text) ||
|
|
39
43
|
!count(value['revision']) ||
|
|
40
44
|
!nullable(value['plansUrl'], text) ||
|
|
41
|
-
!nullable(value['billing'], record)
|
|
45
|
+
!nullable(value['billing'], record) ||
|
|
46
|
+
!(value['organization'] === undefined || nullable(value['organization'], organization))) {
|
|
42
47
|
throw new TypeError('The API returned an invalid entitlement.');
|
|
43
48
|
}
|
|
44
49
|
return value;
|
|
45
50
|
}
|
|
51
|
+
function organization(value) {
|
|
52
|
+
return record(value) && text(value['organizationId']) && nullable(value['name'], text);
|
|
53
|
+
}
|
|
46
54
|
function parseBalance(value) {
|
|
47
55
|
const grant = record(value) ? value['grant'] : undefined;
|
|
48
56
|
const packs = record(value) ? value['packs'] : undefined;
|
|
@@ -74,11 +82,14 @@ async function failure(response) {
|
|
|
74
82
|
}
|
|
75
83
|
return new AccountPlanError(response.status, code, Number.isSafeInteger(retry) && retry > 0 ? retry : null);
|
|
76
84
|
}
|
|
77
|
-
/**
|
|
85
|
+
/**
|
|
86
|
+
* Reads Baukit's `GET /me/entitlement` and `GET /me/credits`. `suite_identity_missing` resolves
|
|
87
|
+
* to `null`. `organizationEntitlement` rejects a non-member with `organization_not_accessible`.
|
|
88
|
+
*/
|
|
78
89
|
export function createAccountPlanBackend(options) {
|
|
79
90
|
const base = options.baseUrl.replace(/\/+$/u, '');
|
|
80
|
-
async function read(path, parse) {
|
|
81
|
-
const headers = { accept: 'application/json', ...(await options.headers?.()) };
|
|
91
|
+
async function read(path, parse, extra = {}) {
|
|
92
|
+
const headers = { accept: 'application/json', ...(await options.headers?.()), ...extra };
|
|
82
93
|
const response = await options.fetch(`${base}${path}`, { headers });
|
|
83
94
|
if (!response.ok) {
|
|
84
95
|
const error = await failure(response);
|
|
@@ -91,6 +102,7 @@ export function createAccountPlanBackend(options) {
|
|
|
91
102
|
return {
|
|
92
103
|
refreshEntitlement: () => read('/me/entitlement', parseEntitlement),
|
|
93
104
|
creditBalance: () => read('/me/credits', parseBalance),
|
|
105
|
+
organizationEntitlement: (organizationId) => read('/me/entitlement', parseEntitlement, { [ORGANIZATION_HEADER]: organizationId }),
|
|
94
106
|
};
|
|
95
107
|
}
|
|
96
108
|
//# sourceMappingURL=account-plan.js.map
|
package/dist/account-plan.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"account-plan.js","sourceRoot":"","sources":["../src/account-plan.ts"],"names":[],"mappings":"AAMA,mDAAmD;AACnD,MAAM,CAAC,MAAM,wBAAwB,GAAG;IACtC,wBAAwB;IACxB,0BAA0B;IAC1B,qBAAqB;IACrB,sBAAsB;
|
|
1
|
+
{"version":3,"file":"account-plan.js","sourceRoot":"","sources":["../src/account-plan.ts"],"names":[],"mappings":"AAMA,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;AAeD,SAAS,MAAM,CAAC,KAAc;IAC5B,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAA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I,GAAG,IAAI,EAAE,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC;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/entitlement.d.ts
CHANGED
|
@@ -6,6 +6,11 @@ export interface BillingClaim {
|
|
|
6
6
|
readonly soldBy?: string;
|
|
7
7
|
readonly manageUrl?: string;
|
|
8
8
|
}
|
|
9
|
+
/** The organization a member read the plan for. */
|
|
10
|
+
export interface EntitlementOrganization {
|
|
11
|
+
readonly organizationId: string;
|
|
12
|
+
readonly name: string | null;
|
|
13
|
+
}
|
|
9
14
|
/** The body of the product backend's `GET /me/entitlement`. */
|
|
10
15
|
export interface VerifiedEntitlement {
|
|
11
16
|
readonly mode: PlanMode;
|
|
@@ -16,6 +21,8 @@ export interface VerifiedEntitlement {
|
|
|
16
21
|
/** The hub `/plans` page in hub mode, null in standalone mode. */
|
|
17
22
|
readonly plansUrl: string | null;
|
|
18
23
|
readonly billing: BillingClaim | null;
|
|
24
|
+
/** Set when the backend served an organization's plan to a member. */
|
|
25
|
+
readonly organization?: EntitlementOrganization | null;
|
|
19
26
|
}
|
|
20
27
|
/** The body of the product backend's `GET /me/credits`. */
|
|
21
28
|
export interface CreditBalanceSnapshot {
|
|
@@ -36,6 +43,8 @@ export interface CreditBalanceSnapshot {
|
|
|
36
43
|
export interface EntitlementBackend {
|
|
37
44
|
refreshEntitlement(): Promise<VerifiedEntitlement | null>;
|
|
38
45
|
creditBalance?(): Promise<CreditBalanceSnapshot | null>;
|
|
46
|
+
/** The plan of an organization the account acts for, sent with `X-Organization-Id`. */
|
|
47
|
+
organizationEntitlement?(organizationId: string): Promise<VerifiedEntitlement | null>;
|
|
39
48
|
}
|
|
40
49
|
export interface TicketClaimsLike {
|
|
41
50
|
readonly sub: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"entitlement.d.ts","sourceRoot":"","sources":["../src/entitlement.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,QAAQ,GAAG,KAAK,GAAG,YAAY,CAAC;AAE5C,MAAM,MAAM,cAAc,GACxB,KAAK,GAAG,WAAW,GAAG,YAAY,GAAG,QAAQ,GAAG,cAAc,GAAG,OAAO,CAAC;AAE3E,wFAAwF;AACxF,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC;IACjC,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED,+DAA+D;AAC/D,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IACxB,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;IAC1B,kEAAkE;IAClE,QAAQ,CAAC,QAAQ,EAAE,MAAM,G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
|
-
{"version":3,"file":"entitlement.js","sourceRoot":"","sources":["../src/entitlement.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"entitlement.js","sourceRoot":"","sources":["../src/entitlement.ts"],"names":[],"mappings":"AA0DA,MAAM,UAAU,qBAAqB,CACnC,MAAwB,EACxB,WAA0B,IAAI;IAE9B,OAAO;QACL,IAAI,EAAE,KAAK;QACX,YAAY,EAAE,MAAM,CAAC,GAAG;QACxB,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,QAAQ,EAAE,MAAM,CAAC,QAAQ;QACzB,QAAQ,EAAE,MAAM,CAAC,GAAG;QACpB,QAAQ;QACR,OAAO,EAAE,MAAM,CAAC,OAAO,IAAI,IAAI;KAChC,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/paywall.d.ts
CHANGED
|
@@ -11,6 +11,10 @@ export type Paywall = {
|
|
|
11
11
|
} | {
|
|
12
12
|
readonly kind: 'covered';
|
|
13
13
|
readonly channel: 'family' | 'organization';
|
|
14
|
+
} | {
|
|
15
|
+
readonly kind: 'organization_managed';
|
|
16
|
+
readonly organizationId: string;
|
|
17
|
+
readonly organizationName: string | null;
|
|
14
18
|
} | {
|
|
15
19
|
readonly kind: 'none';
|
|
16
20
|
};
|
|
@@ -22,7 +26,9 @@ export interface PaywallContext {
|
|
|
22
26
|
/**
|
|
23
27
|
* The C18 rules: a plan managed on the web or in a store shows where to manage it and
|
|
24
28
|
* never sells a second plan, family and organization coverage offer no purchase, and
|
|
25
|
-
* standalone mode sells nothing. Promo grants and free accounts may buy.
|
|
29
|
+
* standalone mode sells nothing. Promo grants and free accounts may buy. An organization's
|
|
30
|
+
* plan covers its members, and a feature it lacks names the organization whose admins manage
|
|
31
|
+
* the plan instead of offering a purchase.
|
|
26
32
|
*/
|
|
27
33
|
export declare function paywallFor(entitlement: VerifiedEntitlement, context: PaywallContext): Paywall;
|
|
28
34
|
//# sourceMappingURL=paywall.d.ts.map
|
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,EAAkB,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAG5E,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;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
|
|
1
|
+
{"version":3,"file":"paywall.d.ts","sourceRoot":"","sources":["../src/paywall.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAkB,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAG5E,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,CA2C7F"}
|
package/dist/paywall.js
CHANGED
|
@@ -2,11 +2,23 @@ import { plansLink } from './plans-link.js';
|
|
|
2
2
|
/**
|
|
3
3
|
* The C18 rules: a plan managed on the web or in a store shows where to manage it and
|
|
4
4
|
* never sells a second plan, family and organization coverage offer no purchase, and
|
|
5
|
-
* standalone mode sells nothing. Promo grants and free accounts may buy.
|
|
5
|
+
* standalone mode sells nothing. Promo grants and free accounts may buy. An organization's
|
|
6
|
+
* plan covers its members, and a feature it lacks names the organization whose admins manage
|
|
7
|
+
* the plan instead of offering a purchase.
|
|
6
8
|
*/
|
|
7
9
|
export function paywallFor(entitlement, context) {
|
|
8
10
|
if (entitlement.mode === 'standalone')
|
|
9
11
|
return { kind: 'none' };
|
|
12
|
+
const organization = entitlement.organization;
|
|
13
|
+
if (organization) {
|
|
14
|
+
if (context.feature === undefined || entitlement.features.includes(context.feature))
|
|
15
|
+
return { kind: 'covered', channel: 'organization' };
|
|
16
|
+
return {
|
|
17
|
+
kind: 'organization_managed',
|
|
18
|
+
organizationId: organization.organizationId,
|
|
19
|
+
organizationName: organization.name,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
10
22
|
const link = entitlement.plansUrl === null
|
|
11
23
|
? null
|
|
12
24
|
: plansLink(entitlement.plansUrl, {
|
package/dist/paywall.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"paywall.js","sourceRoot":"","sources":["../src/paywall.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"paywall.js","sourceRoot":"","sources":["../src/paywall.ts"],"names":[],"mappings":"AACA,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,EAAE,CAAC;QACjB,IAAI,OAAO,CAAC,OAAO,KAAK,SAAS,IAAI,WAAW,CAAC,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC;YACjF,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,cAAc,EAAE,CAAC;QACtD,OAAO;YACL,IAAI,EAAE,sBAAsB;YAC5B,cAAc,EAAE,YAAY,CAAC,cAAc;YAC3C,gBAAgB,EAAE,YAAY,CAAC,IAAI;SACpC,CAAC;IACJ,CAAC;IACD,MAAM,IAAI,GACR,WAAW,CAAC,QAAQ,KAAK,IAAI;QAC3B,CAAC,CAAC,IAAI;QACN,CAAC,CAAC,SAAS,CAAC,WAAW,CAAC,QAAQ,EAAE;YAC9B,IAAI,EAAE,OAAO,CAAC,SAAS;YACvB,GAAG,CAAC,OAAO,CAAC,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,CAAC;SACvE,CAAC,CAAC;IACT,MAAM,OAAO,GAA+B,WAAW,CAAC,OAAO,EAAE,OAAO,CAAC;IACzE,QAAQ,OAAO,EAAE,CAAC;QAChB,KAAK,KAAK;YACR,OAAO;gBACL,IAAI,EAAE,QAAQ;gBACd,OAAO;gBACP,SAAS,EAAE,IAAI,IAAI,WAAW,CAAC,OAAO,EAAE,SAAS,IAAI,IAAI;gBACzD,MAAM,EAAE,IAAI;aACb,CAAC;QACJ,KAAK,WAAW,CAAC;QACjB,KAAK,YAAY;YACf,OAAO;gBACL,IAAI,EAAE,QAAQ;gBACd,OAAO;gBACP,SAAS,EAAE,IAAI;gBACf,MAAM,EAAE,WAAW,CAAC,OAAO,EAAE,MAAM,IAAI,IAAI;aAC5C,CAAC;QACJ,KAAK,QAAQ,CAAC;QACd,KAAK,cAAc;YACjB,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/react.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ReactNode } from 'react';
|
|
2
|
-
import { type FeatureAccess } from './access.js';
|
|
3
|
-
import type { VerifiedEntitlement } from './entitlement.js';
|
|
2
|
+
import { type FeatureAccess, type OrganizationPlanState } from './access.js';
|
|
3
|
+
import type { EntitlementBackend, VerifiedEntitlement } from './entitlement.js';
|
|
4
4
|
import type { PurchaseFlow, PurchaseState } from './flow.js';
|
|
5
5
|
import type { Paywall } from './paywall.js';
|
|
6
6
|
export interface PurchasesProviderProps {
|
|
@@ -13,8 +13,14 @@ export declare function PurchasesProvider({ flow, productId, children, }: Purcha
|
|
|
13
13
|
export declare function usePurchaseFlow(): PurchaseFlow;
|
|
14
14
|
export declare function usePurchaseState(): PurchaseState;
|
|
15
15
|
export declare function useEntitlement(): VerifiedEntitlement | null;
|
|
16
|
+
export interface FeatureOptions {
|
|
17
|
+
/** Check the organization's plan from `useOrganizationPlan` instead of the account's. */
|
|
18
|
+
readonly organization?: OrganizationPlanState;
|
|
19
|
+
}
|
|
16
20
|
/** Access to a feature key from the backend-verified entitlement. See `featureAccess`. */
|
|
17
|
-
export declare function useFeature(key: string): FeatureAccess;
|
|
21
|
+
export declare function useFeature(key: string, options?: FeatureOptions): FeatureAccess;
|
|
22
|
+
/** Loads the plan of the organization the account acts for. */
|
|
23
|
+
export declare function useOrganizationPlan(backend: Pick<EntitlementBackend, 'organizationEntitlement'>, organizationId: string): OrganizationPlanState;
|
|
18
24
|
export interface LockedFeatureProps {
|
|
19
25
|
readonly feature: string;
|
|
20
26
|
readonly children?: ReactNode;
|
|
@@ -23,6 +29,7 @@ export interface LockedFeatureProps {
|
|
|
23
29
|
readonly loading?: ReactNode;
|
|
24
30
|
/** Rendered when the plan cannot be read. Offer a retry, never a purchase. */
|
|
25
31
|
readonly unavailable?: ReactNode;
|
|
32
|
+
readonly organization?: OrganizationPlanState;
|
|
26
33
|
}
|
|
27
34
|
/** Renders `children` only while the verified entitlement grants `feature`. */
|
|
28
35
|
export declare function LockedFeature(props: LockedFeatureProps): ReactNode;
|
package/dist/react.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../src/react.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,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,5 +1,6 @@
|
|
|
1
|
-
import { createContext, createElement, Fragment, useContext, useSyncExternalStore } from 'react';
|
|
2
|
-
import { featureAccess } from './access.js';
|
|
1
|
+
import { createContext, createElement, Fragment, useContext, useEffect, useState, useSyncExternalStore, } from 'react';
|
|
2
|
+
import { featureAccess, organizationFeatureAccess, } from './access.js';
|
|
3
|
+
import { AccountPlanError } from './account-plan.js';
|
|
3
4
|
const PurchaseFlowContext = createContext(null);
|
|
4
5
|
export function PurchasesProvider({ flow, productId, children, }) {
|
|
5
6
|
return createElement(PurchaseFlowContext.Provider, { value: { flow, productId } }, children);
|
|
@@ -21,13 +22,41 @@ export function useEntitlement() {
|
|
|
21
22
|
return usePurchaseState().entitlement;
|
|
22
23
|
}
|
|
23
24
|
/** Access to a feature key from the backend-verified entitlement. See `featureAccess`. */
|
|
24
|
-
export function useFeature(key) {
|
|
25
|
+
export function useFeature(key, options = {}) {
|
|
25
26
|
const { productId } = usePurchasesContext();
|
|
26
|
-
|
|
27
|
+
const state = usePurchaseState();
|
|
28
|
+
if (options.organization !== undefined)
|
|
29
|
+
return organizationFeatureAccess(options.organization, key, { productId });
|
|
30
|
+
return featureAccess(state, key, { productId });
|
|
31
|
+
}
|
|
32
|
+
function organizationFailure(error) {
|
|
33
|
+
if (error instanceof AccountPlanError && error.code === 'organization_not_accessible')
|
|
34
|
+
return { status: 'not_accessible' };
|
|
35
|
+
return { status: 'unavailable' };
|
|
36
|
+
}
|
|
37
|
+
/** Loads the plan of the organization the account acts for. */
|
|
38
|
+
export function useOrganizationPlan(backend, organizationId) {
|
|
39
|
+
const [state, setState] = useState({ status: 'loading' });
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
let active = true;
|
|
42
|
+
setState({ status: 'loading' });
|
|
43
|
+
const load = backend.organizationEntitlement?.(organizationId) ?? Promise.resolve(null);
|
|
44
|
+
load.then((entitlement) => {
|
|
45
|
+
if (active)
|
|
46
|
+
setState(entitlement === null ? { status: 'not_accessible' } : { status: 'ready', entitlement });
|
|
47
|
+
}, (error) => {
|
|
48
|
+
if (active)
|
|
49
|
+
setState(organizationFailure(error));
|
|
50
|
+
});
|
|
51
|
+
return () => {
|
|
52
|
+
active = false;
|
|
53
|
+
};
|
|
54
|
+
}, [backend, organizationId]);
|
|
55
|
+
return state;
|
|
27
56
|
}
|
|
28
57
|
/** Renders `children` only while the verified entitlement grants `feature`. */
|
|
29
58
|
export function LockedFeature(props) {
|
|
30
|
-
const access = useFeature(props.feature);
|
|
59
|
+
const access = useFeature(props.feature, props.organization === undefined ? {} : { organization: props.organization });
|
|
31
60
|
switch (access.status) {
|
|
32
61
|
case 'granted':
|
|
33
62
|
return createElement(Fragment, null, props.children);
|
package/dist/react.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react.js","sourceRoot":"","sources":["../src/react.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
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,M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EAA+E;AAC/E,MAAM,UAAU,aAAa,CAAC,KAAyB;IACrD,MAAM,MAAM,GAAG,UAAU,CACvB,KAAK,CAAC,OAAO,EACb,KAAK,CAAC,YAAY,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,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.18.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",
|
package/src/access.ts
CHANGED
|
@@ -17,6 +17,37 @@ export type FeatureAccess =
|
|
|
17
17
|
readonly paywall: Paywall;
|
|
18
18
|
};
|
|
19
19
|
|
|
20
|
+
/** An organization's plan as a client loads it through `organizationEntitlement`. */
|
|
21
|
+
export type OrganizationPlanState =
|
|
22
|
+
| { readonly status: 'loading' }
|
|
23
|
+
| { readonly status: 'unavailable' }
|
|
24
|
+
| { readonly status: 'not_accessible' }
|
|
25
|
+
| { readonly status: 'ready'; readonly entitlement: VerifiedEntitlement };
|
|
26
|
+
|
|
27
|
+
/** Access to `feature` through an organization's plan. A non-member sees neither feature nor paywall. */
|
|
28
|
+
export function organizationFeatureAccess(
|
|
29
|
+
organization: OrganizationPlanState,
|
|
30
|
+
feature: string,
|
|
31
|
+
context: Omit<PaywallContext, 'feature'>,
|
|
32
|
+
): FeatureAccess {
|
|
33
|
+
switch (organization.status) {
|
|
34
|
+
case 'loading':
|
|
35
|
+
case 'unavailable':
|
|
36
|
+
return { status: organization.status };
|
|
37
|
+
case 'not_accessible':
|
|
38
|
+
return { status: 'locked', entitlement: null, paywall: { kind: 'none' } };
|
|
39
|
+
case 'ready': {
|
|
40
|
+
const { entitlement } = organization;
|
|
41
|
+
if (entitlement.features.includes(feature)) return { status: 'granted', entitlement };
|
|
42
|
+
return {
|
|
43
|
+
status: 'locked',
|
|
44
|
+
entitlement,
|
|
45
|
+
paywall: paywallFor(entitlement, { ...context, feature }),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
20
51
|
export function featureAccess(
|
|
21
52
|
state: PurchaseState,
|
|
22
53
|
feature: string,
|
package/src/account-plan.ts
CHANGED
|
@@ -10,7 +10,12 @@ export const ACCOUNT_PLAN_ERROR_CODES = [
|
|
|
10
10
|
'entitlements_unavailable',
|
|
11
11
|
'credits_unavailable',
|
|
12
12
|
'feature_not_entitled',
|
|
13
|
+
'organization_not_accessible',
|
|
14
|
+
'organization_id_invalid',
|
|
13
15
|
] as const;
|
|
16
|
+
|
|
17
|
+
/** The request header that names the organization a request acts for. */
|
|
18
|
+
export const ORGANIZATION_HEADER = 'x-organization-id';
|
|
14
19
|
export type AccountPlanErrorCode = (typeof ACCOUNT_PLAN_ERROR_CODES)[number];
|
|
15
20
|
|
|
16
21
|
export class AccountPlanError extends Error {
|
|
@@ -60,13 +65,18 @@ function parseEntitlement(value: unknown): VerifiedEntitlement {
|
|
|
60
65
|
!value['features'].every(text) ||
|
|
61
66
|
!count(value['revision']) ||
|
|
62
67
|
!nullable(value['plansUrl'], text) ||
|
|
63
|
-
!nullable(value['billing'], record)
|
|
68
|
+
!nullable(value['billing'], record) ||
|
|
69
|
+
!(value['organization'] === undefined || nullable(value['organization'], organization))
|
|
64
70
|
) {
|
|
65
71
|
throw new TypeError('The API returned an invalid entitlement.');
|
|
66
72
|
}
|
|
67
73
|
return value as unknown as VerifiedEntitlement;
|
|
68
74
|
}
|
|
69
75
|
|
|
76
|
+
function organization(value: unknown): boolean {
|
|
77
|
+
return record(value) && text(value['organizationId']) && nullable(value['name'], text);
|
|
78
|
+
}
|
|
79
|
+
|
|
70
80
|
function parseBalance(value: unknown): CreditBalanceSnapshot {
|
|
71
81
|
const grant = record(value) ? value['grant'] : undefined;
|
|
72
82
|
const packs = record(value) ? value['packs'] : undefined;
|
|
@@ -104,11 +114,18 @@ async function failure(response: Response): Promise<AccountPlanError> {
|
|
|
104
114
|
);
|
|
105
115
|
}
|
|
106
116
|
|
|
107
|
-
/**
|
|
117
|
+
/**
|
|
118
|
+
* Reads Baukit's `GET /me/entitlement` and `GET /me/credits`. `suite_identity_missing` resolves
|
|
119
|
+
* to `null`. `organizationEntitlement` rejects a non-member with `organization_not_accessible`.
|
|
120
|
+
*/
|
|
108
121
|
export function createAccountPlanBackend(options: AccountPlanBackendOptions): EntitlementBackend {
|
|
109
122
|
const base = options.baseUrl.replace(/\/+$/u, '');
|
|
110
|
-
async function read<T>(
|
|
111
|
-
|
|
123
|
+
async function read<T>(
|
|
124
|
+
path: string,
|
|
125
|
+
parse: (value: unknown) => T,
|
|
126
|
+
extra: Record<string, string> = {},
|
|
127
|
+
): Promise<T | null> {
|
|
128
|
+
const headers = { accept: 'application/json', ...(await options.headers?.()), ...extra };
|
|
112
129
|
const response = await options.fetch(`${base}${path}`, { headers });
|
|
113
130
|
if (!response.ok) {
|
|
114
131
|
const error = await failure(response);
|
|
@@ -120,5 +137,7 @@ export function createAccountPlanBackend(options: AccountPlanBackendOptions): En
|
|
|
120
137
|
return {
|
|
121
138
|
refreshEntitlement: () => read('/me/entitlement', parseEntitlement),
|
|
122
139
|
creditBalance: () => read('/me/credits', parseBalance),
|
|
140
|
+
organizationEntitlement: (organizationId) =>
|
|
141
|
+
read('/me/entitlement', parseEntitlement, { [ORGANIZATION_HEADER]: organizationId }),
|
|
123
142
|
};
|
|
124
143
|
}
|
package/src/entitlement.ts
CHANGED
|
@@ -10,6 +10,12 @@ export interface BillingClaim {
|
|
|
10
10
|
readonly manageUrl?: string;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
/** The organization a member read the plan for. */
|
|
14
|
+
export interface EntitlementOrganization {
|
|
15
|
+
readonly organizationId: string;
|
|
16
|
+
readonly name: string | null;
|
|
17
|
+
}
|
|
18
|
+
|
|
13
19
|
/** The body of the product backend's `GET /me/entitlement`. */
|
|
14
20
|
export interface VerifiedEntitlement {
|
|
15
21
|
readonly mode: PlanMode;
|
|
@@ -20,6 +26,8 @@ export interface VerifiedEntitlement {
|
|
|
20
26
|
/** The hub `/plans` page in hub mode, null in standalone mode. */
|
|
21
27
|
readonly plansUrl: string | null;
|
|
22
28
|
readonly billing: BillingClaim | null;
|
|
29
|
+
/** Set when the backend served an organization's plan to a member. */
|
|
30
|
+
readonly organization?: EntitlementOrganization | null;
|
|
23
31
|
}
|
|
24
32
|
|
|
25
33
|
/** The body of the product backend's `GET /me/credits`. */
|
|
@@ -36,6 +44,8 @@ export interface CreditBalanceSnapshot {
|
|
|
36
44
|
export interface EntitlementBackend {
|
|
37
45
|
refreshEntitlement(): Promise<VerifiedEntitlement | null>;
|
|
38
46
|
creditBalance?(): Promise<CreditBalanceSnapshot | null>;
|
|
47
|
+
/** The plan of an organization the account acts for, sent with `X-Organization-Id`. */
|
|
48
|
+
organizationEntitlement?(organizationId: string): Promise<VerifiedEntitlement | null>;
|
|
39
49
|
}
|
|
40
50
|
|
|
41
51
|
export interface TicketClaimsLike {
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { cleanup, renderHook, waitFor } from '@testing-library/react';
|
|
3
|
+
import { createElement, type ReactNode } from 'react';
|
|
4
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
AccountPlanError,
|
|
8
|
+
ACCOUNT_PLAN_ERROR_CODES,
|
|
9
|
+
createAccountPlanBackend,
|
|
10
|
+
createNoopPurchasesPort,
|
|
11
|
+
ORGANIZATION_HEADER,
|
|
12
|
+
organizationFeatureAccess,
|
|
13
|
+
paywallFor,
|
|
14
|
+
PurchaseFlow,
|
|
15
|
+
type VerifiedEntitlement,
|
|
16
|
+
} from './index.js';
|
|
17
|
+
import { PurchasesProvider, useFeature, useOrganizationPlan } from './react.js';
|
|
18
|
+
|
|
19
|
+
const STUDIO = '00000000-0000-0000-0000-00000000000a';
|
|
20
|
+
const AGENCY = '00000000-0000-0000-0000-00000000000b';
|
|
21
|
+
const PRIVACY = 'ahp.privacy';
|
|
22
|
+
const AUTOMATION = 'ahp.automation';
|
|
23
|
+
const context = { productId: 'ahp' };
|
|
24
|
+
|
|
25
|
+
function organizationPlan(
|
|
26
|
+
organizationId: string,
|
|
27
|
+
tier: string,
|
|
28
|
+
features: string[],
|
|
29
|
+
revision = 1,
|
|
30
|
+
): VerifiedEntitlement {
|
|
31
|
+
return {
|
|
32
|
+
mode: 'hub',
|
|
33
|
+
suiteSubject: 'suite|alice',
|
|
34
|
+
tier,
|
|
35
|
+
features,
|
|
36
|
+
revision,
|
|
37
|
+
plansUrl: null,
|
|
38
|
+
billing: null,
|
|
39
|
+
organization: { organizationId, name: `Org ${organizationId.slice(-1)}` },
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
const studio = organizationPlan(STUDIO, 'business', [PRIVACY, AUTOMATION]);
|
|
43
|
+
const agency = organizationPlan(AGENCY, 'starter', [PRIVACY]);
|
|
44
|
+
|
|
45
|
+
function json(status: number, body: unknown): Response {
|
|
46
|
+
return new Response(JSON.stringify(body), {
|
|
47
|
+
status,
|
|
48
|
+
headers: { 'content-type': 'application/json' },
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
function failure(status: number, code: string): Response {
|
|
52
|
+
return json(status, { error: { code, message: code, details: {} } });
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
afterEach(() => {
|
|
56
|
+
cleanup();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe('paywallFor with an organization subject', () => {
|
|
60
|
+
it('gives one account each organization its own tier', () => {
|
|
61
|
+
expect(paywallFor(studio, { ...context, feature: AUTOMATION })).toEqual({
|
|
62
|
+
kind: 'covered',
|
|
63
|
+
channel: 'organization',
|
|
64
|
+
});
|
|
65
|
+
expect(paywallFor(agency, { ...context, feature: AUTOMATION })).toEqual({
|
|
66
|
+
kind: 'organization_managed',
|
|
67
|
+
organizationId: AGENCY,
|
|
68
|
+
organizationName: 'Org b',
|
|
69
|
+
});
|
|
70
|
+
expect(paywallFor(agency, context)).toEqual({ kind: 'covered', channel: 'organization' });
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('never sells a plan to a member, whatever the billing claim says', () => {
|
|
74
|
+
const billed = {
|
|
75
|
+
...agency,
|
|
76
|
+
plansUrl: 'https://accounts.example.test/plans',
|
|
77
|
+
billing: { channel: 'web' as const },
|
|
78
|
+
};
|
|
79
|
+
expect(paywallFor(billed, { ...context, feature: AUTOMATION }).kind).toBe(
|
|
80
|
+
'organization_managed',
|
|
81
|
+
);
|
|
82
|
+
expect(paywallFor({ ...agency, mode: 'standalone' }, context)).toEqual({ kind: 'none' });
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
describe('organizationFeatureAccess', () => {
|
|
87
|
+
it('grants, locks and fails closed per organization state', () => {
|
|
88
|
+
expect(
|
|
89
|
+
organizationFeatureAccess({ status: 'ready', entitlement: studio }, AUTOMATION, context),
|
|
90
|
+
).toEqual({ status: 'granted', entitlement: studio });
|
|
91
|
+
expect(
|
|
92
|
+
organizationFeatureAccess({ status: 'ready', entitlement: agency }, AUTOMATION, context),
|
|
93
|
+
).toMatchObject({ status: 'locked', paywall: { kind: 'organization_managed' } });
|
|
94
|
+
expect(organizationFeatureAccess({ status: 'loading' }, PRIVACY, context)).toEqual({
|
|
95
|
+
status: 'loading',
|
|
96
|
+
});
|
|
97
|
+
expect(organizationFeatureAccess({ status: 'unavailable' }, PRIVACY, context)).toEqual({
|
|
98
|
+
status: 'unavailable',
|
|
99
|
+
});
|
|
100
|
+
expect(organizationFeatureAccess({ status: 'not_accessible' }, PRIVACY, context)).toEqual({
|
|
101
|
+
status: 'locked',
|
|
102
|
+
entitlement: null,
|
|
103
|
+
paywall: { kind: 'none' },
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
describe('createAccountPlanBackend organizationEntitlement', () => {
|
|
109
|
+
it('names the organization in the header and parses its plan', async () => {
|
|
110
|
+
const fetch = vi.fn((_url: string, init: { headers: Record<string, string> }) =>
|
|
111
|
+
Promise.resolve(json(200, init.headers[ORGANIZATION_HEADER] === STUDIO ? studio : agency)),
|
|
112
|
+
);
|
|
113
|
+
const backend = createAccountPlanBackend({
|
|
114
|
+
baseUrl: 'https://api.example.test/api/v1',
|
|
115
|
+
fetch,
|
|
116
|
+
headers: () => ({ authorization: 'Bearer t' }),
|
|
117
|
+
});
|
|
118
|
+
await expect(backend.organizationEntitlement?.(STUDIO)).resolves.toEqual(studio);
|
|
119
|
+
await expect(backend.organizationEntitlement?.(AGENCY)).resolves.toEqual(agency);
|
|
120
|
+
expect(fetch).toHaveBeenCalledWith('https://api.example.test/api/v1/me/entitlement', {
|
|
121
|
+
headers: {
|
|
122
|
+
accept: 'application/json',
|
|
123
|
+
authorization: 'Bearer t',
|
|
124
|
+
[ORGANIZATION_HEADER]: STUDIO,
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('rejects a non-member and a malformed organization body', async () => {
|
|
130
|
+
expect(ACCOUNT_PLAN_ERROR_CODES).toContain('organization_not_accessible');
|
|
131
|
+
const responses = [
|
|
132
|
+
failure(403, 'organization_not_accessible'),
|
|
133
|
+
json(200, { ...studio, organization: { organizationId: 7 } }),
|
|
134
|
+
];
|
|
135
|
+
const backend = createAccountPlanBackend({
|
|
136
|
+
baseUrl: 'https://api.example.test',
|
|
137
|
+
fetch: () => Promise.resolve(responses.shift() ?? failure(500, 'internal_error')),
|
|
138
|
+
});
|
|
139
|
+
await expect(backend.organizationEntitlement?.(STUDIO)).rejects.toEqual(
|
|
140
|
+
new AccountPlanError(403, 'organization_not_accessible'),
|
|
141
|
+
);
|
|
142
|
+
await expect(backend.organizationEntitlement?.(STUDIO)).rejects.toThrow(TypeError);
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
describe('useFeature with an organization plan', () => {
|
|
147
|
+
function wrapper({ children }: { children: ReactNode }) {
|
|
148
|
+
const flow = new PurchaseFlow({
|
|
149
|
+
port: createNoopPurchasesPort(),
|
|
150
|
+
backend: { refreshEntitlement: () => Promise.resolve(null) },
|
|
151
|
+
});
|
|
152
|
+
return createElement(PurchasesProvider, { flow, productId: 'ahp' }, children);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
it('gates without a suite session, as for an organization token', async () => {
|
|
156
|
+
const backend = { organizationEntitlement: () => Promise.resolve(studio) };
|
|
157
|
+
const view = renderHook(
|
|
158
|
+
() => useFeature(AUTOMATION, { organization: useOrganizationPlan(backend, STUDIO) }),
|
|
159
|
+
{ wrapper },
|
|
160
|
+
);
|
|
161
|
+
expect(view.result.current).toEqual({ status: 'loading' });
|
|
162
|
+
await waitFor(() => {
|
|
163
|
+
expect(view.result.current.status).toBe('granted');
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('locks a lost grant on the next read and hides a non-member', async () => {
|
|
168
|
+
let current: VerifiedEntitlement = studio;
|
|
169
|
+
const backend = {
|
|
170
|
+
organizationEntitlement: (id: string) =>
|
|
171
|
+
id === STUDIO
|
|
172
|
+
? Promise.resolve(current)
|
|
173
|
+
: Promise.reject(new AccountPlanError(403, 'organization_not_accessible')),
|
|
174
|
+
};
|
|
175
|
+
const view = renderHook(
|
|
176
|
+
({ id }: { id: string }) =>
|
|
177
|
+
useFeature(AUTOMATION, { organization: useOrganizationPlan(backend, id) }),
|
|
178
|
+
{ wrapper, initialProps: { id: STUDIO } },
|
|
179
|
+
);
|
|
180
|
+
await waitFor(() => {
|
|
181
|
+
expect(view.result.current.status).toBe('granted');
|
|
182
|
+
});
|
|
183
|
+
current = organizationPlan(STUDIO, 'starter', [PRIVACY], 2);
|
|
184
|
+
view.rerender({ id: AGENCY });
|
|
185
|
+
await waitFor(() => {
|
|
186
|
+
expect(view.result.current).toEqual({
|
|
187
|
+
status: 'locked',
|
|
188
|
+
entitlement: null,
|
|
189
|
+
paywall: { kind: 'none' },
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
view.rerender({ id: STUDIO });
|
|
193
|
+
await waitFor(() => {
|
|
194
|
+
expect(view.result.current).toMatchObject({
|
|
195
|
+
status: 'locked',
|
|
196
|
+
paywall: { kind: 'organization_managed', organizationId: STUDIO },
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it('reports an unreachable hub as unavailable', async () => {
|
|
202
|
+
const backend = {
|
|
203
|
+
organizationEntitlement: () =>
|
|
204
|
+
Promise.reject(new AccountPlanError(503, 'entitlements_unavailable', 5)),
|
|
205
|
+
};
|
|
206
|
+
const view = renderHook(
|
|
207
|
+
() => useFeature(PRIVACY, { organization: useOrganizationPlan(backend, STUDIO) }),
|
|
208
|
+
{ wrapper },
|
|
209
|
+
);
|
|
210
|
+
await waitFor(() => {
|
|
211
|
+
expect(view.result.current).toEqual({ status: 'unavailable' });
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
});
|
package/src/paywall.ts
CHANGED
|
@@ -11,6 +11,11 @@ export type Paywall =
|
|
|
11
11
|
readonly soldBy: string | null;
|
|
12
12
|
}
|
|
13
13
|
| { readonly kind: 'covered'; readonly channel: 'family' | 'organization' }
|
|
14
|
+
| {
|
|
15
|
+
readonly kind: 'organization_managed';
|
|
16
|
+
readonly organizationId: string;
|
|
17
|
+
readonly organizationName: string | null;
|
|
18
|
+
}
|
|
14
19
|
| { readonly kind: 'none' };
|
|
15
20
|
|
|
16
21
|
export interface PaywallContext {
|
|
@@ -22,10 +27,22 @@ export interface PaywallContext {
|
|
|
22
27
|
/**
|
|
23
28
|
* The C18 rules: a plan managed on the web or in a store shows where to manage it and
|
|
24
29
|
* never sells a second plan, family and organization coverage offer no purchase, and
|
|
25
|
-
* standalone mode sells nothing. Promo grants and free accounts may buy.
|
|
30
|
+
* standalone mode sells nothing. Promo grants and free accounts may buy. An organization's
|
|
31
|
+
* plan covers its members, and a feature it lacks names the organization whose admins manage
|
|
32
|
+
* the plan instead of offering a purchase.
|
|
26
33
|
*/
|
|
27
34
|
export function paywallFor(entitlement: VerifiedEntitlement, context: PaywallContext): Paywall {
|
|
28
35
|
if (entitlement.mode === 'standalone') return { kind: 'none' };
|
|
36
|
+
const organization = entitlement.organization;
|
|
37
|
+
if (organization) {
|
|
38
|
+
if (context.feature === undefined || entitlement.features.includes(context.feature))
|
|
39
|
+
return { kind: 'covered', channel: 'organization' };
|
|
40
|
+
return {
|
|
41
|
+
kind: 'organization_managed',
|
|
42
|
+
organizationId: organization.organizationId,
|
|
43
|
+
organizationName: organization.name,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
29
46
|
const link =
|
|
30
47
|
entitlement.plansUrl === null
|
|
31
48
|
? null
|
package/src/react.ts
CHANGED
|
@@ -1,8 +1,22 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
createContext,
|
|
3
|
+
createElement,
|
|
4
|
+
Fragment,
|
|
5
|
+
useContext,
|
|
6
|
+
useEffect,
|
|
7
|
+
useState,
|
|
8
|
+
useSyncExternalStore,
|
|
9
|
+
} from 'react';
|
|
2
10
|
import type { ReactNode } from 'react';
|
|
3
11
|
|
|
4
|
-
import {
|
|
5
|
-
|
|
12
|
+
import {
|
|
13
|
+
featureAccess,
|
|
14
|
+
organizationFeatureAccess,
|
|
15
|
+
type FeatureAccess,
|
|
16
|
+
type OrganizationPlanState,
|
|
17
|
+
} from './access.js';
|
|
18
|
+
import { AccountPlanError } from './account-plan.js';
|
|
19
|
+
import type { EntitlementBackend, VerifiedEntitlement } from './entitlement.js';
|
|
6
20
|
import type { PurchaseFlow, PurchaseState } from './flow.js';
|
|
7
21
|
import type { Paywall } from './paywall.js';
|
|
8
22
|
|
|
@@ -47,10 +61,52 @@ export function useEntitlement(): VerifiedEntitlement | null {
|
|
|
47
61
|
return usePurchaseState().entitlement;
|
|
48
62
|
}
|
|
49
63
|
|
|
64
|
+
export interface FeatureOptions {
|
|
65
|
+
/** Check the organization's plan from `useOrganizationPlan` instead of the account's. */
|
|
66
|
+
readonly organization?: OrganizationPlanState;
|
|
67
|
+
}
|
|
68
|
+
|
|
50
69
|
/** Access to a feature key from the backend-verified entitlement. See `featureAccess`. */
|
|
51
|
-
export function useFeature(key: string): FeatureAccess {
|
|
70
|
+
export function useFeature(key: string, options: FeatureOptions = {}): FeatureAccess {
|
|
52
71
|
const { productId } = usePurchasesContext();
|
|
53
|
-
|
|
72
|
+
const state = usePurchaseState();
|
|
73
|
+
if (options.organization !== undefined)
|
|
74
|
+
return organizationFeatureAccess(options.organization, key, { productId });
|
|
75
|
+
return featureAccess(state, key, { productId });
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function organizationFailure(error: unknown): OrganizationPlanState {
|
|
79
|
+
if (error instanceof AccountPlanError && error.code === 'organization_not_accessible')
|
|
80
|
+
return { status: 'not_accessible' };
|
|
81
|
+
return { status: 'unavailable' };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Loads the plan of the organization the account acts for. */
|
|
85
|
+
export function useOrganizationPlan(
|
|
86
|
+
backend: Pick<EntitlementBackend, 'organizationEntitlement'>,
|
|
87
|
+
organizationId: string,
|
|
88
|
+
): OrganizationPlanState {
|
|
89
|
+
const [state, setState] = useState<OrganizationPlanState>({ status: 'loading' });
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
let active = true;
|
|
92
|
+
setState({ status: 'loading' });
|
|
93
|
+
const load = backend.organizationEntitlement?.(organizationId) ?? Promise.resolve(null);
|
|
94
|
+
load.then(
|
|
95
|
+
(entitlement) => {
|
|
96
|
+
if (active)
|
|
97
|
+
setState(
|
|
98
|
+
entitlement === null ? { status: 'not_accessible' } : { status: 'ready', entitlement },
|
|
99
|
+
);
|
|
100
|
+
},
|
|
101
|
+
(error: unknown) => {
|
|
102
|
+
if (active) setState(organizationFailure(error));
|
|
103
|
+
},
|
|
104
|
+
);
|
|
105
|
+
return () => {
|
|
106
|
+
active = false;
|
|
107
|
+
};
|
|
108
|
+
}, [backend, organizationId]);
|
|
109
|
+
return state;
|
|
54
110
|
}
|
|
55
111
|
|
|
56
112
|
export interface LockedFeatureProps {
|
|
@@ -61,11 +117,15 @@ export interface LockedFeatureProps {
|
|
|
61
117
|
readonly loading?: ReactNode;
|
|
62
118
|
/** Rendered when the plan cannot be read. Offer a retry, never a purchase. */
|
|
63
119
|
readonly unavailable?: ReactNode;
|
|
120
|
+
readonly organization?: OrganizationPlanState;
|
|
64
121
|
}
|
|
65
122
|
|
|
66
123
|
/** Renders `children` only while the verified entitlement grants `feature`. */
|
|
67
124
|
export function LockedFeature(props: LockedFeatureProps): ReactNode {
|
|
68
|
-
const access = useFeature(
|
|
125
|
+
const access = useFeature(
|
|
126
|
+
props.feature,
|
|
127
|
+
props.organization === undefined ? {} : { organization: props.organization },
|
|
128
|
+
);
|
|
69
129
|
switch (access.status) {
|
|
70
130
|
case 'granted':
|
|
71
131
|
return createElement(Fragment, null, props.children);
|