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' ;
9- import { RequestModifier , RequestModifier__symbol } from './request-modifier' ;
106
117/**
128 * Request processing capability.
139 *
14- * This is a {@link RequestModifier request modifier} implementation able to provide request processing means for
15- * handlers.
10+ * Modifies request processing context in a certain way when delegates to handler.
1611 *
17- * Request processing capabilities could be be {@link RequestCapabilities combined}.
12+ * Request processing capabilities could be {@link RequestCapability.combine combined}.
1813 *
1914 * @typeparam TInput A type of request processing means required in order to apply this capability.
2015 * @typeparam TExt A type of extension to request processing means this capability applies.
2116 */
22- export abstract class RequestCapability < TInput , TExt = object >
23- implements RequestModifier < TInput , TExt > , RequestCapabilities < TInput , TExt > {
17+ export abstract class RequestCapability < TInput , TExt = object > {
2418
2519 /**
26- * A reference to request modifier .
20+ * Builds request capability by the given `provider` .
2721 *
28- * @default `this` instance.
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.
25+ *
26+ * @returns Request processing capability that call the given `provider` in order to apply.
2927 */
30- get [ RequestModifier__symbol ] ( ) : RequestModifier < TInput , TExt > {
31- return this ;
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 ;
3241 }
3342
3443 /**
35- * Builds request modification to apply by { @link for handler} .
44+ * Combines two request processing capabilities .
3645 *
37- * @typeparam TMeans A type of request processing means to modify.
38- * @param context Request processing context to modify.
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.
3951 *
40- * @returns Request modifications to apply, or promise-like instance resolving to it.
52+ * @return Combined request processing capability that applies modifications to request by the `first` capability,
53+ * and then - by the `second` one.
4154 */
42- abstract modification < TMeans extends TInput > (
43- context : RequestContext < TMeans > ,
44- ) : RequestModification < TMeans , TExt > | PromiseLike < RequestModification < TMeans , TExt > > ;
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+ }
4575
4676 /**
4777 * Provides request processing capability to the given handler.
@@ -53,25 +83,46 @@ export abstract class RequestCapability<TInput, TExt = object>
5383 *
5484 * @returns New request processing handler.
5585 */
56- for < TMeans extends TInput > ( handler : RequestHandler < TMeans & TExt > ) : RequestHandler < TMeans > {
57- return async ( { next, modifiedBy } ) => modifiedBy ( this [ RequestModifier__symbol ] )
58- ? next ( handler )
59- : next ( handler , this ) ;
60- }
86+ abstract for < TMeans extends TInput > ( handler : RequestHandler < TMeans & TExt > ) : RequestHandler < TMeans > ;
6187
6288 /**
63- * Combines this capability with the `next` capability set .
89+ * Combines this capability with the `next` one .
6490 *
65- * @typeparam TNext A type of extension to request processing means applied by `next` capability set .
66- * @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.
6793 *
68- * @return New request processing capability set that applies modifications to request by this capability,
69- * 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 .
7096 *
71- * @see RequestCapabilities .combine
97+ * @see RequestCapability .combine
7298 */
73- and < TNext > ( next : RequestCapabilities < TInput & TExt , TNext > ) : RequestCapabilities < TInput , TExt & TNext > {
74- 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 ) ;
75101 }
76102
77103}
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