Skip to main content

alux_http_typescript/
handler.rs

1//! Reads one endpoint as the call a caller writes.
2
3use crate::input::{
4    TsBodyInput, TsCookieInput, TsFormInput, TsHeaderInput, TsInputsAlg, TsMultipartInput, TsPathInput, TsQueryInput,
5    TsRawBodyInput, TsUnstatedInput,
6};
7use crate::output::TsOutputAlg;
8use crate::route::{TsCall, TsHttpModule, TsRouteImpl, TsSelector};
9use alux_ext::{ApplyAlg, HandlerContextAlg, OperationAlg};
10use alux_http::{
11    HandlerAlg, HandlerEndpointAlg, HttpInputAlg, HttpMethod, HttpSelectorAlg, OutputKindAlg, RouteAlg, RoutePath,
12    SelectorAlg,
13};
14use alux_shape::{Spelling, words_of};
15use alux_shape_typescript::TsShape;
16use std::sync::Arc;
17
18/// The package that interprets a program: what turns one into calls, installed once.
19///
20/// A generated module imports `endpoint` from here rather than restating how a request is made, so a
21/// surface and what makes a surface's requests are upgraded separately.
22pub const RUNTIME_PACKAGE: &str = "@alux-network/api";
23
24/// Interprets typed HTTP programs as a TypeScript client module.
25///
26/// Nothing is applied and no request is made. Each endpoint is read for what a caller states and
27/// what they receive, which is the same program an executing interpretation answers.
28#[derive(Debug, Clone, Copy)]
29pub struct TsHttpClient {
30    shapes: TsShape,
31    members: Spelling,
32}
33
34impl TsHttpClient {
35    /// Emits a client whose member and parameter names are spelled this way.
36    pub fn new(members: Spelling) -> Self {
37        Self { shapes: TsShape::new(members), members }
38    }
39}
40
41impl<Context> HandlerContextAlg<Context> for TsHttpClient
42where
43    Context: Send + Sync + 'static,
44{
45    // A client applies nothing, so the handle it names is only what the program's obligation asks
46    // for: an owned reference to the domain the operations read.
47    type Handle = Arc<Context>;
48}
49
50impl HandlerAlg for TsHttpClient {
51    type Endpoint = TsCall;
52}
53
54impl HttpInputAlg for TsHttpClient {
55    type Path<I> = TsPathInput<I>;
56    type Query<I> = TsQueryInput<I>;
57    type Body<I> = TsBodyInput<I>;
58    type Form<I> = TsFormInput<I>;
59    type Multipart<I> = TsMultipartInput<I>;
60    type RawBody<I> = TsRawBodyInput<I>;
61    type Header<I> = TsHeaderInput<I>;
62    type Cookie<I> = TsCookieInput<I>;
63    // A caller states an authentication value in a header; an endpoint context it never states.
64    type Auth<I> = TsHeaderInput<I>;
65    type Context<I> = TsUnstatedInput<I>;
66}
67
68impl<Handle, Inputs, Args, Transform, Answering, Output> HandlerEndpointAlg<Handle, Inputs, Args, Transform, Output>
69    for TsHttpClient
70where
71    Inputs: TsInputsAlg,
72    Transform: OutputKindAlg<Self, Output, Transform = Answering>,
73    Answering: TsOutputAlg<Output>,
74{
75    fn finish_handler<Handler>(&self, _handler: Handler) -> TsCall
76    where
77        Handler: OperationAlg + ApplyAlg<Handle, Args, Output = Output> + Send + Sync + 'static,
78    {
79        // An argument name arrives as it was authored, so its words are read out of it and spelled
80        // the way this surface spells names.
81        let named = |name: &&'static str| self.members.spell(&words_of(name));
82        let parameters = Handler::ARG_NAMES.iter().map(named).zip(Inputs::describe(&self.shapes)).collect();
83
84        TsCall {
85            name: self.members.spell(&words_of(Handler::NAME)),
86            doc: Handler::DOC,
87            parameters,
88            answer: Answering::answer(&self.shapes),
89        }
90    }
91}
92
93impl SelectorAlg for TsHttpClient {
94    type Selector = TsSelector;
95
96    fn identity(&self) -> Self::Selector {
97        SelectorAlg::identity(&TsRouteImpl)
98    }
99
100    fn compose(&self, first: Self::Selector, second: Self::Selector) -> Self::Selector {
101        SelectorAlg::compose(&TsRouteImpl, first, second)
102    }
103}
104
105impl RouteAlg for TsHttpClient {
106    type Route = TsHttpModule;
107    type Selector = TsSelector;
108    type Endpoint = TsCall;
109
110    fn initial(&self) -> Self::Route {
111        RouteAlg::initial(&TsRouteImpl)
112    }
113
114    fn coproduct(&self, left: Self::Route, right: Self::Route) -> Self::Route {
115        RouteAlg::coproduct(&TsRouteImpl, left, right)
116    }
117
118    fn precompose(&self, selector: Self::Selector, route: Self::Route) -> Self::Route {
119        RouteAlg::precompose(&TsRouteImpl, selector, route)
120    }
121
122    fn lift(&self, endpoint: Self::Endpoint) -> Self::Route {
123        RouteAlg::lift(&TsRouteImpl, endpoint)
124    }
125}
126
127impl HttpSelectorAlg for TsHttpClient {
128    type Selector = TsSelector;
129
130    fn http_method(&self, method: HttpMethod) -> Self::Selector {
131        HttpSelectorAlg::http_method(&TsRouteImpl, method)
132    }
133
134    fn http_path(&self, path: &RoutePath) -> Self::Selector {
135        HttpSelectorAlg::http_path(&TsRouteImpl, path)
136    }
137
138    fn http_prefix(&self, prefix: &RoutePath) -> Self::Selector {
139        HttpSelectorAlg::http_prefix(&TsRouteImpl, prefix)
140    }
141}