Skip to content

Commit

Permalink
Merge pull request #4 from notificationapi-com/YWEKB1Ew/2447-detect-f…
Browse files Browse the repository at this point in the history
…ront-end-integration

Add identify function
  • Loading branch information
sahandseifi authored Oct 4, 2024
2 parents 91c5c70 + c8ca3e7 commit fd256bf
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
26 changes: 25 additions & 1 deletion lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
BaseDeliveryOptions,
Channels,
DeliveryOptionsForEmail,
DeliveryOptionsForInappWeb
DeliveryOptionsForInappWeb,
PostUserRequest
} from './interfaces';
import {
GetPreferencesResponse,
Expand Down Expand Up @@ -69,6 +70,7 @@ type NotificationAPIClientSDK = {
| BaseDeliveryOptions;
}>
): Promise<any>;

Check warning on line 72 in lib/client.ts

View workflow job for this annotation

GitHub Actions / Test & Build

Unexpected any. Specify a different type
postUser(params: PostUserRequest): Promise<any>;
};
websocket: {
object: WebSocket | undefined;
Expand Down Expand Up @@ -101,6 +103,7 @@ type NotificationAPIClientSDK = {
| BaseDeliveryOptions;
}): Promise<void>;
getPreferences(): Promise<GetPreferencesResponse>;
identify(params: PostUserRequest): Promise<void>;
};

export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
Expand All @@ -123,6 +126,11 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
data
);
},

// The functions below are nice wrappers over the generic
// rest api function above. They must follow REST API naming:
// Method + Resource, representing the end-point.

getNotifications: function (before, count) {
return NotificationAPIClientSDK.rest.generic(
'GET',
Expand All @@ -145,6 +153,9 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
'preferences',
params
);
},
postUser: function (params: PostUserRequest) {
return NotificationAPIClientSDK.rest.generic('POST', '', params);
}
},
websocket: {
Expand Down Expand Up @@ -194,6 +205,11 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
});
return websocket;
},

// These functions are developer friendly wrappers over the rest APIs
// They may or may not do additional tasks.
// e.g. identify simply maps to postUsers

getInAppNotifications: async (params) => {
const maxCountNeeded =
params.maxCountNeeded ||
Expand Down Expand Up @@ -269,5 +285,13 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
},
updateDeliveryOption: async (params) => {
return NotificationAPIClientSDK.rest.postPreferences([params]);
},
identify: async (params: PostUserRequest) => {
if (params.id && params.id !== NotificationAPIClientSDK.config.userId) {
throw new Error(
'The id in the parameters does not match the initialized userId.'
);
}
return NotificationAPIClientSDK.rest.postUser(params);
}
};
48 changes: 48 additions & 0 deletions lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,51 @@ export interface InAppWebDeliveryOptions {
batchingWindow?: number;
};
}

export interface User {
id: string;
email?: string;
number?: string;
pushTokens?: PushToken[];
webPushTokens?: WebPushToken[];
lastSeenTime?: string;
createdAt?: string;
updatedAt?: string;
}

export type PostUserRequest = Omit<
Partial<User>,
'lastSeenTime' | 'createdAt' | 'updatedAt'
>;

export interface PushToken {
type: PushProviders;
token: string;
device: Device;
}

export enum PushProviders {
FCM = 'FCM',
APN = 'APN'
}

export interface Device {
app_id?: string;
ad_id?: string;
device_id: string;
platform?: string;
manufacturer?: string;
model?: string;
}

export interface PushSubscription {
endpoint: string;
keys: {
p256dh: string;
auth: string;
};
}

export interface WebPushToken {
sub: PushSubscription;
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@notificationapi/core",
"version": "0.0.8",
"version": "0.0.9",
"type": "module",
"main": "dist/main.js",
"types": "dist/main.d.ts",
Expand Down

0 comments on commit fd256bf

Please sign in to comment.