Merge branch 'ts-final' of github.com:keen-studio/payload into ts-final

This commit is contained in:
James
2020-11-21 21:00:54 -05:00
26 changed files with 83 additions and 68 deletions

View File

@@ -2,7 +2,7 @@ import crypto from 'crypto';
const algorithm = 'aes-256-ctr';
export function encrypt(text) {
export function encrypt(text: string): string {
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv(algorithm, this.secret, iv);
@@ -15,7 +15,7 @@ export function encrypt(text) {
return result;
}
export function decrypt(hash) {
export function decrypt(hash: string): string {
const iv = hash.slice(0, 32);
const content = hash.slice(32);

View File

@@ -1,3 +1,4 @@
import { Access } from '../config/types';
import { Forbidden } from '../errors';
const executeAccess = async (operation, access) => {

View File

@@ -1,7 +1,9 @@
import { Response, NextFunction } from 'express';
import executeAccess from './executeAccess';
import { Forbidden } from '../errors';
import { PayloadRequest } from '../express/types/payloadRequest';
const getExecuteStaticAccess = ({ config, Model }) => async (req, res, next) => {
const getExecuteStaticAccess = ({ config, Model }) => async (req: PayloadRequest, res: Response, next: NextFunction) => {
try {
if (req.path) {
const accessResult = await executeAccess({ req, isReadingStaticFile: true }, config.access.read);

View File

@@ -1,6 +1,8 @@
import { Request } from 'express';
import { Config } from '../config/types';
import parseCookies from '../utilities/parseCookies';
const getExtractJWT = (config) => (req) => {
const getExtractJWT = (config: Config) => (req: Request): string | null => {
if (req && req.get) {
const jwtFromHeader = req.get('Authorization');
const origin = req.get('Origin');

View File

@@ -1,6 +1,7 @@
const httpStatus = require('http-status');
import { Request, Response, NextFunction } from 'express';
import httpStatus from 'http-status';
async function policiesHandler(req, res, next) {
export default async function policiesHandler(req: Request, res: Response, next: NextFunction): Promise<any> {
try {
const accessResults = await this.operations.collections.auth.access({
req,
@@ -12,5 +13,3 @@ async function policiesHandler(req, res, next) {
return next(error);
}
}
export default policiesHandler;

View File

@@ -1,6 +1,7 @@
const httpStatus = require('http-status');
import { Request, Response, NextFunction } from 'express';
import httpStatus from 'http-status';
async function forgotPasswordHandler(req, res, next) {
export default async function forgotPasswordHandler(req: Request, res: Response, next: NextFunction): Promise<any> {
try {
await this.operations.collections.auth.forgotPassword({
req,
@@ -18,5 +19,3 @@ async function forgotPasswordHandler(req, res, next) {
return next(error);
}
}
export default forgotPasswordHandler;

View File

@@ -1,4 +1,6 @@
async function initHandler(req, res, next) {
import { Request, Response, NextFunction } from 'express';
export default async function initHandler(req: Request, res: Response, next: NextFunction): Promise<any> {
try {
const initialized = await this.operations.collections.auth.init({ Model: req.collection.Model });
return res.status(200).json({ initialized });
@@ -6,5 +8,3 @@ async function initHandler(req, res, next) {
return next(error);
}
}
export default initHandler;

View File

@@ -1,6 +1,7 @@
import { Request, Response, NextFunction } from 'express';
import httpStatus from 'http-status';
async function loginHandler(req, res, next) {
export default async function loginHandler(req: Request, res: Response, next: NextFunction): Promise<any> {
try {
const result = await this.operations.collections.auth.login({
req,
@@ -21,5 +22,3 @@ async function loginHandler(req, res, next) {
return next(error);
}
}
export default loginHandler;

View File

@@ -1,4 +1,6 @@
async function logoutHandler(req, res, next) {
import { Request, Response, NextFunction } from 'express';
export default async function logoutHandler(req: Request, res: Response, next: NextFunction) {
try {
const message = await this.operations.collections.auth.logout({
collection: req.collection,
@@ -11,5 +13,3 @@ async function logoutHandler(req, res, next) {
return next(error);
}
}
export default logoutHandler;

View File

@@ -1,4 +1,6 @@
async function me(req, res, next) {
import { NextFunction, Request, Response } from 'express';
export default async function me(req: Request, res: Response, next: NextFunction): Promise<any> {
try {
const response = await this.operations.collections.auth.me({ req });
return res.status(200).json(response);
@@ -6,5 +8,3 @@ async function me(req, res, next) {
return next(err);
}
}
export default me;

View File

@@ -1,6 +1,7 @@
import { Request, Response, NextFunction } from 'express';
import getExtractJWT from '../getExtractJWT';
async function refreshHandler(req, res, next) {
export default async function refreshHandler(req: Request, res: Response, next: NextFunction): Promise<any> {
try {
const extractJWT = getExtractJWT(this.config);
const token = extractJWT(req);
@@ -20,5 +21,3 @@ async function refreshHandler(req, res, next) {
return next(error);
}
}
export default refreshHandler;

View File

@@ -1,4 +1,7 @@
async function registerFirstUser(req, res, next) {
import { Request, Response, NextFunction } from 'express';
export default async function registerFirstUser(req: Request, res: Response, next: NextFunction): Promise<any> {
try {
const firstUser = await this.operations.collections.auth.registerFirstUser({
req,
@@ -12,5 +15,3 @@ async function registerFirstUser(req, res, next) {
return next(error);
}
}
export default registerFirstUser;

View File

@@ -1,6 +1,7 @@
import { Request, Response, NextFunction } from 'express';
import httpStatus from 'http-status';
async function resetPassword(req, res, next) {
async function resetPassword(req: Request, res: Response, next: NextFunction): Promise<any> {
try {
const result = await this.operations.collections.auth.resetPassword({
req,

View File

@@ -1,6 +1,7 @@
import { Request, Response, NextFunction } from 'express';
import httpStatus from 'http-status';
async function unlockHandler(req, res, next) {
export default async function unlockHandler(req: Request, res: Response, next: NextFunction): Promise<any> {
try {
await this.operations.collections.auth.unlock({
req,
@@ -16,5 +17,3 @@ async function unlockHandler(req, res, next) {
return next(error);
}
}
export default unlockHandler;

View File

@@ -1,6 +1,7 @@
import { Request, Response, NextFunction } from 'express';
import httpStatus from 'http-status';
async function verifyEmail(req, res, next) {
async function verifyEmail(req: Request, res: Response, next: NextFunction): Promise<any> {
try {
await this.operations.collections.auth.verifyEmail({
collection: req.collection,

View File

@@ -1,7 +1,7 @@
import path from 'path';
import fs from 'fs';
const findConfig = () => {
const findConfig = (): string => {
// If the developer has specified a config path,
// format it if relative and use it directly if absolute
if (process.env.PAYLOAD_CONFIG_PATH) {

View File

@@ -1,5 +1,5 @@
import { Config } from './types';
import defaultUser from '../auth/default';
import defaultUser from '../auth/defaultUser';
import sanitizeCollection from '../collections/config/sanitize';
import { InvalidConfiguration } from '../errors';
import sanitizeGlobals from '../globals/config/sanitize';

View File

@@ -60,7 +60,7 @@ export type Config = {
cookiePrefix?: string;
csrf?: string[];
cors?: string[];
publicENV: { [key: string]: string };
publicENV?: { [key: string]: string };
routes?: {
api?: string;
admin?: string;
@@ -102,7 +102,7 @@ export type Config = {
maxComplexity?: number;
disablePlaygroundInProduction?: boolean;
};
components: { [key: string]: JSX.Element | (() => JSX.Element) };
components?: { [key: string]: JSX.Element | (() => JSX.Element) };
paths?: { [key: string]: string };
hooks?: {
afterError?: () => void;

View File

@@ -9,11 +9,10 @@ export type Message = {
html: string
}
export type MockEmailHandler = { account: TestAccount; transport: Transporter };
export type BuildEmailResult = Promise<{
transport: Mail,
transportOptions?: SMTPConnection.Options,
fromName: string,
fromAddress: string,
}>
export type MockEmailHandler = { account: TestAccount; transport: Transporter };
} | MockEmailHandler>

View File

@@ -0,0 +1,9 @@
import { Request } from 'express';
import { Payload } from '../../index';
export type PayloadRequest = Request & {
payload: Payload;
locale?: string;
// user: User
};

View File

@@ -1,9 +1,10 @@
import express, { Express, Router } from 'express';
import express, { Express, Request, Router } from 'express';
import crypto from 'crypto';
import { TestAccount } from 'nodemailer';
import {
Config,
EmailOptions,
InitOptions,
} from './config/types';
import {
@@ -18,7 +19,7 @@ import {
UpdateOptions,
DeleteOptions,
} from './types';
import Logger from './utilities/logger';
import Logger, { PayloadLogger } from './utilities/logger';
import bindOperations from './init/bindOperations';
import bindRequestHandlers from './init/bindRequestHandlers';
import bindResolvers from './init/bindResolvers';
@@ -41,21 +42,22 @@ import localOperations from './collections/operations/local';
import localGlobalOperations from './globals/operations/local';
import { encrypt, decrypt } from './auth/crypto';
import { MockEmailHandler, BuildEmailResult, Message } from './email/types';
import { PayloadRequest } from './express/types/payloadRequest';
require('isomorphic-fetch');
class Payload {
export class Payload {
config: Config;
collections: Collection[] = [];
logger: typeof Logger;
logger: PayloadLogger;
express: Express
router: Router;
emailOptions: any;
emailOptions: EmailOptions;
email: BuildEmailResult;
@@ -84,6 +86,7 @@ class Payload {
initAdmin: typeof initAdmin;
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) {
this.logger = Logger();
@@ -153,7 +156,7 @@ class Payload {
}
// Configure email service
this.email = buildEmail(this.emailOptions);
this.email = buildEmail(this.config.email);
// Initialize collections & globals
this.initCollections();
@@ -162,7 +165,7 @@ class Payload {
// Connect to database
connectMongoose(this.mongoURL);
options.express.use((req, res, next) => {
options.express.use((req: PayloadRequest, res, next) => {
req.payload = this;
next();
});
@@ -201,7 +204,7 @@ class Payload {
if (typeof options.onInit === 'function') options.onInit(this);
}
async sendEmail(message: Message) {
async sendEmail(message: Message): Promise<any> {
const email = await this.email;
const result = email.transport.sendMail(message);
return result;
@@ -212,73 +215,73 @@ class Payload {
return email.account;
}
async create(options: CreateOptions) {
async create(options: CreateOptions): Promise<any> {
let { create } = localOperations;
create = create.bind(this);
return create(options);
}
async find(options: FindOptions) {
async find(options: FindOptions): Promise<any> {
let { find } = localOperations;
find = find.bind(this);
return find(options);
}
async findGlobal(options: FindGlobalOptions) {
async findGlobal(options: FindGlobalOptions): Promise<any> {
let { findOne } = localGlobalOperations;
findOne = findOne.bind(this);
return findOne(options);
}
async updateGlobal(options: UpdateGlobalOptions) {
async updateGlobal(options: UpdateGlobalOptions): Promise<any> {
let { update } = localGlobalOperations;
update = update.bind(this);
return update(options);
}
async findByID(options: FindByIDOptions) {
async findByID(options: FindByIDOptions): Promise<any> {
let { findByID } = localOperations;
findByID = findByID.bind(this);
return findByID(options);
}
async update(options: UpdateOptions) {
async update(options: UpdateOptions): Promise<any> {
let { update } = localOperations;
update = update.bind(this);
return update(options);
}
async delete(options: DeleteOptions) {
async delete(options: DeleteOptions): Promise<any> {
let { delete: deleteOperation } = localOperations;
deleteOperation = deleteOperation.bind(this);
return deleteOperation(options);
}
async login(options) {
async login(options): Promise<any> {
let { login } = localOperations.auth;
login = login.bind(this);
return login(options);
}
async forgotPassword(options) {
async forgotPassword(options): Promise<any> {
let { forgotPassword } = localOperations.auth;
forgotPassword = forgotPassword.bind(this);
return forgotPassword(options);
}
async resetPassword(options) {
async resetPassword(options): Promise<any> {
let { resetPassword } = localOperations.auth;
resetPassword = resetPassword.bind(this);
return resetPassword(options);
}
async unlock(options) {
async unlock(options): Promise<any> {
let { unlock } = localOperations.auth;
unlock = unlock.bind(this);
return unlock(options);
}
async verifyEmail(options) {
async verifyEmail(options): Promise<any> {
let { verifyEmail } = localOperations.auth;
verifyEmail = verifyEmail.bind(this);
return verifyEmail(options);

View File

@@ -19,7 +19,7 @@ import deleteHandler from '../collections/requestHandlers/delete';
import findOne from '../globals/requestHandlers/findOne';
import globalUpdate from '../globals/requestHandlers/update';
function bindRequestHandlers(ctx) {
function bindRequestHandlers(ctx): void {
const payload = ctx;
payload.requestHandlers = {

View File

@@ -1,7 +1,8 @@
import falsey from 'falsey';
import pino from 'pino';
import memoize from 'micro-memoize';
import { PayloadLogger } from '../types';
export type PayloadLogger = pino.Logger;
export default memoize((name = 'payload') => pino({
name,

View File

@@ -1,4 +1,6 @@
function parseCookies(req) {
import { Request } from 'express';
export default function parseCookies(req: Request): { [key: string]: string } {
const list = {};
const rc = req.headers.cookie;
@@ -11,5 +13,3 @@ function parseCookies(req) {
return list;
}
export default parseCookies;