* 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> = {}
)
| 83 | * token if useAuthenticityToken is true. |
| 84 | */ |
| 85 | async 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} |
no test coverage detected