@@ -12,7 +12,7 @@ import rootPkgJson from '../package.json' with { type: 'json' }
12
12
import type { operations } from '../types/api'
13
13
import type { OpErrorType , OpReturnType } from '../types/api-helpers'
14
14
import type { ReadStream } from 'node:fs'
15
- import type { IncomingMessage } from 'node:http'
15
+ import type { ClientRequest , IncomingMessage } from 'node:http'
16
16
import type { Agent , RequestOptions } from 'node:https'
17
17
18
18
type BatchPackageFetchResultType = SocketSdkResultType < 'batchPackageFetch' >
@@ -67,16 +67,13 @@ async function createDeleteRequest(
67
67
urlPath : string ,
68
68
options : RequestOptions
69
69
) : Promise < IncomingMessage > {
70
- const req = https . request ( `${ baseUrl } ${ urlPath } ` , {
71
- method : 'DELETE' ,
72
- ...options
73
- } )
74
- const { 0 : res } = ( await events . once ( req , 'response' ) ) as [ IncomingMessage ]
75
- if ( ! isResponseOk ( res ) ) {
76
- req . destroy ( )
77
- throw new ResponseError ( res , 'Delete request failed' )
78
- }
79
- return res
70
+ const req = https
71
+ . request ( `${ baseUrl } ${ urlPath } ` , {
72
+ method : 'DELETE' ,
73
+ ...options
74
+ } )
75
+ . end ( )
76
+ return await getResponse ( req )
80
77
}
81
78
82
79
async function createGetRequest (
@@ -90,12 +87,7 @@ async function createGetRequest(
90
87
...options
91
88
} )
92
89
. end ( )
93
- const { 0 : res } = ( await events . once ( req , 'response' ) ) as [ IncomingMessage ]
94
- if ( ! isResponseOk ( res ) ) {
95
- req . destroy ( )
96
- throw new ResponseError ( res , 'Get request failed' )
97
- }
98
- return res
90
+ return await getResponse ( req )
99
91
}
100
92
101
93
async function createPostRequest (
@@ -110,12 +102,7 @@ async function createPostRequest(
110
102
...options
111
103
} )
112
104
. end ( JSON . stringify ( postJson ) )
113
- const { 0 : res } = ( await events . once ( req , 'response' ) ) as [ IncomingMessage ]
114
- if ( ! isResponseOk ( res ) ) {
115
- req . destroy ( )
116
- throw new ResponseError ( res , 'Post request failed' )
117
- }
118
- return res
105
+ return await getResponse ( req )
119
106
}
120
107
121
108
function createRequestBodyForFilepaths (
@@ -189,12 +176,21 @@ async function createUploadRequest(
189
176
}
190
177
// Close request after writing all data.
191
178
req . end ( )
179
+ return await getResponse ( req )
180
+ }
192
181
193
- const { 0 : res } = ( await events . once ( req , 'response' ) ) as [ IncomingMessage ]
194
- if ( ! isResponseOk ( res ) ) {
195
- throw new ResponseError ( res , 'Upload failed' )
182
+ async function getResponse ( req : ClientRequest ) : Promise < IncomingMessage > {
183
+ try {
184
+ const { 0 : res } = ( await events . once ( req , 'response' , {
185
+ signal : abortSignal
186
+ } ) ) as [ IncomingMessage ]
187
+ if ( ! isResponseOk ( res ) ) {
188
+ throw new ResponseError ( res , `${ req . method } request failed` )
189
+ }
190
+ return res
191
+ } finally {
192
+ req . destroy ( )
196
193
}
197
- return res
198
194
}
199
195
200
196
async function getResponseJson (
@@ -278,14 +274,7 @@ export class SocketSdk {
278
274
}
279
275
)
280
276
. end ( JSON . stringify ( componentsObj ) )
281
- // Adds the second 'abort' listener to abortSignal.
282
- const { 0 : res } = ( await events . once ( req , 'response' , {
283
- signal : abortSignal
284
- } ) ) as [ IncomingMessage ]
285
- if ( ! isResponseOk ( res ) ) {
286
- throw new ResponseError ( res , 'Batch purl request failed' )
287
- }
288
- return res
277
+ return await getResponse ( req )
289
278
}
290
279
291
280
async * #createBatchPurlGenerator(
@@ -665,10 +654,7 @@ export class SocketSdk {
665
654
...this . #reqOptions
666
655
}
667
656
)
668
- const { 0 : res } = await events . once ( req , 'response' )
669
- if ( ! isResponseOk ( res ) ) {
670
- throw new ResponseError ( res , 'Get request failed' )
671
- }
657
+ const res = await getResponse ( req )
672
658
if ( file ) {
673
659
res . pipe ( createWriteStream ( file ) )
674
660
} else {
0 commit comments