The `electrode-hapi-compat` utility provides functions to simplify the creation and detection of Hapi.js plugins compatible across different major versions, specifically Hapi 16 and Hapi 17/18+. It directly addresses the breaking change in Hapi plugin signatures introduced with Hapi 17, offering a `universalHapiPlugin` function. This function allows developers to define distinct plugin implementations for Hapi 16 and Hapi 17+ environments, automatically serving the correct version based on the Hapi framework detected in the host application. The package also includes helper functions such as `isHapi17OrUp` and `isHapi18OrUp` for conditional logic and offers a mechanism to manually set the Hapi version for testing. The current stable version is 1.3.3. While not on a strict regular cadence, it receives updates for broader utility, such as supporting other frameworks like Fastify in earlier minor versions. Its primary differentiator is providing a concise, focused solution for Hapi plugin compatibility across significant framework version boundaries.
npm install electrode-hapi-compatVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to create a Hapi plugin compatible with both Hapi 16 and Hapi 17+ using `universalHapiPlugin`, and how to check the currently detected Hapi version.
Utilize `electrode-hapi-compat`'s `universalHapiPlugin` to provide separate plugin implementations for Hapi 16 and Hapi 17+, handling each version's API signature correctly.
Only use `hapiVersion` for explicit, controlled testing scenarios where global state manipulation is acceptable and managed. Avoid using it in production code or shared contexts. For isolated testing, consider mocking the utility's behavior rather than modifying its global state.
Prefer `const { namedExport } = require('electrode-hapi-compat');` for consistency and compatibility. If working strictly in an ESM environment, consider dynamic `import('electrode-hapi-compat')` and destructuring the resulting Promise, or ensure your build setup correctly handles CommonJS module interoperability.Ensure your `hapi16` and `hapi17OrUp` plugin implementations provided to `universalHapiPlugin` correctly match the API for their respective Hapi versions. Verify `electrode-hapi-compat` is detecting the Hapi version correctly, or explicitly set `require('electrode-hapi-compat').hapiVersion` for focused testing.Check that the `registers` object contains valid `hapi16` and `hapi17OrUp` functions, and that the `pkg` object has at least `name` and `version` properties as shown in the `universalHapiPlugin` examples in the documentation. Ensure `universalHapiPlugin` is called correctly and its result is used as the plugin.
For compatibility with this library's CommonJS-oriented examples, ensure your file is treated as CommonJS (e.g., `.js` file with `"type": "commonjs"` in `package.json`, or using a `.cjs` file extension). If you must use ESM, consider dynamic `import('electrode-hapi-compat')` to load the module asynchronously.