-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpsFunc.js
More file actions
211 lines (182 loc) · 4.86 KB
/
Copy pathhttpsFunc.js
File metadata and controls
211 lines (182 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
const https = require( "https" )
class HttpsAdapter
{
constructor ( { hostname, adapter, path, port = 80 } )
{
const { headers } = adapter
this.hostname = hostname
this.headers = headers
this.prefix = path
this._maxRedirects = 3
this.port = port
}
/**
* @param {string} path
* @param {{ [x: string]: any; }} params
*/
__pathAppendingQueryString ( path, params )
{
// Exit early if there's nothing to append
if ( !( params instanceof Object ) || !Object.keys( params ).length )
{
return path
}
return `${ path }?${ Object.keys( params )
.map( function ( key )
{
return `${ key }=${ params[ key ] }`
} )
.join( "&" ) }`
}
__buildRequestOptions ( config )
{
const { hostname, headers } = this
return {
...config,
hostname,
headers,
}
}
/**
* @param {string} path
*/
__prefixedPath ( path )
{
const prefix = _.get( this, "prefix", false )
if ( !_.isString( prefix ) || _.isEmpty( prefix ) ) return path
return `/${ _.trim( prefix, "/" ) }/${ _.trimStart( path, "/" ) }`
}
__reset ()
{
/* Not Currently Used */
}
async _raw_request_ ( endpoint, __config )
{
var __redirectCount = _.get( __config, "__redirectCount", 0 )
const aUrl = ParseUrl( endpoint )
const options = {
hostname: aUrl.hostname,
path: `${ aUrl.pathname }${ aUrl.search }`,
method: "GET",
headers: this.headers,
__redirectCount,
}
return this.makeRequest( options, {} )
}
async __followRedirect ( response, config = {} )
{
let __redirectCount = config.__redirectCount || 0
if ( ++__redirectCount > this._maxRedirects )
{
throw "Too Many Redirects"
}
let location = response.redirectTo()
Logger.dev( "[ DEV ] Redirecting To", { location } )
return this._raw_request_( location, {
__redirectCount,
} )
}
__logResponse ( options, response )
{
const { method, hostname, path } = options
const url = `https://${ hostname }${ path }`
if ( response.isError() )
{
Logger.http( `Response: ${ response.status() } | ${ method } | ERROR | ${ url }`, { log_type: "HTTPS" } )
Logger.debug( `\n-------------------- Begin Error Response Body --------------------\n` )
Logger.debug( `${ response.body }` )
Logger.debug( `\n--------------------- End Error Response Body---------------------\n` )
}
else if ( response.isRedirect() )
{
Logger.warn( `Response: ${ response.status() } | ${ method } | REDIRECT | ${ path }`, { log_type: "HTTPS" } )
}
else
{
Logger.http( `Response: ${ response.status() } | ${ method } | OK | ${ url }`, { log_type: "HTTPS" } )
}
}
async makeRequest ( options, data = {} )
{
Logger.debug( `HttpsAdapter->makeRequest:${ JSON.stringify( options ) }` )
// Double-Negate Object to compare length=0
const payload = Object.keys( data ).length ? JSON.stringify( data ) : ""
options.rejectUnauthorized = false
options.timeout = 5000
return new Promise( ( resolve, reject ) =>
{
const req = https.request( options, ( res ) =>
{
var body = ""
res.on( "data", ( chunk ) =>
{
body += chunk
} )
res.on( "end", async () =>
{
const response = new Response( body, res.statusCode, res.headers )
this.__logResponse( options, response )
if ( response.isRedirect() )
{
var __redirectCount = _.get( options, "__redirectCount", 0 )
const redirectResponse = await this.__followRedirect( response, {
__redirectCount,
} )
resolve( redirectResponse )
}
if ( response.isSuccess() )
{
HTTP_STATUS = res.statusCode
Logger.http( response )
resolve( response )
}
else
{
HTTP_STATUS = res.statusCode
Logger.http( response )
reject( response )
}
this.__reset()
} )
} )
req.on( "error", ( error ) =>
{
Logger.log( `HTTP ERROR: ${ JSON.stringify( error ) }` )
const message = `Request Error https://${ options.hostname }/${ options.path }`
var response = new Response( JSON.stringify( { message, error } ), 500, {} )
reject( response )
} )
req.write( payload )
req.end()
} )
}
async __ANY ( method, endpoint, params )
{
const options = this.__buildRequestOptions( {
method: method,
path: this.__prefixedPath( endpoint ),
} )
return this.makeRequest( options, params )
}
async get ( endpoint, params )
{
Logger.debug( `HttpsAdapter->get:${ endpoint }` )
const path = this.__pathAppendingQueryString( endpoint, params )
return this.__ANY( "GET", path )
}
async post ( endpoint, params )
{
Logger.debug( `HttpsAdapter->post:${ endpoint }` )
return this.__ANY( "POST", endpoint, params )
}
async put ( endpoint, params )
{
Logger.debug( `HttpsAdapter->put:${ endpoint }` )
return this.__ANY( "PUT", endpoint, params )
}
async delete ( endpoint, params )
{
Logger.debug( `HttpsAdapter->delete:${ endpoint }` )
return this.__ANY( "DELETE", endpoint, params )
}
}