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

koa 2.14.7 No generics,I want to be able to add generics #1764

Closed
jonlyes opened this issue Jul 1, 2023 · 1 comment
Closed

koa 2.14.7 No generics,I want to be able to add generics #1764

jonlyes opened this issue Jul 1, 2023 · 1 comment

Comments

@jonlyes
Copy link

jonlyes commented Jul 1, 2023

In koa 2.14.2, because there are no generics, the Context type needs to be overridden and inherited when using Koa-multer. Hopefully, the koa team will add generics in the next update

Code instance
router:

import Router from "koa-router";
import article from "../../controller/article";
import authMiddleware from "../../middleware/authMiddleware";
import fileMiddleware from "../../middleware/fileMiddleware";

const articleRouter = new Router({
  prefix: "/article",
});

// 添加博客文章
articleRouter.post(
  "/",
  authMiddleware.verifyAuth,
  fileMiddleware.coverHandle,
  article.createArticle
);

export default articleRouter;

filemiddleware

import { Context, Next } from "koa";
import multer from "koa-multer";
import config from "../app/config";

// // 上传文件中间件

class FileMiddleware {
  async coverHandle(ctx:Context) {
    const coverUpload = multer({
      dest: config.COVER_PATH,
    });

    return coverUpload.single("cover");
  }
}

export default new FileMiddleware()

controller

import { Context } from "koa";
import { ArticleList, ArticleId, Article } from "../types/article";
import articleService from "../service/articleService";

import { IncomingMessage } from 'http'

interface ContextWithFile extends Context {
  req: IncomingMessage & { file?: unknown }
}

class articleController {
  // 添加博客文章
  async createArticle(ctx: ContextWithFile) {
    const { title, content, isShare } = ctx.request.body as Article;

    console.log(ctx.req.file);
  }
}

export default new articleController();
@iwanofski
Copy link

Koa core does not provide typescript support and such this ticket will not be fixed here. Please refer to https://www.npmjs.com/package/@types/koa

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

No branches or pull requests

2 participants