This is a collection of TypeScript utilities and examples for use cases, including:
context.ts
- context usingAsyncLocalStorage
.decorators.ts
- types and functions for creating decorators.
In order to use context.ts
, run npm install zod
.
In order to use decorators.ts
, enable decorators in your tsconfig.json
file:
{
"compilerOptions": {
"experimentalDecorators": true
}
}
getContext
- returns the current context.getZodContext
- returns the current context if it fits the received schema, otherwise throws an error.runWithContext
- runs a function with a new context.
Func<Return>
- a function that returnsReturn
.MethodDesc<Return>
- a descriptor for a method that returnsReturn
.MethodDecorator
- a decorator for a method.ClassDecorator
- a decorator for a class.MethodDescValueGetter
- a function that receives a method descriptor and the original method, and returns a new method.ExistingMethodDesc
- a method descriptor with an existing method.
getMethodsDescriptors
- receives a class prototype and returns an array of existing method descriptors.getClassName
- receives a class prototype and returns the class name.
The code in this repository serves as a proof of concept and might not work in all cases. It is recommended to test it thoroughly before using it in a production environment. I encourage you to experiment with it and adjust it to your needs. If you find any issues or have suggestions for improvements, please let me know.