neutrinojs / neutrino

Create and build modern JavaScript projects with zero initial configuration.
https://neutrinojs.org
Mozilla Public License 2.0
3.95k stars 213 forks source link

Add helper functions for type-safety #1439

Closed yordis closed 5 years ago

yordis commented 5 years ago

These functions are just the identity function. Its only purpose is to enable a good typespec and inline documentation in editors.

Since we removed the magic around loading middlewares this will enable some documentation from type specs in the editors when they import the middlewares.

I had been using this and I saw would be a good idea to share with you, I totally understand that we don't need this since could be solved differently.

export type NeutrinoMiddleware = (neutrino: Neutrino) => Neutrino;

export type NeutrinoMiddlewareFactory<T> = (
  options: T
) => (neutrino: Neutrino) => Neutrino;

export interface Neutrino {
  // ...
  use(middleware: NeutrinoMiddleware): void;
}

Usage

// mymiddleware.ts

import { createMiddlewareFactory } from 'neutrino/middleware';

export const inlineChunkHtml = createMiddlewareFactory<InlineChunkHtmlOptions>(options => neutrino => {
   // ...
});

Followup

Inspiration

https://github.com/mui-org/material-ui/blob/ede30a5a0af518e780414bd853b93bb275863f48/packages/material-ui-styles/src/createStyles/createStyles.js#L1

https://material-ui.com/styles/api/#createstyles-styles-styles

yordis commented 5 years ago

@edmorley hey mate, would you mind sharing your thoughts?