feat: decorate fastify instance with addittional properties#5093
feat: decorate fastify instance with addittional properties#5093DemonHa wants to merge 8 commits intofastify:mainfrom
Conversation
|
It feels kind of wrong to change the FastifyInstance to a type. Doesnt this result in potential issues because you maybe cant extend from a type? Also i think AppInstance or app is a term of expressjs. A AppInstance or App in fastify is the FastifyInstance. |
|
@Uzlopak Actually you can extend a type the same way you do with an interface |
|
@Uzlopak If you are talking about declaration merging then you are correct, but this is the thing we are trying to avoid |
|
@DemonHa what can we do to help you ship this? |
Getting feedback as early as possible would be a great help. |
|
Was thinking how would it work with a plugin like autoload or even when defining routes on a separate function. We would probably need a generic to tell us which plugins have been registered. Wdyt @mcollina ? |
rozzilla
left a comment
There was a problem hiding this comment.
Good approach overall! We should just avoid breaking changes 😉
| // Used to type plugins written in javascript | ||
| export type GetPluginTypes<Decorators extends FastifyPluginDecorators = { decorators: {}, dependencies: [] }> = FastifyPluginCallback<{}, RawServerBase, FastifyTypeProviderDefault, FastifyBaseLogger, Decorators["decorators"] & ExtractSixGeneric<ExtractFirstFunctionParameter<Decorators["dependencies"][number] extends undefined ? {} : Decorators["dependencies"][number]>>>; | ||
|
|
||
| // Testing types |
There was a problem hiding this comment.
Typescript tests should live in test/types folder 😉
There was a problem hiding this comment.
Actually GetPluginTypes should not be living on the test folder at all, I just put it there for demo purposes. I think I should move it on the type definitions, since it will be exported and used by the libraries written in javascript (no library should be written in javascript IMO) or they can still use the declaration merging we currently have
examples/typescript-plugin-safety.ts
Outdated
| RawServer extends RawServerBase = RawServerDefault, | ||
| TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, | ||
| Logger extends FastifyBaseLogger = FastifyBaseLogger, | ||
| Fn extends FastifyPluginCallback<Options, RawServer, TypeProvider, Logger, Decorators["decorators"]> | FastifyPluginAsync<Options, RawServer, TypeProvider, Logger, Decorators["decorators"]> = FastifyPluginCallback<Options, RawServer, TypeProvider, Logger, Decorators["decorators"]> |
There was a problem hiding this comment.
nit (formatting)
| Fn extends FastifyPluginCallback<Options, RawServer, TypeProvider, Logger, Decorators["decorators"]> | FastifyPluginAsync<Options, RawServer, TypeProvider, Logger, Decorators["decorators"]> = FastifyPluginCallback<Options, RawServer, TypeProvider, Logger, Decorators["decorators"]> | |
| Fn extends FastifyPluginCallback<Options, RawServer, TypeProvider, Logger, Decorators["decorators"]> | FastifyPluginAsync<Options, RawServer, TypeProvider, Logger, Decorators["decorators"]> = FastifyPluginCallback<Options, RawServer, TypeProvider, Logger, Decorators["decorators"]> |
examples/typescript-plugin-safety.ts
Outdated
| ): Fn; | ||
|
|
||
| // Used to type plugins written in javascript | ||
| export type GetPluginTypes<Decorators extends FastifyPluginDecorators = { decorators: {}, dependencies: [] }> = FastifyPluginCallback<{}, RawServerBase, FastifyTypeProviderDefault, FastifyBaseLogger, Decorators["decorators"] & ExtractSixGeneric<ExtractFirstFunctionParameter<Decorators["dependencies"][number] extends undefined ? {} : Decorators["dependencies"][number]>>>; |
There was a problem hiding this comment.
nit (formatting)
| export type GetPluginTypes<Decorators extends FastifyPluginDecorators = { decorators: {}, dependencies: [] }> = FastifyPluginCallback<{}, RawServerBase, FastifyTypeProviderDefault, FastifyBaseLogger, Decorators["decorators"] & ExtractSixGeneric<ExtractFirstFunctionParameter<Decorators["dependencies"][number] extends undefined ? {} : Decorators["dependencies"][number]>>>; | |
| export type GetPluginTypes<Decorators extends FastifyPluginDecorators = { decorators: {}, dependencies: [] }> = FastifyPluginCallback<{}, RawServerBase, FastifyTypeProviderDefault, FastifyBaseLogger, Decorators["decorators"] & ExtractSixGeneric<ExtractFirstFunctionParameter<Decorators["dependencies"][number] extends undefined ? {} : Decorators["dependencies"][number]>>>; |
|
Hi guys! I have been away for quite some times since a lot of things have been going on lately. |
I love that approach! We keep compatibility with old code while allowing new types to be introduced. Since declaration merging will still be allowed, we will probably have to mark it as deprecated. Anyway, one thing at the time. I think we should proceed that way you're suggesting 😀 |
rozzilla
left a comment
There was a problem hiding this comment.
We need to fix conflicts, but now this change LGTM!
Yes, there might be quite some conflicts since this is a really old PR. I'll try to fix them today. |
gurgunday
left a comment
There was a problem hiding this comment.
Yeah I thought about this again and it is looking pretty good to me now!
|
@DemonHa what is missing? |
I am creating the PR on the other projects as well and I am also going to create a little test application |
| import fastify, { FastifyBaseLogger, FastifyPluginAsync, FastifyPluginCallback, FastifyPluginOptions, FastifyTypeProvider, FastifyTypeProviderDefault, RawServerBase, RawServerDefault } from '../fastify' | ||
| import { FastifyDecorators, FastifyInstance } from '../types/instance' | ||
|
|
||
| type ExtractSixGeneric<T> = T extends FastifyInstance<any, any, any, any, any, infer U> ? U : void |
There was a problem hiding this comment.
Can we use other types and avoid any's?
|
Closing as too old |
Proposal regarding to #5061
We are decorating the FastifyInstance to take into account decorators so it can work together with fastifyPlugin in order to have to end to end type safe apis.
This PR is a draft so any suggestion will be highly appreciated.
Checklist
npm run testandnpm run benchmarkand the Code of conduct