codinglab-io / discord-bot

Discord bot for Codinglab's server.
https://discord.com/invite/codinglab-174169014568878080
MIT License
19 stars 14 forks source link

feat: allow users to define environment shape with zod #57

Closed neolectron closed 1 year ago

neolectron commented 1 year ago

Problem

Modules aren't encapsulated, they should have a schema to define the shape of the environment they need.

Solution

Allow modules to export a zodSchema to validate env.

neolectron commented 1 year ago

I think one of the blocker is that we do not have a module-creator.

I tried several things, and in order to get all the keys inside the env object in a module, I need to declare my modules as satisfies BotModule which can be annoying for modules dev.

Then the solution was to have a function like getEnv() that would be typesafe and return the requested environment variables. I couldn't do it simply.

Therefore I would consider putting this on blocked until we have the module-creator. I picture something like :

export const myModule = createModule((ctx) => ({
  env: {
    MY_ENV_VAR: z.string().optionnal()
  },
  storage: {
    channelList: z.string().array()
  },
  slashCommands: {/*...*/},
  eventHandlers: {
    messageCreate: async () => {
        // Here I would be able to use ctx.env and ctx.storage
        console.log(ctx.storage.channelList) // typesafe + can be validated
    }
  },
}));
neolectron commented 1 year ago

deprecated by #81