From 16e8325bd90ec189522b6131378e6ec61f878f88 Mon Sep 17 00:00:00 2001 From: avdb Date: Fri, 19 Jan 2024 13:37:30 +0100 Subject: [PATCH] PR changes --- src/GraphqlQueryStore.ts | 6 ++-- src/createGraphqlRequestHandler.ts | 3 +- src/errors.ts | 23 ++++++++++----- src/processGraphqlRequest.ts | 7 +++-- src/utils.ts | 47 +++++++----------------------- 5 files changed, 34 insertions(+), 52 deletions(-) diff --git a/src/GraphqlQueryStore.ts b/src/GraphqlQueryStore.ts index 42bb059..c3eb521 100644 --- a/src/GraphqlQueryStore.ts +++ b/src/GraphqlQueryStore.ts @@ -1,20 +1,20 @@ /* eslint-disable max-classes-per-file */ /* eslint-disable class-methods-use-this */ import { createHash } from 'crypto' -import { parse, specifiedRules, validate } from 'graphql' +import { GraphQLSchema, parse, specifiedRules, validate } from 'graphql' import { compileQuery, isCompiledQuery } from 'graphql-jit' import { createComplexityRule, simpleEstimator } from 'graphql-query-complexity' import { GraphqlValidationError } from './errors' export default class GraphqlQueryStore { - schema: any + schema: GraphQLSchema validationRules: any maximumComplexity: number defaultComplexity: number store: Map constructor( - schema: any, + schema: GraphQLSchema, { validationRules = [], queryComplexity: { maximumComplexity = 1000, defaultComplexity = 1 } = {}, diff --git a/src/createGraphqlRequestHandler.ts b/src/createGraphqlRequestHandler.ts index 0655b7a..2cbe58d 100644 --- a/src/createGraphqlRequestHandler.ts +++ b/src/createGraphqlRequestHandler.ts @@ -1,3 +1,4 @@ +import { IncomingMessage } from 'http' import processGraphqlRequest from './processGraphqlRequest' import { readRequestBody } from './utils' @@ -7,7 +8,7 @@ export default function createGraphqlRequestHandler( processFileUploads?: any ) { return async ( - req: any, + req: IncomingMessage, res: { writeHead: (arg0: number, arg1?: { 'Content-Type': string } | undefined) => void end: (arg0: string) => void diff --git a/src/errors.ts b/src/errors.ts index 721ef22..502f5c5 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -8,8 +8,8 @@ const UNAUTHORIZED_ERROR = 'UNAUTHORIZED_ERROR' const USER_INPUT_ERROR = 'USER_INPUT_ERROR' export class GraphqlError extends Error { - originalError: Error | null - code: string + originalError: any + code: string | null extensions: any static CODE: string errors?: any @@ -17,7 +17,7 @@ export class GraphqlError extends Error { constructor( message = 'Something went wrong', code = INTERNAL_SERVER_ERROR, - originalError?: Error | null, + originalError?: any, extensions?: { errors: any } @@ -61,7 +61,7 @@ GraphqlValidationError.CODE = VALIDATION_ERROR export class GraphqlContextError extends GraphqlError { constructor( message = 'Context creation failed.', - originalError?: Error | null, + originalError?: any, extensions?: { errors: any } @@ -76,8 +76,8 @@ GraphqlContextError.CODE = CONTEXT_ERROR export class GraphqlUnauthorizedError extends GraphqlError { constructor( message = 'Unauthorized.', - originalError: Error | null, - extensions: { + originalError?: any, + extensions?: { errors: any } ) { @@ -91,8 +91,8 @@ GraphqlUnauthorizedError.CODE = UNAUTHORIZED_ERROR export class GraphqlUserInputError extends GraphqlError { constructor( message = 'Bad user input error.', - originalError: Error | null, - extensions: { + originalError?: any, + extensions?: { errors: any } ) { @@ -102,3 +102,10 @@ export class GraphqlUserInputError extends GraphqlError { } GraphqlUserInputError.CODE = USER_INPUT_ERROR + +export class GraphqlErrorWithMessageAndOriginalError extends GraphqlError { + constructor(message = 'Something went wrong', originalError: any) { + super(message, undefined, originalError) + this.name = 'GraphqlErrorWithMessageAndOriginalError' + } +} diff --git a/src/processGraphqlRequest.ts b/src/processGraphqlRequest.ts index 3e48c48..f7fbdf8 100644 --- a/src/processGraphqlRequest.ts +++ b/src/processGraphqlRequest.ts @@ -1,4 +1,5 @@ // eslint-disable-next-line import/extensions +import { IncomingMessage } from 'http' import { GraphqlContextError, GraphqlValidationError } from './errors' import { badRequest, badRequestJson, json, methodNotAllowed } from './responses' @@ -9,7 +10,7 @@ type GraphqlResponse = { } export default async function processGraphqlRequest( - req: { method: string; headers: { [x: string]: any } }, + req: IncomingMessage, { store, context = {}, @@ -18,8 +19,8 @@ export default async function processGraphqlRequest( }: { store: any context: any - processFileUploads: any - readRequestBody: (req: any) => Promise + processFileUploads: (req: IncomingMessage) => Promise + readRequestBody: (req: IncomingMessage) => Promise } ): Promise { if (!store) { diff --git a/src/utils.ts b/src/utils.ts index 2043c49..91d8876 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,59 +1,32 @@ +import { IncomingMessage, ServerResponse } from 'http' + const OK = 200 const METHOD_NOT_ALLOWED = 405 -const BED_REQUEST = 400 +const BAD_REQUEST = 400 -export const readRequestBody = (req: { - on: ( - arg0: string, - arg1: (chunk: any) => number - ) => { - (): any - new (): any - on: { - (arg0: string, arg1: (error: any) => void): { - (): any - new (): any - on: { (arg0: string, arg1: () => void): void; new (): any } - } - new (): any - } - } -}) => +export const readRequestBody = (req: IncomingMessage) => new Promise((resolve, reject) => { const body: any[] = [] req.on('data', (chunk: any) => body.push(chunk)) - .on('error', (error: Error) => reject(error)) + .on('error', (error: any) => reject(error)) .on('end', () => resolve(JSON.parse(Buffer.concat(body).toString()))) }) -export const methodNotAllowed = ( - res: { writeHead: (arg0: number) => void; end: (arg0: string) => void }, - message = 'Method Not Allowed' -) => { +export const methodNotAllowed = (res: ServerResponse, message = 'Method Not Allowed') => { res.writeHead(METHOD_NOT_ALLOWED) res.end(message) } -export const badRequest = ( - res: { writeHead: (arg0: number) => void; end: (arg0: string) => void }, - message = 'Bad Request' -) => { - res.writeHead(BED_REQUEST) +export const badRequest = (res: ServerResponse, message = 'Bad Request') => { + res.writeHead(BAD_REQUEST) res.end(message) } -export const json = ( - res: { - writeHead: (arg0: number, arg1: { 'Content-Type': string }) => void - end: (arg0: string) => void - }, - result: any, - statusCode = OK -) => { +export const json = (res: ServerResponse, result: any, statusCode = OK) => { res.writeHead(statusCode, { 'Content-Type': 'application/json', }) res.end(JSON.stringify(result)) } -export const badRequestJson = (res: any, result: any) => json(res, result, BED_REQUEST) +export const badRequestJson = (res: any, result: any) => json(res, result, BAD_REQUEST)