-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
setup for typescript, eslint, prettier git hooks & nx configuration #1139
setup for typescript, eslint, prettier git hooks & nx configuration #1139
Conversation
Almost forgot about the kind of most important thing: typing support for services. There are a couple of approaches, to achieve this. However, when calling a service action, the IDE can't help with providing available actions and the required parameters. There is a module called moleculer-ts, however this and other ts-moleculer projects seem to require the use of class-style service-definitions. That would be quite a major change, I haven't worked with rewrites so I don't know how much work something like that would be. Anyways, I guess it's not necessary, as long as we write type definitions for the services, actions, settings since we can then import them. Not as neat but way better than tapping in the dark. A method annotation might look something like that: /** @type {import('moleculer').ServiceSchema<CoreServiceSettings>} */
const CoreService = {
name: 'core',
// Settings type is defined by CoreServiceSettings.
settings: {},
....
methods: {
/**
* @param {import('./service').MethodAuthenticateContext} ctx
* @param {ApiGatewayService.Route} route
* @param {IncomingMessage} req
* @param {ServerResponse} res
*/
authenticate(ctx, route, req, res) {
if (req.headers.signature) {
return ctx.call('signature.authenticate', { route, req, res });
}
if (req.headers.authorization) {
return ctx.call('auth.authenticate', { route, req, res });
}
ctx.meta.webId = 'anon';
return Promise.resolve(null);
}, |
Oh, two more things:
|
Thanks for the PR ! That's very precious.
That sounds like a good option ! I don't think TS will be used soon on the frontend components. But I guess adding the TS config is not much expensive ?
I wouldn't enforce anything with TS, while we do tests.
We can keep the airbnb rules and, if some of these rules are too hard, we can turn them off, like you already did for some of them. Linting is very useful, thanks for activating this feature !
I have tried to see if autocomplete worked with the CoreService of Archipelago (with a yarn link to your branch of SemApps) but there was no suggestion in vscode. Did I miss something ? If there something more to do to make autocomplete work in Archipelago ?
Indeed this |
* @param {ApiGatewayService.Route} route | ||
* @param {IncomingMessage} req | ||
* @param {ServerResponse} res | ||
*/ | ||
authorize(ctx, route, req, res) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
authorize
and authenticate
are two methods defined by the ApiGateway mixin. But I guess there is no way to guess them from this mixin ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now, I am not aware of a way to guess the types from that mixin, unfortunately. This seems to be a typescript limitation right now, at least if the service configuration objects are not put in a function call straight away (see https://stackoverflow.com/questions/69976058/is-there-a-way-to-infer-a-generic-type-without-using-a-function).
The type definitions here were rather for demonstration purposes but since this becomes rather bloaty here, I'd probably leave it out?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes better to leave it out until Moleculer improves its type integration.
Actually, I found a PR in moleculerjs/moleculer#829 |
054b6e2
to
9e542b1
Compare
Alright, I made some changes and got some remarks:
|
rename index.d.ts to have a different name see typescript-eslint/typescript-eslint#955
23197c9
to
cfdb515
Compare
For me there is now way too many linter issues for this to be useful. My IDE is cluttered with them. Typescript is something that should be implemented slowly over time, it is not a priority right now. I would prefer if the linter returned less issues, and we can fix them. |
Totally see that. I commented out the typescript-rules of the configuration. I left the ts parser on, to avoid complications with the Now it's ~700 issues remaining for each frontend and middleware. I'd agree with most of them, if that's still too much, I could take an afternoon or two and address them. The majority of them is rather easy to fix, I think. |
Sorry, the tsconfig checkJs setting was still on which made the IDE complain. |
Closing in favor of #1152 |
I created configurations for typescript, eslint and nx in both the middleware and frontend packages.
NX
I added nx (which integrates well with lerna, the company behind nx took it over a while ago) but for the current use cases, I don't think there are any benefits right now.
Typescript
Typescript is set up to analyze the json files only. This way, we don't need to convert all files to .ts files. Types, interfaces, etc. can be defined in
d.ts
files and referenced in jsdoc/** @type
annotations. This seems to be the least invasive way of progressing to me. An open question remains how/if to enforce stricter typing requirements for newly written code. This could be done, by for example adding stricter linting rules (e.g. warnings for usage untyped variables) or by allowing both typescript and javascript files in the project where javascript files are migrated step by step. For the latter case, a compilation to javascript needs to be set up (which is fairly easy though). The issue with adding stricter linting rules might be, that it can feel messy to have ~500 linting errors/warnings in the code base.Eslint
With eslint, I tried to things: using the standard
eslint:recommended
rules and an extended and well-developed rule-set from airbnb.eslint:recommended
+plugin:@typescript-eslint/recommended
rules. That works fine, eslint generates quite some warnings and errors but at least the errors were fixable quite affordably. A couple of things seemed quite strange so I kept them untouched for someone else to have a second look at (just runyarn lint
on the appropriate commit).let
declarations toconst
where applicable). In this PR I kept the airbnb configurations in separate commits and ran autofix (on the middleware project) as well. The issue here is that a lot of problems are not auto-fixable and it would probably take some time to rework all of them. So again, it's a question of taste and if it feels messy to have so many linting errors/warnings everywhere.Prettier
The prettier integration with eslint works pretty well. Airbnb eslint rules come with styling guidelines as well. When adding prettier to the eslint configuration, prettier handles all that instead of airbnb.
In my editor (vscode) I enabled autoformatting and autofixing eslint issues on save. I can really recommend!
Pre-commit hooks
In analogy to the pre-commit prettier hooks in activitypods, I added that here as well.
Interested to hear what you think :)