Skip to content

Commit 27871ac

Browse files
committed
Abstract out getResponse
1 parent 3b58324 commit 27871ac

File tree

1 file changed

+25
-39
lines changed

1 file changed

+25
-39
lines changed

src/index.ts

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import rootPkgJson from '../package.json' with { type: 'json' }
1212
import type { operations } from '../types/api'
1313
import type { OpErrorType, OpReturnType } from '../types/api-helpers'
1414
import type { ReadStream } from 'node:fs'
15-
import type { IncomingMessage } from 'node:http'
15+
import type { ClientRequest, IncomingMessage } from 'node:http'
1616
import type { Agent, RequestOptions } from 'node:https'
1717

1818
type BatchPackageFetchResultType = SocketSdkResultType<'batchPackageFetch'>
@@ -67,16 +67,13 @@ async function createDeleteRequest(
6767
urlPath: string,
6868
options: RequestOptions
6969
): 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)
8077
}
8178

8279
async function createGetRequest(
@@ -90,12 +87,7 @@ async function createGetRequest(
9087
...options
9188
})
9289
.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)
9991
}
10092

10193
async function createPostRequest(
@@ -110,12 +102,7 @@ async function createPostRequest(
110102
...options
111103
})
112104
.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)
119106
}
120107

121108
function createRequestBodyForFilepaths(
@@ -189,12 +176,21 @@ async function createUploadRequest(
189176
}
190177
// Close request after writing all data.
191178
req.end()
179+
return await getResponse(req)
180+
}
192181

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()
196193
}
197-
return res
198194
}
199195

200196
async function getResponseJson(
@@ -278,14 +274,7 @@ export class SocketSdk {
278274
}
279275
)
280276
.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)
289278
}
290279

291280
async *#createBatchPurlGenerator(
@@ -665,10 +654,7 @@ export class SocketSdk {
665654
...this.#reqOptions
666655
}
667656
)
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)
672658
if (file) {
673659
res.pipe(createWriteStream(file))
674660
} else {

0 commit comments

Comments
 (0)