Búsqueda de sitios web

feathersjs - Feathers Typescript - Contexto de Hook Tipado


Mis objetivos:

  • Ser capaz de hacer app.get([las opciones tipeadas aparecen aquí])
  • Ser capaz de hacer app.service([las opciones tipadas aparecen aquí]) -> esto no sucede al usar HookContext

En mi intento de resolver esto, intenté lo siguiente:

en el archivo declarations.d.ts:

imports....
  import config from '@config/default.json';
 declarations...

// A mapping of service names to types. Will be extended in service files.
export interface EntityServices {
    users: Service<UserInterface>;
    subscribers: Service<SubscriberInterface>;
    permissions: Service<PermissionInterface>;
}

type Config = typeof config;

interface ModifiedConfig extends Config {
    sequelizeClient: Sequelize.Sequelize;
    'view engine': 'ejs';
}

export interface ServiceTypes extends EntityServices {}

type initialApplication = ExpressFeathers<ServiceTypes>;

type AppGet = <T extends keyof ModifiedConfig>(path: T) => ModifiedConfig[T];
type AppSet = <T extends keyof ModifiedConfig>(
    path: T,
    config: ModifiedConfig[T],
) => initialApplication;

// The application instance type that will be used everywhere else
export interface Application extends Omit<initialApplication, 'get' | 'set'> {
    get: AppGet;
    set: AppSet;
}

export type Paths = keyof ServiceTypes;
export type EntityPaths = keyof EntityServices;

export interface APIHookContext extends FeathersHookContext {
    app: Application;
    path: Paths;
}

export interface HookContext extends FeathersHookContext {}

Esto funciona perfectamente al principio, pero el problema surge en el uso:

En una función hook, si cambio:

Esto :

export const sendNewUserEmail = async (context: HookContext) => { [some code...] return context; };

A esto:

export const sendNewUserEmail = async (context: APIHookContext) => { [some code...] return context; };

Entiendo todo sobre escribir, PERO, se desata el caos con algunos de los siguientes:

La llamada service.hooks(hooks); ahora se queja de un desajuste entre la aplicación y el objeto de rutas de APIHookContext y HookContext.

Al inspeccionar más a fondo, veo que dentro de la tipificación de plumas, HookContext se refiere a "this", que creo que es la aplicación no inicializada. Por lo tanto, mi objeto está poblado mientras que "this" está vacío.

Intenté más allá extrayendo la función service.hooks() y actualizando su tipo, pero luego surge el problema con importaciones de feathers como funciones de "feathers-hooks-common". Estas pasan HookContext, que luego no coincide con APIHookContext.

Un ejemplo del error de tipo en la función service.hooks:

...type '(((context: HookContext<any, Service<any>>) => Promise<HookContext<any, Service<any>>>) | ((context: APIHookContext) => Promise<...>))[]' is not assignable to type 'Hook<any, Service<any>>[]'. Type '((context: HookContext<any, Service<any>>) => Promise<HookContext<any, Service<any>>>) | ((context: APIHookContext) => Promise<...>)' is not assignable to type 'Hook<any, Service<any>>'. Type '(context: APIHookContext) => Promise<APIHookContext>' is not assignable to type 'Hook<any, Service<any>>'. Types of parameters 'context' and 'hook' are incompatible. Type 'HookContext<any, Service<any>>' is not assignable to type 'APIHookContext'. Types of property 'app' are incompatible. Type 'Application<{}>' is missing the following properties from type 'Application': request, response, init, defaultConfiguration, and 40 more.ts(2345)

Me gustaría tener un objeto de aplicación consistente que se use dentro de métodos hook, métodos de clase, etc., que tenga todos los servicios y sus tipos y cosas como un objeto get tipado, etc.

He podido lograr esto en el archivo declaration.d.ts, pero los conflictos mencionados y la referencia a "this" me están bloqueando.

Hazme saber qué es posible y si hay una manera de alcanzar este nivel de mecanografía.

Saludos