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

Typescript Support: Origin is undefined #154

Closed
2 tasks done
bkawk opened this issue Oct 4, 2021 · 4 comments · Fixed by #155
Closed
2 tasks done

Typescript Support: Origin is undefined #154

bkawk opened this issue Oct 4, 2021 · 4 comments · Fixed by #155

Comments

@bkawk
Copy link

bkawk commented Oct 4, 2021

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

3.0.0

Plugin version

3.0.0

Node.js version

v14.15.4

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

11.5.2

Description

Following the async example in a typescript environment with fastify plugin results in origin is undefined

Steps to Reproduce

Trying to set up CORS for fastify vai the fastify-plugin in a fairly strict typescript environment. The docs showing the async configuration https://github.com/fastify/fastify-cors#configuring-cors-asynchronously

import fp from 'fastify-plugin';
import cors from 'fastify-cors';

export default fp(async (fastify) => {
  fastify.register(cors, () => (callback: any): void => {
    let corsOptions;
    if (/localhost/.test(origin)) {
      corsOptions = { origin: false };
    } else {
      corsOptions = { origin: 'www.mywebsite.com' };
    }
    callback(null, corsOptions);
  });
});

The error I get is

'origin' is not defined.eslintno-undef
Cannot find name 'origin'.ts(2304)

How do I define origin?

If I try the non-async example from the docs origin is still undefined

import fp from 'fastify-plugin';
import cors from 'fastify-cors';

export default fp(async (fastify) => {
  fastify.register(cors, {
    origin: (origin, callback) => {
      // eslint-disable-next-line no-console
      console.log(origin); //<======== undefined
      if (/localhost/.test(origin)) {
        callback(null, true);
      } else {
        callback(new Error('Not allowed by CORS'), false);
      }
    },
  });
});

Expected Behavior

I expect typescript to not complain about origin being undefined

Demo code can be found here https://github.com/bkawk/fastify-swagger/blob/cors/src/plugins/cors.ts

@mcollina
Copy link
Member

mcollina commented Oct 4, 2021

Would you like to send a Pull Request to address this issue? Remember to add unit tests.

@bkawk
Copy link
Author

bkawk commented Oct 4, 2021

Would you like to send a Pull Request to address this issue? Remember to add unit tests.

Unfortunately, this issue goes beyond my understanding, otherwise, I'd love to help out

@Eomm Eomm mentioned this issue Oct 4, 2021
@damey2011
Copy link

This still happens with Fastify 4 and Fastify CORS 8.1.0

@Eomm
Copy link
Member

Eomm commented Oct 1, 2022

Please open a new detailed issue adding a Minimal, Reproducible Example.

@joshmeads joshmeads mentioned this issue Dec 14, 2022
2 tasks
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 a pull request may close this issue.

4 participants