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 types #18526

Open
mbhaskar98 opened this issue Oct 20, 2023 · 4 comments
Open

Koa types #18526

mbhaskar98 opened this issue Oct 20, 2023 · 4 comments
Assignees
Labels
issue: bug Issue reporting a bug severity: medium If it breaks the basic use of the product but can be worked around source: typescript Source is related to TypeScript (typings, tooling, ...) status: confirmed Confirmed by a Strapi Team member or multiple community members

Comments

@mbhaskar98
Copy link

Bug report

Required System information

  • Node.js version: 18.16.1
  • NPM version: 9.8.0
  • Strapi version: 4.13.7
  • Database: mysql2
  • Operating system: darwin-x64
  • Is your project Javascript or Typescript: Typescript

Describe the bug

To get auto complete for context object (type Koa context) in controllers or middlewares I installed @types/Koa. The body object according to Koa Context type is available directly with the context object but the at runtime it is undefined. At runtime the body is found under ctx.request.body["data"] but accoeding to Koa Context there is no body object under request object.

Expected behavior

Is TypeScript with Koa available? Is the context object actualy Koa context?

Screenshots

Strapi-context-issue
@Madanpurohit

This comment has been minimized.

@nick-aj
Copy link

nick-aj commented Oct 26, 2023

Hello @Madanpurohit,

Thank for submitting this issue with us, we can confirm that we were able to reproduce it.
Here are my details:
For the following extended core controller:

module.exports = createCoreController('api::order.order', ({ strapi }) => ({

        async createCompleteOrder(ctx, next) {

            // Order Fields: products, owner, cofirmed, confirmation_data
            // TODO (Implement isOwner policy)
            const user = ctx.state.user;
            const order = await strapi.entityService.create("api::order.order", {
                data: {
                    products: ctx.request.body.products,
                    owner: user.id
                }
            });

The following error is thrown:

Property 'body' does not exist on type 'Request'.ts(2339)

@nick-aj nick-aj self-assigned this Oct 26, 2023
@nick-aj nick-aj added issue: bug Issue reporting a bug severity: medium If it breaks the basic use of the product but can be worked around status: confirmed Confirmed by a Strapi Team member or multiple community members source: typescript Source is related to TypeScript (typings, tooling, ...) labels Oct 26, 2023
@hekmatnajafi
Copy link

a quick fix for me was npm install --save-dev @types/koa-bodyparser

@Duplicated
Copy link

Duplicated commented May 2, 2024

Not to mention that the dev doc is misleading too: https://docs.strapi.io/dev-docs/backend-customization/requests-responses#ctxrequest

Or maybe this should go into a separate issue for documentation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue: bug Issue reporting a bug severity: medium If it breaks the basic use of the product but can be worked around source: typescript Source is related to TypeScript (typings, tooling, ...) status: confirmed Confirmed by a Strapi Team member or multiple community members
Projects
Status: To be reviewed (Open)
Status: Reproducible on v4
Development

No branches or pull requests

5 participants