Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:security/recommended"
]
}
2 changes: 1 addition & 1 deletion .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:

- name: Pretty
run: npm run-script prettier-check
# complete the test coverage
# complete the test coverage
- name: Test
run: npm test

Expand Down
21 changes: 11 additions & 10 deletions .github/workflows/pullRequests.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
name: pull_request_pipeline

on:
pull_request:
branches:
- main
name: Pull Request Pipeline
jobs:
pull_request_pipeline:
name: Pull Request Pipeline
name: pull_request_pipeline
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Checkout Code
uses: actions/checkout@v2

- name: Setup Node
uses: actions/setup-node@v4
Expand All @@ -22,16 +24,15 @@ jobs:
path: node_modules
key: 18.x-${{ runner.OS }}-build-${{ hashFiles('package.json') }}

- name: NPM Install
- name: Install Dependencies
if: steps.cache-modules.outputs.cache-hit != 'true'
run: npm install

- name: Lint
run: npm run-script lint
run: npm run lint

- name: Pretty
run: npm run-script prettier-check
# complete the test coverage
- name: Test
run: npm test
- name: Check Prettier
run: npm run prettier-check

- name: Run Tests
run: npm test
10 changes: 5 additions & 5 deletions lib/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const api = async (
method: "GET" | "POST" | "PATCH" | "DELETE",
method: 'GET' | 'POST' | 'PATCH' | 'DELETE',
host: string,
resource: string,
clientId: string,
Expand All @@ -16,8 +16,8 @@ export const api = async (
method,
body: JSON.stringify(data),
headers: {
Authorization: `Basic ${token}`,
},
Authorization: `Basic ${token}`
}
}
);

Expand All @@ -35,8 +35,8 @@ export const generateBasicTokenForUser = (
hashedUserId?: string
) => {
const token = hashedUserId
? btoa(clientId + ":" + userId + ":" + hashedUserId)
: btoa(clientId + ":" + userId);
? btoa(clientId + ':' + userId + ':' + hashedUserId)
: btoa(clientId + ':' + userId);

return token;
};
63 changes: 33 additions & 30 deletions lib/client.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { api } from "./api";
import { api } from './api';
import {
BaseDeliveryOptions,
Channels,
DeliveryOptionsForEmail,
DeliveryOptionsForInappWeb,
} from "./interfaces";
DeliveryOptionsForInappWeb
} from './interfaces';
import {
GetPreferencesResponse,
InAppNotification,
WebSocket_NewNotification_Message,
} from "./interfaces";
WebSocket_NewNotification_Message
} from './interfaces';

