Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit f894ae8

Browse files
Improve TS type coverage (#677)
1 parent 7b9e5ec commit f894ae8

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

.eslintrc.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ rules:
5151
node/no-unpublished-import: error
5252
node/no-unpublished-require: error
5353
node/no-unsupported-features/es-builtins: error
54-
node/no-unsupported-features/es-syntax: off # TODO enable
54+
node/no-unsupported-features/es-syntax: error
5555
node/no-unsupported-features/node-builtins: error
5656
node/process-exit-as-throw: error
5757
node/shebang: error
@@ -444,6 +444,8 @@ overrides:
444444
extends:
445445
- plugin:import/typescript
446446
rules:
447+
node/no-unsupported-features/es-syntax: off
448+
447449
##########################################################################
448450
# `@typescript-eslint/eslint-plugin` rule list based on `v3.5.x`
449451
##########################################################################

src/index.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type {
1515
GraphQLFormattedError,
1616
} from 'graphql';
1717
import accepts from 'accepts';
18-
import httpError, { HttpError } from 'http-errors';
18+
import httpError from 'http-errors';
1919
import {
2020
Source,
2121
parse,
@@ -31,7 +31,9 @@ import type { GraphiQLOptions, GraphiQLData } from './renderGraphiQL';
3131
import { parseBody } from './parseBody';
3232
import { renderGraphiQL } from './renderGraphiQL';
3333

34-
type Request = IncomingMessage;
34+
// `url` is always defined for IncomingMessage coming from http.Server
35+
type Request = IncomingMessage & { url: string };
36+
3537
type Response = ServerResponse & { json?: (data: unknown) => void };
3638
type MaybePromise<T> = Promise<T> | T;
3739

@@ -361,11 +363,10 @@ export function graphqlHTTP(options: Options): Middleware {
361363
// If an error was caught, report the httpError status, or 500.
362364
response.statusCode = error.status ?? 500;
363365

364-
if (error.headers) {
365-
const err = error as HttpError;
366-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
367-
for (const [key, value] of Object.entries(err.headers!)) {
368-
response.setHeader(key, value);
366+
const { headers } = error;
367+
if (headers != null) {
368+
for (const [key, value] of Object.entries(headers)) {
369+
response.setHeader(key, String(value));
369370
}
370371
}
371372

@@ -465,9 +466,7 @@ export interface GraphQLParams {
465466
export async function getGraphQLParams(
466467
request: Request,
467468
): Promise<GraphQLParams> {
468-
// `url` is always defined for IncomingMessage coming from http.Server
469-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
470-
const urlData = new URLSearchParams(request.url!.split('?')[1]);
469+
const urlData = new URLSearchParams(request.url.split('?')[1]);
471470
const bodyData = await parseBody(request);
472471

473472
// GraphQL Query string.

src/parseBody.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@ import querystring from 'querystring';
66
import getBody from 'raw-body';
77
import httpError from 'http-errors';
88
import contentType from 'content-type';
9+
import type { ParsedMediaType } from 'content-type';
910

1011
type Request = IncomingMessage & { body?: unknown };
11-
interface ParsedMediaType {
12-
type: string;
13-
parameters: { [key: string]: string | undefined };
14-
}
1512

1613
/**
1714
* Provided a "Request" provided by express or connect (typically a node style
@@ -106,7 +103,7 @@ async function readBody(
106103
} catch (err) {
107104
throw err.type === 'encoding.unsupported'
108105
? httpError(415, `Unsupported charset "${charset.toUpperCase()}".`)
109-
: httpError(400, `Invalid body: ${err.message as string}.`);
106+
: httpError(400, `Invalid body: ${String(err.message)}.`);
110107
}
111108
}
112109

0 commit comments

Comments
 (0)