alien-middleware 0.10.4 → 0.11.0
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;
|
|
@@ -115,7 +115,7 @@ type RequestContext<TEnv extends object = any, TProperties extends object = neve
|
|
|
115
115
|
*/
|
|
116
116
|
type MiddlewareContext<T extends MiddlewareChain | Middleware[]> = [
|
|
117
117
|
T
|
|
118
|
-
] extends [never] ? RequestContext<{}, never, unknown> : T extends MiddlewareChain ? RequestContext<Env<T>, Properties<T>, Platform<T>> : T extends Middleware[] ? MiddlewareContext<ApplyMiddlewares<T
|
|
118
|
+
] extends [never] ? RequestContext<{}, never, unknown> : T extends MiddlewareChain ? RequestContext<Env<T>, Properties<T>, Platform<T>> : T extends Middleware[] ? MiddlewareContext<MiddlewareChain<ApplyMiddlewares<T>>> : never;
|
|
119
119
|
type IsolatedContext<T extends MiddlewareChain> = RequestContext<InputEnv<T>, InputProperties<T>, Platform<T>>;
|
|
120
120
|
type RequestMiddleware<T extends MiddlewareChain = MiddlewareChain> = (context: RequestContext<InputEnv<T>, InputProperties<T>, Platform<T>>) => Awaitable<Response | RequestPlugin | void>;
|
|
121
121
|
type ResponseCallback = (response: Response) => Awaitable<Response | void>;
|
|
@@ -161,25 +161,18 @@ type MiddlewareInputs<T extends Middleware> = T extends Middleware<infer TEnv, i
|
|
|
161
161
|
} : never;
|
|
162
162
|
type MiddlewarePlatform<T extends Middleware> = T extends Middleware<any, any, infer TPlatform> ? TPlatform : never;
|
|
163
163
|
/**
|
|
164
|
-
* This applies a middleware to a chain. If the type `
|
|
164
|
+
* This applies a middleware to a chain. If the type `TSecond` is itself a
|
|
165
165
|
* chain, it's treated as a nested chain, which won't leak its plugins into the
|
|
166
166
|
* parent chain.
|
|
167
167
|
*
|
|
168
168
|
* The `TFirst` type is allowed to be `never`, which results in the middleware's
|
|
169
169
|
* output types being used as the request handler's input types.
|
|
170
170
|
*/
|
|
171
|
-
type ApplyMiddleware<TFirst extends MiddlewareChain, TSecond extends Middleware> = ApplyMiddlewareOutputs<TFirst, TSecond> extends infer TCurrent extends MiddlewareTypes['current'] ?
|
|
171
|
+
type ApplyMiddleware<TFirst extends MiddlewareChain, TSecond extends Middleware> = ApplyMiddlewareOutputs<TFirst, TSecond> extends infer TCurrent extends MiddlewareTypes['current'] ? {
|
|
172
172
|
initial: CastNever<Inputs<TFirst>, MiddlewareInputs<TSecond>>;
|
|
173
173
|
current: TCurrent;
|
|
174
174
|
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]>;
|
|
175
|
+
} : never;
|
|
183
176
|
type EmptyMiddlewareChain<TPlatform = unknown> = MiddlewareChain<{
|
|
184
177
|
initial: {
|
|
185
178
|
env: {};
|
|
@@ -191,12 +184,23 @@ type EmptyMiddlewareChain<TPlatform = unknown> = MiddlewareChain<{
|
|
|
191
184
|
};
|
|
192
185
|
platform: TPlatform;
|
|
193
186
|
}>;
|
|
194
|
-
|
|
187
|
+
/**
|
|
188
|
+
* Convert a `Middleware` type into a `MiddlewareTypes` type.
|
|
189
|
+
* @internal For similar behavior with public APIs, use `ApplyMiddlewares<[T]>`.
|
|
190
|
+
*/
|
|
191
|
+
type ApplyFirstMiddleware<T extends Middleware> = T extends MiddlewareChain<infer TInternal> ? TInternal : ApplyMiddleware<EmptyMiddlewareChain<MiddlewarePlatform<T>>, T>;
|
|
192
|
+
/**
|
|
193
|
+
* Flatten a list of middlewares into a `MiddlewareTypes` type.
|
|
194
|
+
*/
|
|
195
|
+
type ApplyMiddlewares<T extends Middleware[]> = T extends [
|
|
196
|
+
...infer TRest extends Middleware[],
|
|
197
|
+
infer TLast extends Middleware
|
|
198
|
+
] ? ApplyMiddleware<MiddlewareChain<ApplyMiddlewares<TRest>>, TLast> : ApplyFirstMiddleware<T[0]>;
|
|
195
199
|
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']>, () => {
|
|
200
|
+
type RouteContext<T extends RouterTypes = any, TPathParams extends object = any, TMethod extends RouteMethod = RouteMethod> = MiddlewareContext<MiddlewareChain<ApplyMiddleware<MiddlewareChain<T['$Router']>, () => {
|
|
197
201
|
params: TPathParams;
|
|
198
202
|
method: TMethod;
|
|
199
|
-
}
|
|
203
|
+
}>>>;
|
|
200
204
|
type RouteHandler<T extends RouterTypes = any, TPathParams extends object = any, TMethod extends RouteMethod = RouteMethod> = (context: RouteContext<T, TPathParams, TMethod>) => Awaitable<Response | void>;
|
|
201
205
|
declare class RouterTypes<T extends MiddlewareChain = any> extends Function {
|
|
202
206
|
/** This property won't exist at runtime. It contains type information for inference purposes. */
|
|
@@ -229,7 +233,7 @@ declare class MiddlewareChain<T extends MiddlewareTypes = any> {
|
|
|
229
233
|
*
|
|
230
234
|
* @returns a new `MiddlewareChain` instance
|
|
231
235
|
*/
|
|
232
|
-
use<const TMiddleware extends ExtractMiddleware<this>>(middleware: TMiddleware): ApplyMiddleware<this, TMiddleware
|
|
236
|
+
use<const TMiddleware extends ExtractMiddleware<this>>(middleware: TMiddleware): RequestHandler<ApplyMiddleware<this, TMiddleware>>;
|
|
233
237
|
/**
|
|
234
238
|
* Create a middleware function that encapsulates this middleware chain, so
|
|
235
239
|
* any modifications it makes to the request context are not leaked.
|
|
@@ -253,6 +257,6 @@ declare function chain<TEnv extends object = {}, TProperties extends object = {}
|
|
|
253
257
|
};
|
|
254
258
|
platform: TPlatform;
|
|
255
259
|
}>;
|
|
256
|
-
declare function chain<T extends Middleware | MiddlewareChain>(middleware: T): T extends Middleware ? ApplyFirstMiddleware<T
|
|
260
|
+
declare function chain<T extends Middleware | MiddlewareChain>(middleware: T): T extends Middleware ? RequestHandler<ApplyFirstMiddleware<T>> : T;
|
|
257
261
|
|
|
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
|
|
262
|
+
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-Dj_pJd-o.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-Dj_pJd-o.js';
|
|
2
|
+
export { b as RouteContext, c as RouteHandler } from './index-Dj_pJd-o.js';
|
|
3
3
|
import '@hattip/core';
|
|
4
4
|
import 'pathic';
|
|
5
5
|
|
package/dist/router.js
CHANGED