more typescript errors resolved

This commit is contained in:
Dan Ribbens
2020-11-22 00:01:47 -05:00
parent cbf12524fe
commit cb33417924
22 changed files with 56 additions and 48 deletions

View File

@@ -11,8 +11,7 @@ export function encrypt(text: string): string {
const ivString = iv.toString('hex');
const encryptedString = encrypted.toString('hex');
const result = `${ivString}${encryptedString}`;
return result;
return `${ivString}${encryptedString}`;
}
export function decrypt(hash: string): string {
@@ -23,6 +22,5 @@ export function decrypt(hash: string): string {
const decrypted = Buffer.concat([decipher.update(Buffer.from(content, 'hex')), decipher.final()]);
const result = decrypted.toString();
return result;
return decrypted.toString();
}

View File

@@ -1,7 +1,7 @@
import { Access } from '../config/types';
import { Forbidden } from '../errors';
import { Access } from '../config/types';
const executeAccess = async (operation, access) => {
const executeAccess = async (operation, access: Access): Promise<boolean> => {
if (access) {
const result = await access(operation);

View File

@@ -16,8 +16,7 @@ const getExtractJWT = (config: Config) => (req: Request): string | null => {
if (cookies && cookies[tokenCookieName]) {
if (!origin || (config.csrf && config.csrf.indexOf(origin) > -1)) {
const token = cookies[tokenCookieName];
return token;
return cookies[tokenCookieName];
}
}
}

View File

@@ -2,7 +2,7 @@ import passport from 'passport';
import AnonymousStrategy from 'passport-anonymous';
import jwtStrategy from './strategies/jwt';
function initAuth() {
function initAuth(): void {
passport.use(new AnonymousStrategy.Strategy());
passport.use('jwt', jwtStrategy(this));
}

View File

@@ -1,2 +1,2 @@
const isLocked = (date) => !!(date && date > Date.now());
const isLocked = (date: number): boolean => !!(date && date > Date.now());
export default isLocked;

View File

@@ -1,7 +1,8 @@
import { Request, Response, NextFunction } from 'express';
import { Response, NextFunction } from 'express';
import httpStatus from 'http-status';
import { PayloadRequest } from '../../express/types/payloadRequest';
export default async function policiesHandler(req: Request, res: Response, next: NextFunction): Promise<any> {
export default async function policiesHandler(req: PayloadRequest, res: Response, next: NextFunction): Promise<any> {
try {
const accessResults = await this.operations.collections.auth.access({
req,

View File

@@ -1,7 +1,8 @@
import { Request, Response, NextFunction } from 'express';
import { Response, NextFunction } from 'express';
import httpStatus from 'http-status';
import { PayloadRequest } from '../../express/types/payloadRequest';
export default async function forgotPasswordHandler(req: Request, res: Response, next: NextFunction): Promise<any> {
export default async function forgotPasswordHandler(req: PayloadRequest, res: Response, next: NextFunction): Promise<any> {
try {
await this.operations.collections.auth.forgotPassword({
req,

View File

@@ -1,6 +1,7 @@
import { Request, Response, NextFunction } from 'express';
import { Response, NextFunction } from 'express';
import { PayloadRequest } from '../../express/types/payloadRequest';
export default async function initHandler(req: Request, res: Response, next: NextFunction): Promise<any> {
export default async function initHandler(req: PayloadRequest, 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 });

View File

@@ -1,7 +1,8 @@
import { Request, Response, NextFunction } from 'express';
import { Response, NextFunction } from 'express';
import httpStatus from 'http-status';
import { PayloadRequest } from '../../express/types/payloadRequest';
export default async function loginHandler(req: Request, res: Response, next: NextFunction): Promise<any> {
export default async function loginHandler(req: PayloadRequest, res: Response, next: NextFunction): Promise<any> {
try {
const result = await this.operations.collections.auth.login({
req,

View File

@@ -1,6 +1,7 @@
import { Request, Response, NextFunction } from 'express';
import { Response, NextFunction } from 'express';
import { PayloadRequest } from '../../express/types/payloadRequest';
export default async function logoutHandler(req: Request, res: Response, next: NextFunction) {
export default async function logoutHandler(req: PayloadRequest, res: Response, next: NextFunction) {
try {
const message = await this.operations.collections.auth.logout({
collection: req.collection,

View File

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

View File

@@ -1,7 +1,8 @@
import { Request, Response, NextFunction } from 'express';
import { Response, NextFunction } from 'express';
import getExtractJWT from '../getExtractJWT';
import { PayloadRequest } from '../../express/types/payloadRequest';
export default async function refreshHandler(req: Request, res: Response, next: NextFunction): Promise<any> {
export default async function refreshHandler(req: PayloadRequest, res: Response, next: NextFunction): Promise<any> {
try {
const extractJWT = getExtractJWT(this.config);
const token = extractJWT(req);

View File

@@ -1,7 +1,7 @@
import { Response, NextFunction } from 'express';
import { PayloadRequest } from '../../express/types/payloadRequest';
import { Request, Response, NextFunction } from 'express';
export default async function registerFirstUser(req: Request, res: Response, next: NextFunction): Promise<any> {
export default async function registerFirstUser(req: PayloadRequest, res: Response, next: NextFunction): Promise<any> {
try {
const firstUser = await this.operations.collections.auth.registerFirstUser({
req,

View File

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

View File

@@ -1,7 +1,8 @@
import { Request, Response, NextFunction } from 'express';
import { Response, NextFunction } from 'express';
import httpStatus from 'http-status';
import { PayloadRequest } from '../../express/types/payloadRequest';
export default async function unlockHandler(req: Request, res: Response, next: NextFunction): Promise<any> {
export default async function unlockHandler(req: PayloadRequest, res: Response, next: NextFunction): Promise<any> {
try {
await this.operations.collections.auth.unlock({
req,

View File

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

View File

@@ -1,7 +1,7 @@
import PassportAPIKey from 'passport-headerapikey';
import crypto from 'crypto';
export default ({ operations, secret }, { Model, config }) => {
export default ({ operations, secret }, { Model, config }): PassportAPIKey => {
const opts = {
header: 'Authorization',
prefix: `${config.labels.singular} API-Key `,
@@ -13,7 +13,7 @@ export default ({ operations, secret }, { Model, config }) => {
.digest('hex');
try {
const where = {};
const where: { [key: string]: any } = {};
if (config.auth.verify) {
where.and = [
{

View File

@@ -1,24 +1,21 @@
import passportJwt from 'passport-jwt';
import passportJwt, { StrategyOptions } from 'passport-jwt';
import { Strategy as PassportStrategy } from 'passport-strategy';
import getExtractJWT from '../getExtractJWT';
const JwtStrategy = passportJwt.Strategy;
export default ({ secret, config, collections, operations }) => {
const opts = {
session: false,
export default ({ secret, config, collections, operations }): PassportStrategy => {
const opts: StrategyOptions = {
passReqToCallback: true,
jwtFromRequest: getExtractJWT(config),
secretOrKey: secret,
};
const extractJWT = getExtractJWT(config);
opts.jwtFromRequest = extractJWT;
opts.secretOrKey = secret;
return new JwtStrategy(opts, async (req, token, done) => {
try {
const collection = collections[token.collection];
const where = {};
const where: { [key: string]: any } = {};
if (collection.config.auth.verify) {
where.and = [
{

View File

@@ -3,6 +3,7 @@ import { Transporter } from 'nodemailer';
import SMTPConnection from 'nodemailer/lib/smtp-connection';
import { Collection } from '../collections/config/types';
import { Global } from '../globals/config/types';
import { PayloadRequest } from '../express/types/payloadRequest';
type MockEmailTransport = {
transport?: 'mock';
@@ -80,7 +81,7 @@ export type Config = {
window?: number;
max?: number;
trustProxy?: boolean;
skip?: (req: Request) => boolean; // TODO: Type join Request w/ PayloadRequest
skip?: (req: PayloadRequest) => boolean;
};
upload?: {
limits?: {

View File

@@ -1,9 +1,11 @@
import { Request } from 'express';
import { Payload } from '../../index';
import { Collection } from '../../collections/config/types';
export type PayloadRequest = Request & {
payload: Payload;
locale?: string;
collection?: Collection;
file?: {
name: string,
}

View File

@@ -1,6 +1,6 @@
export type Document = {
id: string;
[key: string]: any;
};
export type CreateOptions = {