From 17e4d78bdc4741d5ffb76da95f45058910e21f10 Mon Sep 17 00:00:00 2001 From: James Date: Sun, 29 Nov 2020 14:27:56 -0500 Subject: [PATCH] further typing --- src/auth/requestHandlers/access.ts | 5 ++++- src/config/types.ts | 1 + src/express/middleware/errorHandler.ts | 8 ++++++-- src/index.ts | 13 +++++++------ src/init/bindRequestHandlers.ts | 5 ++++- src/types/index.ts | 24 ++++++++++++++++-------- 6 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/auth/requestHandlers/access.ts b/src/auth/requestHandlers/access.ts index 443ca9f955..eb38a292f9 100644 --- a/src/auth/requestHandlers/access.ts +++ b/src/auth/requestHandlers/access.ts @@ -1,8 +1,11 @@ import { Response, NextFunction } from 'express'; import httpStatus from 'http-status'; import { PayloadRequest } from '../../express/types/payloadRequest'; +import { Permissions } from '../types'; -export default async function policiesHandler(req: PayloadRequest, res: Response, next: NextFunction): Promise { +export type AccessRequestHandler = (req: PayloadRequest, res: Response, next: NextFunction) => unknown; + +export default async function accessRequestHandler(req: PayloadRequest, res: Response, next: NextFunction): Promise | void> { try { const accessResults = await this.operations.collections.auth.access({ req, diff --git a/src/config/types.ts b/src/config/types.ts index c4e9a138f4..be966d5be5 100644 --- a/src/config/types.ts +++ b/src/config/types.ts @@ -76,6 +76,7 @@ export type PayloadConfig = { graphQL?: string; graphQLPlayground?: string; }; + debug?: boolean express?: { json: { limit?: number diff --git a/src/express/middleware/errorHandler.ts b/src/express/middleware/errorHandler.ts index e48d4658ea..5aa6e06854 100644 --- a/src/express/middleware/errorHandler.ts +++ b/src/express/middleware/errorHandler.ts @@ -1,10 +1,14 @@ +/* eslint-disable @typescript-eslint/no-unused-vars */ import httpStatus from 'http-status'; import { Response, NextFunction } from 'express'; -import formatErrorResponse from '../responses/formatError'; +import { Config } from '../../config/types'; +import formatErrorResponse, { ErrorResponse } from '../responses/formatError'; import { PayloadRequest } from '../types/payloadRequest'; +export type ErrorHandler = (err: Error, req: PayloadRequest, res: Response, next: NextFunction) => Promise> + // NextFunction must be passed for Express to use this middleware as error handler -const errorHandler = (config, logger) => async (err, req: PayloadRequest, res: Response, next: NextFunction): Promise => { +const errorHandler = (config: Config, logger) => async (err: Error, req: PayloadRequest, res: Response, next: NextFunction): Promise => { const data = formatErrorResponse(err); let response; let status = err.status || httpStatus.INTERNAL_SERVER_ERROR; diff --git a/src/index.ts b/src/index.ts index 14d69195d8..d371ee17fb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,6 +11,7 @@ import { Collection, } from './collections/config/types'; import { + Document, CreateOptions, FindOptions, FindGlobalOptions, @@ -201,7 +202,7 @@ export class Payload { * @param options * @returns created document */ - async create(options: CreateOptions): Promise { + async create(options: CreateOptions): Promise { let { create } = localOperations; create = create.bind(this); return create(options); @@ -218,13 +219,13 @@ export class Payload { return find(options); } - async findGlobal(options: FindGlobalOptions): Promise { + async findGlobal(options: FindGlobalOptions): Promise { let { findOne } = localGlobalOperations; findOne = findOne.bind(this); return findOne(options); } - async updateGlobal(options: UpdateGlobalOptions): Promise { + async updateGlobal(options: UpdateGlobalOptions): Promise { let { update } = localGlobalOperations; update = update.bind(this); return update(options); @@ -235,7 +236,7 @@ export class Payload { * @param options * @returns document with specified ID */ - async findByID(options: FindByIDOptions): Promise { + async findByID(options: FindByIDOptions): Promise { let { findByID } = localOperations; findByID = findByID.bind(this); return findByID(options); @@ -246,13 +247,13 @@ export class Payload { * @param options * @returns Updated document */ - async update(options: UpdateOptions): Promise { + async update(options: UpdateOptions): Promise { let { update } = localOperations; update = update.bind(this); return update(options); } - async delete(options: DeleteOptions): Promise { + async delete(options: DeleteOptions): Promise { let { localDelete: deleteOperation } = localOperations; deleteOperation = deleteOperation.bind(this); return deleteOperation(options); diff --git a/src/init/bindRequestHandlers.ts b/src/init/bindRequestHandlers.ts index dcb1968ef2..b53e266391 100644 --- a/src/init/bindRequestHandlers.ts +++ b/src/init/bindRequestHandlers.ts @@ -1,4 +1,4 @@ -import access from '../auth/requestHandlers/access'; +import access, { AccessRequestHandler } from '../auth/requestHandlers/access'; import forgotPassword from '../auth/requestHandlers/forgotPassword'; import init from '../auth/requestHandlers/init'; import login from '../auth/requestHandlers/login'; @@ -23,6 +23,9 @@ import { Payload } from '../index'; export type RequestHandlers = { collections: { create: CreateRequestHandler + auth: { + access: AccessRequestHandler + } } } diff --git a/src/types/index.ts b/src/types/index.ts index 3928a63f96..a5e38ad8f9 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -9,7 +9,9 @@ export type Document = { export type CreateOptions = { collection: string; - data: any; + data: { + [key: string]: unknown + }; }; export type FindOptions = { @@ -37,7 +39,9 @@ export type FindGlobalOptions = { }; export type UpdateGlobalOptions = { global: string; - data: any; + data: { + [key: string]: unknown + }; }; export type FindByIDOptions = { @@ -47,7 +51,9 @@ export type FindByIDOptions = { export type UpdateOptions = { collection: string; id: string; - data: any; + data: { + [key: string]: unknown + }; }; export type DeleteOptions = { @@ -59,14 +65,16 @@ export type ForgotPasswordOptions = { collection: string; generateEmailHTML?: (token: string) => string; expiration: Date; - data: any; + data: { + [key: string]: unknown + }; }; export interface OperationArguments { - data?: {[key: string]: any}; - originalDoc?: {[key: string]: any}; - fullOriginalDoc?: {[key: string]: any}; - fullData?: {[key: string]: any}; + data?: {[key: string]: unknown}; + originalDoc?: Document; + fullOriginalDoc?: {[key: string]: unknown}; + fullData?: {[key: string]: unknown}; operation?: unknown; hook?: string; req?: PayloadRequest;