Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vuthehuyht opened this issue Nov 20, 2020 · 0 comments

Comments

@vuthehuyht
Copy link

vuthehuyht commented Nov 20, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant