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

Improve PluginSpecificConfiguration intellisense adding Refs generic #4526

Open
lgraziani2712 opened this issue Aug 28, 2024 · 2 comments
Open
Labels
feature New functionality or improvement

Comments

@lgraziani2712
Copy link

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

@lgraziani2712 lgraziani2712 added the feature New functionality or improvement label Aug 28, 2024
@lgraziani2712
Copy link
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.

@damusix
Copy link
Contributor

damusix commented Sep 30, 2024

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality or improvement
Projects
None yet
Development

No branches or pull requests

2 participants