zce / velite

Turns Markdown / MDX, YAML, JSON, or others into app's data layer with Zod schema.
http://velite.js.org
MIT License
339 stars 19 forks source link

`prepare`, `complete` events access to configuration `output` object #161

Open Lordfirespeed opened 1 month ago

Lordfirespeed commented 1 month ago

Title.

I'd like to add some additional code-generation to my Velite setup, but finding it rather tricky to get the output object without hacky workarounds.

zce commented 1 month ago

Sorry, I didn't understand your specific intent. Could you provide the code snippets to help me understand your requirements better?

zce commented 1 week ago

ping @Lordfirespeed

Lordfirespeed commented 1 week ago

Sorry, thankyou for the ping.

My particular use-case was that I wanted to write files to the .velite directory, but I didn't want to hard-code the .velite directory name. I wanted access to this object of the resolved configuration, from the prepare (invoked here) and complete (invoked here) hooks.

Changing the signatures of the prepare and complete hooks to include a config parameter, then passing in the resolved config would suffice:

prepare?: (data: Result<T>, config: Config) => Promisable<void | false>
complete?: (data: Result<T>, config: Config) => Promisable<void>
Here's a TypeScript snippet showing this change would be backwards-compatible. ```ts type Promisable = T | Promise type Result = { foo: T } type Config = { bar: string baz: number } type Test = (data: Result, config: Config) => Promisable const testInstance: Test = async (data: Result) => undefined; ```