nuxt-community / date-fns-module

Modern JavaScript date utility library - date-fns for Nuxt.js
MIT License
78 stars 8 forks source link

Update usage in asyncData #39

Closed azrikahar closed 4 years ago

azrikahar commented 4 years ago

Since $dateFns is available in the context, we can use object destructuring to access it. The reasoning behind this is because this usage pattern is used by other Nuxt modules, so I believe having the same pattern will (hopefully) make things more intuitive :)

Example usage by other modules

Day.js module

Link to asyncData usage in Nuxt Day.js docs

asyncData({ $dayjs }) {
  return {
    now: $dayjs().format('YYYY/MM/DD')
  }
}

Content module

Link to asyncData usage in Nuxt Content docs

async asyncData({ $content, params }) {
  const article = await $content('articles', params.slug).fetch()

  return {
    article
  }
}

Axios module

Link to asyncData usage in Nuxt Axios docs

async asyncData({ $axios }) {
  const ip = await $axios.$get('http://icanhazip.com')
  return { ip }
}

Http module

Link to asyncData usage in Nuxt Http docs

async asyncData({ $http }) {
  const res = await $http.get('http://icanhazip.com')
  const ip = await res.text()
  return { ip }
}
codecov[bot] commented 4 years ago

Codecov Report

Merging #39 into master will not change coverage. The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff            @@
##            master       #39   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines           12        12           
  Branches         3         3           
=========================================
  Hits            12        12           

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 10ea44d...6626ba3. Read the comment docs.

ricardogobbosouza commented 4 years ago

Thanks @azrikahar