add hook types

This commit is contained in:
Elliot DeNolf
2020-11-23 10:23:04 -05:00
parent 7670a23611
commit d10f3f140f
12 changed files with 142 additions and 54 deletions

View File

@@ -1,11 +1,12 @@
import crypto from 'crypto';
import { AfterForgotPasswordHook, BeforeOperationHook } from '../../collections/config/types';
import { APIError } from '../../errors';
async function forgotPassword(incomingArgs) {
const { config, sendEmail: email } = this;
if (!Object.prototype.hasOwnProperty.call(incomingArgs.data, 'email')) {
throw new APIError('Missing email.');
throw new APIError('Missing email.', 400);
}
let args = incomingArgs;
@@ -14,7 +15,7 @@ async function forgotPassword(incomingArgs) {
// beforeOperation - Collection
// /////////////////////////////////////
await args.collection.config.hooks.beforeOperation.reduce(async (priorHook, hook) => {
await args.collection.config.hooks.beforeOperation.reduce(async (priorHook: BeforeOperationHook, hook: BeforeOperationHook) => {
await priorHook;
args = (await hook({
@@ -38,7 +39,7 @@ async function forgotPassword(incomingArgs) {
// Forget password
// /////////////////////////////////////
let token = crypto.randomBytes(20);
let token: string | Buffer = crypto.randomBytes(20);
token = token.toString('hex');
const user = await Model.findOne({ email: data.email.toLowerCase() });
@@ -90,7 +91,7 @@ async function forgotPassword(incomingArgs) {
// afterForgotPassword - Collection
// /////////////////////////////////////
await collectionConfig.hooks.afterForgotPassword.reduce(async (priorHook, hook) => {
await collectionConfig.hooks.afterForgotPassword.reduce(async (priorHook: AfterForgotPasswordHook, hook: AfterForgotPasswordHook) => {
await priorHook;
await hook({ args });
}, Promise.resolve());

View File

@@ -3,6 +3,7 @@ import { AuthenticationError, LockedAuth } from '../../errors';
import getCookieExpiration from '../../utilities/getCookieExpiration';
import isLocked from '../isLocked';
import removeInternalFields from '../../utilities/removeInternalFields';
import { BeforeLoginHook, BeforeOperationHook } from '../../collections/config/types';
async function login(incomingArgs) {
const { config, operations, secret } = this;
@@ -13,7 +14,7 @@ async function login(incomingArgs) {
// beforeOperation - Collection
// /////////////////////////////////////
await args.collection.config.hooks.beforeOperation.reduce(async (priorHook, hook) => {
await args.collection.config.hooks.beforeOperation.reduce(async (priorHook: BeforeOperationHook, hook: BeforeOperationHook) => {
await priorHook;
args = (await hook({

View File

@@ -1,4 +1,5 @@
import jwt from 'jsonwebtoken';
import { BeforeOperationHook } from '../../collections/config/types';
import { Forbidden } from '../../errors';
import getCookieExpiration from '../../utilities/getCookieExpiration';
@@ -9,7 +10,7 @@ async function refresh(incomingArgs) {
// beforeOperation - Collection
// /////////////////////////////////////
await args.collection.config.hooks.beforeOperation.reduce(async (priorHook, hook) => {
await args.collection.config.hooks.beforeOperation.reduce(async (priorHook: BeforeOperationHook, hook: BeforeOperationHook) => {
await priorHook;
args = (await hook({