mirror of
https://github.com/bitwarden/browser
synced 2025-12-22 03:03:43 +00:00
convert appid service to ts
This commit is contained in:
31
src/services/appId.service.ts
Normal file
31
src/services/appId.service.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import UtilsService from './utils.service';
|
||||
|
||||
export default class AppIdService {
|
||||
static getAppId(): Promise<string> {
|
||||
return AppIdService.makeAndGetAppId('appId');
|
||||
}
|
||||
|
||||
static getAnonymousAppId(): Promise<string> {
|
||||
return AppIdService.makeAndGetAppId('anonymousAppId');
|
||||
}
|
||||
|
||||
private static async makeAndGetAppId(key: string) {
|
||||
const existingId = await UtilsService.getObjFromStorage<string>(key);
|
||||
if(existingId != null) {
|
||||
return existingId;
|
||||
}
|
||||
|
||||
const guid = UtilsService.newGuid();
|
||||
await UtilsService.saveObjToStorage(key, guid);
|
||||
return guid;
|
||||
}
|
||||
|
||||
// TODO: remove these in favor of static methods
|
||||
getAppId(): Promise<string> {
|
||||
return AppIdService.getAppId();
|
||||
}
|
||||
|
||||
getAnonymousAppId(): Promise<string> {
|
||||
return AppIdService.getAnonymousAppId();
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,15 @@ const AnalyticsIds = {
|
||||
};
|
||||
|
||||
export default class UtilsService {
|
||||
// ref: http://stackoverflow.com/a/2117523/1090359
|
||||
static newGuid(): string {
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
||||
const r = Math.random() * 16 | 0;
|
||||
const v = c === 'x' ? r : (r & 0x3 | 0x8);
|
||||
return v.toString(16);
|
||||
});
|
||||
}
|
||||
|
||||
static extendObject(...objects: any[]): any {
|
||||
for (let i = 1; i < objects.length; i++) {
|
||||
for (const key in objects[i]) {
|
||||
|
||||
Reference in New Issue
Block a user