type NotificationAPIClientSDKConfig = {
userId: string;
Expand All @@ -28,17 +28,17 @@ type NotificationAPIClientSDKConfig = {
};

const defaultConfig: NotificationAPIClientSDKConfig = {
host: "api.notificationapi.com",
websocketHost: "ws.notificationapi.com",
userId: "",
clientId: "",
hashedUserId: "",
host: 'api.notificationapi.com',
websocketHost: 'ws.notificationapi.com',
userId: '',
clientId: '',
hashedUserId: '',
getInAppDefaultCount: 100,
getInAppDefaultOldest: new Date(
Date.now() - 30 * 24 * 60 * 60 * 1000
).toISOString(),
onNewInAppNotifications: undefined,
keepWebSocketAliveForSeconds: 24 * 60 * 60, // 24 hours
keepWebSocketAliveForSeconds: 24 * 60 * 60 // 24 hours
};

type NotificationAPIClientSDK = {
Expand All @@ -51,7 +51,7 @@ type NotificationAPIClientSDK = {
): NotificationAPIClientSDK;
rest: {
generic(
method: "GET" | "POST" | "PATCH" | "DELETE",
method: 'GET' | 'POST' | 'PATCH' | 'DELETE',
resource: string,
data?: any
): Promise<any>;
Expand Down Expand Up @@ -108,7 +108,7 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
init: function (config) {
this.config = { ...defaultConfig, ...config };
return {
...this,
...this
};
},
rest: {
Expand All @@ -125,27 +125,27 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
},
getNotifications: function (before, count) {
return NotificationAPIClientSDK.rest.generic(
"GET",
'GET',
`notifications/INAPP_WEB?count=${count}&before=${before}`
);
},
patchNotifications: function (params) {
return NotificationAPIClientSDK.rest.generic(
"PATCH",
"notifications/INAPP_WEB",
'PATCH',
'notifications/INAPP_WEB',
params
);
},
getPreferences: function () {
return NotificationAPIClientSDK.rest.generic("GET", "preferences");
return NotificationAPIClientSDK.rest.generic('GET', 'preferences');
},
postPreferences: function (params) {
return NotificationAPIClientSDK.rest.generic(
"POST",
"preferences",
'POST',
'preferences',
params
);
},
}
},
websocket: {
object: undefined,
Expand All @@ -161,7 +161,7 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
return;
}

if (body.route === "inapp_web/new_notifications") {
if (body.route === 'inapp_web/new_notifications') {
const message = body as WebSocket_NewNotification_Message;
if (NotificationAPIClientSDK.config.onNewInAppNotifications) {
NotificationAPIClientSDK.config.onNewInAppNotifications(
Expand All @@ -179,15 +179,18 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
callback(NotificationAPIClientSDK.websocket.object);
}
}
},
}
},
openWebSocket: function () {
const websocket = NotificationAPIClientSDK.websocket.connect(() => {
setTimeout(() => {
this.websocket.disconnect(() => {
this.websocket.connect();
});
}, 9 * 60 * 1000);
setTimeout(
() => {
this.websocket.disconnect(() => {
this.websocket.connect();
});
},
9 * 60 * 1000
);
});
return websocket;
},
Expand Down Expand Up @@ -233,14 +236,14 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
return {
items: result,
hasMore,
oldestReceived,
oldestReceived
};
},
updateInAppNotifications: async (params) => {
const body: {
[key: string]: any;
} = {
trackingIds: params.ids,
trackingIds: params.ids
};

if (params.archived === true) {
Expand All @@ -266,5 +269,5 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
},
updateDeliveryOption: async (params) => {
return NotificationAPIClientSDK.rest.postPreferences([params]);
},
}
};
38 changes: 19 additions & 19 deletions lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,31 @@ export interface InAppNotification {
}

export interface WebSocket_NewNotification_Message {
route: "inapp_web/new_notifications";
route: 'inapp_web/new_notifications';
payload: {
notifications: InAppNotification[];
};
}

export type Channels =
| "EMAIL"
| "INAPP_WEB"
| "SMS"
| "CALL"
| "PUSH"
| "WEB_PUSH";
| 'EMAIL'
| 'INAPP_WEB'
| 'SMS'
| 'CALL'
| 'PUSH'
| 'WEB_PUSH';

export type BaseDeliveryOptions = "off" | "instant";
export type BaseDeliveryOptions = 'off' | 'instant';

export type DeliveryOptionsForInappWeb = "off" | "instant";
export type DeliveryOptionsForInappWeb = 'off' | 'instant';

export type DeliveryOptionsForEmail =
| "off"
| "instant"
| "hourly"
| "daily"
| "weekly"
| "monthly";
| 'off'
| 'instant'
| 'hourly'
| 'daily'
| 'weekly'
| 'monthly';

export interface GetPreferencesResponse {
preferences: {
Expand All @@ -82,7 +82,7 @@ export interface GetPreferencesResponse {
notificationId: string;
title: string;
channels: Channels[];
options: NotificationConfig["options"];
options: NotificationConfig['options'];
}[];
subNotifications: {
notificationId: string;
Expand All @@ -103,9 +103,9 @@ export interface NotificationConfig {
throttling?: {
max: number;
period: number;
unit: "seconds" | "minutes" | "hours" | "days" | "months" | "years";
unit: 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years';
forever: boolean;
scope: ["userId", "notificationId"];
scope: ['userId', 'notificationId'];
};
retention?: number;
options?: {
Expand Down Expand Up @@ -135,7 +135,7 @@ export interface EmailDeliveryOptions {
monthly?: {
enabled: boolean;
hour?: string;
date?: "first" | "last";
date?: 'first' | 'last';
};
}

Expand Down
2 changes: 1 addition & 1 deletion lib/main.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { NotificationAPIClientSDK } from "./client";
export { NotificationAPIClientSDK } from './client';
Loading