From 7670a23611a2711e83849033fec2f77b047f90b5 Mon Sep 17 00:00:00 2001 From: Elliot DeNolf Date: Sun, 22 Nov 2020 18:21:45 -0500 Subject: [PATCH] more types and intellisense comments --- src/config/build.ts | 5 +++++ src/config/types.ts | 2 +- src/errors/APIError.ts | 2 +- src/fields/config/types.ts | 21 +++++++++++++++++++-- src/globals/buildModel.ts | 3 ++- src/globals/init.ts | 4 +--- src/index.ts | 32 ++++++++++++++++++++++++++++++-- src/types/index.ts | 2 ++ 8 files changed, 61 insertions(+), 10 deletions(-) diff --git a/src/config/build.ts b/src/config/build.ts index a96a296571f..e72a24a129d 100644 --- a/src/config/build.ts +++ b/src/config/build.ts @@ -2,6 +2,11 @@ import { Config } from './types'; import sanitize from './sanitize'; import validate from './validate'; +/** + * @description Builds and validates Payload configuration + * @param config Payload Config + * @returns Built and sanitized Payload Config + */ export function buildConfig(config: Config): Config { const validated = validate(config); const sanitized = sanitize(validated); diff --git a/src/config/types.ts b/src/config/types.ts index c96d2b3d0aa..5dada444455 100644 --- a/src/config/types.ts +++ b/src/config/types.ts @@ -25,7 +25,7 @@ export type InitOptions = { secret: string; license?: string; email?: EmailOptions; - local?: boolean; // I have no idea what this is + local?: boolean; // If true, disables all routes onInit?: () => void; }; diff --git a/src/errors/APIError.ts b/src/errors/APIError.ts index edf711cd110..b889d164bab 100644 --- a/src/errors/APIError.ts +++ b/src/errors/APIError.ts @@ -29,7 +29,7 @@ class APIError extends ExtendableError { * @param {object} data - response data to be returned. * @param {boolean} isPublic - Whether the message should be visible to user or not. */ - constructor(message: string, status: number = httpStatus.INTERNAL_SERVER_ERROR, data: any, isPublic = false) { + constructor(message: string, status: number = httpStatus.INTERNAL_SERVER_ERROR, data: any = null, isPublic = false) { super(message, status, data, isPublic); } } diff --git a/src/fields/config/types.ts b/src/fields/config/types.ts index 74e02ab4a6e..969480bf370 100644 --- a/src/fields/config/types.ts +++ b/src/fields/config/types.ts @@ -9,7 +9,8 @@ export type FieldHook = (args: { originalDoc?: any, data?: any, operation?: 'create' | 'update', - req?: PayloadRequest}) => Promise | any; + req?: PayloadRequest +}) => Promise | any; type FieldBase = { name: string; @@ -136,4 +137,20 @@ export type BlockField = FieldBase & { blocks?: Block[]; }; -export type Field = NumberField | TextField | EmailField | TextareaField | CodeField | CheckboxField | DateField | BlockField | RadioField | RelationshipField | ArrayField | RichTextField | GroupField | RowField | SelectField | SelectManyField | UploadField; +export type Field = NumberField + | TextField + | EmailField + | TextareaField + | CodeField + | CheckboxField + | DateField + | BlockField + | RadioField + | RelationshipField + | ArrayField + | RichTextField + | GroupField + | RowField + | SelectField + | SelectManyField + | UploadField; diff --git a/src/globals/buildModel.ts b/src/globals/buildModel.ts index 8731579c85b..a874d97e1f3 100644 --- a/src/globals/buildModel.ts +++ b/src/globals/buildModel.ts @@ -1,8 +1,9 @@ import mongoose from 'mongoose'; import buildSchema from '../mongoose/buildSchema'; import localizationPlugin from '../localization/plugin'; +import { Config } from '../config/types'; -const buildModel = (config) => { +const buildModel = (config: Config): mongoose.PaginateModel | null => { if (config.globals && config.globals.length > 0) { const globalsSchema = new mongoose.Schema({}, { discriminatorKey: 'globalType', timestamps: true }); diff --git a/src/globals/init.ts b/src/globals/init.ts index 266cd977eb4..537821fb706 100644 --- a/src/globals/init.ts +++ b/src/globals/init.ts @@ -1,7 +1,7 @@ import express from 'express'; import buildModel from './buildModel'; -function initGlobals() { +export default function initGlobals(): void { if (this.config.globals) { this.globals = { Model: buildModel(this.config), @@ -23,5 +23,3 @@ function initGlobals() { } } } - -export default initGlobals; diff --git a/src/index.ts b/src/index.ts index f1e024b64be..26e36a0f993 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,6 +18,7 @@ import { FindByIDOptions, UpdateOptions, DeleteOptions, + FindResponse, } from './types'; import Logger, { PayloadLogger } from './utilities/logger'; import bindOperations from './init/bindOperations'; @@ -46,6 +47,9 @@ import { PayloadRequest } from './express/types/payloadRequest'; require('isomorphic-fetch'); +/** + * @description Payload + */ export class Payload { config: Config; @@ -90,7 +94,11 @@ export class Payload { performFieldOperations: typeof performFieldOperations; // requestHandlers: { collections: { create: any; find: any; findByID: any; update: any; delete: any; auth: { access: any; forgotPassword: any; init: any; login: any; logout: any; me: any; refresh: any; registerFirstUser: any; resetPassword: any; verifyEmail: any; unlock: any; }; }; globals: { ...; }; }; - init(options: InitOptions) { + /** + * @description Initializes Payload + * @param options + */ + init(options: InitOptions): void { this.logger = Logger(); this.logger.info('Starting Payload...'); @@ -217,13 +225,23 @@ export class Payload { return email.account; } + /** + * @description Performs create operation + * @param options + * @returns created document + */ async create(options: CreateOptions): Promise { let { create } = localOperations; create = create.bind(this); return create(options); } - async find(options: FindOptions): Promise { + /** + * @description Find documents with criteria + * @param options + * @returns documents satisfying query + */ + async find(options: FindOptions): Promise { let { find } = localOperations; find = find.bind(this); return find(options); @@ -241,12 +259,22 @@ export class Payload { return update(options); } + /** + * @description Find document by ID + * @param options + * @returns document with specified ID + */ async findByID(options: FindByIDOptions): Promise { let { findByID } = localOperations; findByID = findByID.bind(this); return findByID(options); } + /** + * @description Update document + * @param options + * @returns Updated document + */ async update(options: UpdateOptions): Promise { let { update } = localOperations; update = update.bind(this); diff --git a/src/types/index.ts b/src/types/index.ts index ed08ec803fc..807f887f4e4 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -15,6 +15,8 @@ export type CreateOptions = { export type FindOptions = { collection: string; where?: { [key: string]: any }; + depth?: number; + limit?: number; }; export type FindResponse = {