Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# notificationapi-js-core

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

# Docs

Expand All @@ -22,3 +22,15 @@ npm run test
```

100% code coverage required.

Update the version:

```
npm version major|minor|patch
```

Major: for breaking changes
Minor: for new features
Patch: for bug fixes

The pipeline will automatically build and publish the package to npm.
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 @@
DeliveryOptionsForEmail,
DeliveryOptionsForInappWeb,
PostUserRequest,
User,
UserAccountMetadata,
WS_REGION
} from './interfaces';
Expand All @@ -33,6 +34,9 @@

// Debug mode:
debug: boolean;

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

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

type NotificationAPIClientSDK = {
Expand All @@ -63,10 +68,10 @@
generic(
method: 'GET' | 'POST' | 'PATCH' | 'DELETE',
resource: string,
data?: any

Check warning on line 71 in lib/client.ts

View workflow job for this annotation

GitHub Actions / pull_request_pipeline

Unexpected any. Specify a different type
): Promise<any>;

Check warning on line 72 in lib/client.ts

View workflow job for this annotation

GitHub Actions / pull_request_pipeline

Unexpected any. Specify a different type
getNotifications(before: string, count: number): Promise<any>;

Check warning on line 73 in lib/client.ts

View workflow job for this annotation

GitHub Actions / pull_request_pipeline

Unexpected any. Specify a different type
patchNotifications(params: any): Promise<any>;

Check warning on line 74 in lib/client.ts

View workflow job for this annotation

GitHub Actions / pull_request_pipeline

Unexpected any. Specify a different type

Check warning on line 74 in lib/client.ts

View workflow job for this annotation

GitHub Actions / pull_request_pipeline

Unexpected any. Specify a different type
getPreferences(): Promise<GetPreferencesResponse>;
postPreferences(
params: Array<{
Expand All @@ -78,7 +83,7 @@
| DeliveryOptionsForInappWeb
| BaseDeliveryOptions;
}>
): Promise<any>;

Check warning on line 86 in lib/client.ts

View workflow job for this annotation

GitHub Actions / pull_request_pipeline

Unexpected any. Specify a different type
postUser(params: PostUserRequest): Promise<any>;
getUserAccountMetadata(): Promise<{
userAccountMetadata: UserAccountMetadata;
Expand Down Expand Up @@ -119,6 +124,19 @@
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 @@
},
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
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.17",
"version": "1.0.0",
"type": "module",
"main": "dist/main.js",
"types": "dist/main.d.ts",
Expand Down
Loading