Skip to content

Commit d91517b

Browse files
refactor: reduce cognitive complexity in HTTP auth resolver
1 parent 6ab043e commit d91517b

File tree

1 file changed

+58
-34
lines changed

1 file changed

+58
-34
lines changed

src/domains/services/validation.service.ts

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,50 @@ const convertGitHubWebUrl = (url: string): string => {
8181
return url;
8282
};
8383

84+
/**
85+
* Helper function to fetch with error handling
86+
*/
87+
const fetchWithErrorHandling = async (
88+
url: string,
89+
headers: Record<string, string>,
90+
errorMessage: string,
91+
): Promise<Response> => {
92+
const res = await fetch(url, { headers });
93+
if (!res.ok) {
94+
throw new Error(`${errorMessage}: ${url} - ${res.statusText}`);
95+
}
96+
return res;
97+
};
98+
99+
/**
100+
* Helper function to fetch content from GitHub API
101+
*/
102+
const fetchGitHubApiContent = async (
103+
url: string,
104+
headers: Record<string, string>,
105+
): Promise<string> => {
106+
headers['Accept'] = 'application/vnd.github.v3+json';
107+
const res = await fetchWithErrorHandling(
108+
url,
109+
headers,
110+
'Failed to fetch GitHub API URL',
111+
);
112+
const fileInfo = (await res.json()) as GitHubFileInfo;
113+
114+
if (!fileInfo.download_url) {
115+
throw new Error(
116+
`No download URL found in GitHub API response for: ${url}`,
117+
);
118+
}
119+
120+
const contentRes = await fetchWithErrorHandling(
121+
fileInfo.download_url,
122+
headers,
123+
'Failed to fetch content from download URL',
124+
);
125+
return await contentRes.text();
126+
};
127+
84128
/**
85129
* Custom resolver for private repositories
86130
*/
@@ -108,43 +152,23 @@ const createHttpWithAuthResolver = () => ({
108152
}
109153

110154
if (url.includes('api.github.com')) {
111-
headers['Accept'] = 'application/vnd.github.v3+json';
112-
const res = await fetch(url, { headers });
113-
if (!res.ok) {
114-
throw new Error(
115-
`Failed to fetch GitHub API URL: ${url} - ${res.statusText}`
116-
);
117-
}
118-
const fileInfo = (await res.json()) as GitHubFileInfo;
119-
120-
if (fileInfo.download_url) {
121-
const contentRes = await fetch(fileInfo.download_url, { headers });
122-
if (!contentRes.ok) {
123-
throw new Error(
124-
`Failed to fetch content from download URL: ${fileInfo.download_url} - ${contentRes.statusText}`
125-
);
126-
}
127-
return await contentRes.text();
128-
}
129-
throw new Error(
130-
`No download URL found in GitHub API response for: ${url}`
131-
);
132-
} else if (url.includes('raw.githubusercontent.com')) {
155+
return await fetchGitHubApiContent(url, headers);
156+
}
157+
if (url.includes('raw.githubusercontent.com')) {
133158
headers['Accept'] = 'application/vnd.github.v3.raw';
134-
const res = await fetch(url, { headers });
135-
if (!res.ok) {
136-
throw new Error(
137-
`Failed to fetch GitHub URL: ${url} - ${res.statusText}`
138-
);
139-
}
140-
return await res.text();
141-
} else {
142-
const res = await fetch(url, { headers });
143-
if (!res.ok) {
144-
throw new Error(`Failed to fetch URL: ${url} - ${res.statusText}`);
145-
}
159+
const res = await fetchWithErrorHandling(
160+
url,
161+
headers,
162+
'Failed to fetch GitHub URL',
163+
);
146164
return await res.text();
147165
}
166+
const res = await fetchWithErrorHandling(
167+
url,
168+
headers,
169+
'Failed to fetch URL',
170+
);
171+
return await res.text();
148172
},
149173
});
150174

0 commit comments

Comments
 (0)