Skip to content

Commit

Permalink
Fix types of MiddlewareNext (#2280)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie authored Dec 11, 2024
2 parents b984d2f + 83c5465 commit 05576ad
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-tables-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"graphile-config": patch
---

Fix TypeScript types of MiddlewareNext
30 changes: 12 additions & 18 deletions utils/graphile-config/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import type {
} from "./interfaces.js";
import { isPromiseLike } from "./utils.js";

export interface MiddlewareNext<
TRawResult,
TAwaitedResult = Awaited<TRawResult>,
> {
export interface MiddlewareNext<TRawResult> {
(): TRawResult;
callback(
callback: (error: object | null, result: TAwaitedResult) => TRawResult,
callback: (
error: object | null,
result: Awaited<TRawResult>,
) => TRawResult | Awaited<TRawResult>,
): TRawResult;
}

Expand Down Expand Up @@ -131,10 +131,7 @@ function executeMiddleware<
),
);
const middleware = middlewareList[idx];
const result = middleware(
next as MiddlewareNext<unknown, unknown>,
arg,
) as any;
const result = middleware(next as MiddlewareNext<unknown>, arg) as any;
if (!allowAsync && isPromiseLike(result)) {
throw new Error(
`'${String(
Expand All @@ -145,28 +142,25 @@ function executeMiddleware<
return result;
}

function makeNext<TRawResult, TAwaitedResult = Awaited<TRawResult>>(
function makeNext<TRawResult>(
fn: () => TRawResult,
): MiddlewareNext<TRawResult, TAwaitedResult> {
): MiddlewareNext<TRawResult> {
let called = false;
const next = fn as MiddlewareNext<TRawResult, TAwaitedResult>;
const next = fn as MiddlewareNext<TRawResult>;
next.callback = (callback) => {
if (called) {
throw new Error(`next() was already called; don't call it twice!`);
}
called = true;
let result: PromiseOrDirect<TAwaitedResult>;
let result: PromiseOrDirect<Awaited<TRawResult>>;
try {
result = fn() as PromiseOrDirect<TAwaitedResult>;
result = fn() as PromiseOrDirect<Awaited<TRawResult>>;
} catch (error) {
return callback(error, undefined as any);
}
if (isPromiseLike(result)) {
return result.then(
(result) =>
callback(null, result) as
| TAwaitedResult
| PromiseLike<TAwaitedResult>,
(result) => callback(null, result) as Awaited<TRawResult> | TRawResult,
callback as any,
) as TRawResult;
} else {
Expand Down

0 comments on commit 05576ad

Please sign in to comment.