i18next / i18next-http-middleware

i18next-http-middleware is a middleware to be used with Node.js web frameworks like express or Fastify and also for Deno.
MIT License
152 stars 28 forks source link

18next-http-middleware in fastify #39

Open Rafael-006 opened 3 years ago

Rafael-006 commented 3 years ago

🐛 Bug Report

I am using this middleware in fastify application with typescript.

When i do compilation using tsc for my application, the type definition of 18next-http-middleware in node_modules throws error as mentioned below,

node_modules/i18next-http-middleware/index.d.ts:9:8 - error TS2307: Cannot find module 'express-serve-static-core' or its corresponding type declarations.

9 } from "express-serve-static-core";

Expected behavior

I've registered this middleware in fastify and typescript configuration are done which includes node_modules to the outDir.

import i18next from 'i18next';
import Backend from 'i18next-fs-backend';
import middleware from 'i18next-http-middleware';
import fastify from 'fastify';

i18next
  .use(Backend)
  .use(middleware.LanguageDetector)
  .init({
    fallbackLng: 'en',
    backend: {
      loadPath: './locales/{{lng}}/translation.json'
    }
  });

fastify.register(middleware.plugin, {
    i18next
  });

fastify();

When i run tsc, expected the compilation to run successfully and output the js files in outDir.

Your Environment

adrai commented 3 years ago

Not maintaining TypeScript myself, fastify was probably not respected when #30 was introduced. Can you have a look at it @fominua?

fominua commented 3 years ago

Hey!

Haven't worked with fastify before, I need some time to get familiar with this and remember the changes in this merge.

g7fernandes commented 1 year ago

@fominua @Rafael-006, did you find a solution?

I'm having typing issues in this same snippet.

No overload matches this call.
  Overload 1 of 3, '(plugin: FastifyPluginCallback<HandleOptions & { i18next?: i18n | undefined; }, Server>, opts?: FastifyRegisterOptions<HandleOptions & { ...; }> | undefined): FastifyInstance<...> & PromiseLike<...>', gave the following error.
    Argument of type '(instance: any, options: HandleOptions & { i18next?: i18n | undefined; }, next: NextFunction) => Handler' is not assignable to parameter of type 'FastifyPluginCallback<HandleOptions & { i18next?: i18n | undefined; }, Server>'.
      Types of parameters 'next' and 'done' are incompatible.
        Type '(err?: Error | undefined) => void' is not assignable to type 'NextFunction'.
          Types of parameters 'err' and 'deferToNext' are incompatible.
            Type 'string' is not assignable to type 'Error'.
  Overload 2 of 3, '(plugin: FastifyPluginAsync<HandleOptions & { i18next?: i18n | undefined; }, Server>, opts?: FastifyRegisterOptions<HandleOptions & { ...; }> | undefined): FastifyInstance<...> & PromiseLike<...>', gave the following error.
    Argument of type '(instance: any, options: HandleOptions & { i18next?: i18n | undefined; }, next: NextFunction) => Handler' is not assignable to parameter of type 'FastifyPluginAsync<HandleOptions & { i18next?: i18n | undefined; }, Server>'.
  Overload 3 of 3, '(plugin: FastifyPluginCallback<HandleOptions & { i18next?: i18n | undefined; }, Server> | FastifyPluginAsync<HandleOptions & { ...; }, Server> | Promise<...> | Promise<...>, opts?: FastifyRegisterOptions<...> | undefined): FastifyInstance<...> & PromiseLike<...>', gave the following error.
    Argument of type '(instance: any, options: HandleOptions & { i18next?: i18n | undefined; }, next: NextFunction) => Handler' is not assignable to parameter of type 'FastifyPluginCallback<HandleOptions & { i18next?: i18n | undefined; }, Server> | FastifyPluginAsync<HandleOptions & { ...; }, Server> | Promise<...> | Promise<...>'.
      Type '(instance: any, options: HandleOptions & { i18next?: i18n | undefined; }, next: NextFunction) => Handler' is not assignable to type 'FastifyPluginCallback<HandleOptions & { i18next?: i18n | undefined; }, Server>'.
productdevbook commented 1 year ago

@g7fernandes same problem this fixed ?