@stigg/js-client-sdk 0.20.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.
@@ -0,0 +1,171 @@
1
+ import { AccessDeniedReason, BillingModel, BillingPeriod, EntitlementResetPeriod, MonthlyAccordingTo, PromotionalEntitlementStatus, SubscriptionStatus, TrialPeriodUnits, WeeklyAccordingTo } from './api/generated/types';
2
+ export { BillingPeriod, BillingModel, ErrorCode, AccessDeniedReason, EntitlementResetPeriod, TrialPeriodUnits, } from './api/generated/types';
3
+ export declare enum FeatureType {
4
+ Boolean = "BOOLEAN",
5
+ Number = "NUMBER"
6
+ }
7
+ export declare enum MeterType {
8
+ Fluctuating = "Fluctuating",
9
+ Incremental = "Incremental",
10
+ None = "None"
11
+ }
12
+ export declare type BaseEntitlement = {
13
+ isValid: boolean;
14
+ isGranted: boolean;
15
+ featureType?: FeatureType;
16
+ featureDisplayName?: string;
17
+ featureDescription?: string | null;
18
+ accessDeniedReason?: AccessDeniedReason | null;
19
+ featureId?: string | null;
20
+ };
21
+ export interface FallbackEntitlement extends BaseEntitlement {
22
+ isValid: false;
23
+ }
24
+ export interface InvalidEntitlement extends BaseEntitlement {
25
+ isValid: false;
26
+ isGranted: false;
27
+ accessDeniedReason: AccessDeniedReason.FeatureNotFound | AccessDeniedReason.CustomerNotFound | AccessDeniedReason.Unknown;
28
+ }
29
+ export interface ValidEntitlement extends BaseEntitlement {
30
+ isValid: true;
31
+ }
32
+ export interface BooleanEntitlement extends ValidEntitlement {
33
+ featureType: FeatureType.Boolean;
34
+ }
35
+ export interface NumberEntitlement extends ValidEntitlement {
36
+ featureType: FeatureType.Number;
37
+ usageLimit: number;
38
+ featureUnits?: string;
39
+ featureUnitsPlural?: string;
40
+ hasUnlimitedUsage: boolean;
41
+ }
42
+ export interface MeteredEntitlement extends NumberEntitlement {
43
+ featureType: FeatureType.Number;
44
+ meterType: MeterType.Fluctuating | MeterType.Incremental | MeterType.None;
45
+ currentUsage: number;
46
+ requestedUsage: number | null;
47
+ resetPeriod?: EntitlementResetPeriod | null;
48
+ nextResetDate?: any | null;
49
+ resetPeriodConfiguration?: {
50
+ __typename: 'MonthlyResetPeriodConfig';
51
+ monthlyAccordingTo?: MonthlyAccordingTo | null | undefined;
52
+ } | {
53
+ __typename: 'WeeklyResetPeriodConfig';
54
+ weeklyAccordingTo?: WeeklyAccordingTo | null | undefined;
55
+ } | null;
56
+ }
57
+ export declare type Entitlement = FallbackEntitlement | InvalidEntitlement | BooleanEntitlement | NumberEntitlement | MeteredEntitlement;
58
+ export declare const INVALID_ENTITLEMENT: InvalidEntitlement;
59
+ export declare type PackageEntitlement = {
60
+ usageLimit: number;
61
+ featureType: FeatureType;
62
+ featureMeterType?: MeterType | null;
63
+ featureUnits?: string | null;
64
+ featureUnitsPlural?: string | null;
65
+ featureDisplayName: string;
66
+ featureDescription?: string | null;
67
+ featureId: string;
68
+ hasUnlimitedUsage?: boolean | null;
69
+ resetPeriod?: EntitlementResetPeriod | null;
70
+ };
71
+ export declare type PromotionalEntitlement = {
72
+ usageLimit: number;
73
+ featureType: FeatureType;
74
+ featureMeterType?: MeterType | null;
75
+ featureUnits?: string | null;
76
+ featureUnitsPlural?: string | null;
77
+ featureDisplayName: string;
78
+ featureDescription?: string | null;
79
+ featureId: string;
80
+ hasUnlimitedUsage?: boolean | null;
81
+ resetPeriod?: EntitlementResetPeriod | null;
82
+ status: PromotionalEntitlementStatus;
83
+ endDate?: Date;
84
+ isVisible: boolean;
85
+ };
86
+ export declare type Price = {
87
+ billingModel: BillingModel;
88
+ billingPeriod: BillingPeriod;
89
+ amount: number;
90
+ currency: string;
91
+ feature?: {
92
+ featureUnits?: string | null | undefined;
93
+ featureUnitsPlural?: string | null | undefined;
94
+ displayName: string;
95
+ } | null;
96
+ };
97
+ export declare type BasePlan = {
98
+ displayName: string;
99
+ id: string;
100
+ };
101
+ export declare type Plan = {
102
+ id: string;
103
+ displayName: string;
104
+ description?: string | null;
105
+ order: number;
106
+ basePlan?: BasePlan;
107
+ entitlements: PackageEntitlement[];
108
+ inheritedEntitlements: PackageEntitlement[];
109
+ prices: Price[];
110
+ defaultTrialConfig?: DefaultTrialConfig | null;
111
+ compatibleAddons: Addon[];
112
+ };
113
+ export declare type DefaultTrialConfig = {
114
+ duration: number;
115
+ units: TrialPeriodUnits;
116
+ };
117
+ export declare type EntitlementOptions = {
118
+ requestedUsage?: number;
119
+ shouldTrack?: boolean;
120
+ fallbackGrantedValue?: boolean;
121
+ };
122
+ export declare type FetchEntitlement = {
123
+ customerId: string;
124
+ featureId: string;
125
+ options?: EntitlementOptions;
126
+ };
127
+ export declare type Addon = {
128
+ id: string;
129
+ displayName: string;
130
+ description: string;
131
+ entitlements: PackageEntitlement[];
132
+ };
133
+ export declare type SubscriptionAddon = {
134
+ quantity: number;
135
+ addon: Addon;
136
+ };
137
+ export declare type Subscription = {
138
+ id: string;
139
+ status: SubscriptionStatus;
140
+ plan: Plan;
141
+ price: Price | null;
142
+ addons: SubscriptionAddon[];
143
+ billingId?: string;
144
+ crmId?: string;
145
+ startDate: Date;
146
+ endDate?: Date;
147
+ };
148
+ export declare type Customer = {
149
+ id: string;
150
+ name?: string;
151
+ email?: string;
152
+ createdAt: Date;
153
+ updatedAt: Date;
154
+ subscriptions: Subscription[];
155
+ promotionalEntitlements: PromotionalEntitlement[];
156
+ getActiveSubscriptions(): Subscription[];
157
+ getActivePromotionalEntitlements(): PromotionalEntitlement[];
158
+ getActiveTrials(): Subscription[];
159
+ };
160
+ declare type CustomerState = Omit<Customer, 'getActiveSubscriptions' | 'getActivePromotionalEntitlements' | 'getActiveTrials'>;
161
+ export declare class CustomerModel implements Customer {
162
+ id: string;
163
+ createdAt: Date;
164
+ updatedAt: Date;
165
+ subscriptions: Subscription[];
166
+ promotionalEntitlements: PromotionalEntitlement[];
167
+ constructor(state: CustomerState);
168
+ getActiveSubscriptions(): Subscription[];
169
+ getActiveTrials(): Subscription[];
170
+ getActivePromotionalEntitlements(): PromotionalEntitlement[];
171
+ }
@@ -0,0 +1,9 @@
1
+ import CachedEntitlement from './cachedEntitlement';
2
+ export interface CacheService {
3
+ isLoaded: boolean;
4
+ setEntitlements(entitlements: Map<string, CachedEntitlement>): void;
5
+ getEntitlement(featureId: string): CachedEntitlement | null;
6
+ getEntitlements(): Map<string, CachedEntitlement>;
7
+ validateFeature(featureId: string): boolean;
8
+ setFeatures(features: string[]): void;
9
+ }
@@ -0,0 +1,24 @@
1
+ import { EntitlementResetPeriod, FeatureType, MeterType } from '../models';
2
+ import { ResetPeriodConfigurationFragment } from '../api/generated/types';
3
+ export interface CalculatedEntitlement {
4
+ featureId: string;
5
+ featureType: FeatureType;
6
+ usageLimit?: number | null;
7
+ featureUnits?: string;
8
+ featureUnitsPlural?: string;
9
+ featureDisplayName?: string;
10
+ featureDescription?: string | null;
11
+ hasUnlimitedUsage: boolean;
12
+ meterType: MeterType.Fluctuating | MeterType.Incremental | MeterType.None;
13
+ }
14
+ export interface EntitlementUsage {
15
+ currentUsage: number;
16
+ resetPeriod?: EntitlementResetPeriod | null;
17
+ nextResetDate?: any | null;
18
+ resetPeriodConfiguration?: ResetPeriodConfigurationFragment | null;
19
+ }
20
+ export default class CachedEntitlement {
21
+ readonly calculatedEntitlement: CalculatedEntitlement;
22
+ readonly featureUsage: EntitlementUsage;
23
+ constructor(calculatedEntitlement: CalculatedEntitlement, featureUsage: EntitlementUsage);
24
+ }
@@ -0,0 +1,9 @@
1
+ import CachedEntitlement from './cachedEntitlement';
2
+ import { AccessDeniedReason } from '../api/generated/types';
3
+ export declare class EntitlementDecisionService {
4
+ static decideEntitlementPolicy(entitlement?: CachedEntitlement | null, requestUsage?: number): Decision;
5
+ }
6
+ export interface Decision {
7
+ readonly isGranted: boolean;
8
+ readonly accessDeniedReason?: AccessDeniedReason;
9
+ }
@@ -0,0 +1,21 @@
1
+ import { CacheService } from './cacheService';
2
+ import { ApolloClient, NormalizedCacheObject } from '@apollo/client/core';
3
+ import { Entitlement, InvalidEntitlement } from '../models';
4
+ import { Decision } from './entitlementDecisionService';
5
+ import CachedEntitlement from './cachedEntitlement';
6
+ export declare class EntitlementsService {
7
+ private readonly customerId;
8
+ private readonly cacheService;
9
+ private readonly entitlementsApi;
10
+ private readonly modelMapper;
11
+ private loadingEntitlements;
12
+ constructor(customerId: string, cacheService: CacheService, client: ApolloClient<NormalizedCacheObject>);
13
+ getEntitlement<T extends Entitlement>(featureRefId: string, requestedUsage?: number): T | InvalidEntitlement;
14
+ loadEntitlements(): Promise<void>;
15
+ private doLoadEntitlements;
16
+ getEntitlements(): Promise<Entitlement[]>;
17
+ }
18
+ export declare type GetEntitlementResult = {
19
+ entitlement: CachedEntitlement | null;
20
+ decision: Decision;
21
+ };
@@ -0,0 +1,13 @@
1
+ import { CacheService } from './cacheService';
2
+ import CachedEntitlement from './cachedEntitlement';
3
+ export declare class InMemoryCacheService implements CacheService {
4
+ private entitlements;
5
+ isLoaded: boolean;
6
+ private features;
7
+ constructor();
8
+ getEntitlement(featureId: string): CachedEntitlement | null;
9
+ validateFeature(featureId: string): boolean;
10
+ setFeatures: (features: string[]) => Set<string>;
11
+ setEntitlements(entitlements: Map<string, CachedEntitlement>): void;
12
+ getEntitlements(): Map<string, CachedEntitlement>;
13
+ }
@@ -0,0 +1,21 @@
1
+ import { CustomerFragment, EntitlementFragment, GetPlansQuery, SlimEntitlementFragment } from '../api/generated/types';
2
+ import { Customer, Entitlement, Plan } from '../models';
3
+ import CachedEntitlement from '../services/cachedEntitlement';
4
+ import { Decision } from '../services/entitlementDecisionService';
5
+ export declare class ModelMapper {
6
+ mapEntitlementByDecision: (decision: Decision, featureId: string, entitlement: CachedEntitlement, requestedUsage?: number | undefined) => Entitlement;
7
+ mapSlimEntitlement(entitlement: SlimEntitlementFragment): Entitlement;
8
+ mapFullEntitlementByDecision(entitlement: CachedEntitlement, decision: Decision, customerId: string, requestedUsage?: number): Entitlement;
9
+ mapFullEntitlement(fullEntitlement: EntitlementFragment): Entitlement;
10
+ mapCachedEntitlement(entitlement: EntitlementFragment, currentUsage?: number, nextResetDate?: number | null): CachedEntitlement;
11
+ mapCachedEntitlements(updatedEntitlements: EntitlementFragment[]): Map<string, CachedEntitlement>;
12
+ mapPlans(resultData: GetPlansQuery): Plan[];
13
+ private mapPrice;
14
+ private mapPlan;
15
+ mapCustomer(customer: CustomerFragment): Customer;
16
+ private mapPackageEntitlement;
17
+ private mapPromotionalEntitlement;
18
+ private mapSubscription;
19
+ private mapAddon;
20
+ private sortPlans;
21
+ }
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "@stigg/js-client-sdk",
3
+ "version": "0.20.0",
4
+ "description": "Stigg client-side SDK for Browser",
5
+ "main": "dist/index.js",
6
+ "typings": "dist/index.d.ts",
7
+ "repository": "https://github.com/stiggio/js-client-sdk.git",
8
+ "license": "MIT",
9
+ "scripts": {
10
+ "start": "webpack serve",
11
+ "build:types": "tsc --emitDeclarationOnly",
12
+ "build": "yarn build:types && webpack",
13
+ "build:watch": "webpack --watch",
14
+ "build:prod": "yarn build:types && webpack --env production",
15
+ "generate": "graphql-codegen && yarn run fix:lint",
16
+ "fix": "run-s fix:*",
17
+ "fix:prettier": "prettier \"src/**/*.ts\" --write",
18
+ "fix:lint": "eslint src --ext .ts --fix",
19
+ "test": "jest",
20
+ "test:watch": "jest --watch",
21
+ "test:cov": "jest --coverage",
22
+ "prepare": "husky install"
23
+ },
24
+ "files": [
25
+ "dist/**/*"
26
+ ],
27
+ "dependencies": {
28
+ "@apollo/client": "^3.4.17",
29
+ "graphql": "^16.0.1",
30
+ "husky": "^7.0.4",
31
+ "cross-fetch": "^3.1.4",
32
+ "lint-staged": "^12.0.2",
33
+ "lodash": "^4.17.21"
34
+ },
35
+ "devDependencies": {
36
+ "@commitlint/cli": "^14.1.0",
37
+ "@commitlint/config-conventional": "^14.1.0",
38
+ "@graphql-codegen/cli": "^2.3.0",
39
+ "@graphql-codegen/fragment-matcher": "^3.2.1",
40
+ "@graphql-codegen/schema-ast": "^2.4.0",
41
+ "@graphql-codegen/typescript": "^2.4.0",
42
+ "@graphql-codegen/typescript-operations": "^2.2.0",
43
+ "@types/jest": "^27.0.2",
44
+ "@types/lodash": "^4.14.177",
45
+ "@typescript-eslint/eslint-plugin": "^5.4.0",
46
+ "@typescript-eslint/parser": "^5.4.0",
47
+ "eslint": "^8.2.0",
48
+ "eslint-config-prettier": "^8.3.0",
49
+ "eslint-plugin-prettier": "^4.0.0",
50
+ "jest": "^27.3.1",
51
+ "jest-fetch-mock": "^3.0.3",
52
+ "npm-run-all": "^4.1.5",
53
+ "prettier": "^2.4.1",
54
+ "ts-jest": "^27.0.7",
55
+ "ts-loader": "^9.2.6",
56
+ "typescript": "^4.5.3",
57
+ "msw": "^0.35.0",
58
+ "webpack": "^5.64.1",
59
+ "webpack-cli": "^4.9.1",
60
+ "webpack-dev-server": "^4.5.0"
61
+ },
62
+ "lint-staged": {
63
+ "*.{js,jsx,ts,tsx}": [
64
+ "prettier --check",
65
+ "eslint --cache"
66
+ ]
67
+ }
68
+ }