spikard 0.2.1 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +1 -1
  3. data/README.md +626 -626
  4. data/ext/spikard_rb/Cargo.toml +17 -17
  5. data/ext/spikard_rb/extconf.rb +10 -10
  6. data/ext/spikard_rb/src/lib.rs +6 -6
  7. data/lib/spikard/app.rb +374 -374
  8. data/lib/spikard/background.rb +27 -27
  9. data/lib/spikard/config.rb +396 -396
  10. data/lib/spikard/converters.rb +85 -85
  11. data/lib/spikard/handler_wrapper.rb +116 -116
  12. data/lib/spikard/provide.rb +228 -228
  13. data/lib/spikard/response.rb +109 -109
  14. data/lib/spikard/schema.rb +243 -243
  15. data/lib/spikard/sse.rb +111 -111
  16. data/lib/spikard/streaming_response.rb +21 -21
  17. data/lib/spikard/testing.rb +221 -221
  18. data/lib/spikard/upload_file.rb +131 -131
  19. data/lib/spikard/version.rb +5 -5
  20. data/lib/spikard/websocket.rb +59 -59
  21. data/lib/spikard.rb +43 -43
  22. data/sig/spikard.rbs +349 -349
  23. data/vendor/crates/spikard-core/Cargo.toml +40 -0
  24. data/vendor/crates/spikard-core/src/bindings/mod.rs +3 -0
  25. data/vendor/crates/spikard-core/src/bindings/response.rs +133 -0
  26. data/vendor/crates/spikard-core/src/debug.rs +63 -0
  27. data/vendor/crates/spikard-core/src/di/container.rs +726 -0
  28. data/vendor/crates/spikard-core/src/di/dependency.rs +273 -0
  29. data/vendor/crates/spikard-core/src/di/error.rs +118 -0
  30. data/vendor/crates/spikard-core/src/di/factory.rs +538 -0
  31. data/vendor/crates/spikard-core/src/di/graph.rs +545 -0
  32. data/vendor/crates/spikard-core/src/di/mod.rs +192 -0
  33. data/vendor/crates/spikard-core/src/di/resolved.rs +411 -0
  34. data/vendor/crates/spikard-core/src/di/value.rs +283 -0
  35. data/vendor/crates/spikard-core/src/http.rs +153 -0
  36. data/vendor/crates/spikard-core/src/lib.rs +28 -0
  37. data/vendor/crates/spikard-core/src/lifecycle.rs +422 -0
  38. data/vendor/crates/spikard-core/src/parameters.rs +719 -0
  39. data/vendor/crates/spikard-core/src/problem.rs +310 -0
  40. data/vendor/crates/spikard-core/src/request_data.rs +189 -0
  41. data/vendor/crates/spikard-core/src/router.rs +249 -0
  42. data/vendor/crates/spikard-core/src/schema_registry.rs +183 -0
  43. data/vendor/crates/spikard-core/src/type_hints.rs +304 -0
  44. data/vendor/crates/spikard-core/src/validation.rs +699 -0
  45. data/vendor/crates/spikard-http/Cargo.toml +58 -0
  46. data/vendor/crates/spikard-http/src/auth.rs +247 -0
  47. data/vendor/crates/spikard-http/src/background.rs +249 -0
  48. data/vendor/crates/spikard-http/src/bindings/mod.rs +3 -0
  49. data/vendor/crates/spikard-http/src/bindings/response.rs +1 -0
  50. data/vendor/crates/spikard-http/src/body_metadata.rs +8 -0
  51. data/vendor/crates/spikard-http/src/cors.rs +490 -0
  52. data/vendor/crates/spikard-http/src/debug.rs +63 -0
  53. data/vendor/crates/spikard-http/src/di_handler.rs +423 -0
  54. data/vendor/crates/spikard-http/src/handler_response.rs +190 -0
  55. data/vendor/crates/spikard-http/src/handler_trait.rs +228 -0
  56. data/vendor/crates/spikard-http/src/handler_trait_tests.rs +284 -0
  57. data/vendor/crates/spikard-http/src/lib.rs +529 -0
  58. data/vendor/crates/spikard-http/src/lifecycle/adapter.rs +149 -0
  59. data/vendor/crates/spikard-http/src/lifecycle.rs +428 -0
  60. data/vendor/crates/spikard-http/src/middleware/mod.rs +285 -0
  61. data/vendor/crates/spikard-http/src/middleware/multipart.rs +86 -0
  62. data/vendor/crates/spikard-http/src/middleware/urlencoded.rs +147 -0
  63. data/vendor/crates/spikard-http/src/middleware/validation.rs +287 -0
  64. data/vendor/crates/spikard-http/src/openapi/mod.rs +309 -0
  65. data/vendor/crates/spikard-http/src/openapi/parameter_extraction.rs +190 -0
  66. data/vendor/crates/spikard-http/src/openapi/schema_conversion.rs +308 -0
  67. data/vendor/crates/spikard-http/src/openapi/spec_generation.rs +195 -0
  68. data/vendor/crates/spikard-http/src/parameters.rs +1 -0
  69. data/vendor/crates/spikard-http/src/problem.rs +1 -0
  70. data/vendor/crates/spikard-http/src/query_parser.rs +369 -0
  71. data/vendor/crates/spikard-http/src/response.rs +399 -0
  72. data/vendor/crates/spikard-http/src/router.rs +1 -0
  73. data/vendor/crates/spikard-http/src/schema_registry.rs +1 -0
  74. data/vendor/crates/spikard-http/src/server/handler.rs +80 -0
  75. data/vendor/crates/spikard-http/src/server/lifecycle_execution.rs +98 -0
  76. data/vendor/crates/spikard-http/src/server/mod.rs +805 -0
  77. data/vendor/crates/spikard-http/src/server/request_extraction.rs +119 -0
  78. data/vendor/crates/spikard-http/src/sse.rs +447 -0
  79. data/vendor/crates/spikard-http/src/testing/form.rs +14 -0
  80. data/vendor/crates/spikard-http/src/testing/multipart.rs +60 -0
  81. data/vendor/crates/spikard-http/src/testing/test_client.rs +285 -0
  82. data/vendor/crates/spikard-http/src/testing.rs +377 -0
  83. data/vendor/crates/spikard-http/src/type_hints.rs +1 -0
  84. data/vendor/crates/spikard-http/src/validation.rs +1 -0
  85. data/vendor/crates/spikard-http/src/websocket.rs +324 -0
  86. data/vendor/crates/spikard-rb/Cargo.toml +42 -0
  87. data/vendor/crates/spikard-rb/build.rs +8 -0
  88. data/vendor/crates/spikard-rb/src/background.rs +63 -0
  89. data/vendor/crates/spikard-rb/src/config.rs +294 -0
  90. data/vendor/crates/spikard-rb/src/conversion.rs +392 -0
  91. data/vendor/crates/spikard-rb/src/di.rs +409 -0
  92. data/vendor/crates/spikard-rb/src/handler.rs +534 -0
  93. data/vendor/crates/spikard-rb/src/lib.rs +2020 -0
  94. data/vendor/crates/spikard-rb/src/lifecycle.rs +267 -0
  95. data/vendor/crates/spikard-rb/src/server.rs +283 -0
  96. data/vendor/crates/spikard-rb/src/sse.rs +231 -0
  97. data/vendor/crates/spikard-rb/src/test_client.rs +404 -0
  98. data/vendor/crates/spikard-rb/src/test_sse.rs +143 -0
  99. data/vendor/crates/spikard-rb/src/test_websocket.rs +221 -0
  100. data/vendor/crates/spikard-rb/src/websocket.rs +233 -0
  101. metadata +80 -2
