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

make type definitons "module": "nodenext" compatible #184

Merged
merged 11 commits into from
Jul 22, 2022

Conversation

wight554
Copy link
Contributor

@wight554 wight554 commented May 25, 2022

Checklist

@Fdawgs Fdawgs requested a review from a team May 25, 2022 18:29
Copy link
Member

@climba03003 climba03003 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure which TypeScript update make it not work.
But it will break more people than it should.
So, I think this PR should not be merged.

@wight554
Copy link
Contributor Author

Not sure which TypeScript update make it not work. But it will break more people than it should. So, I think this PR should not be merged.

Can you give more context on "breaking"? It passes the test suite, so everything should be fine
Also codebase itself is untouched

@climba03003
Copy link
Member

Not sure which TypeScript update make it not work. But it will break more people than it should. So, I think this PR should not be merged.

Can you give more context on "breaking"? It passes the test suite, so everything should be fine Also codebase itself is untouched

I would like to see a repro for this error first.

// tsconfig.json
{
  "compilerOptions": {
    "module": "NodeNext"
  }
}

// index.ts
import defaultFastifyCookie, { fastifyCookie } from "@fastify/cookie";
import Fastify from "fastify";

const fastify = Fastify();
fastify.register(fastifyCookie);
fastify.register(defaultFastifyCookie);

// package.json
{
  "name": "test",
  "version": "1.0.0",
  "scripts": {},
  "dependencies": {
    "@fastify/cookie": "^6.0.0",
    "@types/node": "^17.0.35",
    "fastify": "^4.0.0-rc.2",
    "typescript": "^4.7.2"
  }
}

From what I have tested, I do not face any error currently.
image
image
It is working correctly.

@wight554
Copy link
Contributor Author

Not sure which TypeScript update make it not work. But it will break more people than it should. So, I think this PR should not be merged.

Can you give more context on "breaking"? It passes the test suite, so everything should be fine Also codebase itself is untouched

I would like to see a repro for this error first.

// tsconfig.json
{
  "compilerOptions": {
    "module": "NodeNext"
  }
}

// index.ts
import defaultFastifyCookie, { fastifyCookie } from "@fastify/cookie";
import Fastify from "fastify";

const fastify = Fastify();
fastify.register(fastifyCookie);
fastify.register(defaultFastifyCookie);

// package.json
{
  "name": "test",
  "version": "1.0.0",
  "scripts": {},
  "dependencies": {
    "@fastify/cookie": "^6.0.0",
    "@types/node": "^17.0.35",
    "fastify": "^4.0.0-rc.2",
    "typescript": "^4.7.2"
  }
}

From what I have tested, I do not face any error currently. image image It is working correctly.

sure
a bit more than this:

{
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
 }

@wight554
Copy link
Contributor Author

wight554 commented May 25, 2022

also "type": "module", in package.json
if you investigate further, you'll see that using default import breaks typescript (default import is not callable) and using named import breaks node runtime after successful compilation:

SyntaxError: Named export 'fastifyCookie' not found. The requested module 'fastify-cookie' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from 'fastify-cookie';
const {fastifyCookie} = pkg;

@climba03003
Copy link
Member

The problem actually comes from
"type": "module" with "module": "NodeNext"

@wight554
Copy link
Contributor Author

it seems to be expected behaviour from TS side, it is CJS module so it should use CJS export
you can find more investigation in this issue: bendrucker/snakecase-keys#72

plugin.d.ts Outdated Show resolved Hide resolved
@wight554
Copy link
Contributor Author

@climba03003 pushed changes to fix default export function type

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a unit test? we use tsd to test our types

@wight554
Copy link
Contributor Author

Can you please add a unit test? we use tsd to test our types

Idk what can also be tested, current test suite looks completed to me

Copy link
Member

@climba03003 climba03003 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will not block this PR to be merged and I would like to see the namespace written in PascalCase.

@fastify/typescript just ping more people in to decide what's the next steps.

@wight554
Copy link
Contributor Author

I will not block this PR to be merged and I would like to see the namespace written in PascalCase.

@fastify/typescript just ping more people in to decide what's the next steps.

PascalCase sounds good to me, will add corresponding changes

@wight554
Copy link
Contributor Author

wight554 commented Jul 16, 2022

