nodejs-backend-architecture-typescript icon indicating copy to clipboard operation
nodejs-backend-architecture-typescript copied to clipboard

Argument of type '(req: ProtectedRequest, res: Response<any>, next: NextFunction) => Promise<void>' is not assignable to parameter of type 'AsyncFunction'

Open vuthehuyht opened this issue 4 years ago • 0 comments

I customized type of request in app-request file. But when I call type of requests, my typescript get error and I do not know reason. I try to fix but not. Here error:

Argument of type '(req: ProtectedRequest, res: Response<any>, next: NextFunction) => Promise<void>' is not assignable to parameter of type 'AsyncFunction'

signup.ts

import express from 'express';
import { SuccessResponse } from '../../../middleware/ApiResponse';
import UserRepo from '../../../database/repository/UserRepo';
import User from '../../../database/model/User';
import validator from '../../../helpers/validator';
import asyncHandler from '../../../helpers/asyncHandler';
import { BadRequestError } from '../../../middleware/ApiError';
import bcrypt from 'bcrypt';
import schema from './schema';
import _ from 'lodash';
import { PublishRequest } from 'app-request';

const router = express.Router();

router.post(
  '',
  validator(schema.signup),
  asyncHandler(async (req: PublishRequest, res) => {
    const user = await UserRepo.findByEmail(req.body.email);
    if (user) throw new BadRequestError('User already registered');

    const passwordHash = await bcrypt.hash(req.body.password, 10);

    const { user: createdUser } = await UserRepo.create(
      {
        fullname: req.body.fullname,
        username: req.body.username,
        phone: req.body.phone,
        email: req.body.email,
        profilePicUrl: req.body.profilePicUrl,
        password: passwordHash,
      } as User
    );

    new SuccessResponse('Signup Successful', {
      user: _.pick(createdUser, ['_id', 'fullname', 'name', 'phone', 'email', 'profilePicUrl'])
    }).send(res);
  }),
);

export default router;

app-request.d.ts

import { Request } from 'express';
import User from '../database/model/User';
import Keystore from '../database/model/Keystore';

declare interface PublicRequest extends Request {
  apiKey: string;
}

declare interface ProtectedRequest extends Request {
  user: User;
  accessToken: string;
  keystore: Keystore;
}

declare interface Tokens {
  accessToken: string;
  refreshToken: string;
}

Please help me fix it. Thanks

vuthehuyht avatar Nov 20 '20 09:11 vuthehuyht