@@ -0,0 +1,422 @@
1
+ //! Lifecycle hooks for request/response processing
2
+ //!
3
+ //! Transport-agnostic lifecycle system shared across HTTP, WASM, and future runtimes.
4
+ //! Hooks operate on generic request/response carriers so higher-level crates can
5
+ //! plug in their own types without pulling in server frameworks.
6
+
7
+ use std::{future::Future, pin::Pin, sync::Arc};
8
+
9
+ type RequestHookFutureSend<'a, Req, Resp> =
10
+ Pin<Box<dyn Future<Output = Result<HookResult<Req, Resp>, String>> + Send + 'a>>;
11
+ type ResponseHookFutureSend<'a, Resp> =
12
+ Pin<Box<dyn Future<Output = Result<HookResult<Resp, Resp>, String>> + Send + 'a>>;
13
+
14
+ type RequestHookFutureLocal<'a, Req, Resp> = Pin<Box<dyn Future<Output = Result<HookResult<Req, Resp>, String>> + 'a>>;
15
+ type ResponseHookFutureLocal<'a, Resp> = Pin<Box<dyn Future<Output = Result<HookResult<Resp, Resp>, String>> + 'a>>;
16
+
17
+ /// Result of a lifecycle hook execution
18
+ #[derive(Debug)]
19
+ pub enum HookResult<T, U> {
20
+ /// Continue to the next phase with the (possibly modified) value
21
+ Continue(T),
22
+ /// Short-circuit the request pipeline and return this response immediately
23
+ ShortCircuit(U),
24
+ }
25
+
26
+ /// Trait for lifecycle hooks on native targets (Send + Sync, Send futures).
27
+ pub trait NativeLifecycleHook<Req, Resp>: Send + Sync {
28
+ /// Hook name for debugging and error messages
29
+ fn name(&self) -> &str;
30
+
31
+ /// Execute hook with a request
32
+ fn execute_request<'a>(&'a self, req: Req) -> RequestHookFutureSend<'a, Req, Resp>;
33
+
34
+ /// Execute hook with a response
35
+ fn execute_response<'a>(&'a self, resp: Resp) -> ResponseHookFutureSend<'a, Resp>;
36
+ }
37
+
38
+ /// Trait for lifecycle hooks on local (wasm) targets (no Send requirements).
39
+ pub trait LocalLifecycleHook<Req, Resp> {
40
+ /// Hook name for debugging and error messages
41
+ fn name(&self) -> &str;
42
+
43
+ /// Execute hook with a request
44
+ fn execute_request<'a>(&'a self, req: Req) -> RequestHookFutureLocal<'a, Req, Resp>;
45
+
46
+ /// Execute hook with a response
47
+ fn execute_response<'a>(&'a self, resp: Resp) -> ResponseHookFutureLocal<'a, Resp>;
48
+ }
49
+
50
+ #[cfg(target_arch = "wasm32")]
51
+ pub use LocalLifecycleHook as LifecycleHook;
52
+ #[cfg(not(target_arch = "wasm32"))]
53
+ pub use NativeLifecycleHook as LifecycleHook;
54
+
55
+ /// Target-specific hook alias used by the rest of the codebase.
56
+ #[cfg(not(target_arch = "wasm32"))]
57
+ type CoreHook<Req, Resp> = dyn NativeLifecycleHook<Req, Resp>;
58
+ #[cfg(target_arch = "wasm32")]
59
+ type CoreHook<Req, Resp> = dyn LocalLifecycleHook<Req, Resp>;
60
+
61
+ /// Target-specific container alias to make downstream imports clearer.
62
+ pub type TargetLifecycleHooks<Req, Resp> = LifecycleHooks<Req, Resp>;
63
+
64
+ /// Container for all lifecycle hooks
65
+ #[derive(Clone)]
66
+ pub struct LifecycleHooks<Req, Resp> {
67
+ on_request: Vec<Arc<CoreHook<Req, Resp>>>,
68
+ pre_validation: Vec<Arc<CoreHook<Req, Resp>>>,
69
+ pre_handler: Vec<Arc<CoreHook<Req, Resp>>>,
70
+ on_response: Vec<Arc<CoreHook<Req, Resp>>>,
71
+ on_error: Vec<Arc<CoreHook<Req, Resp>>>,
72
+ }
73
+
74
+ impl<Req, Resp> Default for LifecycleHooks<Req, Resp> {
75
+ fn default() -> Self {
76
+ Self {
77
+ on_request: Vec::new(),
78
+ pre_validation: Vec::new(),
79
+ pre_handler: Vec::new(),
80
+ on_response: Vec::new(),
81
+ on_error: Vec::new(),
82
+ }
83
+ }
84
+ }
85
+
86
+ impl<Req, Resp> std::fmt::Debug for LifecycleHooks<Req, Resp> {
87
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
88
+ f.debug_struct("LifecycleHooks")
89
+ .field("on_request_count", &self.on_request.len())
90
+ .field("pre_validation_count", &self.pre_validation.len())
91
+ .field("pre_handler_count", &self.pre_handler.len())
92
+ .field("on_response_count", &self.on_response.len())
93
+ .field("on_error_count", &self.on_error.len())
94
+ .finish()
95
+ }
96
+ }
97
+
98
+ impl<Req, Resp> LifecycleHooks<Req, Resp> {
99
+ /// Create a new empty hooks container
100
+ pub fn new() -> Self {
101
+ Self::default()
102
+ }
103
+
104
+ /// Builder constructor for ergonomic hook registration
105
+ pub fn builder() -> LifecycleHooksBuilder<Req, Resp> {
106
+ LifecycleHooksBuilder::new()
107
+ }
108
+
109
+ /// Check if any hooks are registered
110
+ #[inline(always)]
111
+ pub fn is_empty(&self) -> bool {
112
+ self.on_request.is_empty()
113
+ && self.pre_validation.is_empty()
114
+ && self.pre_handler.is_empty()
115
+ && self.on_response.is_empty()
116
+ && self.on_error.is_empty()
117
+ }
118
+
119
+ pub fn add_on_request(&mut self, hook: Arc<CoreHook<Req, Resp>>) {
120
+ self.on_request.push(hook);
121
+ }
122
+
123
+ pub fn add_pre_validation(&mut self, hook: Arc<CoreHook<Req, Resp>>) {
124
+ self.pre_validation.push(hook);
125
+ }
126
+
127
+ pub fn add_pre_handler(&mut self, hook: Arc<CoreHook<Req, Resp>>) {
128
+ self.pre_handler.push(hook);
129
+ }
130
+
131
+ pub fn add_on_response(&mut self, hook: Arc<CoreHook<Req, Resp>>) {
132
+ self.on_response.push(hook);
133
+ }
134
+
135
+ pub fn add_on_error(&mut self, hook: Arc<CoreHook<Req, Resp>>) {
136
+ self.on_error.push(hook);
137
+ }
138
+
139
+ pub async fn execute_on_request(&self, mut req: Req) -> Result<HookResult<Req, Resp>, String> {
140
+ if self.on_request.is_empty() {
141
+ return Ok(HookResult::Continue(req));
142
+ }
143
+
144
+ for hook in &self.on_request {
145
+ match hook.execute_request(req).await? {
146
+ HookResult::Continue(r) => req = r,
147
+ HookResult::ShortCircuit(response) => return Ok(HookResult::ShortCircuit(response)),
148
+ }
149
+ }
150
+
151
+ Ok(HookResult::Continue(req))
152
+ }
153
+
154
+ pub async fn execute_pre_validation(&self, mut req: Req) -> Result<HookResult<Req, Resp>, String> {
155
+ if self.pre_validation.is_empty() {
156
+ return Ok(HookResult::Continue(req));
157
+ }
158
+
159
+ for hook in &self.pre_validation {
160
+ match hook.execute_request(req).await? {
161
+ HookResult::Continue(r) => req = r,
162
+ HookResult::ShortCircuit(response) => return Ok(HookResult::ShortCircuit(response)),
163
+ }
164
+ }
165
+
166
+ Ok(HookResult::Continue(req))
167
+ }
168
+
169
+ pub async fn execute_pre_handler(&self, mut req: Req) -> Result<HookResult<Req, Resp>, String> {
170
+ if self.pre_handler.is_empty() {
171
+ return Ok(HookResult::Continue(req));
172
+ }
173
+
174
+ for hook in &self.pre_handler {
175
+ match hook.execute_request(req).await? {
176
+ HookResult::Continue(r) => req = r,
177
+ HookResult::ShortCircuit(response) => return Ok(HookResult::ShortCircuit(response)),
178
+ }
179
+ }
180
+
181
+ Ok(HookResult::Continue(req))
182
+ }
183
+
184
+ pub async fn execute_on_response(&self, mut resp: Resp) -> Result<Resp, String> {
185
+ if self.on_response.is_empty() {
186
+ return Ok(resp);
187
+ }
188
+
189
+ for hook in &self.on_response {
190
+ match hook.execute_response(resp).await? {
191
+ HookResult::Continue(r) => resp = r,
192
+ HookResult::ShortCircuit(r) => resp = r,
193
+ }
194
+ }
195
+
196
+ Ok(resp)
197
+ }
198
+
199
+ pub async fn execute_on_error(&self, mut resp: Resp) -> Result<Resp, String> {
200
+ if self.on_error.is_empty() {
201
+ return Ok(resp);
202
+ }
203
+
204
+ for hook in &self.on_error {
205
+ match hook.execute_response(resp).await? {
206
+ HookResult::Continue(r) => resp = r,
207
+ HookResult::ShortCircuit(r) => resp = r,
208
+ }
209
+ }
210
+
211
+ Ok(resp)
212
+ }
213
+ }
214
+
215
+ /// Helper struct for implementing request hooks from closures
216
+ struct RequestHookFn<F, Req, Resp> {
217
+ name: String,
218
+ func: F,
219
+ _marker: std::marker::PhantomData<fn(Req, Resp)>,
220
+ }
221
+
222
+ struct ResponseHookFn<F, Req, Resp> {
223
+ name: String,
224
+ func: F,
225
+ _marker: std::marker::PhantomData<fn(Req, Resp)>,
226
+ }
227
+
228
+ #[cfg(not(target_arch = "wasm32"))]
229
+ impl<F, Fut, Req, Resp> NativeLifecycleHook<Req, Resp> for RequestHookFn<F, Req, Resp>
230
+ where
231
+ F: Fn(Req) -> Fut + Send + Sync,
232
+ Fut: Future<Output = Result<HookResult<Req, Resp>, String>> + Send + 'static,
233
+ Req: Send + 'static,
234
+ Resp: Send + 'static,
235
+ {
236
+ fn name(&self) -> &str {
237
+ &self.name
238
+ }
239
+
240
+ fn execute_request<'a>(&'a self, req: Req) -> RequestHookFutureSend<'a, Req, Resp> {
241
+ Box::pin((self.func)(req))
242
+ }
243
+
244
+ fn execute_response<'a>(&'a self, _resp: Resp) -> ResponseHookFutureSend<'a, Resp> {
245
+ Box::pin(async move { Err("Request hook called with response - this is a bug".to_string()) })
246
+ }
247
+ }
248
+
249
+ #[cfg(target_arch = "wasm32")]
250
+ impl<F, Fut, Req, Resp> LocalLifecycleHook<Req, Resp> for RequestHookFn<F, Req, Resp>
251
+ where
252
+ F: Fn(Req) -> Fut + Send + Sync,
253
+ Fut: Future<Output = Result<HookResult<Req, Resp>, String>> + 'static,
254
+ Req: 'static,
255
+ Resp: 'static,
256
+ {
257
+ fn name(&self) -> &str {
258
+ &self.name
259
+ }
260
+
261
+ fn execute_request<'a>(&'a self, req: Req) -> RequestHookFutureLocal<'a, Req, Resp> {
262
+ Box::pin((self.func)(req))
263
+ }
264
+
265
+ fn execute_response<'a>(&'a self, _resp: Resp) -> ResponseHookFutureLocal<'a, Resp> {
266
+ Box::pin(async move { Err("Request hook called with response - this is a bug".to_string()) })
267
+ }
268
+ }
269
+
270
+ #[cfg(not(target_arch = "wasm32"))]
271
+ impl<F, Fut, Req, Resp> NativeLifecycleHook<Req, Resp> for ResponseHookFn<F, Req, Resp>
272
+ where
273
+ F: Fn(Resp) -> Fut + Send + Sync,
274
+ Fut: Future<Output = Result<HookResult<Resp, Resp>, String>> + Send + 'static,
275
+ Req: Send + 'static,
276
+ Resp: Send + 'static,
277
+ {
278
+ fn name(&self) -> &str {
279
+ &self.name
280
+ }
281
+
282
+ fn execute_request<'a>(&'a self, _req: Req) -> RequestHookFutureSend<'a, Req, Resp> {
283
+ Box::pin(async move { Err("Response hook called with request - this is a bug".to_string()) })
284
+ }
285
+
286
+ fn execute_response<'a>(&'a self, resp: Resp) -> ResponseHookFutureSend<'a, Resp> {
287
+ Box::pin((self.func)(resp))
288
+ }
289
+ }
290
+
291
+ #[cfg(target_arch = "wasm32")]
292
+ impl<F, Fut, Req, Resp> LocalLifecycleHook<Req, Resp> for ResponseHookFn<F, Req, Resp>
293
+ where
294
+ F: Fn(Resp) -> Fut + Send + Sync,
295
+ Fut: Future<Output = Result<HookResult<Resp, Resp>, String>> + 'static,
296
+ Req: 'static,
297
+ Resp: 'static,
298
+ {
299
+ fn name(&self) -> &str {
300
+ &self.name
301
+ }
302
+
303
+ fn execute_request<'a>(&'a self, _req: Req) -> RequestHookFutureLocal<'a, Req, Resp> {
304
+ Box::pin(async move { Err("Response hook called with request - this is a bug".to_string()) })
305
+ }
306
+
307
+ fn execute_response<'a>(&'a self, resp: Resp) -> ResponseHookFutureLocal<'a, Resp> {
308
+ Box::pin((self.func)(resp))
309
+ }
310
+ }
311
+
312
+ /// Builder Pattern for LifecycleHooks
313
+ pub struct LifecycleHooksBuilder<Req, Resp> {
314
+ hooks: LifecycleHooks<Req, Resp>,
315
+ }
316
+
317
+ impl<Req, Resp> LifecycleHooksBuilder<Req, Resp> {
318
+ pub fn new() -> Self {
319
+ Self {
320
+ hooks: LifecycleHooks::default(),
321
+ }
322
+ }
323
+
324
+ pub fn on_request(mut self, hook: Arc<CoreHook<Req, Resp>>) -> Self {
325
+ self.hooks.add_on_request(hook);
326
+ self
327
+ }
328
+
329
+ pub fn pre_validation(mut self, hook: Arc<CoreHook<Req, Resp>>) -> Self {
330
+ self.hooks.add_pre_validation(hook);
331
+ self
332
+ }
333
+
334
+ pub fn pre_handler(mut self, hook: Arc<CoreHook<Req, Resp>>) -> Self {
335
+ self.hooks.add_pre_handler(hook);
336
+ self
337
+ }
338
+
339
+ pub fn on_response(mut self, hook: Arc<CoreHook<Req, Resp>>) -> Self {
340
+ self.hooks.add_on_response(hook);
341
+ self
342
+ }
343
+
344
+ pub fn on_error(mut self, hook: Arc<CoreHook<Req, Resp>>) -> Self {
345
+ self.hooks.add_on_error(hook);
346
+ self
347
+ }
348
+
349
+ pub fn build(self) -> LifecycleHooks<Req, Resp> {
350
+ self.hooks
351
+ }
352
+ }
353
+
354
+ impl<Req, Resp> Default for LifecycleHooksBuilder<Req, Resp> {
355
+ fn default() -> Self {
356
+ Self::new()
357
+ }
358
+ }
359
+
360
+ /// Create a request hook from an async function or closure (native targets).
361
+ #[cfg(not(target_arch = "wasm32"))]
362
+ pub fn request_hook<Req, Resp, F, Fut>(name: impl Into<String>, func: F) -> Arc<dyn LifecycleHook<Req, Resp>>
363
+ where
364
+ F: Fn(Req) -> Fut + Send + Sync + 'static,
365
+ Fut: Future<Output = Result<HookResult<Req, Resp>, String>> + Send + 'static,
366
+ Req: Send + 'static,
367
+ Resp: Send + 'static,
368
+ {
369
+ Arc::new(RequestHookFn {
370
+ name: name.into(),
371
+ func,
372
+ _marker: std::marker::PhantomData,
373
+ })
374
+ }
375
+
376
+ /// Create a request hook from an async function or closure (wasm targets).
377
+ #[cfg(target_arch = "wasm32")]
378
+ pub fn request_hook<Req, Resp, F, Fut>(name: impl Into<String>, func: F) -> Arc<dyn LifecycleHook<Req, Resp>>
379
+ where
380
+ F: Fn(Req) -> Fut + Send + Sync + 'static,
381
+ Fut: Future<Output = Result<HookResult<Req, Resp>, String>> + 'static,
382
+ Req: 'static,
383
+ Resp: 'static,
384
+ {
385
+ Arc::new(RequestHookFn {
386
+ name: name.into(),
387
+ func,
388
+ _marker: std::marker::PhantomData,
389
+ })
390
+ }
391
+
392
+ /// Create a response hook from an async function or closure (native targets).
393
+ #[cfg(not(target_arch = "wasm32"))]
394
+ pub fn response_hook<Req, Resp, F, Fut>(name: impl Into<String>, func: F) -> Arc<dyn LifecycleHook<Req, Resp>>
395
+ where
396
+ F: Fn(Resp) -> Fut + Send + Sync + 'static,
397
+ Fut: Future<Output = Result<HookResult<Resp, Resp>, String>> + Send + 'static,
398
+ Req: Send + 'static,
399
+ Resp: Send + 'static,
400
+ {
401
+ Arc::new(ResponseHookFn {
402
+ name: name.into(),
403
+ func,
404
+ _marker: std::marker::PhantomData,
405
+ })
406
+ }
407
+
408
+ /// Create a response hook from an async function or closure (wasm targets).
409
+ #[cfg(target_arch = "wasm32")]
410
+ pub fn response_hook<Req, Resp, F, Fut>(name: impl Into<String>, func: F) -> Arc<dyn LifecycleHook<Req, Resp>>
411
+ where
412
+ F: Fn(Resp) -> Fut + Send + Sync + 'static,
413
+ Fut: Future<Output = Result<HookResult<Resp, Resp>, String>> + 'static,
414
+ Req: 'static,
415
+ Resp: 'static,
416
+ {
417
+ Arc::new(ResponseHookFn {
418
+ name: name.into(),
419
+ func,
420
+ _marker: std::marker::PhantomData,
421
+ })
422
+ }