Skip to content
This repository was archived by the owner on Mar 16, 2025. It is now read-only.

Commit 83fc6fd

Browse files
authored
Merge pull request #12 from hatsyjs/drop-request-modifier
Drop request modifier
2 parents eb75d6e + 652e62d commit 83fc6fd

13 files changed

+199
-597
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ By default, HTTP request processing context contains the following properties:
142142
- `requestAddresses` object containing request `url` and remote `ip`.
143143
- `log` - A `Console` instance to log messages with.
144144
- `next()` method the handler can use to delegate to another one.
145-
- `modifiedBy()` method to check whether context modified by the specific modifier.
146145

147146
However, the request handler may require more properties to operate. This is where context extension comes into play:
148147

src/core/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
export * from './error.means';
22
export * from './request-body.means';
3-
export * from './request-capabilities';
43
export * from './request-capability';
54
export * from './request-context';
65
export * from './request-handler';
76
export * from './request-modification';
8-
export * from './request-modifier';
97
export * from './request-processor';
108
export * from './request-value-transformer';

src/core/request-capabilities.ts

Lines changed: 0 additions & 129 deletions
This file was deleted.

src/core/request-capability.ts

Lines changed: 84 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,76 @@
22
* @packageDocumentation
33
* @module @hatsy/hatsy/core
44
*/
5-
import { RequestCapabilities } from './request-capabilities';
6-
import { RequestContext } from './request-context';
75
import { 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+
}

src/core/request-context.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
import { RequestHandler } from './request-handler';
66
import { RequestModification } from './request-modification';
7-
import { RequestModifier, RequestModifierRef } from './request-modifier';
87

98
/**
109
* Request processing context.
@@ -37,7 +36,7 @@ export namespace RequestContext {
3736
* context with the given `modifications` applied. The rest of the properties remain unchanged.
3837
*
3938
* @param handler Target handler to delegate request processing to.
40-
* @param modification Request processing means modification or modifier. `this` context will be passed to the next
39+
* @param modification Request processing means modification. `this` context will be passed to the next
4140
* `handler` when omitted.
4241
*
4342
* @returns A promise resolved when request processing finishes. Resolves to `true` when request is responded,
@@ -46,21 +45,9 @@ export namespace RequestContext {
4645
next<TExt = object>(
4746
this: void,
4847
handler: RequestHandler<TMeans & TExt>,
49-
modification?: RequestModification<TMeans, TExt> | RequestModifier<TMeans, TExt>,
48+
modification?: RequestModification<TMeans, TExt>,
5049
): Promise<boolean>;
5150

52-
/**
53-
* Checks whether request modified with the given request modifier.
54-
*
55-
* @typeparam TInput A type of request processing means the target modifier is able modify.
56-
* @typeparam TExt A type of extension to request processing means applied by the target modifier.
57-
* @param ref Request modifier reference.
58-
*
59-
* @returns This request context instance with request processing means applied, or `undefined` if target request
60-
* modifier is not applied yet.
61-
*/
62-
modifiedBy<TInput, TExt>(ref: RequestModifierRef<TInput, TExt>): RequestContext<TMeans & TInput & TExt> | undefined;
63-
6451
}
6552

6653
}

0 commit comments

Comments
 (0)