iron-session icon indicating copy to clipboard operation
iron-session copied to clipboard

withIronSessionApiRoute - API not using convention for composition (BREAKING CHANGE)

Open Izhaki opened this issue 2 years ago • 0 comments

Currently

withIronSessionApiRoute API is:

export function withIronSessionApiRoute(
  handler: NextApiHandler,
  options: IronSessionOptions,
): NextApiHandler {}

And used like this:

export default withIronSessionApiRoute(eventsRoute, sessionOptions);

The issue

This means that in order to compose other decorators you'll write this:

export default withIronSessionApiRoute(withCSRF(csrfOptions)(eventsRoute), sessionOptions);

This is not how composition in functional programming works, which inspired both React HOCs and server decorators APIs (see https://reactjs.org/docs/higher-order-components.html#convention-maximizing-composability).

What it should be

const withIronSessionApiRoute =
  (options: IronSessionOptions) =>
  (handler: NextApiHandler): NextApiHandler => {
    // ...    
  };

Then when used:

export default withIronSessionApiRoute(sessionOptions)(eventsRoute);

And when composed:

export default compose(
  withIronSessionApiRoute(sessionOptions),
  withCSRF(csrfOptions),
)(eventsRoute);

Izhaki avatar Dec 06 '21 10:12 Izhaki