MCPcopy Create free account
hub / github.com/dzcode-io/dzcode.io / fetchV2

Function fetchV2

web/src/common/utils/fetch/index.ts:10–45  ·  view source on GitHub ↗
(
  endpoint: E,
  config: Pick<C, Exclude<D, "response">>,
)

Source from the content-addressed store, hash-verified

8}
9
10export const fetchV2 = async <
11 T extends Endpoints,
12 E extends keyof T,
13 C extends T[E],
14 D extends keyof C,
15>(
16 endpoint: E,
17 config: Pick<C, Exclude<D, "response">>,
18): Promise<C[D & "response"]> => {
19 const { body, params, query } = config as Endpoint;
20
21 const queryString = query ? "?" + query.map(([key, value]) => `${key}=${value}`).join("&") : "";
22
23 const domain = (endpoint as string).slice(0, (endpoint as string).indexOf(":"));
24 let url = (endpoint as string).slice(domain.length + 1);
25
26 if (params) {
27 Object.keys(params).forEach((param) => {
28 url = url.replace(`:${param}`, params[param]);
29 });
30 }
31 console.log(url);
32
33 let baseURL = "";
34
35 switch (domain) {
36 case "data":
37 baseURL = fullstackConfig.data.url;
38 break;
39 case "api":
40 baseURL = fullstackConfig.api.url;
41 break;
42 }
43 const response = await fetch(`${baseURL}/${url}${queryString}`, { body });
44 return (await response.json()) as C[D & "response"];
45};

Callers 13

fetchContributionsFunction · 0.90
fetchArticlesListFunction · 0.90
fetchCurrentArticleFunction · 0.90
fetchProjectsListFunction · 0.90
fetchTeamListFunction · 0.90
fetchDocumentationListFunction · 0.90
fetchCurrentDocumentFunction · 0.90
fetchTopProjectsFunction · 0.90

Calls 1

logMethod · 0.80

Tested by

no test coverage detected