Skip to content

Remove necessity for next to always be defined when using .run#215

Open
sf185127 wants to merge 1 commit into
hoangvvo:mainfrom
sf185127:patch-1
Open

Remove necessity for next to always be defined when using .run#215
sf185127 wants to merge 1 commit into
hoangvvo:mainfrom
sf185127:patch-1

Conversation

@sf185127

Copy link
Copy Markdown

It is possible to hit a scenario where there is no "next" function but if I want to have universal middleware it gets an invalid function back from next-connect when i try to call it.

IE:

const withGlobal = () =>
  createRouter().use(async (req, res, next) => {
    const nextResults = next ? await next() : undefined;
    return {
      ...nextResults,
      props: { ...nextResults?.props, global: { yo: "yo" } },
    };
  });

and in the page i have

export const getServerSideProps = async (ctx: GetServerSidePropsContext) =>
  await createRouter()
    .use(withGlobal())
    .run(ctx.req, ctx.res);

Running this throws TypeError: Function.prototype.apply was called on undefined, which is a undefined and not a function

which i traced back to this file in Router.exec line 80 it expects there to be a function inside fns at ++i but since i'm using run (and not a get or post handler) i shouldnt need to add anymore fns on the end of this.

FYI

export const getServerSideProps = async (ctx: GetServerSidePropsContext) =>
  await createRouter()
    .use(withGlobal())
    .use(() => ({}))
    .run(ctx.req, ctx.res);

does work because creates the fn that next() is looking for, but its ugly and idontwanna

It is possible to hit a scenario where there is no "next" function but if I want to have universal middleware it gets an invalid function back from next-connect when i try to call it.

IE:
```
const withGlobal = () =>
  createRouter().use(async (req, res, next) => {
    const nextResults = next ? await next() : undefined;
    return {
      ...nextResults,
      props: { ...nextResults?.props, global: { yo: "yo" } },
    };
  });
```
and in the page i have
```
export const getServerSideProps = async (ctx: GetServerSidePropsContext) =>
  await createRouter()
    .use(withGlobal())
    .run(ctx.req, ctx.res);
```

Running this throws `TypeError: Function.prototype.apply was called on undefined, which is a undefined and not a function`

which i traced back to this file in Router.exec line 80
it expects there to be a function inside fns at ++i but since i'm using run (and not a get or post handler) i shouldnt need to add anymore fns on the end of this.

FYI 
```
export const getServerSideProps = async (ctx: GetServerSidePropsContext) =>
  await createRouter()
    .use(withGlobal())
    .use(() => ({}))
    .run(ctx.req, ctx.res);
```
*does* work because creates the fn that next() is looking for, but its ugly and idontwanna
@changeset-bot

changeset-bot Bot commented Nov 13, 2022

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: e8344ed

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@codecov

codecov Bot commented Nov 13, 2022

Copy link
Copy Markdown

Codecov Report

Merging #215 (e8344ed) into main (e5ac7fa) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##              main      #215   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            5         5           
  Lines          436       436           
  Branches        60        60           
=========================================
  Hits           436       436           
Impacted Files Coverage Δ
src/router.ts 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants