hywax / nuxt-cron

A Nuxt module for cron jobs in your app.
https://nuxt-cron.hywax.space
MIT License
64 stars 3 forks source link

async callbacks #49

Closed nikolasdas closed 8 months ago

nikolasdas commented 8 months ago

Any plans for defineCronHandler to accept an async CronTick callback? Would be very helpful! :)

hywax commented 8 months ago

Now you can use asynchronous callbacks.

import { defineCronHandler } from '#nuxt/cron'

export default defineCronHandler('everyMinute', async () => {
  const data = await $fetch<{ repos: any[] }>('https://ungh.cc/orgs/unjs/repos')

  console.log(`UnJS has ${data.repos.length} repositories available`)
}, { runOnInit: true })

// stdOut: UnJS has 88 repositories available

If it doesn't make sense to you, write a case study.

nikolasdas commented 8 months ago

Sorry I should have clarified this a bit more, passing a async callback seems to work fine, yes. But the type definition requires a sync function, would it be possible to change this?

hywax commented 8 months ago

Can you give me an example, I guess I don't understand what you're talking about.

nikolasdas commented 8 months ago

https://github.com/hywax/nuxt-cron/blob/b69fcc23e05fcf5146666a6cc5c3f9cc5ffe056f/src/runtime/types.d.ts#L24 Because of this type definition I get a @typescript-eslint/no-misused-promises warning. I know this might be nitpicky, but I try to have my eslint rules strict and avoid eslint-disable comments 🙈 I can create a PR if you'd like, but wanted to make sure it is okay to use an async function in the first place

hywax commented 8 months ago

Looked at the types, I can replace CronTick

export type CronTick = () => void | Promise<void>

Am I correct in assuming that's what you're looking for?

nikolasdas commented 8 months ago

Yes, this would make my linter and subsequently me happier 👍

hywax commented 8 months ago

Fixed it in v1.5.0.

Thank you so much for your contribution!

nikolasdas commented 8 months ago

Thank you for your awesome support! And this cool nuxt module of course :)