22 * @packageDocumentation
33 * @module @hatsy /hatsy/core
44 */
5- import { RequestCapabilities } from './request-capabilities' ;
6- import { RequestContext } from './request-context' ;
75import { RequestHandler } from './request-handler' ;
8- import { RequestModification } from './request-modification' ;
96
107/**
118 * Request processing capability.
129 *
1310 * Modifies request processing context in a certain way when delegates to handler.
1411 *
15- * Request processing capabilities could be be {@link RequestCapabilities combined}.
12+ * Request processing capabilities could be {@link RequestCapability.combine combined}.
1613 *
1714 * @typeparam TInput A type of request processing means required in order to apply this capability.
1815 * @typeparam TExt A type of extension to request processing means this capability applies.
1916 */
20- export abstract class RequestCapability < TInput , TExt = object > implements RequestCapabilities < TInput , TExt > {
17+ export abstract class RequestCapability < TInput , TExt = object > {
2118
2219 /**
23- * Builds request modification to apply by { @link for handler} .
20+ * Builds request capability by the given `provider` .
2421 *
25- * @typeparam TMeans A type of request processing means to modify.
26- * @param context Request processing context to modify.
22+ * @typeparam TInput A type of request processing means required by this provider.
23+ * @typeparam TExt A type of extension to request processing means this provider applies.
24+ * @param provider Request processing capability provider.
2725 *
28- * @returns Request modifications to apply, or promise-like instance resolving to it .
26+ * @returns Request processing capability that call the given `provider` in order to apply .
2927 */
30- abstract modification < TMeans extends TInput > (
31- context : RequestContext < TMeans > ,
32- ) : RequestModification < TMeans , TExt > | PromiseLike < RequestModification < TMeans , TExt > > ;
28+ static of < TInput , TExt > (
29+ this : void ,
30+ provider : RequestCapability . Provider < TInput , TExt > ,
31+ ) : RequestCapability < TInput , TExt > {
32+
33+ const capability : RequestCapability < TInput , TExt > = {
34+ for : provider ,
35+ and < TNext > ( next : RequestCapability < TInput & TExt , TNext > ) : RequestCapability < TInput , TExt & TNext > {
36+ return RequestCapability . combine ( capability , next ) ;
37+ } ,
38+ } ;
39+
40+ return capability ;
41+ }
42+
43+ /**
44+ * Combines two request processing capabilities.
45+ *
46+ * @typeparam TInput A type of request processing means expected by the `first` capability.
47+ * @typeparam TExt A type of request processing means extension applied by the `first` capability.
48+ * @typeparam TNext A type of request processing means extension applied by the `second` capability.
49+ * @param first First capability to combine.
50+ * @param second Second capability to combine. Receives requests modified by the `first` one.
51+ *
52+ * @return Combined request processing capability that applies modifications to request by the `first` capability,
53+ * and then - by the `second` one.
54+ */
55+ static combine < TInput , TExt , TNext > (
56+ this : void ,
57+ first : RequestCapability < TInput , TExt > ,
58+ second : RequestCapability < TInput & TExt , TNext > ,
59+ ) : RequestCapability < TInput , TExt & TNext > {
60+
61+ const chain : RequestCapability < TInput , TExt & TNext > = {
62+
63+ for < TMeans extends TInput > ( delegate : RequestHandler < TMeans & TExt & TNext > ) : RequestHandler < TMeans > {
64+ return first . for ( second . for ( delegate ) ) ;
65+ } ,
66+
67+ and < T > ( next : RequestCapability < TInput & TExt & TNext , T > ) : RequestCapability < TInput , TExt & TNext & T > {
68+ return RequestCapability . combine < TInput , TExt & TNext , T > ( chain , next ) ;
69+ } ,
70+
71+ } ;
72+
73+ return chain ;
74+ }
3375
3476 /**
3577 * Provides request processing capability to the given handler.
@@ -41,23 +83,46 @@ export abstract class RequestCapability<TInput, TExt = object> implements Reques
4183 *
4284 * @returns New request processing handler.
4385 */
44- for < TMeans extends TInput > ( handler : RequestHandler < TMeans & TExt > ) : RequestHandler < TMeans > {
45- return async context => context . next ( handler , await this . modification ( context ) ) ;
46- }
86+ abstract for < TMeans extends TInput > ( handler : RequestHandler < TMeans & TExt > ) : RequestHandler < TMeans > ;
4787
4888 /**
49- * Combines this capability with the `next` capability set .
89+ * Combines this capability with the `next` one .
5090 *
51- * @typeparam TNext A type of extension to request processing means applied by `next` capability set .
52- * @param next Next capability set that receives requests modified by this capability.
91+ * @typeparam TNext A type of extension to request processing means applied by `next` capability.
92+ * @param next Next capability that receives requests modified by this capability.
5393 *
54- * @return New request processing capability set that applies modifications to request by this capability,
55- * and then - by the `next` capability set .
94+ * @return New request processing capability that applies modifications to request by this capability first ,
95+ * and then - by the `next` one .
5696 *
57- * @see RequestCapabilities .combine
97+ * @see RequestCapability .combine
5898 */
59- and < TNext > ( next : RequestCapabilities < TInput & TExt , TNext > ) : RequestCapabilities < TInput , TExt & TNext > {
60- return RequestCapabilities . combine < TInput , TExt , TNext > ( this , next ) ;
99+ and < TNext > ( next : RequestCapability < TInput & TExt , TNext > ) : RequestCapability < TInput , TExt & TNext > {
100+ return RequestCapability . combine < TInput , TExt , TNext > ( this , next ) ;
61101 }
62102
63103}
104+
105+ export namespace RequestCapability {
106+
107+ /**
108+ * Request processing capability provider signature.
109+ *
110+ * Builds a request processing handler that modifies request and delegates to another one.
111+ *
112+ * @typeparam TInput A type of request processing means required by this provider.
113+ * @typeparam TExt A type of extension to request processing means this provider applies.
114+ */
115+ export type Provider < TInput , TExt = object > =
116+ /**
117+ * @typeparam TMeans A type of request processing means expected by constructed handler.
118+ *
119+ * @param handler Request processing handler that will receive modified request context.
120+ *
121+ * @returns New request processing handler.
122+ */
123+ < TMeans extends TInput > (
124+ this : void ,
125+ handler : RequestHandler < TMeans & TExt > ,
126+ ) => RequestHandler < TMeans > ;
127+
128+ }
0 commit comments