Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
67 changes: 66 additions & 1 deletion lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
DeliveryOptionsForEmail,
DeliveryOptionsForInappWeb,
PostUserRequest,
User,
UserAccountMetadata,
WS_REGION
} from './interfaces';
Expand All @@ -33,6 +34,9 @@ type NotificationAPIClientSDKConfig = {

// Debug mode:
debug: boolean;

// SDK Dev Mode:
sdkDevMode: boolean;
};

const defaultConfig: NotificationAPIClientSDKConfig = {
Expand All @@ -47,7 +51,8 @@ const defaultConfig: NotificationAPIClientSDKConfig = {
).toISOString(),
onNewInAppNotifications: undefined,
keepWebSocketAliveForSeconds: 24 * 60 * 60, // 24 hours
debug: false
debug: false,
sdkDevMode: false
};

type NotificationAPIClientSDK = {
Expand Down Expand Up @@ -119,6 +124,19 @@ type NotificationAPIClientSDK = {
getUserAccountMetadata(): Promise<{
userAccountMetadata: UserAccountMetadata;
}>;
user: {
get: () => Promise<User>;
};
slack: {
getOAuthUrl: (props?: { destinationUrl?: string }) => string;
getChannels: () => Promise<{
channels: {
id: string;
name: string;
}[];
}>;
setChannel: (channelId: string) => Promise<void>;
};
};

export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
Expand Down Expand Up @@ -395,5 +413,52 @@ export const NotificationAPIClientSDK: NotificationAPIClientSDK = {
},
getUserAccountMetadata: async () => {
return NotificationAPIClientSDK.rest.getUserAccountMetadata();
},

user: {
get: async () => {
return NotificationAPIClientSDK.rest.generic('GET', '') as Promise<User>;
}
},

slack: {
getOAuthUrl: (props?: { destinationUrl?: string }) => {
const sdkDevMode = NotificationAPIClientSDK.config.sdkDevMode;
const domain = sdkDevMode
? 'localhost:3001'
: NotificationAPIClientSDK.config.host.replace('api.', 'app.');
// if no redirectUri is provided, use the current page's URL
const destination = props?.destinationUrl || window.location.href;

const state = encodeURIComponent(
JSON.stringify({
destination,
userId: NotificationAPIClientSDK.config.userId,
clientId: NotificationAPIClientSDK.config.clientId,
hashedUserId: NotificationAPIClientSDK.config.hashedUserId
})
);

const url =
'https://slack.com/oauth/v2/authorize?' +
'client_id=1146598856352.8825220259395' +
'&scope=chat:write,channels:read,channels:join,chat:write.customize,chat:write.public,groups:read,im:read,users:read' +
`&redirect_uri=https://${domain}/slack/oauth/callback` +
`&state=${state}`;
return url;
},
getChannels: async () => {
const response = await NotificationAPIClientSDK.rest.generic(
'GET',
'slack/channels'
);
console.log(response);
return response;
},
setChannel: async (channelId: string) => {
return NotificationAPIClientSDK.identify({
slackChannel: channelId
});
}
}
};
56 changes: 56 additions & 0 deletions lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,64 @@ export interface User {
lastSeenTime?: string;
createdAt?: string;
updatedAt?: string;
slackChannel?: string;
slackToken?: OauthV2AccessResponse;
}

interface WebAPICallResult {
ok: boolean;
error?: string;
response_metadata?: {
warnings?: string[];
next_cursor?: string;
scopes?: string[];
acceptedScopes?: string[];
retryAfter?: number;
messages?: string[];
};
}

interface AuthedUser {
access_token?: string;
expires_in?: number;
id?: string;
refresh_token?: string;
scope?: string;
token_type?: string;
}

interface Enterprise {
id?: string;
name?: string;
}

export interface IncomingWebhook {
channel?: string;
channel_id?: string;
configuration_url?: string;
url?: string;
}

type OauthV2AccessResponse = WebAPICallResult & {
access_token?: string;
app_id?: string;
authed_user?: AuthedUser;
bot_user_id?: string;
enterprise?: Enterprise;
error?: string;
expires_in?: number;
incoming_webhook?: IncomingWebhook;
is_enterprise_install?: boolean;
needed?: string;
ok?: boolean;
provided?: string;
refresh_token?: string;
scope?: string;
team?: Enterprise;
token_type?: string;
warning?: string;
};

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