Skip to content

Commit 5f39f21

Browse files
authored
Merge pull request #24 from notificationapi-com/s2K4mDqS/3325-slack-library-updates
S2K4mDqS/3325-slack-library-updates
2 parents f3b299b + 0f74871 commit 5f39f21

File tree

5 files changed

+137
-5
lines changed

5 files changed

+137
-5
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# notificationapi-js-core
22

33
With [NotificationAPI](https://www.notificationapi.com), software developers can implement any kind of notifications into their app in a matter of minutes.
4-
notificationapi-js-core focuses on providing headless UI support to Vanilla JavaScript users looking to use our infrastructure within their own designs.
4+
notificationapi-js-core focuses on providing headless UI support to Vanilla JavaScript users looking to use our infrastructure within their own designs.
55

66
# Docs
77

@@ -22,3 +22,15 @@ npm run test
2222
```
2323

2424
100% code coverage required.
25+
26+
Update the version:
27+
28+
```
29+
npm version major|minor|patch
30+
```
31+
32+
Major: for breaking changes
33+
Minor: for new features
34+
Patch: for bug fixes
35+
36+
The pipeline will automatically build and publish the package to npm.

lib/client.ts

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
DeliveryOptionsForEmail,
88
DeliveryOptionsForInappWeb,
99
PostUserRequest,
10+
User,
1011
UserAccountMetadata,
1112
WS_REGION
1213
} from './interfaces';
@@ -33,6 +34,9 @@ type NotificationAPIClientSDKConfig = {
3334

3435
// Debug mode:
3536
debug: boolean;
37+
38+
// SDK Dev Mode:
39+
sdkDevMode: boolean;
3640
};
3741

3842
const defaultConfig: NotificationAPIClientSDKConfig = {
@@ -47,7 +51,8 @@ const defaultConfig: NotificationAPIClientSDKConfig = {
4751
).toISOString(),
4852
onNewInAppNotifications: undefined,
4953
keepWebSocketAliveForSeconds: 24 * 60 * 60, // 24 hours
50-
debug: false
54+
debug: false,
55+
sdkDevMode: false
5156
};
5257

5358
type NotificationAPIClientSDK = {
@@ -119,6 +124,19 @@ type NotificationAPIClientSDK = {
119124
getUserAccountMetadata(): Promise<{
120125
userAccountMetadata: UserAccountMetadata;
121126
}>;
127+
user: {
128+
get: () => Promise<User>;
129+
};
130+
slack: {
131+
getOAuthUrl: (props?: { destinationUrl?: string }) => string;
132+
getChannels: () => Promise<{
133+
channels: {
134+
id: string;
135+
name: string;
136+
}[];
137+
}>;
138+
setChannel: (channelId: string) => Promise<void>;
139+
};
122140
};
123141

124142
export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
@@ -395,5 +413,51 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
395413
},
396414
getUserAccountMetadata: async () => {
397415
return NotificationAPIClientSDK.rest.getUserAccountMetadata();
416+
},
417+
418+
user: {
419+
get: async () => {
420+
return NotificationAPIClientSDK.rest.generic('GET', '') as Promise<User>;
421+
}
422+
},
423+
424+
slack: {
425+
getOAuthUrl: (props?: { destinationUrl?: string }) => {
426+
const sdkDevMode = NotificationAPIClientSDK.config.sdkDevMode;
427+
const domain = sdkDevMode
428+
? 'localhost:3001'
429+
: NotificationAPIClientSDK.config.host.replace('api.', 'app.');
430+
// if no redirectUri is provided, use the current page's URL
431+
const destination = props?.destinationUrl || window.location.href;
432+
433+
const state = encodeURIComponent(
434+
JSON.stringify({
435+
destination,
436+
userId: NotificationAPIClientSDK.config.userId,
437+
clientId: NotificationAPIClientSDK.config.clientId,
438+
hashedUserId: NotificationAPIClientSDK.config.hashedUserId
439+
})
440+
);
441+
442+
const url =
443+
'https://slack.com/oauth/v2/authorize?' +
444+
'client_id=1146598856352.8825220259395' +
445+
'&scope=chat:write,channels:read,channels:join,chat:write.customize,chat:write.public,groups:read,im:read,users:read' +
446+
`&redirect_uri=https://${domain}/slack/oauth/callback` +
447+
`&state=${state}`;
448+
return url;
449+
},
450+
getChannels: async () => {
451+
const response = await NotificationAPIClientSDK.rest.generic(
452+
'GET',
453+
'slack/channels'
454+
);
455+
return response;
456+
},
457+
setChannel: async (channelId: string) => {
458+
return NotificationAPIClientSDK.identify({
459+
slackChannel: channelId
460+
});
461+
}
398462
}
399463
};

lib/interfaces.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,64 @@ export interface User {
178178
lastSeenTime?: string;
179179
createdAt?: string;
180180
updatedAt?: string;
181+
slackChannel?: string;
182+
slackToken?: OauthV2AccessResponse;
181183
}
182184

185+
interface WebAPICallResult {
186+
ok: boolean;
187+
error?: string;
188+
response_metadata?: {
189+
warnings?: string[];
190+
next_cursor?: string;
191+
scopes?: string[];
192+
acceptedScopes?: string[];
193+
retryAfter?: number;
194+
messages?: string[];
195+
};
196+
}
197+
198+
interface AuthedUser {
199+
access_token?: string;
200+
expires_in?: number;
201+
id?: string;
202+
refresh_token?: string;
203+
scope?: string;
204+
token_type?: string;
205+
}
206+
207+
interface Enterprise {
208+
id?: string;
209+
name?: string;
210+
}
211+
212+
export interface IncomingWebhook {
213+
channel?: string;
214+
channel_id?: string;
215+
configuration_url?: string;
216+
url?: string;
217+
}
218+
219+
type OauthV2AccessResponse = WebAPICallResult & {
220+
access_token?: string;
221+
app_id?: string;
222+
authed_user?: AuthedUser;
223+
bot_user_id?: string;
224+
enterprise?: Enterprise;
225+
error?: string;
226+
expires_in?: number;
227+
incoming_webhook?: IncomingWebhook;
228+
is_enterprise_install?: boolean;
229+
needed?: string;
230+
ok?: boolean;
231+
provided?: string;
232+
refresh_token?: string;
233+
scope?: string;
234+
team?: Enterprise;
235+
token_type?: string;
236+
warning?: string;
237+
};
238+
183239
export type PostUserRequest = Omit<
184240
Partial<User>,
185241
'lastSeenTime' | 'createdAt' | 'updatedAt'

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@notificationapi/core",
3-
"version": "0.0.17",
3+
"version": "1.0.0",
44
"type": "module",
55
"main": "dist/main.js",
66
"types": "dist/main.d.ts",

0 commit comments

Comments
 (0)