MCPcopy Index your code
hub / github.com/code-dot-org/code-dot-org / sendRequest

Function sendRequest

apps/src/util/HttpClient.ts:85–109  ·  view source on GitHub ↗

* Sends a request to the given endpoint. Adds the Rails authenticity * token if useAuthenticityToken is true.

(
  method: string,
  endpoint: string,
  body?: BodyInit,
  useAuthenticityToken = false,
  headers: Record<string, string> = {}
)

Source from the content-addressed store, hash-verified

83 * token if useAuthenticityToken is true.
84 */
85async function sendRequest(
86 method: string,
87 endpoint: string,
88 body?: BodyInit,
89 useAuthenticityToken = false,
90 headers: Record<string, string> = {}
91): Promise<Response> {
92 if (useAuthenticityToken) {
93 const token = await getAuthenticityToken();
94 headers[AUTHENTICITY_TOKEN_HEADER] = token;
95 }
96 const response = await fetch(endpoint, {
97 method,
98 body,
99 headers,
100 });
101 if (!response.ok) {
102 throw new NetworkError(
103 response.status + ' ' + response.statusText,
104 response
105 );
106 }
107
108 return response;
109}
110
111/**
112 * Performs a GET request to the given endpoint. Use {@link fetchJson}

Callers 4

getFunction · 0.85
putFunction · 0.85
postFunction · 0.85
deleteRequestFunction · 0.85

Calls 2

getAuthenticityTokenFunction · 0.90
fetchFunction · 0.50

Tested by

no test coverage detected