Skip to content

Commit 1ed0448

Browse files
refactor: Modify fetchLite() to works with options, instead of direct params
1 parent 1611ba6 commit 1ed0448

File tree

4 files changed

+37
-35
lines changed

4 files changed

+37
-35
lines changed

src/NPM.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ export const getPackageVersions = async (
3535
// This avoids loading processes via `npm view`.
3636
// The process is cached if it is triggered quickly, within lifetime.
3737
const execPromise = new Promise<string[] | null>((resolve) =>
38-
fetchLite<NPMRegistryPackage>(`https://registry.npmjs.org/${name}`).then(
39-
(data) => {
40-
if (data?.versions) {
41-
return resolve(Object.keys(data.versions))
42-
}
43-
44-
return resolve(null)
38+
fetchLite<NPMRegistryPackage>({
39+
url: `https://registry.npmjs.org/${name}`,
40+
}).then((data) => {
41+
if (data?.versions) {
42+
return resolve(Object.keys(data.versions))
4543
}
46-
)
44+
45+
return resolve(null)
46+
})
4747
)
4848

4949
packagesCache.set(name, new Cache(execPromise))
@@ -147,11 +147,11 @@ export const getPackagesAdvisories = async (
147147

148148
if (Object.keys(packages).length) {
149149
// Query advisories through the NPM Registry.
150-
const responseAdvisories = await fetchLite<PackagesAdvisories | undefined>(
151-
"https://registry.npmjs.org/-/npm/v1/security/advisories/bulk",
152-
"post",
153-
packages
154-
)
150+
const responseAdvisories = await fetchLite<PackagesAdvisories | undefined>({
151+
body: packages,
152+
method: "post",
153+
url: "https://registry.npmjs.org/-/npm/v1/security/advisories/bulk",
154+
})
155155

156156
// Fills the packages with their respective advisories.
157157
if (responseAdvisories) {

src/TestUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export const vscodeSimulator = async (options: SimulatorOptions = {}) => {
156156

157157
UtilsMock.cacheEnabled = (): boolean => options.cacheEnabled === true
158158

159-
UtilsMock.fetchLite = (url: string): unknown => {
159+
UtilsMock.fetchLite = ({ url }: { url: string }): unknown => {
160160
if (url.endsWith("/bulk")) {
161161
return options.packagesAdvisories
162162
}

src/Utils.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,39 +136,39 @@ describe("utils", () => {
136136
it("fetchLite: access to NPM Registry (advisories)", async () => {
137137
expect.assertions(1)
138138

139-
const fetchSuccess = await fetchLite(
140-
"https://registry.npmjs.org/-/npm/v1/security/advisories/bulk",
141-
"post",
142-
{ "npm-outdated": ["2.0.3"] }
143-
)
139+
const fetchSuccess = await fetchLite({
140+
body: { "npm-outdated": ["2.0.3"] },
141+
method: "post",
142+
url: "https://registry.npmjs.org/-/npm/v1/security/advisories/bulk",
143+
})
144144

145145
expect(fetchSuccess).toStrictEqual({})
146146
})
147147

148148
it("fetchLite: access to NPM Registry (package)", async () => {
149149
expect.assertions(1)
150150

151-
const fetchSuccess = await fetchLite(
152-
"https://registry.npmjs.org/node-fetch"
153-
)
151+
const fetchSuccess = await fetchLite({
152+
url: "https://registry.npmjs.org/node-fetch",
153+
})
154154

155155
expect(fetchSuccess).toBeInstanceOf(Object)
156156
})
157157

158158
it("fetchLite: access to a private NPM Registry without auth token", async () => {
159159
expect.assertions(1)
160160

161-
const fetchSuccess = await fetchLite<{ error: string }>(
162-
"https://registry.npmjs.org/@fortawesome/pro-light-svg-icons"
163-
)
161+
const fetchSuccess = await fetchLite<{ error: string }>({
162+
url: "https://registry.npmjs.org/@fortawesome/pro-light-svg-icons",
163+
})
164164

165165
expect(fetchSuccess?.error).toBe("Not found")
166166
})
167167

168168
it("fetchLite: invalid URL", async () => {
169169
expect.assertions(1)
170170

171-
const fetchSuccess = await fetchLite("invalid")
171+
const fetchSuccess = await fetchLite({ url: "invalid" })
172172

173173
expect(fetchSuccess).toBeUndefined()
174174
})

src/Utils.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,19 +120,21 @@ export const promiseLimit = (
120120
// During testing, this function is mocked to return false in some cases.
121121
export const cacheEnabled = (): boolean => true
122122

123+
interface FetchLite {
124+
body?: object
125+
method?: "get" | "post"
126+
url: string
127+
}
128+
123129
// A simple post request.
124130
// Based on https://github.com/vasanthv/fetch-lite/blob/master/index.js
125-
export const fetchLite = <T>(
126-
paramUrl: string,
127-
method?: "get" | "post" | undefined,
128-
body?: object | undefined
129-
): Promise<T | undefined> => {
131+
export const fetchLite = <T>(options: FetchLite): Promise<T | undefined> => {
130132
return new Promise<T | undefined>((resolve) => {
131-
const { hostname, path } = url.parse(paramUrl)
133+
const { hostname, path } = url.parse(options.url)
132134
const headers = { "content-type": "application/json" }
133135

134136
const thisReq = https.request(
135-
{ headers, hostname, method: method ?? "get", path },
137+
{ headers, hostname, method: options.method ?? "get", path },
136138
(response: IncomingMessage) => {
137139
const responseBuffers: Buffer[] = []
138140

@@ -158,8 +160,8 @@ export const fetchLite = <T>(
158160
thisReq.setHeader("Content-Encoding", "gzip")
159161
thisReq.setHeader("Accept-Encoding", "gzip")
160162

161-
if (body !== undefined) {
162-
const bodyStringify = zlib.gzipSync(JSON.stringify(body))
163+
if (options.body !== undefined) {
164+
const bodyStringify = zlib.gzipSync(JSON.stringify(options.body))
163165

164166
thisReq.setHeader("Content-Length", bodyStringify.length)
165167
thisReq.write(bodyStringify)

0 commit comments

Comments
 (0)