11import { 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' ;
46import Company from './company' ;
7+ import Conversation from './conversation' ;
58import 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' ;
1011import Segment from './segment' ;
1112import Message from './message' ;
12- import Conversation from './conversation' ;
13- import Note from './note' ;
14- import Customer from './customer' ;
15- import DataAttribute from './dataAttribute' ;
1613import Team from './team' ;
17-
18- import axios , { Axios , AxiosDefaults , AxiosResponse } from 'axios' ;
19- import { merge , omit } from 'lodash' ;
14+ import Tag from './tag' ;
2015
2116import { BadResponseError } from './errors/badResponse.error' ;
2217
2318interface 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+
2946export 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}
0 commit comments