1
- import { el } from " @faker-js/faker" ;
2
- import { APIRequestContext , APIResponse , expect } from " @playwright/test" ;
1
+ import { ur } from ' @faker-js/faker' ;
2
+ import { APIRequestContext , APIResponse } from ' @playwright/test' ;
3
3
4
- export enum RequestMethods {
4
+ export enum RequestMethod {
5
5
GET = 'GET' ,
6
6
POST = 'POST' ,
7
7
PUT = 'PUT' ,
@@ -20,6 +20,13 @@ export enum StatusCode {
20
20
SERVER_ERROR = 500 ,
21
21
}
22
22
23
+ export enum PaginationType {
24
+ PAGE_PAGINATION = 'page' ,
25
+ OFFSET_PAGINATION = 'offset' ,
26
+ CURSOR_PAGINATION = 'cursor' ,
27
+ }
28
+
29
+
23
30
export interface ApiOptionalParams < T > {
24
31
responseDataKey ?: string ,
25
32
queryParams ?: { [ key : string ] : any } ,
@@ -33,18 +40,21 @@ export interface ApiOptionalParams<T> {
33
40
pageNumber ?: number ,
34
41
limit ?: number ,
35
42
offset ?: number ,
43
+ cursor ?: boolean ,
44
+ cursorKey ?: string ,
45
+ paginationType ?: PaginationType ,
46
+ responseKey ?: string ,
36
47
}
37
48
38
-
39
49
export class ApiClient {
40
- constructor ( public apiRequestContext : APIRequestContext ) {
41
- this . apiRequestContext = apiRequestContext
50
+ constructor ( public request : APIRequestContext ) {
51
+ this . request = request ;
42
52
}
43
53
44
54
/**
45
- * @description resuable code to add the authorization header if an authorization is requiired to make the request
46
- * @param headers
47
- */
55
+ * @description resuable code to add the authorization header if an authorization is requiired to make the request
56
+ * @param headers
57
+ */
48
58
private async addAuthorizationHeader ( headers : { [ key : string ] : string } ) {
49
59
headers [ 'Authorization' ] = `Bearer ${ process . env . API_TOKEN } `
50
60
}
@@ -56,7 +66,7 @@ export class ApiClient {
56
66
* @param options
57
67
* @returns
58
68
*/
59
- private async makeRequest < T > ( method : RequestMethods , url : string , options ?: ApiOptionalParams < T > ) : Promise < APIResponse | undefined > {
69
+ private async makeRequest < T > ( method : RequestMethod , url : string , options ?: ApiOptionalParams < T > ) : Promise < APIResponse | undefined > {
60
70
let response : APIResponse | undefined
61
71
let headers : Record < string , string > = {
62
72
'Accept' : '*/*'
@@ -71,106 +81,107 @@ export class ApiClient {
71
81
}
72
82
switch ( method . valueOf ( ) ) {
73
83
case 'GET' :
74
- response = await this . apiRequestContext . get ( url , { headers, params : options ?. queryParams } )
84
+ response = await this . request . get ( url , { headers, params : options ?. queryParams } )
75
85
break ;
76
86
case 'POST' :
77
- response = await this . apiRequestContext . post ( url , { headers, data : options ?. requestData , multipart : options ?. multiPartData ! } )
87
+ response = await this . request . post ( url , { headers, data : options ?. requestData , multipart : options ?. multiPartData ! } )
78
88
break ;
79
89
case 'PUT' :
80
- response = await this . apiRequestContext . put ( url , { headers, data : options ?. requestData , multipart : options ?. multiPartData ! } )
90
+ response = await this . request . put ( url , { headers, data : options ?. requestData , multipart : options ?. multiPartData ! } )
81
91
break ;
82
92
case 'PATCH' :
83
- response = await this . apiRequestContext . patch ( url , { headers, data : options ?. requestData , multipart : options ?. multiPartData ! } )
93
+ response = await this . request . patch ( url , { headers, data : options ?. requestData , multipart : options ?. multiPartData ! } )
84
94
break ;
85
95
case 'DELETE' :
86
- response = await this . apiRequestContext . delete ( url )
96
+ response = await this . request . delete ( url )
87
97
break ;
88
98
}
89
99
return response
90
100
}
91
101
92
- /**
93
- * @description function that supports pagination via page pagination or by limit and offset pagination
94
- */
95
- protected async paginateRequest < T > ( method : RequestMethods , url : string , options ?: ApiOptionalParams < T > ) {
96
- let existingQueryParams = { ...options ?. queryParams }
97
- let response : APIResponse | undefined
98
- let responses : APIResponse [ ] = [ ]
99
- try {
100
- while ( true ) {
101
- if ( options ?. pagePagination && options ?. pageNumber !== undefined ) {
102
- existingQueryParams [ 'page' ] = options . pageNumber
103
- response = await this . makeRequest ( method , url , { queryParams : existingQueryParams , requestData : options . requestData , authoriaztionRequired : options . authoriaztionRequired } )
104
- let responseObject = await response ?. json ( )
105
- if ( ! responseObject || responseObject . length === 0 ) {
106
- break
107
- }
108
- responses . push ( ...responseObject )
109
- options . pageNumber ++
110
- }
111
- if ( options ?. limitOffsetPagination ) {
112
- existingQueryParams [ 'limit' ] = options . limit
113
- existingQueryParams [ 'offset' ] = options . offset
114
- response = await this . makeRequest ( method , url , { queryParams : existingQueryParams , requestData : options . requestData , authoriaztionRequired : options . authoriaztionRequired } )
115
- let responseObject = await response ?. json ( )
116
- if ( ! responseObject || responseObject . length === 0 ) {
117
- break
118
- }
119
- if ( options . responseDataKey !== undefined ) {
120
- let responseKey = responseObject [ options . responseDataKey ]
121
- if ( responseKey . length === 0 ) {
122
- break ;
123
- }
124
- responses . push ( ...responseKey )
125
- } else {
126
- if ( Array . isArray ( responseObject ) ) {
127
- responses . push ( ...responseObject )
128
- } else {
129
- responses . push ( responseObject )
130
- }
131
- }
132
- if ( options . offset !== undefined && options . limit !== undefined ) {
133
- options . offset += options . limit
134
- }
102
+
103
+ private async paginateBy < T > ( method : RequestMethod , url : string , paginationType : PaginationType , options ?: ApiOptionalParams < T > ) {
104
+ let response : APIResponse | undefined ;
105
+ let responses : APIResponse [ ] = [ ] ;
106
+ let queryParams = options ?. queryParams ? { ...options . queryParams } : { } ;
107
+ while ( true ) {
108
+ if ( paginationType === PaginationType . PAGE_PAGINATION && options ?. pageNumber !== undefined ) {
109
+ queryParams = { ...queryParams , 'page' : options . pageNumber } ;
110
+ } else if ( paginationType === PaginationType . OFFSET_PAGINATION && options ?. limit !== undefined && options . offset !== undefined ) {
111
+ queryParams = { ...queryParams , 'limit' : options . limit , 'offset' : options . offset } ;
112
+ }
113
+ response = await this . makeRequest ( method , url , { ...options , queryParams } ) ;
114
+ let responseObj = await response ?. json ( ) ;
115
+ if ( ! responseObj || responseObj . length === 0 ) {
116
+ break ;
117
+ }
118
+ if ( options ?. responseKey ) {
119
+ let responseKey = responseObj [ options . responseKey ] ;
120
+ if ( responseKey . length === 0 ) {
121
+ break ;
135
122
}
123
+ await this . handleResponseObject ( responses , responseKey ) ;
124
+ } else {
125
+ await this . handleResponseObject ( responses , responseObj ) ;
136
126
}
137
- return responses
127
+ if ( paginationType === PaginationType . PAGE_PAGINATION && options ?. pageNumber !== undefined ) {
128
+ options . pageNumber ++ ;
129
+ } else if ( paginationType === PaginationType . OFFSET_PAGINATION && options ?. offset !== undefined && options . limit !== undefined ) {
130
+ options . offset += options . limit ;
131
+ }
132
+ }
138
133
134
+ return responses ;
135
+ }
136
+ /**
137
+ * @description handle the response object by spreading it to an existing array if the response is already an array otherwise push directly
138
+ * to the array.
139
+ * @param responseObj
140
+ */
141
+ private async handleResponseObject ( responses : APIResponse [ ] , responseObj : any ) {
142
+ if ( Array . isArray ( responseObj ) ) {
143
+ responses . push ( ...responseObj )
144
+ } else {
145
+ responses . push ( responseObj ) ;
146
+ }
147
+ }
148
+
149
+ public async paginateRequest < T > ( method : RequestMethod , url : string , pagintionType : PaginationType , options : ApiOptionalParams < T > ) {
150
+ try {
151
+ let response = await this . paginateBy ( method , url , pagintionType , options ) ;
152
+ return response ;
139
153
} catch ( error ) {
140
- throw new Error ( `caught an error in the paginate request function: ${ error } ` )
154
+ throw new Error ( `an error occured in the paginate request function: ${ error } ` )
141
155
}
142
156
}
143
157
144
- /**
145
- * @description http request that abstracts the logic behind the scenes
146
- */
147
- private async httpRequest < T > ( method : RequestMethods , url : string , options ?: ApiOptionalParams < T > ) {
158
+ private async makeHttpRequest < T > ( method : RequestMethod , url : string , options ?: ApiOptionalParams < T > ) {
148
159
let response = await this . makeRequest ( method , url , options )
149
160
return response ;
150
161
}
151
162
152
163
public async get < T > ( url : string , options ?: ApiOptionalParams < T > ) {
153
- let response = await this . httpRequest ( RequestMethods . GET , url , options )
154
- return response
164
+ let response = await this . makeHttpRequest ( RequestMethod . GET , url , options )
165
+ return response ;
155
166
}
156
167
157
168
public async post < T > ( url : string , options ?: ApiOptionalParams < T > ) {
158
- let response = await this . httpRequest ( RequestMethods . POST , url , options )
159
- return response
169
+ let response = await this . makeHttpRequest ( RequestMethod . POST , url , options )
170
+ return response ;
160
171
}
161
172
162
173
public async put < T > ( url : string , options ?: ApiOptionalParams < T > ) {
163
- let response = await this . httpRequest ( RequestMethods . PUT , url , options ) ;
164
- return response
174
+ let response = await this . makeHttpRequest ( RequestMethod . PUT , url , options )
175
+ return response ;
165
176
}
166
177
167
178
public async patch < T > ( url : string , options ?: ApiOptionalParams < T > ) {
168
- let response = await this . httpRequest ( RequestMethods . PATCH , url , options ) ;
179
+ let response = await this . makeHttpRequest ( RequestMethod . PATCH , url , options )
169
180
return response ;
170
181
}
171
182
172
183
public async delete < T > ( url : string , options ?: ApiOptionalParams < T > ) {
173
- let response = await this . httpRequest ( RequestMethods . DELETE , url , options ) ;
184
+ let response = await this . makeHttpRequest ( RequestMethod . DELETE , url , options )
174
185
return response ;
175
186
}
176
187
}
0 commit comments