Skip to main content

yewlish_fetch_utils/
fetch.rs

1use crate::{
2    helpers::{build_request, build_url, send_request},
3    FetchError, Middleware,
4};
5use serde::Serialize;
6use std::rc::Rc;
7
8/// Enum representing supported HTTP methods.
9#[derive(Debug)]
10pub enum HttpMethod {
11    GET,
12    POST,
13    PUT,
14    DELETE,
15    PATCH,
16}
17
18impl HttpMethod {
19    #[must_use]
20    pub fn as_str(&self) -> &str {
21        match self {
22            HttpMethod::GET => "GET",
23            HttpMethod::POST => "POST",
24            HttpMethod::PUT => "PUT",
25            HttpMethod::DELETE => "DELETE",
26            HttpMethod::PATCH => "PATCH",
27        }
28    }
29}
30
31impl From<String> for HttpMethod {
32    fn from(method: String) -> Self {
33        match method.to_uppercase().as_str() {
34            "POST" => HttpMethod::POST,
35            "PUT" => HttpMethod::PUT,
36            "DELETE" => HttpMethod::DELETE,
37            "PATCH" => HttpMethod::PATCH,
38            _ => HttpMethod::GET,
39        }
40    }
41}
42
43impl From<&str> for HttpMethod {
44    fn from(method: &str) -> Self {
45        HttpMethod::from(method.to_string())
46    }
47}
48
49pub struct FetchOptions<'a, S, Q, B> {
50    pub slugs: S,
51    pub query: Q,
52    pub body: B,
53    pub middlewares: &'a [Middleware],
54    pub abort_signal: Rc<web_sys::AbortSignal>,
55}
56
57#[allow(clippy::too_many_lines)]
58/// Asynchronous function to perform an HTTP request using `web_sys` fetch API.
59///
60/// # Parameters
61/// - `method`: The HTTP method to use for the request.
62/// - `url`: The endpoint URL.
63/// - `slugs`: Optional path parameters that implement `Serialize`.
64/// - `query`: Optional query parameters that implement `Serialize`.
65/// - `body`: Optional request body that implements `Serialize`.
66/// - `middlewares`: A vector of middleware functions that can modify the request.
67///
68/// # Returns
69/// A `Result` containing the deserialized response or a `FetchError` error.
70pub async fn fetch<'a, S, Q, B>(
71    method: HttpMethod,
72    url: &str,
73    options: FetchOptions<'a, S, Q, B>,
74) -> Result<String, FetchError>
75where
76    S: Serialize + Default + PartialEq,
77    Q: Serialize + Default + PartialEq,
78    B: Serialize + Default + PartialEq,
79{
80    let url = build_url(url, &options.slugs, &options.query)?;
81
82    let request = build_request(
83        &url,
84        &method,
85        &options.body,
86        options.middlewares,
87        &options.abort_signal,
88    )
89    .await?;
90
91    let response_text = send_request(&request).await?;
92
93    Ok(response_text)
94}