Fetch a URL with the given options. This method performs an HTTP request to the specified URL using the provided options. It returns a promise that resolves to an ImpitResponse object containing the response data. This method is designed to be API-compatible with the https://developer.mozilla.org/en-US/docs/Web/API/fetch | Fetch API `fetch` global method. @example ```ts import {
(
&self,
url: String,
request_init: Option<RequestInit>,
)
| 112 | /// }); |
| 113 | /// ``` |
| 114 | pub async fn fetch( |
| 115 | &self, |
| 116 | url: String, |
| 117 | request_init: Option<RequestInit>, |
| 118 | ) -> Result<ImpitResponse, napi::Error> { |
| 119 | let request_options = Some(RequestOptions { |
| 120 | headers: request_init |
| 121 | .as_ref() |
| 122 | .and_then(|init| init.headers.as_ref()) |
| 123 | .cloned() |
| 124 | .unwrap_or_default(), |
| 125 | timeout: request_init |
| 126 | .as_ref() |
| 127 | .and_then(|init| init.timeout) |
| 128 | .map(|timeout| Some(Duration::from_millis(timeout.into()))), |
| 129 | http3_prior_knowledge: request_init |
| 130 | .as_ref() |
| 131 | .and_then(|init| init.force_http3) |
| 132 | .unwrap_or_default(), |
| 133 | }); |
| 134 | |
| 135 | let method = request_init |
| 136 | .as_ref() |
| 137 | .and_then(|init| init.method.to_owned()) |
| 138 | .unwrap_or_default(); |
| 139 | let body = request_init |
| 140 | .and_then(|init| init.body) |
| 141 | .map(|array| array.to_vec()); |
| 142 | |
| 143 | let response = if matches!(method, HttpMethod::Get | HttpMethod::Head) && body.is_some() { |
| 144 | Err(ImpitError::BindingPassthroughError( |
| 145 | "GET/HEAD methods don't support passing a request body".to_string(), |
| 146 | )) |
| 147 | } else { |
| 148 | // Match the HTTP method and execute the corresponding request |
| 149 | match method { |
| 150 | HttpMethod::Get => self.inner.get(url, body, request_options).await, |
| 151 | HttpMethod::Head => self.inner.head(url, body, request_options).await, |
| 152 | HttpMethod::Post => self.inner.post(url, body, request_options).await, |
| 153 | HttpMethod::Put => self.inner.put(url, body, request_options).await, |
| 154 | HttpMethod::Delete => self.inner.delete(url, body, request_options).await, |
| 155 | HttpMethod::Patch => self.inner.patch(url, body, request_options).await, |
| 156 | HttpMethod::Options => self.inner.options(url, body, request_options).await, |
| 157 | HttpMethod::Trace => self.inner.trace(url, body, request_options).await, |
| 158 | } |
| 159 | }; |
| 160 | |
| 161 | match response { |
| 162 | Ok(response) => ImpitResponse::try_from_response(response), |
| 163 | Err(err) => { |
| 164 | let status = match err { |
| 165 | ImpitError::UrlMissingHostnameError(_) => napi::Status::InvalidArg, |
| 166 | ImpitError::UrlProtocolError(_) => napi::Status::InvalidArg, |
| 167 | ImpitError::UrlParsingError(_) => napi::Status::InvalidArg, |
| 168 | ImpitError::InvalidMethod(_) => napi::Status::InvalidArg, |
| 169 | _ => napi::Status::GenericFailure, |
| 170 | }; |
| 171 |
no test coverage detected