Ah well actually @climba03003 PascalCase is no go here. fastifyCookie namespace is merged with fastifyCookie function, and PascalCase for function name doesn't sound great
See https://www.typescriptlang.org/docs/handbook/declaration-merging.html#merging-namespaces-with-classes

@climba03003
Copy link
Member

PascalCase is no go here

Sad for this info.

@climba03003 climba03003 dismissed their stale review July 16, 2022 16:28

Not blocking for land

Copy link
Member

@RafaelGSS RafaelGSS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you include a test for ESM? Reference: https://github.com/fastify/fastify/tree/main/test/esm


declare module 'fastify' {
declare module "fastify" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you restore it? It seems unrelated to the PR goal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tbh I think it's fine to keep those, that's was formatter does for me every time I touch that file, also makes it consistent with other plugins (at least fastify-static)

@wight554
Copy link
Contributor Author

Could you include a test for ESM? Reference: https://github.com/fastify/fastify/tree/main/test/esm

I don't really see any reason to test esm since module is CJS only, these changes make typescript read module as CJS

@RafaelGSS
Copy link
Member

Could you include a test for ESM? Reference: https://github.com/fastify/fastify/tree/main/test/esm

I don't really see any reason to test esm since module is CJS only, these changes make typescript read module as CJS

If the current test suite was any good, it should have detected this problem, shouldn't it? I'm just concerned about preventing a potential regression in any further PR.

@wight554
Copy link
Contributor Author

wight554 commented Jul 16, 2022

Could you include a test for ESM? Reference: https://github.com/fastify/fastify/tree/main/test/esm

I don't really see any reason to test esm since module is CJS only, these changes make typescript read module as CJS

If the current test suite was any good, it should have detected this problem, shouldn't it? I'm just concerned about preventing a potential regression in any further PR.

I think there is no regression as long as it passes current tsd suite, not sure what I can do to test esm in this plugin since there is no esm (.mjs) module exported

@wight554
Copy link
Contributor Author

Actually there's a regression already since 8da8fbe was added
types don't expect these lines to be inside of fastifyCookie named export

@wight554 wight554 requested a review from RafaelGSS July 17, 2022 14:22
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina
Copy link
Member

@wight554 could you rebase?
@RafaelGSS ptal

Copy link
Member

@RafaelGSS RafaelGSS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@wight554
Copy link
Contributor Author

@wight554 could you rebase? @RafaelGSS ptal

should be g2g

@mcollina mcollina merged commit 928f17e into fastify:master Jul 22, 2022
Uzlopak pushed a commit to Uzlopak/fastify-cookie that referenced this pull request Jul 25, 2022
* make type definitons `"module": "nodenext"` compatible

* see microsoft/TypeScript#48845

* fix test

* fix default export type

* make typing extra safe and small refactoring

* restore brackets

* add additional properties to named export and fix test suite

* remove formatting

* revert formatting

* reuse type
Uzlopak added a commit that referenced this pull request Aug 11, 2022
* integrate cookie signer

* add algorithm as plugin-option

* check for matching error message

* improve tests

* remove unnecessary array conversion

* simplify

* fix comment

* restructure SignerFactory

* update typings

* fix exports

* add benchmarks

* improve perf for multi secrets

* rename SignerFactory to Signer

* Merge branch 'master' into integrate-cookie-signer

* export signerFactory

* Update signer.js

* add secure: 'auto' Option (#199)

* add secure: auto Option

* document deviation from cookie serialize

* describe better

* lowercase Lax

* Bumped v7.3.0

Signed-off-by: Matteo Collina <hello@matteocollina.com>

* make type definitons `"module": "nodenext"` compatible (#184)

* make type definitons `"module": "nodenext"` compatible

* see microsoft/TypeScript#48845

* fix test

* fix default export type

* make typing extra safe and small refactoring

* restore brackets

* add additional properties to named export and fix test suite

* remove formatting

* revert formatting

* reuse type

* Bumped v7.3.1

Signed-off-by: Matteo Collina <hello@matteocollina.com>

* modify types

* remove dummy

* fix regex bottleneck found by sonarqube

Signed-off-by: Matteo Collina <hello@matteocollina.com>
Co-authored-by: Matteo Collina <hello@matteocollina.com>
Co-authored-by: Volodymyr Zhdanov <wight554@gmail.com>
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.

4 participants