retina_fetch/mode.rs
1// Copyright (C) 2023 Tristan Gerritsen <tristan@thewoosh.org>
2// All Rights Reserved.
3
4use strum::AsRefStr;
5
6/// The request has an associated [mode][spec], which is an important concept
7/// in [Cross Origin Resource Sharing][cors].
8///
9/// [spec]: https://fetch.spec.whatwg.org/#concept-request-mode
10/// [cors]: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
11#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
12#[derive(AsRefStr)]
13#[strum(serialize_all = "lowercase")]
14pub enum RequestMode {
15 /// ["same-origin"](https://fetch.spec.whatwg.org/#concept-request-mode)
16 /// > _Used to ensure requests are made to same-origin URLs. Fetch will
17 /// > return a network error if the request is not made to a same-origin
18 /// > URL._
19 SameOrigin,
20
21 /// ["cors"](https://fetch.spec.whatwg.org/#concept-request-mode)
22 /// > _For requests whose response tainting gets set to "cors", makes the
23 /// > request a CORS request — in which case, fetch will return a network
24 /// > error if the requested resource does not understand the CORS
25 /// > protocol, or if the requested resource is one that intentionally
26 /// > does not participate in the CORS protocol._
27 Cors,
28
29 /// ["no-cors"](https://fetch.spec.whatwg.org/#concept-request-mode)
30 /// > _Restricts requests to using CORS-safelisted methods and
31 /// > CORS-safelisted request-headers. Upon success, fetch will return an
32 /// > opaque filtered response._
33 #[default]
34 NoCors,
35
36 /// ["navigate"](https://fetch.spec.whatwg.org/#concept-request-mode)
37 /// > _This is a special mode used only when navigating between
38 /// > documents._
39 Navigate,
40
41 /// ["websocket"](https://fetch.spec.whatwg.org/#concept-request-mode)
42 /// > _This is a special mode used only when establishing a WebSocket
43 /// > connection._
44 WebSocket,
45}
46
47impl RequestMode {
48 /// Get the normative string representation, as per [Fetch][spec].
49 ///
50 /// [spec]: https://fetch.spec.whatwg.org/#concept-request-mode
51 pub fn as_str(&self) -> &str {
52 self.as_ref()
53 }
54}