Skip to content

Commit f23cd63

Browse files
committed
docs: update documentation
1 parent f046492 commit f23cd63

File tree

12 files changed

+783
-110
lines changed

12 files changed

+783
-110
lines changed

WeChat/FPUser.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* You can obtainan a client of FPUser,
3+
* which provides all of the FPUser's functionality.
4+
*/
15
export class FPUser {
26
private key: string;
37
private attrs: { [key: string]: string };
@@ -7,30 +11,70 @@ export class FPUser {
711
this.attrs = {};
812
}
913

14+
/**
15+
* Add an attribute to the FPUser client.
16+
*
17+
* @param attrName
18+
* The attribute key.
19+
* @param attrValue
20+
* The attribute value.
21+
*
22+
*/
1023
public with(attrName: string, attrValue: string): FPUser {
1124
this.attrs[attrName] = attrValue;
1225
return this;
1326
}
1427

28+
/**
29+
* Get the unique key of the FPUser client.
30+
*/
1531
public getKey(): string {
1632
return this.key;
1733
}
1834

35+
/**
36+
* Get all attributes of the FPUser client.
37+
*
38+
* @param key
39+
* The uqique FPUser key.
40+
*
41+
*/
1942
public getAttrs(): { [key: string]: string } {
2043
return Object.assign({}, this.attrs);
2144
}
2245

46+
/**
47+
* Extend several attributes of the FPUser client.
48+
*
49+
* @param attrs
50+
* key-value pairs.
51+
*
52+
*/
2353
public extendAttrs(attrs: { [key: string]: string }): FPUser {
2454
for (const key in attrs) {
2555
this.attrs[key] = attrs[key];
2656
}
2757
return this;
2858
}
2959

60+
/**
61+
* Get attribute value of a specified attribute key.
62+
*
63+
* @param attrName
64+
* The attribute key.
65+
*
66+
*/
3067
public get(attrName: string): string | undefined {
3168
return this.attrs[attrName];
3269
}
3370

71+
/**
72+
* Change a stable key for the FPUser client.
73+
*
74+
* @param key
75+
* The uqique FPUser key.
76+
*
77+
*/
3478
public stableRollout(key: string): FPUser {
3579
this.key = key;
3680
return this;

WeChat/FeatureProbe.ts

Lines changed: 116 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Base64 } from "js-base64";
33
import wefetch from "wefetch";
44
import { FPUser } from "./FPUser";
55
import StorageProvider from "./localstorage";
6-
import { FPToggleDetail, IParams, IOption, IStorageProvider } from "./types";
6+
import { FPToggleDetail, IParams, FPOptions, FPStorageProvider } from "./types";
77

88
const PKG_VERSION = "SDK_VERSION";
99
const UA = "WECHAT_MINIPROGRAM/" + PKG_VERSION;
@@ -22,6 +22,10 @@ const EVENTS = {
2222
CACHE_READY: "cache_ready"
2323
};
2424

25+
/**
26+
* You can obtainan a client of FeatureProbe,
27+
* which provides access to all of the SDK's functionality.
28+
*/
2529
class FeatureProbe extends TinyEmitter {
2630
private togglesUrl: string ;
2731
private eventsUrl: string;
@@ -33,7 +37,7 @@ class FeatureProbe extends TinyEmitter {
3337
private timeoutTimer?: NodeJS.Timeout;
3438
private readyPromise: null | Promise<void>;
3539
private status: string;
36-
private storage: IStorageProvider;
40+
private storage: FPStorageProvider;
3741
private timeoutInterval: number;
3842

3943
constructor() {
@@ -51,6 +55,9 @@ class FeatureProbe extends TinyEmitter {
5155
this.readyPromise = null;
5256
}
5357

58+
/**
59+
* Initialize the FeatureProbe client.
60+
*/
5461
public init({
5562
remoteUrl,
5663
togglesUrl,
@@ -59,7 +66,7 @@ class FeatureProbe extends TinyEmitter {
5966
user,
6067
refreshInterval = 1000,
6168
timeoutInterval = 10000,
62-
}: IOption) {
69+
}: FPOptions) {
6370
if (!clientSdkKey) {
6471
throw new Error("clientSdkKey is required");
6572
}
@@ -87,6 +94,9 @@ class FeatureProbe extends TinyEmitter {
8794
this.timeoutInterval = timeoutInterval;
8895
}
8996

97+
/**
98+
* Start the FeatureProbe client.
99+
*/
90100
public async start() {
91101
this.timeoutTimer = setTimeout(() => {
92102
if (this.status === STATUS.PENDING) {
@@ -108,13 +118,23 @@ class FeatureProbe extends TinyEmitter {
108118
}
109119
}
110120

121+
/**
122+
* Stop the FeatureProbe client, once the client has been stopped,
123+
* SDK will no longer listen for toggle changes or send metrics to Server.
124+
*/
111125
public stop() {
112126
clearInterval(this.timer);
113127
clearTimeout(this.timeoutTimer);
114128
this.timeoutTimer = undefined;
115129
this.timer = undefined;
116130
}
117131

132+
/**
133+
* Returns a Promise which tracks the client's ready state.
134+
*
135+
* The Promise will be resolved if the client successfully get toggles from the server
136+
* or ejected if client error get toggles from the server until `timeoutInterval` countdown reaches.
137+
*/
118138
public waitUntilReady(): Promise<void> {
119139
if (this.readyPromise) {
120140
return this.readyPromise;
@@ -146,50 +166,140 @@ class FeatureProbe extends TinyEmitter {
146166
return this.readyPromise;
147167
}
148168

169+
/**
170+
* Determines the return `boolean` value of a toggle for the current user.
171+
*
172+
*
173+
* @param key
174+
* The unique key of the toggle.
175+
* @param defaultValue
176+
* The default value of the toggle, to be used if the value is not available from FeatureProbe.
177+
*/
149178
public boolValue(key: string, defaultValue: boolean): boolean {
150179
return this.toggleValue(key, defaultValue, "boolean");
151180
}
152181

182+
/**
183+
* Determines the return `number` value of a toggle for the current user.
184+
*
185+
*
186+
* @param key
187+
* The unique key of the toggle.
188+
* @param defaultValue
189+
* The default value of the toggle, to be used if the value is not available from FeatureProbe.
190+
*/
153191
public numberValue(key: string, defaultValue: number): number {
154192
return this.toggleValue(key, defaultValue, "number");
155193
}
156194

195+
/**
196+
* Determines the return `string` value of a toggle for the current user.
197+
*
198+
*
199+
* @param key
200+
* The unique key of the toggle.
201+
* @param defaultValue
202+
* The default value of the toggle, to be used if the value is not available from FeatureProbe.
203+
*/
157204
public stringValue(key: string, defaultValue: string): string {
158205
return this.toggleValue(key, defaultValue, "string");
159206
}
160207

208+
/**
209+
* Determines the return `json` value of a toggle for the current user.
210+
*
211+
*
212+
* @param key
213+
* The unique key of the toggle.
214+
* @param defaultValue
215+
* The default value of the toggle, to be used if the value is not available from FeatureProbe.
216+
*/
161217
public jsonValue(key: string, defaultValue: object): object {
162218
return this.toggleValue(key, defaultValue, "object");
163219
}
164220

221+
/**
222+
* Determines the return `boolean` value of a toggle for the current user, along with information about how it was calculated.
223+
*
224+
*
225+
* @param key
226+
* The unique key of the toggle.
227+
* @param defaultValue
228+
* The default value of the toggle, to be used if the value is not available from FeatureProbe.
229+
*/
165230
public boolDetail(key: string, defaultValue: boolean): FPToggleDetail {
166231
return this.toggleDetail(key, defaultValue, "boolean");
167232
}
168233

234+
/**
235+
* Determines the return `number` value of a toggle for the current user, along with information about how it was calculated.
236+
*
237+
*
238+
* @param key
239+
* The unique key of the toggle.
240+
* @param defaultValue
241+
* The default value of the toggle, to be used if the value is not available from FeatureProbe.
242+
*/
169243
public numberDetail(key: string, defaultValue: number): FPToggleDetail {
170244
return this.toggleDetail(key, defaultValue, "number");
171245
}
172246

247+
/**
248+
* Determines the return `string` value of a toggle for the current user, along with information about how it was calculated.
249+
*
250+
*
251+
* @param key
252+
* The unique key of the toggle.
253+
* @param defaultValue
254+
* The default value of the toggle, to be used if the value is not available from FeatureProbe.
255+
*/
173256
public stringDetail(key: string, defaultValue: string): FPToggleDetail {
174257
return this.toggleDetail(key, defaultValue, "string");
175258
}
176259

260+
/**
261+
* Determines the return `json` value of a toggle for the current user, along with information about how it was calculated.
262+
*
263+
*
264+
* @param key
265+
* The unique key of the toggle.
266+
* @param defaultValue
267+
* The default value of the toggle, to be used if the value is not available from FeatureProbe.
268+
*/
177269
public jsonDetail(key: string, defaultValue: object): FPToggleDetail {
178270
return this.toggleDetail(key, defaultValue, "object");
179271
}
180272

273+
/**
274+
* Returns an object of all available toggles' details to the current user.
275+
*/
181276
public allToggles(): { [key: string]: FPToggleDetail } | undefined {
182277
return Object.assign({}, this.toggles);
183278
}
184279

280+
/**
281+
* Returns the current user.
282+
*
283+
* This is the user that was most recently passed to [[identifyUser]], or, if [[identifyUser]] has never
284+
* been called, the initial user specified when the client was created.
285+
*/
185286
public getUser(): FPUser {
186287
return this.user;
187288
}
188289

290+
/**
291+
* Changing the current user to FeatureProbe.
292+
*
293+
* @param user
294+
* A new FPUser instance.
295+
*/
189296
public identifyUser(user: FPUser) {
190297
this.user = user;
191298
}
192299

300+
/**
301+
* Logout the current user, change the current user to an anonymous user.
302+
*/
193303
public logout() {
194304
const user = new FPUser();
195305
this.identifyUser(user);
@@ -380,6 +490,9 @@ class FeatureProbe extends TinyEmitter {
380490
}
381491
}
382492

493+
/**
494+
* The client of the FeatureProbe.
495+
*/
383496
const featureProbeClient = new FeatureProbe();
384497

385498
export { FeatureProbe, featureProbeClient };

WeChat/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { FPUser } from "./FPUser";
22
import { featureProbeClient, FeatureProbe } from "./FeatureProbe";
3+
import { FPToggleDetail, FPStorageProvider, FPOptions } from "./types";
34

4-
export { FPUser, FeatureProbe, featureProbeClient };
5+
export { FPUser, FeatureProbe, featureProbeClient, FPToggleDetail, FPStorageProvider, FPOptions };
56

67
declare global {
78
let App: (config?: any) => any;

0 commit comments

Comments
 (0)