@roadmanjs/push 0.0.1 → 0.0.3
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/README.md +4 -29
- package/dist/push/Push.test.js +34 -15
- package/dist/push/Push.test.js.map +1 -1
- package/dist/push/push.methods.d.ts +16 -3
- package/dist/push/push.methods.js +55 -8
- package/dist/push/push.methods.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,42 +1,17 @@
|
|
|
1
1
|
|
|
2
2
|
<p align="center">
|
|
3
|
-
<h1 align="center"> Roadman -
|
|
3
|
+
<h1 align="center"> Roadman - Push </h1>
|
|
4
4
|
</p>
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
##
|
|
7
|
+
## A roadman for push notifications
|
|
8
8
|
|
|
9
|
-
### How to use
|
|
10
|
-
```
|
|
11
|
-
yarn add @roadmanjs/auth
|
|
12
|
-
```
|
|
13
9
|
|
|
14
|
-
app.ts
|
|
15
|
-
```ts
|
|
16
|
-
import roadman from 'roadman';
|
|
17
|
-
import {AuthResolvers} from '@roadmanjs/auth';
|
|
18
|
-
|
|
19
|
-
await roadman({
|
|
20
|
-
resolvers: [...AuthResolvers, ...MyOtherResolvers]
|
|
21
|
-
});
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
also exports, middleswares like, isAuth, or FirebaseToken
|
|
25
10
|
|
|
26
11
|
#### Env required
|
|
27
12
|
```sh
|
|
28
13
|
DEBUG=roadman*
|
|
29
14
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
# Couchbase envs
|
|
34
|
-
COUCHBASE_URL=
|
|
35
|
-
COUCHBASE_BUCKET=
|
|
36
|
-
COUCHBASE_USERNAME=
|
|
37
|
-
COUCHBASE_PASSWORD=
|
|
38
|
-
|
|
39
|
-
# Access token generator secrets
|
|
40
|
-
ACCESS_TOKEN_SECRET=xxxxx
|
|
41
|
-
REFRESH_TOKEN_SECRET=xxxx
|
|
15
|
+
FIREBASE_SA_IOS={"firebase admmin service account": ""}
|
|
16
|
+
FIREBASE_SA_ANDROID={"firebase admmin service account": ""}
|
|
42
17
|
```
|
package/dist/push/Push.test.js
CHANGED
|
@@ -2,26 +2,45 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
require("reflect-metadata");
|
|
4
4
|
require("mocha");
|
|
5
|
-
const chai_1 = require("chai");
|
|
6
|
-
const couchset_1 = require("@roadmanjs/couchset");
|
|
7
5
|
const push_methods_1 = require("./push.methods");
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
const chai_1 = require("chai");
|
|
7
|
+
// before((done) => {
|
|
8
|
+
// startCouchbase().then(() => done());
|
|
9
|
+
// });
|
|
11
10
|
describe('Push', () => {
|
|
11
|
+
// it('it should send a notification to user', async () => {
|
|
12
|
+
// const userId = '99bc43ba-02ab-4394-b48b-49a39a95443c';
|
|
13
|
+
// const sentMessage = await sendMessageToUser(userId, {
|
|
14
|
+
// data: {
|
|
15
|
+
// type: 'tv',
|
|
16
|
+
// },
|
|
17
|
+
// notification: {
|
|
18
|
+
// title: 'Basic Notification',
|
|
19
|
+
// body: 'This is a basic notification sent from the server!',
|
|
20
|
+
// imageUrl: 'https://my-cdn.com/app-logo.png',
|
|
21
|
+
// sound: 'vuga_zing',
|
|
22
|
+
// },
|
|
23
|
+
// });
|
|
24
|
+
// console.log("sendMessageToUser", sentMessage)
|
|
25
|
+
// expect(sentMessage).to.be.not.empty;
|
|
26
|
+
// });
|
|
12
27
|
it('it should send a notification to user', async () => {
|
|
13
|
-
const
|
|
14
|
-
const sentMessage = await (0, push_methods_1.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
28
|
+
const topic = 'Tv';
|
|
29
|
+
const sentMessage = await (0, push_methods_1.sendMessageToTopic)({
|
|
30
|
+
topic,
|
|
31
|
+
payload: {
|
|
32
|
+
data: {
|
|
33
|
+
type: 'tv',
|
|
34
|
+
},
|
|
35
|
+
notification: {
|
|
36
|
+
title: 'Notification Test',
|
|
37
|
+
body: 'Notification Test',
|
|
38
|
+
// imageUrl: 'https://my-cdn.com/app-logo.png',
|
|
39
|
+
sound: 'vuga_zing',
|
|
40
|
+
},
|
|
23
41
|
},
|
|
24
42
|
});
|
|
43
|
+
console.log('sendMessageToTopic', sentMessage);
|
|
25
44
|
(0, chai_1.expect)(sentMessage).to.be.not.empty;
|
|
26
45
|
});
|
|
27
46
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Push.test.js","sourceRoot":"","sources":["../../src/push/Push.test.ts"],"names":[],"mappings":";;AAAA,4BAA0B;AAC1B,iBAAe;AAEf,+BAA4B;
|
|
1
|
+
{"version":3,"file":"Push.test.js","sourceRoot":"","sources":["../../src/push/Push.test.ts"],"names":[],"mappings":";;AAAA,4BAA0B;AAC1B,iBAAe;AAEf,iDAAqE;AAErE,+BAA4B;AAG5B,qBAAqB;AACrB,2CAA2C;AAC3C,MAAM;AAEN,QAAQ,CAAC,MAAM,EAAE,GAAG,EAAE;IAClB,4DAA4D;IAC5D,6DAA6D;IAC7D,4DAA4D;IAC5D,kBAAkB;IAClB,0BAA0B;IAC1B,aAAa;IACb,0BAA0B;IAC1B,2CAA2C;IAC3C,0EAA0E;IAC1E,2DAA2D;IAC3D,kCAAkC;IAClC,aAAa;IACb,UAAU;IAEV,oDAAoD;IAEpD,2CAA2C;IAC3C,MAAM;IAEN,EAAE,CAAC,uCAAuC,EAAE,KAAK,IAAI,EAAE;QACnD,MAAM,KAAK,GAAG,IAAI,CAAC;QACnB,MAAM,WAAW,GAAG,MAAM,IAAA,iCAAkB,EAAC;YACzC,KAAK;YACL,OAAO,EAAE;gBACL,IAAI,EAAE;oBACF,IAAI,EAAE,IAAI;iBACb;gBACD,YAAY,EAAE;oBACV,KAAK,EAAE,mBAAmB;oBAC1B,IAAI,EAAE,mBAAmB;oBACzB,+CAA+C;oBAC/C,KAAK,EAAE,WAAW;iBACrB;aACJ;SACJ,CAAC,CAAC;QAEH,OAAO,CAAC,GAAG,CAAC,oBAAoB,EAAE,WAAW,CAAC,CAAC;QAE/C,IAAA,aAAM,EAAC,WAAW,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC;IACxC,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
|
@@ -1,5 +1,19 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { FirebaseProject } from '@roadmanjs/firebase-admin';
|
|
2
|
+
import { MessagingDevicesResponse, MessagingOptions, MessagingPayload, MessagingTopicResponse } from 'firebase-admin/messaging';
|
|
3
|
+
export interface PushOptions {
|
|
4
|
+
options?: MessagingOptions;
|
|
5
|
+
payload: MessagingPayload;
|
|
6
|
+
}
|
|
7
|
+
export interface SendToTopic extends PushOptions {
|
|
8
|
+
topic: string;
|
|
9
|
+
client: FirebaseProject;
|
|
10
|
+
}
|
|
11
|
+
export declare const sendToTopic: (args: SendToTopic) => Promise<MessagingTopicResponse | null>;
|
|
12
|
+
export interface SendMessageToTopic extends PushOptions {
|
|
13
|
+
topic: string;
|
|
14
|
+
}
|
|
15
|
+
export declare const sendMessageToTopic: (args: SendMessageToTopic) => Promise<MessagingTopicResponse[] | null>;
|
|
16
|
+
export interface SendNotification {
|
|
3
17
|
options?: MessagingOptions;
|
|
4
18
|
payload: MessagingPayload;
|
|
5
19
|
iosTokens: string[];
|
|
@@ -13,4 +27,3 @@ export declare const sendNotification: (args: SendNotification) => Promise<Messa
|
|
|
13
27
|
* @param options
|
|
14
28
|
*/
|
|
15
29
|
export declare const sendMessageToUser: (owner: string, payload: MessagingPayload, options?: MessagingOptions) => Promise<MessagingDevicesResponse[]>;
|
|
16
|
-
export {};
|
|
@@ -3,14 +3,64 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.sendMessageToUser = exports.sendNotification = void 0;
|
|
6
|
+
exports.sendMessageToUser = exports.sendNotification = exports.sendMessageToTopic = exports.sendToTopic = void 0;
|
|
7
|
+
const firebase_admin_1 = require("@roadmanjs/firebase-admin");
|
|
7
8
|
const user_1 = require("../user");
|
|
9
|
+
const config_1 = require("../config");
|
|
8
10
|
const auth_1 = require("@roadmanjs/auth");
|
|
9
11
|
const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
|
|
10
12
|
const logs_1 = require("@roadmanjs/logs");
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
const
|
|
13
|
+
const sendToTopic = async (args) => {
|
|
14
|
+
try {
|
|
15
|
+
const { client, options, payload, topic } = args;
|
|
16
|
+
const sentToTopic = await client.messaging().sendToTopic(topic, payload, options);
|
|
17
|
+
(0, logs_1.log)('messaging().sendToTopic', JSON.stringify(sentToTopic));
|
|
18
|
+
return sentToTopic;
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
(0, logs_1.log)('messaging().sendToTopic', error);
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
exports.sendToTopic = sendToTopic;
|
|
26
|
+
const sendMessageToTopic = async (args) => {
|
|
27
|
+
const { payload, topic } = args;
|
|
28
|
+
const response = [];
|
|
29
|
+
const options = args.options || {
|
|
30
|
+
// Required for background/quit data-only messages on iOS
|
|
31
|
+
contentAvailable: true,
|
|
32
|
+
// Required for background/quit data-only messages on Android
|
|
33
|
+
priority: 'high',
|
|
34
|
+
};
|
|
35
|
+
try {
|
|
36
|
+
const iosClient = await (0, firebase_admin_1.configureFirebase)(config_1.firebaseIos);
|
|
37
|
+
const androidClient = await (0, firebase_admin_1.configureFirebase)(config_1.firebaseAndroid);
|
|
38
|
+
if (iosClient) {
|
|
39
|
+
const iosSent = await (0, exports.sendToTopic)({
|
|
40
|
+
client: iosClient,
|
|
41
|
+
options,
|
|
42
|
+
payload,
|
|
43
|
+
topic,
|
|
44
|
+
});
|
|
45
|
+
response.push(iosSent);
|
|
46
|
+
}
|
|
47
|
+
if (androidClient) {
|
|
48
|
+
const androidSent = await (0, exports.sendToTopic)({
|
|
49
|
+
client: androidClient,
|
|
50
|
+
options,
|
|
51
|
+
payload,
|
|
52
|
+
topic,
|
|
53
|
+
});
|
|
54
|
+
response.push(androidSent);
|
|
55
|
+
}
|
|
56
|
+
return response;
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
(0, logs_1.log)('error sending notification to topic', error);
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
exports.sendMessageToTopic = sendMessageToTopic;
|
|
14
64
|
const sendNotification = async (args) => {
|
|
15
65
|
const { payload, iosTokens = [], androidTokens = [] } = args;
|
|
16
66
|
const response = [];
|
|
@@ -58,10 +108,7 @@ exports.sendNotification = sendNotification;
|
|
|
58
108
|
*/
|
|
59
109
|
const sendMessageToUser = async (owner, payload, options) => {
|
|
60
110
|
try {
|
|
61
|
-
const
|
|
62
|
-
if (errorUser) {
|
|
63
|
-
throw new Error('user not found');
|
|
64
|
-
}
|
|
111
|
+
const existingUser = await auth_1.UserModel.findById(owner);
|
|
65
112
|
if (existingUser) {
|
|
66
113
|
const userDevices = await user_1.UserDeviceModel.pagination({
|
|
67
114
|
where: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"push.methods.js","sourceRoot":"","sources":["../../src/push/push.methods.ts"],"names":[],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"push.methods.js","sourceRoot":"","sources":["../../src/push/push.methods.ts"],"names":[],"mappings":";;;;;;AAAA,8DAA6E;AAO7E,kCAAwD;AACxD,sCAAuD;AAEvD,0CAA0C;AAC1C,6DAAqC;AACrC,0CAAoC;AAY7B,MAAM,WAAW,GAAG,KAAK,EAAE,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