-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
247 lines (209 loc) · 8.77 KB
/
index.js
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import { DeviceEventEmitter } from "react-native";
import Constants from "./src/constants";
let TapsellPlusNativeModule = require("react-native").NativeModules.RNTapsellPlus;
let callbacks = {};
callbacks[Constants.ON_RESPONSE_EVENT] = {};
callbacks[Constants.ON_NATIVE_RESPONSE_EVENT] = {};
callbacks[Constants.ON_ERROR_EVENT] = {};
callbacks[Constants.ON_OPENED_EVENT] = {};
callbacks[Constants.ON_CLOSED_EVENT] = {};
callbacks[Constants.ON_REWARDED_EVENT] = {};
callbacks[Constants.ON_SHOW_ERROR_EVENT] = {};
const appEventEmitter = DeviceEventEmitter;
appEventEmitter.addListener(Constants.ON_NATIVE_RESPONSE_EVENT, event => {
if (callbacks[Constants.ON_NATIVE_RESPONSE_EVENT][event.response_id])
callbacks[Constants.ON_NATIVE_RESPONSE_EVENT][event.response_id](event);
});
// When errored
appEventEmitter.addListener(Constants.ON_ERROR_EVENT, event => {
if (callbacks[Constants.ON_ERROR_EVENT][event.response_id])
callbacks[Constants.ON_ERROR_EVENT][event.response_id](event);
});
// When opened
appEventEmitter.addListener(Constants.ON_OPENED_EVENT, event => {
if (callbacks[Constants.ON_OPENED_EVENT][event.response_id])
callbacks[Constants.ON_OPENED_EVENT][event.response_id](event);
});
// When closed
appEventEmitter.addListener(Constants.ON_CLOSED_EVENT, event => {
if (callbacks[Constants.ON_CLOSED_EVENT][event.response_id])
callbacks[Constants.ON_CLOSED_EVENT][event.response_id](event);
});
// When rewarded
appEventEmitter.addListener(Constants.ON_REWARDED_EVENT, event => {
if (callbacks[Constants.ON_REWARDED_EVENT][event.response_id])
callbacks[Constants.ON_REWARDED_EVENT][event.response_id](event);
});
/**
* Main class to interact with
*
* **Note**: Every requestSomething method return a {Promise} which contains a `responseId` string
* this responseId must be stored to be passed to show method
* Also, every show* method requires onOpen, onClose, onRewarded and onError callbacks
*/
const TapsellPlus = class {
static initialize(appKey) {
TapsellPlusNativeModule?.initialize(appKey);
}
static setGDPRConsent(consent) {
TapsellPlusNativeModule?.setGDPRConsent(consent)
}
static setDebugMode(logLevel) {
TapsellPlusNativeModule?.setDebugMode(logLevel);
}
/**
*
* @param {string} zoneId
* @returns {Promise} (Resolve with a {string} responseId). Rejects if there's an error
*/
static async requestRewardedVideoAd(zoneId) {
return TapsellPlusNativeModule?.requestRewardedVideoAd(zoneId)
}
/**
*
* @param {string} responseId passed from the requestRewardedVideoAd in it's promise
* @param {function({response_id: string, zone_id: string}):void} onOpened contains {responseId, zoneId}
* @param {function({response_id: string, zone_id: string}):void} onClosed contains {responseId, zoneId}
* @param {function({response_id: string, zone_id: string}):void} onRewarded contains {responseId, zoneId}
* @param {function({response_id: string, zone_id: string, error_message: string}):void} onError contains {responseId, zoneId, errorMessage}
*/
static showRewardedVideoAd(responseId, onOpened, onClosed, onRewarded, onError) {
callbacks[Constants.ON_OPENED_EVENT][responseId] = onOpened
callbacks[Constants.ON_CLOSED_EVENT][responseId] = onClosed
callbacks[Constants.ON_REWARDED_EVENT][responseId] = onRewarded
callbacks[Constants.ON_ERROR_EVENT][responseId] = onError
TapsellPlusNativeModule?.showRewardedVideoAd(responseId);
}
/**
*
* @param {string} zoneId
* @returns {Promise<string>} (Resolve with a {string} responseId). Rejects if there's an error
*/
static async requestInterstitialAd(zoneId) {
return TapsellPlusNativeModule?.requestInterstitialAd(zoneId);
}
/**
*
* @param {string} responseId passed from the requestInterstitialAd in it's promise
* @param {function({response_id: string, zone_id: string}):void} onOpened contains {responseId, zoneId}
* @param {function({response_id: string, zone_id: string}):void} onClosed contains {responseId, zoneId}
* @param {function({response_id: string, zone_id: string, error_message: string}):void} onError contains {responseId, zoneId, errorMessage}
*/
static showInterstitialAd(responseId, onOpened, onClosed, onError) {
callbacks[Constants.ON_OPENED_EVENT][responseId] = onOpened
callbacks[Constants.ON_CLOSED_EVENT][responseId] = onClosed
callbacks[Constants.ON_ERROR_EVENT][responseId] = onError
TapsellPlusNativeModule?.showInterstitialAd(responseId);
}
/**
* Requests a native ad
* @param {string} zoneId
* @returns {Promise<string>} (Resolve with a string as responseId). Rejects if there's an error
*/
static async requestNativeAd(zoneId) {
return TapsellPlusNativeModule?.requestNativeAd(zoneId);
}
/**
*
* @param {string} responseId passed from the requestNativeAd in it's promise
* @param {function(object):void} onOpened contains an object consisting of {ad_id, zone_id, response_id, title, description, call_to_action_text, icon_url, portrait_static_image_url, landscape_static_image_url} **Note**: Keys might not be present
* @param {function({response_id: string, zone_id: string, error_message: string}):void} onError contains {response_id, zone_id, error_message}
*/
static showNativeAd(responseId, onOpened, onError) {
callbacks[Constants.ON_NATIVE_RESPONSE_EVENT][responseId] = onOpened
callbacks[Constants.ON_ERROR_EVENT][responseId] = onError
TapsellPlusNativeModule?.showNativeAd(responseId);
}
/**
*
* @param {string} responseId is the id used to show the native ad
*/
static nativeAdClicked(responseId) {
TapsellPlusNativeModule?.nativeAdClicked(responseId);
}
/**
*
* @param {string} responseId is the id that is used to show the related ad
* @returns {Promise<string>} that resolves with 'true' if destruction was a success, rejects otherwise
*/
static async desntroyNativeAd(responseId) {
return TapsellPlusNativeModule?.destroyNativeAd(responseId);
}
/**
*
* @param {string} zoneId
* @param {TapsellPlusBannerType} bannerType
* @returns {Promise<string>} (Resolve with a {string} responseId). Rejects if there's an error
*/
static requestStandardBannerAd(zoneId, bannerType) {
return TapsellPlusNativeModule?.requestStandardBannerAd(zoneId, bannerType);
}
/**
*
* @param {string} responseId passed from the requestStandardBannerMathod in it's promise
* @param {TapsellPlusHorizontalGravity} horizontalGravity is the position of ad in the page
* @param {TapsellPlusVerticalGravity} verticalGravity is the position of ad in the page
* @param {function({response_id: string, zone_id: string}):void} onOpened contains {responseId, zoneId}
* @param {function({response_id: string, zone_id: string, error_message: string}):void} onError contains {responseId, zoneId, errorMessage}
*/
static showStandardBannerAd(responseId, horizontalGravity, verticalGravity, onOpened, onError) {
callbacks[Constants.ON_OPENED_EVENT][responseId] = onOpened
callbacks[Constants.ON_ERROR_EVENT][responseId] = onError
TapsellPlusNativeModule?.showStandardBannerAd(responseId, horizontalGravity, verticalGravity);
}
/**
* Attempts to destroy the banner that is already being shown
* Note: No effect if no banner is shown
* @param {string} responseId is used to destroy the banner with this id
* @returns {Promise<string>} that resolved with 'true' if destruction was a success, otherwise rejects with the error
*/
static async destroyStandardBannerAd(responseId) {
return TapsellPlusNativeModule?.destroyStandardBannerAd(responseId);
}
/**
* Attempts to display the banner that was hidden before
* > **Note** This method does not request for a new ad and sets the banner visibility to View.VISIBLE
*/
static displayStandardBanner() {
TapsellPlusNativeModule?.displayStandardBanner();
}
/**
* Attempts to hide the banner that is visible
* > **Note** This method does not remove the request result and just sets the banner visibility to View.GONE
*/
static hideStandardBanner() {
TapsellPlusNativeModule?.hideStandardBanner();
}
}
/**
* Maps an integer into native enum TapsellPlusBannerType
* Used to limit inputs of methods
*/
const TapsellPlusBannerType = {
BANNER_320x50: 1,
BANNER_320x100: 2,
BANNER_250x250: 3,
BANNER_300x250: 4,
BANNER_468x60: 5,
BANNER_728x90: 6
}
/**
* Maps an integer into native horizontal gravity
* Used to limit inputs of methods
*/
const TapsellPlusHorizontalGravity = {
TOP: 1,
CENTER: 5,
BOTTOM: 2
}
/**
* Maps an integer into native vertical gravity
* Used to limit inputs of methods
*/
const TapsellPlusVerticalGravity = {
LEFT: 3,
RIGHT: 4,
CENTER: 5
}
export default TapsellPlus
export { TapsellPlus, TapsellPlusBannerType, TapsellPlusVerticalGravity, TapsellPlusHorizontalGravity };