Skip to content

Improve PluginSpecificConfiguration intellisense adding Refs generic #4526

Open

Description

@lgraziani2712

Runtime

NodeJS

Runtime version

22.7.0

Module version

21.3.10

Used with

hapi application

Any other relevant information

No response

What problem are you trying to solve?

I'm trying to write a plugin that accepts a function that receives the request as a param and it could be nice to have the generic from the route shared to plugins, something like the following:

// plugin.d.ts
interface PluginSpecificConfiguration<Refs extends ReqRef = ReqRefDefaults> {}

// routes.d.ts
interface CommonRouteProperties<Refs extends ReqRef = ReqRefDefaults> {
    plugins?: PluginSpecificConfiguration<Refs> | undefined;
}

With that, we could configure plugins like this

declare module '@hapi/hapi {
  interface PluginSpecificConfiguration<Refs extends ReqRef = ReqRefDefaults> {
    pluginName: PluginInterface<Refs>;
  }
}

I'm interested in writting a PR if y'all accept the idea :)

Do you have a new or modified API suggestion to solve the problem?

No response

Activity

  1. lgraziani2712 commented on Aug 28, 2024

    @lgraziani2712
    Author

    This is the patch i made with pnpm

    diff --git a/lib/types/plugin.d.ts b/lib/types/plugin.d.ts
    index 2be5b9cf39add13eb99a1ad2490965b74b13e444..31a3675d95e6d366dbd464f516ec869632fdd110 100644
    --- a/lib/types/plugin.d.ts
    +++ b/lib/types/plugin.d.ts
    @@ -1,4 +1,4 @@
    -import { RequestRoute } from './request';
    +import { RequestRoute, ReqRef, ReqRefDefaults } from './request';
     import { RouteOptions } from './route';
     import { Server } from './server';
     import { Lifecycle } from './utils';
    @@ -47,7 +47,7 @@ export interface PluginRegistered {
     export interface PluginsStates {
     }
     
    -export interface PluginSpecificConfiguration {
    +export interface PluginSpecificConfiguration<Refs extends ReqRef = ReqRefDefaults> {
     }
     
     export interface PluginNameVersion {
    diff --git a/lib/types/route.d.ts b/lib/types/route.d.ts
    index be2416850c89f295f53cbf756afaf7dd3055a5dd..102226d055dfafa080b239c442dd9979a93970bd 100644
    --- a/lib/types/route.d.ts
    +++ b/lib/types/route.d.ts
    @@ -788,7 +788,7 @@ export interface CommonRouteProperties<Refs extends ReqRef = ReqRefDefaults> {
          * Plugin-specific configuration. plugins is an object where each key is a plugin name and the value is the plugin configuration.
          * [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-routeoptionsplugins)
          */
    -    plugins?: PluginSpecificConfiguration | undefined;
    +    plugins?: PluginSpecificConfiguration<Refs> | undefined;
     
         /**
          * @default none.
  2. damusix commented on Sep 30, 2024

    @damusix
    Contributor

    @lgraziani2712 can you try cloning hapi and testing this? We need to be sure this doesn't break existing typings. Send a PR. I'm for it, but the reqrefdefaults can cause some pain downstream (tricky to type); it already does with pres and ext helpers of sorts.

  3. added a commit that references this issue on Aug 6, 2025
    df1c649
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew functionality or improvement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions