Skip to content

Commit

Permalink
more types and intellisense comments
Browse files Browse the repository at this point in the history
  • Loading branch information
denolfe committed Nov 22, 2020
1 parent 53811b2 commit 7670a23
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/config/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
2 changes: 1 addition & 1 deletion src/errors/APIError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
21 changes: 19 additions & 2 deletions src/fields/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export type FieldHook = (args: {
originalDoc?: any,
data?: any,
operation?: 'create' | 'update',
req?: PayloadRequest}) => Promise<any> | any;
req?: PayloadRequest
}) => Promise<any> | any;

type FieldBase = {
name: string;
Expand Down Expand Up @@ -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;
3 changes: 2 additions & 1 deletion src/globals/buildModel.ts
Original file line number Diff line number Diff line change
@@ -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<any> | null => {
if (config.globals && config.globals.length > 0) {
const globalsSchema = new mongoose.Schema({}, { discriminatorKey: 'globalType', timestamps: true });

Expand Down
4 changes: 1 addition & 3 deletions src/globals/init.ts
Original file line number Diff line number Diff line change
@@ -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),
Expand All @@ -23,5 +23,3 @@ function initGlobals() {
}
}
}

export default initGlobals;
32 changes: 30 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
FindByIDOptions,
UpdateOptions,
DeleteOptions,
FindResponse,
} from './types';
import Logger, { PayloadLogger } from './utilities/logger';
import bindOperations from './init/bindOperations';
Expand Down Expand Up @@ -46,6 +47,9 @@ import { PayloadRequest } from './express/types/payloadRequest';

require('isomorphic-fetch');

/**
* @description Payload
*/
export class Payload {
config: Config;

Expand Down Expand Up @@ -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...');

Expand Down Expand Up @@ -217,13 +225,23 @@ export class Payload {
return email.account;
}

/**
* @description Performs create operation
* @param options
* @returns created document
*/
async create(options: CreateOptions): Promise<any> {
let { create } = localOperations;
create = create.bind(this);
return create(options);
}

async find(options: FindOptions): Promise<any> {
/**
* @description Find documents with criteria
* @param options
* @returns documents satisfying query
*/
async find(options: FindOptions): Promise<FindResponse> {
let { find } = localOperations;
find = find.bind(this);
return find(options);
Expand All @@ -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<any> {
let { findByID } = localOperations;
findByID = findByID.bind(this);
return findByID(options);
}

/**
* @description Update document
* @param options
* @returns Updated document
*/
async update(options: UpdateOptions): Promise<any> {
let { update } = localOperations;
update = update.bind(this);
Expand Down
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export type CreateOptions = {
export type FindOptions = {
collection: string;
where?: { [key: string]: any };
depth?: number;
limit?: number;
};

export type FindResponse = {
Expand Down

0 comments on commit 7670a23

Please sign in to comment.