Skip to content

Commit

Permalink
refactor(handler): thorough check for Response type (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
h3ssan authored Aug 27, 2024
1 parent c51cc5d commit 2187f0d
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,18 @@ export type Response = readonly [body: ResponseBody | null, init: ResponseInit];

/** Checks whether the passed value is the `graphql-http` server agnostic response. */
function isResponse(val: unknown): val is Response {
// TODO: make sure the contents of init match ResponseInit
return (
Array.isArray(val) &&
(typeof val[0] === 'string' || val[0] === null) &&
isObject(val[1])
);
// Make sure the contents of body match string | null
if (!Array.isArray(val)) return false;
if (typeof val[0] !== 'string' || val[0] !== null) return false;
if (!isObject(val[1])) return false;

// Make sure the contents of init match ResponseInit
const init = val[1];
if (typeof init.status !== 'number') return false;
if (typeof init.statusText !== 'string') return false;
if (init.headers && !isObject(init.headers)) return false;

return true;
}

/**
Expand Down

0 comments on commit 2187f0d

Please sign in to comment.