-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfoundationService.ts
More file actions
135 lines (106 loc) · 3.33 KB
/
foundationService.ts
File metadata and controls
135 lines (106 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { Platform } from 'react-native';
import {
Foundation,
ComapiConfig,
InterfaceContainer,
Environment
} from "@comapi/sdk-js-foundation";
import AuthService from "./authService";
import Config from "./config";
import AsyncStorageData from "./asyncStorageData"
// https://stackoverflow.com/questions/42829838/react-native-atob-btoa-not-working-without-remote-js-debugging
import {decode, encode} from 'base-64';
if (!global.btoa) {
global.btoa = encode;
}
if (!global.atob) {
global.atob = decode;
}
class FoundationService {
static sdk: Foundation;
static initialise() {
if (this.sdk === undefined) {
// Create and initialize the interface container
// This is internally used for dependency injection (IOC)
var interfaceContainer = new InterfaceContainer();
interfaceContainer.initialise();
let comapiConfig = new ComapiConfig()
.withApiSpace(Config.apiSpaceId)
// NOTE: authChallenge needs to call internal methods hence the bind
.withAuthChallenge(AuthService.authChallenge.bind(AuthService))
.withLogLevel(3);
// Mechanism to enable this container to be used whilst bootstrapping library
comapiConfig.interfaceContainer = interfaceContainer;
interfaceContainer.bindComapiConfig(comapiConfig);
// Create instance of alternative LocalStorageData service and put it in the container
let asyncStorageData = new AsyncStorageData();
interfaceContainer.setInterface("LocalStorageData", asyncStorageData);
// Initialise as normal
return Foundation.initialise(comapiConfig)
.then(sdk => {
this.sdk = sdk;
console.log("foundation interface created");
return sdk;
})
.catch(error => {
console.error("initialise failed", error);
throw error;
});
} else {
return Promise.resolve(this.sdk);
}
}
/**
* Starts a session
*/
static startSession() {
return this.sdk.startSession();
}
/**
* Ends the session
*/
static endSession() {
return this.sdk.endSession();
}
/**
* Sets the native push tokens for the device
* @param token
*/
static setPushDetails(token) {
if (Platform.OS === 'ios') {
console.log('I am an iOS device!');
return this.sdk.device.setAPNSPushDetails(Config.bundleId, Config.environment, token)
.then(result => {
console.log("APNS registration succeeded", result);
return result;
})
.catch(error => {
console.error("APNS registration failed", error);
});
} else if (Platform.OS === 'android') {
console.log('I am an Android device!');
return this.sdk.device.setFCMPushDetails(Config.packageId, token)
.then(result => {
console.log("FCM registration succeeded", result);
return result;
})
.catch(error => {
console.error("FCM registration failed", error);
});
}
}
/**
* Remove push tokens to prevent push notifications
*/
static removePushDetails() {
return this.sdk.device.removePushDetails();
}
/**
* Returns the SDKs user profile details
* @returns The SDKs user profile details
*/
static getProfile(){
return this.sdk.services.profile.getMyProfile();
}
}
export default FoundationService;