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

Add typescript definitions for built-in middlewares #117

Merged
merged 4 commits into from
Mar 4, 2018

Conversation

Juanelorganelo
Copy link
Contributor

@Juanelorganelo Juanelorganelo commented Feb 17, 2018

This pull requests adds typings for the middlewares public API. The file is in the root due to how typescript module resolution works for typings. For more information see this thread

Closes #112

@codecov
Copy link

codecov bot commented Feb 17, 2018

Codecov Report

Merging #117 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #117   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          17     17           
  Lines         347    347           
  Branches       71     71           
=====================================
  Hits          347    347

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9880fcb...a40aec8. Read the comment docs.

Copy link
Member

@lmammino lmammino left a comment

Choose a reason for hiding this comment

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

This is brilliant, thanks @Juanelorganelo!

Just one question before moving this on:

Also, can we move the file in the typescript folder?

@Juanelorganelo
Copy link
Contributor Author

Juanelorganelo commented Feb 22, 2018

@lmammino I tried every possible way I could think of to keep the middlewares.d.ts file in the typescript folder. Apparently this haves to do with how typescript resolves imports to submodules so we can do import * as middlewares from 'middy/middlewares'. Thats why I left the file in the root and I didn't change the typings property of package.json. I could put a postinstall script to move the file to the root, but as mentioned in the thread its kind of hacky and it doesn't work with npm link. Any feedback will be appreciated

@lmammino
Copy link
Member

Sorry if this is taking too long, I have been very busy lately and I don't know enough about Typescript. It would be great to have some support by @joseSantacruz on this.

I'll try to play with this PR a bit and see if I can figure it out if we can avoid to keep the typescript file in the root folder

@lmammino
Copy link
Member

@Juanelorganelo, I did some changes and my editor autocomplete seems to like the definitions now, but I am not sure I am providing types in the right way. Can you double check?

Also, it would be great to have some feedback from 2 typescript masters I know like @remojansen and @fsciuti 😇

I look forward to hearing from all of you guys, thanks in advance!

Copy link
Contributor

@joseSantacruz joseSantacruz left a comment

Choose a reason for hiding this comment

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

Excellent improvement adding the middlewares typings

}

interface ICacheOptions {
calculateCacheId?: (event: any) => Promise<string>;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think is better if you use generics and the latest function type declaration so this interface should be:

interface ICacheOptions {
    calculateCacheId?<T>(event: T): Promise<string>;
    getValue?<T>(key: string): Promise<T>;
    setValue?(key: string): Promise<void>;
}

}

interface IWarmupOptions {
isWarmingUp?: (event: any) => boolean;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think is better if you use generics and the latest function type declaration so this interface should be:

interface IWarmupOptions {
    isWarmingUp?<T>(event: T): boolean;
    onWarmup?<T>(event: T): void;
}

@@ -38,4 +38,4 @@ declare namespace middy {
}
}

export = middy;
export default middy;
Copy link
Contributor

Choose a reason for hiding this comment

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

this is not going to works I tested and give me types error if you use import * as middy from 'middy';, and also remember that the default export is deprecated in favor of module imports. So should stay without default

Choose a reason for hiding this comment

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

I had problems with this before, not sure if it is the same issues but the following helped me microsoft/TypeScript#5073

@joseSantacruz
Copy link
Contributor

Hi @lmammino and @Juanelorganelo I put some comments on the PR, hope this help to improve this new feature

@remojansen
Copy link

It looks good but I would recommend adding a few test files under the typescript folder, like for example types.test.ts. This file should use the type definitions and then you can compile as them as part of the build. You can find a sample PR at https://github.com/godaddy/terminus/pull/4/files

@Juanelorganelo
Copy link
Contributor Author

Juanelorganelo commented Feb 27, 2018

@lmammino Sorry for the delay, I've been really busy at work, anyway I just tested this with the remapped exports and import * as middlewares from "middy/middlewares" fails to compile with the strict flag :'(. I think the solution for this issue is either have like a dist directory with index.js, index.d.ts, middlewares.js and middlewares.d.ts, move the typescript defs to the root dir or make a postinstall script that moves the definitions afterwards. @remojansen also testing the types seems like a great idea, I'll add the tests as soon as we figure out the directory structure. Thank you all for your awesomeness. 👍

@lmammino
Copy link
Member

Thanks @joseSantacruz and @remojansen! Your support is really really appreciated :)

@Juanelorganelo If it gets too tricky to have the typescript definitions inside the typescript folder I am ok for moving them back in the same directory of their related JS file.

@lmammino lmammino merged commit 4f0f299 into middyjs:master Mar 4, 2018
@lmammino
Copy link
Member

lmammino commented Mar 4, 2018

Thanks all for your valuable contribution on this!

@Juanelorganelo @joseSantacruz @remojansen 👍 🍻

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

Successfully merging this pull request may close these issues.

4 participants