alien-middleware 0.10.4 → 0.11.1
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.
|
@@ -169,7 +169,10 @@ async function runMiddlewareChain(requestChain, parentContext) {
|
|
|
169
169
|
return response;
|
|
170
170
|
}
|
|
171
171
|
function createHandler(requestChain) {
|
|
172
|
-
const handler = runMiddlewareChain.bind(
|
|
172
|
+
const handler = runMiddlewareChain.bind(
|
|
173
|
+
null,
|
|
174
|
+
requestChain
|
|
175
|
+
);
|
|
173
176
|
Object.setPrototypeOf(handler, MiddlewareChain.prototype);
|
|
174
177
|
handler[kRequestChain] = requestChain;
|
|
175
178
|
return handler;
|
|
@@ -66,7 +66,10 @@ type AnyMiddlewareTypes = {
|
|
|
66
66
|
};
|
|
67
67
|
platform: any;
|
|
68
68
|
};
|
|
69
|
-
type AnyMiddlewareChain =
|
|
69
|
+
type AnyMiddlewareChain<T extends AnyMiddlewareTypes = AnyMiddlewareTypes> = {
|
|
70
|
+
$MiddlewareChain: T;
|
|
71
|
+
};
|
|
72
|
+
type AnyMiddleware = Middleware | AnyMiddlewareChain;
|
|
70
73
|
type Inputs<T extends AnyMiddlewareChain> = T['$MiddlewareChain']['initial'];
|
|
71
74
|
type InputProperties<T extends AnyMiddlewareChain> = Inputs<T>['properties'];
|
|
72
75
|
type InputEnv<T extends AnyMiddlewareChain> = Inputs<T>['env'];
|
|
@@ -113,11 +116,11 @@ type RequestContext<TEnv extends object = any, TProperties extends object = neve
|
|
|
113
116
|
*
|
|
114
117
|
* When type `T` is `never`, a default context is returned.
|
|
115
118
|
*/
|
|
116
|
-
type MiddlewareContext<T extends
|
|
119
|
+
type MiddlewareContext<T extends AnyMiddlewareChain | AnyMiddleware[]> = [
|
|
117
120
|
T
|
|
118
|
-
] extends [never] ? RequestContext<{}, never, unknown> : T extends
|
|
119
|
-
type IsolatedContext<T extends
|
|
120
|
-
type RequestMiddleware<T extends
|
|
121
|
+
] extends [never] ? RequestContext<{}, never, unknown> : T extends AnyMiddlewareChain ? RequestContext<Env<T>, Properties<T>, Platform<T>> : T extends AnyMiddleware[] ? MiddlewareContext<MiddlewareChain<ApplyMiddlewares<T>>> : never;
|
|
122
|
+
type IsolatedContext<T extends AnyMiddlewareChain> = RequestContext<InputEnv<T>, InputProperties<T>, Platform<T>>;
|
|
123
|
+
type RequestMiddleware<T extends AnyMiddlewareChain = MiddlewareChain> = (context: RequestContext<InputEnv<T>, InputProperties<T>, Platform<T>>) => Awaitable<Response | RequestPlugin | void>;
|
|
121
124
|
type ResponseCallback = (response: Response) => Awaitable<Response | void>;
|
|
122
125
|
interface RequestHandler<T extends MiddlewareTypes = any> extends HattipHandler<T['platform']>, MiddlewareChain<T> {
|
|
123
126
|
}
|
|
@@ -141,45 +144,40 @@ type Middleware<TEnv extends object = any, TProperties extends object = any, TPl
|
|
|
141
144
|
/**
|
|
142
145
|
* Extract a `Middleware` type from a `MiddlewareChain` type.
|
|
143
146
|
*/
|
|
144
|
-
type ExtractMiddleware<T extends
|
|
147
|
+
type ExtractMiddleware<T extends AnyMiddlewareChain> = [T] extends [
|
|
148
|
+
never
|
|
149
|
+
] ? Middleware<{}, {}, any> : Middleware<Env<T>, Properties<T>, Platform<T>>;
|
|
145
150
|
/**
|
|
146
151
|
* Merge a request plugin into a middleware chain.
|
|
147
152
|
*/
|
|
148
|
-
type ApplyMiddlewareResult<TParent extends
|
|
153
|
+
type ApplyMiddlewareResult<TParent extends AnyMiddlewareChain, TResult> = Eval<{
|
|
149
154
|
env: Merge<Env<TParent>, TResult extends {
|
|
150
155
|
env: infer TEnv extends object | undefined;
|
|
151
156
|
} ? TEnv : undefined>;
|
|
152
157
|
properties: Merge<Properties<TParent>, TResult extends RequestPlugin ? Omit<TResult, keyof ReservedProperties> : undefined>;
|
|
153
158
|
}>;
|
|
154
|
-
type ApplyMiddlewareOutputs<TFirst extends
|
|
159
|
+
type ApplyMiddlewareOutputs<TFirst extends AnyMiddlewareChain, TSecond extends AnyMiddleware> = TSecond extends AnyMiddlewareChain ? {
|
|
155
160
|
env: Merge<Env<TFirst>, Env<TSecond>>;
|
|
156
161
|
properties: Merge<Properties<TFirst>, Properties<TSecond>>;
|
|
157
162
|
} : TSecond extends (...args: any[]) => Awaitable<infer TResult> ? ApplyMiddlewareResult<TFirst, Exclude<TResult, Response>> : Current<TFirst>;
|
|
158
|
-
type MiddlewareInputs<T extends
|
|
163
|
+
type MiddlewareInputs<T extends AnyMiddleware> = T extends AnyMiddlewareChain ? Inputs<T> : T extends Middleware<infer TEnv, infer TProperties> ? {
|
|
159
164
|
env: TEnv;
|
|
160
165
|
properties: TProperties;
|
|
161
166
|
} : never;
|
|
162
|
-
type MiddlewarePlatform<T extends
|
|
167
|
+
type MiddlewarePlatform<T extends AnyMiddleware> = T extends AnyMiddlewareChain ? Platform<T> : T extends Middleware<any, any, infer TPlatform> ? TPlatform : never;
|
|
163
168
|
/**
|
|
164
|
-
* This applies a middleware to a chain. If the type `
|
|
169
|
+
* This applies a middleware to a chain. If the type `TSecond` is itself a
|
|
165
170
|
* chain, it's treated as a nested chain, which won't leak its plugins into the
|
|
166
171
|
* parent chain.
|
|
167
172
|
*
|
|
168
173
|
* The `TFirst` type is allowed to be `never`, which results in the middleware's
|
|
169
174
|
* output types being used as the request handler's input types.
|
|
170
175
|
*/
|
|
171
|
-
type ApplyMiddleware<TFirst extends
|
|
176
|
+
type ApplyMiddleware<TFirst extends AnyMiddlewareChain, TSecond extends AnyMiddleware> = ApplyMiddlewareOutputs<TFirst, TSecond> extends infer TCurrent extends MiddlewareTypes['current'] ? {
|
|
172
177
|
initial: CastNever<Inputs<TFirst>, MiddlewareInputs<TSecond>>;
|
|
173
178
|
current: TCurrent;
|
|
174
179
|
platform: CastNever<Platform<TFirst>, MiddlewarePlatform<TSecond>>;
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* Apply a list of middlewares to an empty middleware chain.
|
|
178
|
-
*/
|
|
179
|
-
type ApplyMiddlewares<T extends Middleware[]> = T extends [
|
|
180
|
-
...infer TRest extends Middleware[],
|
|
181
|
-
infer TLast extends Middleware
|
|
182
|
-
] ? ApplyMiddleware<ApplyMiddlewares<TRest>, TLast> : ApplyFirstMiddleware<T[0]>;
|
|
180
|
+
} : never;
|
|
183
181
|
type EmptyMiddlewareChain<TPlatform = unknown> = MiddlewareChain<{
|
|
184
182
|
initial: {
|
|
185
183
|
env: {};
|
|
@@ -191,12 +189,23 @@ type EmptyMiddlewareChain<TPlatform = unknown> = MiddlewareChain<{
|
|
|
191
189
|
};
|
|
192
190
|
platform: TPlatform;
|
|
193
191
|
}>;
|
|
194
|
-
|
|
192
|
+
/**
|
|
193
|
+
* Convert a `Middleware` type into a `MiddlewareTypes` type.
|
|
194
|
+
* @internal For similar behavior with public APIs, use `ApplyMiddlewares<[T]>`.
|
|
195
|
+
*/
|
|
196
|
+
type ApplyFirstMiddleware<T extends AnyMiddleware> = T extends AnyMiddlewareChain<infer TInternal> ? TInternal : ApplyMiddleware<EmptyMiddlewareChain<MiddlewarePlatform<T>>, T>;
|
|
197
|
+
/**
|
|
198
|
+
* Flatten a list of middlewares into a `MiddlewareTypes` type.
|
|
199
|
+
*/
|
|
200
|
+
type ApplyMiddlewares<T extends AnyMiddleware[]> = T extends [
|
|
201
|
+
...infer TRest extends AnyMiddleware[],
|
|
202
|
+
infer TLast extends AnyMiddleware
|
|
203
|
+
] ? ApplyMiddleware<MiddlewareChain<ApplyMiddlewares<TRest>>, TLast> : ApplyFirstMiddleware<T[0]>;
|
|
195
204
|
type RouteMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS' | 'HEAD' | (string & {});
|
|
196
|
-
type RouteContext<T extends RouterTypes = any, TPathParams extends object = any, TMethod extends RouteMethod = RouteMethod> = MiddlewareContext<ApplyMiddleware<MiddlewareChain<T['$Router']>, () => {
|
|
205
|
+
type RouteContext<T extends RouterTypes = any, TPathParams extends object = any, TMethod extends RouteMethod = RouteMethod> = MiddlewareContext<MiddlewareChain<ApplyMiddleware<MiddlewareChain<T['$Router']>, () => {
|
|
197
206
|
params: TPathParams;
|
|
198
207
|
method: TMethod;
|
|
199
|
-
}
|
|
208
|
+
}>>>;
|
|
200
209
|
type RouteHandler<T extends RouterTypes = any, TPathParams extends object = any, TMethod extends RouteMethod = RouteMethod> = (context: RouteContext<T, TPathParams, TMethod>) => Awaitable<Response | void>;
|
|
201
210
|
declare class RouterTypes<T extends MiddlewareChain = any> extends Function {
|
|
202
211
|
/** This property won't exist at runtime. It contains type information for inference purposes. */
|
|
@@ -229,7 +238,7 @@ declare class MiddlewareChain<T extends MiddlewareTypes = any> {
|
|
|
229
238
|
*
|
|
230
239
|
* @returns a new `MiddlewareChain` instance
|
|
231
240
|
*/
|
|
232
|
-
use<const TMiddleware extends ExtractMiddleware<this>>(middleware: TMiddleware): ApplyMiddleware<this, TMiddleware
|
|
241
|
+
use<const TMiddleware extends ExtractMiddleware<this>>(middleware: TMiddleware): RequestHandler<ApplyMiddleware<this, TMiddleware>>;
|
|
233
242
|
/**
|
|
234
243
|
* Create a middleware function that encapsulates this middleware chain, so
|
|
235
244
|
* any modifications it makes to the request context are not leaked.
|
|
@@ -253,6 +262,6 @@ declare function chain<TEnv extends object = {}, TProperties extends object = {}
|
|
|
253
262
|
};
|
|
254
263
|
platform: TPlatform;
|
|
255
264
|
}>;
|
|
256
|
-
declare function chain<T extends
|
|
265
|
+
declare function chain<T extends AnyMiddleware>(middleware: T): T extends AnyMiddlewareChain ? T : RequestHandler<ApplyFirstMiddleware<T>>;
|
|
257
266
|
|
|
258
|
-
export { type ApplyMiddleware as A, type EmptyMiddlewareChain as E, type HattipContext as H, type MiddlewareContext as M, type Router as R, MiddlewareChain as a, type RouteContext as b, type RouteHandler as c, chain as d, type ApplyMiddlewares as e, filterPlatform as f, type EnvAccessor as g, type ExtractMiddleware as h, type Middleware as i, type
|
|
267
|
+
export { type ApplyMiddleware as A, type EmptyMiddlewareChain as E, type HattipContext as H, type MiddlewareContext as M, type Router as R, MiddlewareChain as a, type RouteContext as b, type RouteHandler as c, chain as d, type ApplyMiddlewares as e, filterPlatform as f, type EnvAccessor as g, type ExtractMiddleware as h, type Middleware as i, type MiddlewareTypes as j, type RequestContext as k, type RequestHandler as l, type RequestMiddleware as m, type RequestPlugin as n, type ResponseCallback as o };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { A as ApplyMiddleware, e as ApplyMiddlewares, E as EmptyMiddlewareChain, g as EnvAccessor, h as ExtractMiddleware, H as HattipContext, i as Middleware, a as MiddlewareChain, M as MiddlewareContext, j as
|
|
1
|
+
export { A as ApplyMiddleware, e as ApplyMiddlewares, E as EmptyMiddlewareChain, g as EnvAccessor, h as ExtractMiddleware, H as HattipContext, i as Middleware, a as MiddlewareChain, M as MiddlewareContext, j as MiddlewareTypes, k as RequestContext, l as RequestHandler, m as RequestMiddleware, n as RequestPlugin, o as ResponseCallback, d as chain, f as filterPlatform } from './index-F_xAxN5f.js';
|
|
2
2
|
import '@hattip/core';
|
|
3
3
|
import 'pathic';
|
package/dist/index.js
CHANGED
package/dist/router.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { R as Router, M as MiddlewareContext, a as MiddlewareChain, E as EmptyMiddlewareChain } from './index-
|
|
2
|
-
export { b as RouteContext, c as RouteHandler } from './index-
|
|
1
|
+
import { R as Router, M as MiddlewareContext, a as MiddlewareChain, E as EmptyMiddlewareChain } from './index-F_xAxN5f.js';
|
|
2
|
+
export { b as RouteContext, c as RouteHandler } from './index-F_xAxN5f.js';
|
|
3
3
|
import '@hattip/core';
|
|
4
4
|
import 'pathic';
|
|
5
5
|
|
package/dist/router.js
CHANGED