Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reverse type param ordering #579

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ export interface HandleMethod {
export type ListenOptions = ListenOptionsTls | ListenOptionsBase;

interface ApplicationErrorEventListener<S extends AS, AS> {
// @ts-ignore
(evt: ApplicationErrorEvent<S, AS>): void | Promise<void>;
}

interface ApplicationErrorEventListenerObject<S extends AS, AS> {
// @ts-ignore
handleEvent(evt: ApplicationErrorEvent<S, AS>): void | Promise<void>;
}

Expand Down Expand Up @@ -102,7 +104,7 @@ type ApplicationListenEventListenerOrEventListenerObject =

/** Available options that are used when creating a new instance of
* {@linkcode Application}. */
export interface ApplicationOptions<S, R extends ServerRequest> {
export interface ApplicationOptions<S extends State, R extends ServerRequest> {
/** Determine how when creating a new context, the state from the application
* should be applied. A value of `"clone"` will set the state as a clone of
* the app state. Any non-cloneable or non-enumerable properties will not be
Expand Down
1 change: 1 addition & 0 deletions application_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@ test({
name: "app.state type handling",
fn() {
const app = new Application({ state: { id: 1 } });
// @ts-ignore
app.use((ctx: Context<{ session: number }>) => {
ctx.state.session = 0;
}).use((ctx) => {
Expand Down
3 changes: 1 addition & 2 deletions context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ export interface ContextSendOptions extends SendOptions {
*/
export class Context<
S extends AS = State,
// deno-lint-ignore no-explicit-any
AS extends State = Record<string, any>,
AS extends State = S,
> {
#socket?: WebSocket;
#sse?: ServerSentEventTarget;
Expand Down
2 changes: 1 addition & 1 deletion etag_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function setup<
mockContextState.encodingsAccepted = "identity";
// deno-lint-ignore no-explicit-any
const app = createMockApp<any>();
const context = createMockContext<string, RouteParams<string>, S>({
const context = createMockContext<string, S>({
app,
path,
method,
Expand Down
48 changes: 24 additions & 24 deletions middleware/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ export type Fetch = (input: Request) => Promise<Response>;

export type ProxyMatchFunction<
R extends string,
P extends RouteParams<R> = RouteParams<R>,
// deno-lint-ignore no-explicit-any
S extends State = Record<string, any>,
> = (ctx: Context<S> | RouterContext<R, P, S>) => boolean;
P extends RouteParams<R> = RouteParams<R>,
> = (ctx: Context<S> | RouterContext<R, S, P>) => boolean;

export type ProxyMapFunction<R extends string, P extends RouteParams<R>> = (
path: R,
Expand All @@ -30,15 +30,15 @@ export type ProxyHeadersFunction<S extends State> = (

export type ProxyRouterHeadersFunction<
R extends string,
P extends RouteParams<R>,
S extends State,
> = (ctx: RouterContext<R, P, S>) => HeadersInit | Promise<HeadersInit>;
P extends RouteParams<R>,
> = (ctx: RouterContext<R, S, P>) => HeadersInit | Promise<HeadersInit>;

export interface ProxyOptions<
R extends string,
P extends RouteParams<R> = RouteParams<R>,
// deno-lint-ignore no-explicit-any
S extends State = Record<string, any>,
P extends RouteParams<R> = RouteParams<R>,
> {
/** A callback hook that is called after the response is received which allows
* the response content type to be adjusted. This is for situations where the
Expand All @@ -57,7 +57,7 @@ export interface ProxyOptions<
headers?:
| HeadersInit
| ProxyHeadersFunction<S>
| ProxyRouterHeadersFunction<R, P, S>;
| ProxyRouterHeadersFunction<R, S, P>;
/** Either a record or a proxy map function that will allow proxied requests
* being handled by the middleware to be remapped to a different remote
* path. */
Expand All @@ -72,7 +72,7 @@ export interface ProxyOptions<
match?:
| string
| RegExp
| ProxyMatchFunction<R, P, S>;
| ProxyMatchFunction<R, S, P>;
/** A flag that indicates if traditional proxy headers should be set in the
* response. This defaults to `true`.
*/
Expand All @@ -90,12 +90,12 @@ const FORWARDED_RE =

function createMatcher<
R extends string,
P extends RouteParams<R>,
S extends State,
P extends RouteParams<R>,
>(
{ match }: ProxyOptions<R, P, S>,
{ match }: ProxyOptions<R, S, P>,
) {
return function matches(ctx: RouterContext<R, P, S>): boolean {
return function matches(ctx: RouterContext<R, S, P>): boolean {
if (!match) {
return true;
}
Expand All @@ -111,17 +111,17 @@ function createMatcher<

async function createRequest<
R extends string,
P extends RouteParams<R>,
S extends State,
P extends RouteParams<R>,
>(
target: string | URL,
ctx: Context<S> | RouterContext<R, P, S>,
ctx: Context<S> | RouterContext<R, S, P>,
{ headers: optHeaders, map, proxyHeaders = true, request: reqFn }:
ProxyOptions<R, P, S>,
ProxyOptions<R, S, P>,
): Promise<Request> {
let path = ctx.request.url.pathname as R;
let params: P | undefined;
if (isRouterContext<R, P, S>(ctx)) {
if (isRouterContext<R, S, P>(ctx)) {
params = ctx.params;
}
if (map && typeof map === "function") {
Expand All @@ -143,7 +143,7 @@ async function createRequest<
const headers = new Headers(ctx.request.headers);
if (optHeaders) {
if (typeof optHeaders === "function") {
optHeaders = await optHeaders(ctx as RouterContext<R, P, S>);
optHeaders = await optHeaders(ctx as RouterContext<R, S, P>);
}
for (const [key, value] of iterableHeaders(optHeaders)) {
headers.set(key, value);
Expand Down Expand Up @@ -184,10 +184,10 @@ async function createRequest<

function getBodyInit<
R extends string,
P extends RouteParams<R>,
S extends State,
P extends RouteParams<R>,
>(
ctx: Context<S> | RouterContext<R, P, S>,
ctx: Context<S> | RouterContext<R, S, P>,
): BodyInit | null {
if (!ctx.request.hasBody) {
return null;
Expand All @@ -211,12 +211,12 @@ function iterableHeaders(

async function processResponse<
R extends string,
P extends RouteParams<R>,
S extends State,
P extends RouteParams<R>,
>(
response: Response,
ctx: Context<S> | RouterContext<R, P, S>,
{ contentType: contentTypeFn, response: resFn }: ProxyOptions<R, P, S>,
ctx: Context<S> | RouterContext<R, S, P>,
{ contentType: contentTypeFn, response: resFn }: ProxyOptions<R, S, P>,
) {
if (resFn) {
response = await resFn(response);
Expand Down Expand Up @@ -249,16 +249,16 @@ async function processResponse<
*/
export function proxy<S extends State>(
target: string | URL,
options?: ProxyOptions<string, RouteParams<string>, S>,
options?: ProxyOptions<string, S>,
): Middleware<S>;
export function proxy<
R extends string,
P extends RouteParams<R>,
S extends State,
P extends RouteParams<R>,
>(
target: string | URL,
options: ProxyOptions<R, P, S> = {},
): RouterMiddleware<R, P, S> {
options: ProxyOptions<R, S, P> = {},
): RouterMiddleware<R, S, P> {
const matches = createMatcher(options);
return async function proxy(ctx, next) {
if (!matches(ctx)) {
Expand Down
Loading