further typing
This commit is contained in:
@@ -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<any> {
|
||||
export type AccessRequestHandler = (req: PayloadRequest, res: Response, next: NextFunction) => unknown;
|
||||
|
||||
export default async function accessRequestHandler(req: PayloadRequest, res: Response, next: NextFunction): Promise<Response<Permissions> | void> {
|
||||
try {
|
||||
const accessResults = await this.operations.collections.auth.access({
|
||||
req,
|
||||
|
||||
@@ -76,6 +76,7 @@ export type PayloadConfig = {
|
||||
graphQL?: string;
|
||||
graphQLPlayground?: string;
|
||||
};
|
||||
debug?: boolean
|
||||
express?: {
|
||||
json: {
|
||||
limit?: number
|
||||
|
||||
@@ -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<Response<ErrorResponse>>
|
||||
|
||||
// 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<void> => {
|
||||
const errorHandler = (config: Config, logger) => async (err: Error, req: PayloadRequest, res: Response, next: NextFunction): Promise<void> => {
|
||||
const data = formatErrorResponse(err);
|
||||
let response;
|
||||
let status = err.status || httpStatus.INTERNAL_SERVER_ERROR;
|
||||
|
||||
13
src/index.ts
13
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<any> {
|
||||
async create(options: CreateOptions): Promise<Document> {
|
||||
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<any> {
|
||||
async findGlobal(options: FindGlobalOptions): Promise<Document> {
|
||||
let { findOne } = localGlobalOperations;
|
||||
findOne = findOne.bind(this);
|
||||
return findOne(options);
|
||||
}
|
||||
|
||||
async updateGlobal(options: UpdateGlobalOptions): Promise<any> {
|
||||
async updateGlobal(options: UpdateGlobalOptions): Promise<Document> {
|
||||
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<any> {
|
||||
async findByID(options: FindByIDOptions): Promise<Document> {
|
||||
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<any> {
|
||||
async update(options: UpdateOptions): Promise<Document> {
|
||||
let { update } = localOperations;
|
||||
update = update.bind(this);
|
||||
return update(options);
|
||||
}
|
||||
|
||||
async delete(options: DeleteOptions): Promise<any> {
|
||||
async delete(options: DeleteOptions): Promise<Document> {
|
||||
let { localDelete: deleteOperation } = localOperations;
|
||||
deleteOperation = deleteOperation.bind(this);
|
||||
return deleteOperation(options);
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user