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

[Request]: Typescript typings #2129

Open
Maraket opened this issue Jun 19, 2024 · 2 comments
Open

[Request]: Typescript typings #2129

Maraket opened this issue Jun 19, 2024 · 2 comments
Assignees

Comments

@Maraket
Copy link

Maraket commented Jun 19, 2024

Summary

Provide examples of typescript typings in method prototypes, not just how to import elements rather than using require.

Why is it needed?

When trying to maintain code that is expected to have strict typing, this becomes difficult without an easy reference for what types to use for what calls.
Additionally, this can simplify coding as it allows IDEs to then do auto-completion and catch bugs earlier in the development process.

Suggested solution(s)

Update current examples showing how to write the code with the appropriate types defined. Some examples of what I would expect to see include:

import { RouteDefinition } from '@strapi/strapi`;

export default  {
  routes: [
    {
      method: 'GET',
      path: '/articles/customRoute',
      handler: 'api::api-name.controllerName.functionName', // or 'plugin::plugin-name.controllerName.functionName' for a plugin-specific controller
      config: {
        auth: false,
      },
    },
  ],
} satisfies RouteDefinition;
import { Strapi, factories, ControllerRequestContext } from "@strapi/strapi";

export default factories.createCoreController(
  "api::restaurant.restaurant",
  ({ strapi }: { Strapi }) => ({
    async find(ctx: ControllerRequestContext) {
      const sanitizedQueryParams = await this.sanitizeQuery(ctx);
      const { results, pagination } = await strapi
        .service("api::restaurant.restaurant")
        .find(sanitizedQueryParams);
      const sanitizedResults = await this.sanitizeOutput(results, ctx);

      return this.transformResponse(sanitizedResults, { pagination });
    },
  })
);

Related issue(s)/PR(s)

No response

@pwizla pwizla self-assigned this Jun 28, 2024
@pwizla
Copy link
Collaborator

pwizla commented Jun 28, 2024

Thanks for the suggestion. I'll check with Strapi core engineers and see what we can do.

@Convly
Copy link
Member

Convly commented Jul 1, 2024

Hey,

for instances where you are defining a default export (the type in my example I made up as I don't know the appropriate type)

import { RouteDefinition } from '@strapi/strapi`;

export default  {
  routes: [
    {
      method: 'GET',
      path: '/articles/customRoute',
      handler: 'api::api-name.controllerName.functionName', // or 'plugin::plugin-name.controllerName.functionName' for a plugin-specific controller
      config: {
        auth: false,
      },
    },
  ],
} satisfies RouteDefinition;

That could be a good idea. Another one we have is potentially export typed factories to eliminate the need for manual typings + be able to propose the same benefits for both JS and TS projects

where you are defining methods, then ensure to include the types (again unsure of what ctx should be, and all I can confirm is it is not RequestContext):

import { Strapi, factories, ControllerRequestContext } from "@strapi/strapi";

export default factories.createCoreController(
  "api::restaurant.restaurant",
  ({ strapi }: { Strapi }) => ({
    async find(ctx: ControllerRequestContext) {
      const sanitizedQueryParams = await this.sanitizeQuery(ctx);
      const { results, pagination } = await strapi
        .service("api::restaurant.restaurant")
        .find(sanitizedQueryParams);
      const sanitizedResults = await this.sanitizeOutput(results, ctx);

      return this.transformResponse(sanitizedResults, { pagination });
    },
  })
);

This one is surprising, createCoreController should already infer the correct types for the methods. You shouldn't need to manual add the context type 🤔

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

3 participants