Skip to content

Commit

Permalink
refactor: rename onFetch to useFetch
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `onFetch` can now be accessed using `useFetch`
  • Loading branch information
danielroe committed Apr 27, 2020
1 parent b1e2502 commit 3647769
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@ npm install nuxt-composition-api --save

You will now be able to access the following hooks:

### onFetch
### useFetch

Versions of Nuxt newer than v2.12 support a [custom hook called `fetch`](https://nuxtjs.org/api/pages-fetch/) that allows server-side and client-side asynchronous data-fetching.

You can access this with this package as follows:

```ts
import { defineComponent, ref } from '@vue/composition-api'
import { onFetch } from 'nuxt-composition-api'
import { useFetch } from 'nuxt-composition-api'
import axios from 'axios'

export default defineComponent({
setup() {
const name = ref('')

onFetch(async () => {
useFetch(async () => {
name.value = await axios.get('https://myapi.com/name')
})
},
Expand Down
2 changes: 1 addition & 1 deletion src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ async function serverPrefetch(vm: AugmentedComponentInstance) {
)
}

export const onFetch = (callback: Fetch) => {
export const useFetch = (callback: Fetch) => {
const vm = getCurrentInstance() as AugmentedComponentInstance | undefined
if (!vm) throw new Error('This must be called within a setup function.')

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { onFetch } from './fetch'
export { useFetch } from './fetch'
export { withContext } from './context'
4 changes: 2 additions & 2 deletions test/fixture/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<script>
import { defineComponent, ref } from '@vue/composition-api'
import { onFetch } from '../../..'
import { useFetch } from '../../..'
export function fetcher(result) {
return new Promise(resolve => {
Expand All @@ -20,7 +20,7 @@ export default defineComponent({
setup() {
const name = ref('')
onFetch(async () => {
useFetch(async () => {
name.value = await fetcher('Full Name')
})
Expand Down
2 changes: 1 addition & 1 deletion test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const config = require('./fixture/nuxt.config')

let nuxt

describe('onFetch', () => {
describe('useFetch', () => {
beforeAll(async () => {
nuxt = (await setup(config)).nuxt
}, 60000)
Expand Down

0 comments on commit 3647769

Please sign in to comment.