Skip to content

Commit 6128908

Browse files
VoloVolo
authored andcommitted
Clean up code
1 parent 9060c56 commit 6128908

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1100
-1567
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ import { Client } from './dist/index';
4646
Create a client using access tokens:
4747

4848
```typescript
49-
const client = new Client({ token: 'my_token' });
49+
const client = new Client({ tokenAuth: { token: 'my_token' } });
5050
```
5151

5252
## Request Options
5353

5454
This client library also supports passing in [`request` options](https://github.com/axios/axios#request-config):
5555

5656
```typescript
57-
const client = new Client({ token: 'my_token' });
57+
const client = new Client({ tokenAuth: { token: 'my_token' } });
5858
client.useRequestOpts({
5959
baseURL: 'http://local.test-server.com',
6060
});
@@ -67,7 +67,7 @@ Note that certain request options (such as `json`, and certain `headers` names c
6767
We version our API (see the "Choose Version" section of the [API & Webhooks Reference](https://developers.intercom.com/intercom-api-reference/reference) for details). You can specify which version of the API to use when performing API requests using request options:
6868

6969
```typescript
70-
const client = new Client({ token: 'my_token' });
70+
const client = new Client({ tokenAuth: { token: 'my_token' } });
7171
client.useRequestOpts({
7272
headers: {
7373
'Intercom-Version': 2.4,

lib/client.ts

Lines changed: 62 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,88 @@
11
import { deprecate } from 'util';
2-
import User from './user';
3-
import Event from './event';
2+
import axios, { Axios, AxiosDefaults, AxiosResponse } from 'axios';
3+
import { merge, omit } from 'lodash';
4+
5+
import Admin from './admin';
46
import Company from './company';
7+
import Conversation from './conversation';
58
import Contact from './contact';
6-
import Visitor from './visitor';
7-
import Counts from './counts';
8-
import Admin from './admin';
9-
import Tag from './tag';
9+
import DataAttribute from './dataAttribute';
10+
import Event from './event';
1011
import Segment from './segment';
1112
import Message from './message';
12-
import Conversation from './conversation';
13-
import Note from './note';
14-
import Customer from './customer';
15-
import DataAttribute from './dataAttribute';
1613
import Team from './team';
17-
18-
import axios, { Axios, AxiosDefaults, AxiosResponse } from 'axios';
19-
import { merge, omit } from 'lodash';
14+
import Tag from './tag';
2015

2116
import { BadResponseError } from './errors/badResponse.error';
2217

2318
interface RequestOptions {
2419
url: string;
20+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2521
data?: any;
22+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2623
params?: any;
2724
}
2825

26+
type Constructor = {
27+
usernameAuth?: UsernameAuth;
28+
tokenAuth?: TokenAuth;
29+
apiKeyAuth?: ApiKeyAuth;
30+
};
31+
32+
type UsernameAuth = {
33+
username: string;
34+
password: string;
35+
};
36+
37+
type TokenAuth = {
38+
token: string;
39+
};
40+
41+
type ApiKeyAuth = {
42+
appId: string;
43+
appApiKey: string;
44+
};
45+
2946
export default class Client {
3047
admins: Admin;
3148
axiosInstance: Axios;
3249
companies: Company;
3350
contacts: Contact;
3451
conversations: Conversation;
35-
counts: any;
36-
customers: any;
37-
leads: any;
38-
users: any;
39-
events: Event;
4052
dataAttributes: DataAttribute;
53+
events: Event;
54+
messages: Message;
4155
segments: Segment;
42-
messages: any;
43-
notes: any;
4456
passwordPart?: string;
4557
propertiesToOmitInRequestOpts: string[];
4658
requestOpts: Partial<AxiosDefaults>;
4759
tags: Tag;
4860
teams: Team;
49-
usebaseURL: (baseURL: any) => this;
61+
usebaseURL: (baseURL: string) => this;
5062
usernamePart?: string;
51-
visitors: any;
52-
53-
// TO-DO: Fix any
54-
constructor(...args: any) {
55-
// TO-DO: Refactor it!
56-
if (args.length === 2) {
57-
this.usernamePart = args[0];
58-
this.passwordPart = args[1];
59-
} else if (args.length === 1) {
60-
if (args[0].token) {
61-
this.usernamePart = args[0].token;
62-
this.passwordPart = '';
63-
} else {
64-
this.usernamePart = args[0].appId;
65-
this.passwordPart = args[0].appApiKey;
66-
}
67-
}
63+
64+
constructor(args: Constructor) {
65+
const [usernamePart, passwordPart] = Client.getAuthDetails(args);
66+
67+
this.usernamePart = usernamePart;
68+
this.passwordPart = passwordPart;
69+
6870
if (!this.usernamePart || this.passwordPart === undefined) {
6971
throw new Error(
7072
'Could not construct a client with those parameters'
7173
);
7274
}
73-
this.users = new User(this);
74-
this.events = new Event(this);
75+
76+
this.admins = new Admin(this);
7577
this.companies = new Company(this);
7678
this.contacts = new Contact(this);
77-
this.leads = new Contact(this);
78-
this.visitors = new Visitor(this);
79-
this.counts = new Counts(this);
80-
this.admins = new Admin(this);
81-
this.segments = new Segment(this);
82-
this.messages = new Message(this);
8379
this.conversations = new Conversation(this);
84-
this.notes = new Note(this);
85-
this.customers = new Customer(this);
80+
this.dataAttributes = new DataAttribute(this);
81+
this.events = new Event(this);
82+
this.messages = new Message(this);
83+
this.segments = new Segment(this);
8684
this.tags = new Tag(this);
8785
this.teams = new Team(this);
88-
this.dataAttributes = new DataAttribute(this);
8986
this.requestOpts = {
9087
baseURL: 'https://api.intercom.io',
9188
};
@@ -99,7 +96,6 @@ export default class Client {
9996
this.axiosInstance = this.initiateAxiosInstance();
10097
}
10198
initiateAxiosInstance(): Axios {
102-
// TO-DO: Revise the params
10399
const defaultHeaders = {
104100
'User-Agent': 'intercom-node-client/3.0.0',
105101
Accept: 'application/json',
@@ -256,4 +252,20 @@ export default class Client {
256252
status
257253
);
258254
}
255+
256+
private static getAuthDetails(
257+
args: Constructor
258+
): [username: string | undefined, password: string | undefined] {
259+
if (args.apiKeyAuth) {
260+
return [args.apiKeyAuth.appId, args.apiKeyAuth.appApiKey];
261+
}
262+
if (args.tokenAuth) {
263+
return [args.tokenAuth.token, ''];
264+
}
265+
if (args.usernameAuth) {
266+
return [args.usernameAuth.username, args.usernameAuth.password];
267+
}
268+
269+
return [undefined, undefined];
270+
}
259271
}

lib/conversation/conversation.types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { AdminObject } from '../admin/admin.types';
12
import { JavascriptObject, Seconds, Timestamp } from '../common/common.types';
23

34
export interface ConversationObject {
@@ -88,7 +89,7 @@ interface StatisticsObject {
8889
last_contact_reply_at: Seconds;
8990
last_admin_reply_at: Seconds;
9091
last_close_at: Seconds;
91-
last_closed_by: JavascriptObject; // TO-DO : Admin reference
92+
last_closed_by: AdminObject;
9293
count_reopens: number;
9394
count_assignments: number;
9495
count_conversations_parts: number;

lib/counts.js

Lines changed: 0 additions & 50 deletions
This file was deleted.

lib/customer.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

lib/event.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,14 @@ export default class Event {
4242
summary,
4343
};
4444

45-
// TO-DO: Change to `params` from `data: params`
4645
return summary
4746
? this.client.get<ListParamsWithSummaryResponse>({
4847
url: `/${this.baseUrl}`,
49-
data: params,
48+
params,
5049
})
5150
: this.client.get<ListParamsResponse>({
5251
url: `/${this.baseUrl}`,
53-
data: params,
52+
params,
5453
});
5554
}
5655
}

lib/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
export { default as Client } from './client';
2-
export { default as User } from './user';
3-
export { default as Snippet } from './snippet';
4-
export { default as UserData } from './user-data';
5-
62
import crypto from 'crypto';
73

84
export class IdentityVerification {
9-
static userHash(params: any) {
10-
let secretKey = params.secretKey;
11-
let identifier = params.identifier;
5+
static userHash({
6+
secretKey,
7+
identifier,
8+
}: {
9+
secretKey?: string;
10+
identifier?: string;
11+
}) {
1212
if (!secretKey) {
1313
throw new Error('secretKey must be provided');
1414
}

lib/note.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

lib/segment.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,15 @@ export default class Segment {
1212

1313
return this.client.get<ListResponse>({
1414
url: `/${this.baseUrl}`,
15-
//TO-DO: Change to `params`
16-
data: params,
15+
params,
1716
});
1817
}
1918
find({ id, includeCount: include_count }: FindSegmentData) {
2019
const params = { include_count };
2120

2221
return this.client.get<SegmentObject>({
2322
url: `/${this.baseUrl}/${id}`,
24-
//TO-DO: Change to `params`
25-
data: params,
23+
params,
2624
});
2725
}
2826
}

0 commit comments

Comments
 (0)