@gapi/onesignal-notifications 1.8.122

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.
package/.prettierrc ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "singleQuote": true,
3
+ "printWidth": 80
4
+ }
package/README.md ADDED
@@ -0,0 +1,108 @@
1
+ # @gapi/onesignal-notifications
2
+
3
+ #### @Gapi OneSignal Notifications module @StrongTyped forked and re-written with typescript from [onesignal-node](https://github.com/KolektifLabs/onesignal-node)
4
+
5
+ ##### For questions/issues you can write ticket [here](http://gitlab.youvolio.com/gapi/onesignal-notifications/issues)
6
+ ##### This module is intended to be used with [GAPI](https://github.com/Stradivario/gapi)
7
+
8
+ ## Installation and basic examples:
9
+ ##### To install this Gapi module, run:
10
+
11
+ ```bash
12
+ $ npm install @gapi/onesignal-notifications
13
+ ```
14
+
15
+ ## Consuming @gapi/onesignal-notifications
16
+
17
+ ##### Import inside AppModule or CoreModule
18
+ ```typescript
19
+
20
+ import { Module } from '@rxdi/core';
21
+ import { OneSignalModule } from '@gapi/onesignal-notifications';
22
+
23
+ @Module({
24
+ imports: [
25
+ OneSignalModule.forRoot({
26
+ userAuthKey: 'ZmY2YjVkMjMtMjY0OC00Y2E2LTkxBTQtYTVmOWY1MmJhZDg1',
27
+ app: {
28
+ appAuthKey: 'MTa4NGIzNjQtNGFkMy00MzY4AWJjZTctNzNjYzYyODgzZDhh',
29
+ appId: 'd856cd4h-f834-42cb-b541-22ee20bcf499'
30
+ }
31
+ })
32
+ ],
33
+ services: [NotificationService],
34
+ effects: [YourCustomEffects]
35
+ })
36
+ export class CoreModule { }
37
+ ```
38
+
39
+ ##### Create NotificationService
40
+
41
+ ```typescript
42
+
43
+ import { Service } from '@rxdi/core';
44
+ import { PurchasesType } from '../../../purchases/types/purchases.type';
45
+ import { OneSignalClientService, Notification } from '@gapi/onesignal-notifications';
46
+
47
+ @Service()
48
+ export class NotificationService {
49
+
50
+ constructor(
51
+ private client: OneSignalClientService
52
+ ) { }
53
+
54
+ async createNotification(purchaseData: PurchasesType) {
55
+ const notification: Notification = new Notification({
56
+ contents: {
57
+ en: 'Test notification',
58
+ tr: 'Test mesajı',
59
+ bg: 'Съобщение за проба'
60
+ }
61
+ });
62
+ notification.setTargetDevices(['b188dd55-7c70-4072-b696-8b66a56f9c4c']);
63
+ notification.setParameter('data', { type: 'notification-created', data: {} });
64
+ return await this.client.sendNotification(notification);
65
+ }
66
+
67
+ async sendNotification() {
68
+ const firstNotification = new Notification({
69
+ contents: {
70
+ en: 'Test notification',
71
+ tr: 'Test mesajı',
72
+ bg: 'Съобщение за проба'
73
+ }
74
+ });
75
+ firstNotification.setTargetDevices(['b188dd55-7c70-4072-b696-8b66a56f9c4c']);
76
+ firstNotification.setParameter('data', { 'abc': '123', 'foo': 'bar' });
77
+ return await this.client.sendNotification(firstNotification);
78
+ }
79
+
80
+ }
81
+
82
+ ```
83
+
84
+ ##### Then use it inside your Gapi Application for example inside Effects:
85
+
86
+ ```typescript
87
+
88
+ import { OfType, Effect } from '@rxdi/core';
89
+ import { GapiPubSubService } from '@gapi/core';
90
+ import { EffectTypes } from '../core/api-introspection/EffectTypes';
91
+ import { NotificationService } from '../core/services/notification/notification.service';
92
+
93
+ @Effect()
94
+ export class YourCustomEffects {
95
+
96
+ constructor(
97
+ private notificationService: NotificationService
98
+ ) {}
99
+
100
+ @OfType<EffectTypes>(EffectTypes.myevent)
101
+ async myEventTrigger(result, {payload}, context) {
102
+ await this.notificationService.createNotification(result);
103
+ }
104
+ }
105
+ ```
106
+
107
+ More detailed [DOCUMENTATION](https://github.com/KolektifLabs/onesignal-node) you can find inside original onesignal-node module
108
+ Enjoy ! :)
@@ -0,0 +1,61 @@
1
+ import { OneSignalClient } from './interfaces/client';
2
+ import { SendNotificationResponse } from './interfaces/notificationResponse';
3
+ import { Notification } from './notification';
4
+ import { OneSignalConfig } from './onesignal.config';
5
+ export interface DevicesData<T> {
6
+ players: Player<T>[];
7
+ }
8
+ export interface Player<T> {
9
+ id: string;
10
+ identifier: string;
11
+ session_count: number;
12
+ language: string;
13
+ timezone: number;
14
+ game_version: string;
15
+ device_os: string;
16
+ device_type: number;
17
+ device_model: string;
18
+ ad_id: string;
19
+ tags: T;
20
+ last_active: number;
21
+ playtime: number;
22
+ amount_spend: number;
23
+ created_at: number;
24
+ invalid_identifier: boolean;
25
+ badge_count: number;
26
+ sdk: string;
27
+ test_type: any;
28
+ ip: any;
29
+ }
30
+ export declare class OneSignalClientService implements OneSignalClient {
31
+ API_URI: string;
32
+ app: any;
33
+ apps: any;
34
+ userAuthKey: string;
35
+ constructor(credentials: OneSignalConfig);
36
+ basicRequest(url: string, apiKey: string, method: 'PUT' | 'POST' | 'GET' | 'DELETE', body: any): Promise<any>;
37
+ setRootUrl(rootUrl: string): void;
38
+ setApp(app: any): void;
39
+ sendNotification(notification: Notification): Promise<SendNotificationResponse>;
40
+ cancelNotification(notificationId: string): Promise<any>;
41
+ viewNotification(notificationId: string): Promise<any>;
42
+ viewNotifications(query: {
43
+ limit: number;
44
+ offset: number;
45
+ }): Promise<any>;
46
+ viewApps(): Promise<any>;
47
+ viewApp(appId: string): Promise<any>;
48
+ createApp(body: any): Promise<any>;
49
+ updateApp(body: any): Promise<any>;
50
+ viewDevices<T>(query: {
51
+ limit: number;
52
+ offset: number;
53
+ }): Promise<{
54
+ data: DevicesData<T>;
55
+ }>;
56
+ viewDevice(deviceId: string): Promise<any>;
57
+ addDevice(body: any): Promise<any>;
58
+ editDevice(deviceId: string, body: any): Promise<any>;
59
+ trackOpen(notificationId: string, body: any): Promise<any>;
60
+ csvExport(body: any): Promise<any>;
61
+ }
package/dist/client.js ADDED
@@ -0,0 +1,272 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
12
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
13
+ return new (P || (P = Promise))(function (resolve, reject) {
14
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
15
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
16
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
17
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
18
+ });
19
+ };
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ exports.OneSignalClientService = void 0;
22
+ /* eslint-disable @typescript-eslint/no-explicit-any */
23
+ const core_1 = require("@rxdi/core");
24
+ const request = require("request");
25
+ const constants_1 = require("./constants");
26
+ const onesignal_config_1 = require("./onesignal.config");
27
+ function checkCredential(credentialName, credential) {
28
+ const ALLOWED_CREDENTIALS = [
29
+ { name: 'userAuthKey', type: 'string' },
30
+ { name: 'app', type: 'object', requiredFields: ['appAuthKey', 'appId'] },
31
+ { name: 'apps', type: 'object' },
32
+ ];
33
+ for (let i = 0; i < ALLOWED_CREDENTIALS.length; i++) {
34
+ if (ALLOWED_CREDENTIALS[i].name === credentialName) {
35
+ if (typeof credential !== ALLOWED_CREDENTIALS[i].type) {
36
+ throw new Error(credentialName + ' must be a ' + ALLOWED_CREDENTIALS[i].type);
37
+ }
38
+ if (ALLOWED_CREDENTIALS[i].requiredFields) {
39
+ for (let j = 0; j < ALLOWED_CREDENTIALS[i].requiredFields.length; j++) {
40
+ if (!(ALLOWED_CREDENTIALS[i].requiredFields[j] in credential)) {
41
+ throw new Error(credentialName +
42
+ ' must contain ' +
43
+ ALLOWED_CREDENTIALS[i].requiredFields[j]);
44
+ }
45
+ }
46
+ }
47
+ return true;
48
+ }
49
+ }
50
+ return false;
51
+ }
52
+ let OneSignalClientService = class OneSignalClientService {
53
+ constructor(credentials) {
54
+ if (typeof credentials !== 'object') {
55
+ throw new Error('credentials parameter must be a JSON object');
56
+ }
57
+ this.API_URI = constants_1.Constants.API_ROOT;
58
+ for (const key in credentials) {
59
+ if (credentials.hasOwnProperty(key) &&
60
+ checkCredential(key, credentials[key])) {
61
+ this[key] = credentials[key];
62
+ }
63
+ }
64
+ }
65
+ basicRequest(url, apiKey, method, body) {
66
+ const options = {
67
+ url: url,
68
+ method: method,
69
+ };
70
+ if (apiKey) {
71
+ options.headers = {
72
+ 'Content-Type': 'application/json; charset=utf-8',
73
+ Authorization: 'Basic ' + apiKey,
74
+ };
75
+ }
76
+ if (body) {
77
+ options.body = body;
78
+ options.json = true;
79
+ }
80
+ return new Promise(function (resolve, reject) {
81
+ request(options, function (err, httpResponse, data) {
82
+ if (err) {
83
+ return reject(err);
84
+ }
85
+ return resolve({ httpResponse: httpResponse, data: data });
86
+ });
87
+ });
88
+ }
89
+ setRootUrl(rootUrl) {
90
+ if (!rootUrl) {
91
+ throw new Error('You must set a valid rootUsrl.');
92
+ }
93
+ this.API_URI = rootUrl;
94
+ }
95
+ setApp(app) {
96
+ checkCredential('app', app);
97
+ this.app = app;
98
+ }
99
+ sendNotification(notification) {
100
+ return __awaiter(this, void 0, void 0, function* () {
101
+ if (!notification || !notification.postBody) {
102
+ throw new Error('notification parameter must be a typeof Notification object.');
103
+ }
104
+ const postBody = notification.postBody;
105
+ if (this.apps && this.apps.length > 0) {
106
+ postBody.app_ids = this.apps;
107
+ return yield this.basicRequest(this.API_URI + constants_1.Constants.NOTIFICATIONS_PATH, this.userAuthKey, 'POST', postBody);
108
+ }
109
+ if (this.app) {
110
+ postBody.app_id = this.app.appId;
111
+ return yield this.basicRequest(this.API_URI + constants_1.Constants.NOTIFICATIONS_PATH, this.app.appAuthKey, 'POST', postBody);
112
+ }
113
+ throw new Error('You must set either an "app" or "apps" on Client');
114
+ });
115
+ }
116
+ cancelNotification(notificationId) {
117
+ return __awaiter(this, void 0, void 0, function* () {
118
+ if (!this.app) {
119
+ throw new Error('You must define an "app" object.');
120
+ }
121
+ const notificationUri = this.API_URI +
122
+ constants_1.Constants.NOTIFICATIONS_PATH +
123
+ '/' +
124
+ notificationId +
125
+ '?app_id=' +
126
+ this.app.appId;
127
+ return yield this.basicRequest(notificationUri, this.app.appAuthKey, 'DELETE', null);
128
+ });
129
+ }
130
+ viewNotification(notificationId) {
131
+ return __awaiter(this, void 0, void 0, function* () {
132
+ if (!this.app) {
133
+ throw new Error('You must define an "app" object.');
134
+ }
135
+ const notificationUri = this.API_URI +
136
+ constants_1.Constants.NOTIFICATIONS_PATH +
137
+ '/' +
138
+ notificationId +
139
+ '?app_id=' +
140
+ this.app.appId;
141
+ return yield this.basicRequest(notificationUri, this.app.appAuthKey, 'GET', null);
142
+ });
143
+ }
144
+ viewNotifications(query) {
145
+ return __awaiter(this, void 0, void 0, function* () {
146
+ if (!this.app) {
147
+ throw new Error('You must define an "app" object.');
148
+ }
149
+ const appUri = this.API_URI +
150
+ constants_1.Constants.NOTIFICATIONS_PATH +
151
+ '?app_id=' +
152
+ this.app.appId +
153
+ '&' +
154
+ query;
155
+ return yield this.basicRequest(appUri, this.app.appAuthKey, 'GET', null);
156
+ });
157
+ }
158
+ viewApps() {
159
+ return __awaiter(this, void 0, void 0, function* () {
160
+ if (!this.userAuthKey) {
161
+ throw new Error('You must define "userAuthKey" on Client');
162
+ }
163
+ return yield this.basicRequest(this.API_URI + constants_1.Constants.APPS_PATH, this.userAuthKey, 'GET', null);
164
+ });
165
+ }
166
+ viewApp(appId) {
167
+ return __awaiter(this, void 0, void 0, function* () {
168
+ if (!this.userAuthKey) {
169
+ throw new Error('You must define "userAuthKey" on Client');
170
+ }
171
+ return yield this.basicRequest(this.API_URI + constants_1.Constants.APPS_PATH + '/' + appId, this.userAuthKey, 'GET', null);
172
+ });
173
+ }
174
+ createApp(body) {
175
+ return __awaiter(this, void 0, void 0, function* () {
176
+ if (!body.name) {
177
+ throw new Error('You must specify a name in body');
178
+ }
179
+ if (!this.userAuthKey) {
180
+ throw new Error('You must define "userAuthKey" on Client');
181
+ }
182
+ return yield this.basicRequest(this.API_URI + constants_1.Constants.APPS_PATH, this.userAuthKey, 'POST', body);
183
+ });
184
+ }
185
+ updateApp(body) {
186
+ return __awaiter(this, void 0, void 0, function* () {
187
+ if (!this.app) {
188
+ throw new Error('You must define an "app" object.');
189
+ }
190
+ if (!this.userAuthKey) {
191
+ throw new Error('You must define "userAuthKey" on Client');
192
+ }
193
+ return yield this.basicRequest(this.API_URI + constants_1.Constants.APPS_PATH + '/' + this.app.appId, this.userAuthKey, 'PUT', body);
194
+ });
195
+ }
196
+ viewDevices(query) {
197
+ return __awaiter(this, void 0, void 0, function* () {
198
+ if (!this.app) {
199
+ throw new Error('You must define an "app" object.');
200
+ }
201
+ const viewUri = this.API_URI +
202
+ constants_1.Constants.DEVICES_PATH +
203
+ '?app_id=' +
204
+ this.app.appId +
205
+ '&' +
206
+ query;
207
+ return yield this.basicRequest(viewUri, this.app.appAuthKey, 'GET', null);
208
+ });
209
+ }
210
+ viewDevice(deviceId) {
211
+ return __awaiter(this, void 0, void 0, function* () {
212
+ if (!this.app) {
213
+ throw new Error('You must define an "app" object.');
214
+ }
215
+ const viewUri = this.API_URI +
216
+ constants_1.Constants.DEVICES_PATH +
217
+ '/' +
218
+ deviceId +
219
+ '?app_id=' +
220
+ this.app.appId;
221
+ return yield this.basicRequest(viewUri, this.app.appAuthKey, 'GET', null);
222
+ });
223
+ }
224
+ addDevice(body) {
225
+ return __awaiter(this, void 0, void 0, function* () {
226
+ if (!this.app) {
227
+ throw new Error('You must define an "app" object.');
228
+ }
229
+ if (!('app_id' in body)) {
230
+ body.app_id = this.app.appId;
231
+ }
232
+ return yield this.basicRequest(this.API_URI + constants_1.Constants.DEVICES_PATH, this.app.appAuthKey, 'POST', body);
233
+ });
234
+ }
235
+ editDevice(deviceId, body) {
236
+ return __awaiter(this, void 0, void 0, function* () {
237
+ if (!this.app) {
238
+ throw new Error('You must define an "app" object.');
239
+ }
240
+ return yield this.basicRequest(this.API_URI + constants_1.Constants.DEVICES_PATH + '/' + deviceId, this.app.appAuthKey, 'PUT', body);
241
+ });
242
+ }
243
+ trackOpen(notificationId, body) {
244
+ return __awaiter(this, void 0, void 0, function* () {
245
+ if (!this.app) {
246
+ throw new Error('You must define an "app" object.');
247
+ }
248
+ if (!('app_id' in body)) {
249
+ body.app_id = this.app.appId;
250
+ }
251
+ return yield this.basicRequest(this.API_URI + constants_1.Constants.NOTIFICATIONS_PATH + '/' + notificationId, this.app.appAuthKey, 'PUT', body);
252
+ });
253
+ }
254
+ csvExport(body) {
255
+ return __awaiter(this, void 0, void 0, function* () {
256
+ if (!this.app) {
257
+ throw new Error('You must define an "app" object.');
258
+ }
259
+ const csvUri = this.API_URI +
260
+ constants_1.Constants.DEVICES_PATH +
261
+ '/csv_export' +
262
+ '?app_id=' +
263
+ this.app.appId;
264
+ return yield this.basicRequest(csvUri, this.app.appAuthKey, 'POST', body);
265
+ });
266
+ }
267
+ };
268
+ OneSignalClientService = __decorate([
269
+ core_1.Service(),
270
+ __metadata("design:paramtypes", [onesignal_config_1.OneSignalConfig])
271
+ ], OneSignalClientService);
272
+ exports.OneSignalClientService = OneSignalClientService;