Repository: Kong/swrv (ts)

Issue:
throw better error when useSwrv is not top level in setup
I find that when new users to the setup/composition api use swrv, they get a little confused with this error:

```
TypeError: cannot read 'proxy' of null
```

https://github.com/Kong/swrv/blob/ac9981615abadac080d4df3ea9ba325ef5ac54e4/src/use-swrv.ts#L160

Example issue:
https://github.com/Kong/swrv/issues/40

Let's improve it


Pull request description:
chore(swrv) improved error message for top level
Fixes #133

Patch:
diff --git a/src/use-swrv.ts b/src/use-swrv.ts
index c467481..9c66493 100644
--- a/src/use-swrv.ts
+++ b/src/use-swrv.ts
@@ -157,7 +157,11 @@ function useSWRV<Data = any, Error = any> (...args): IResponse<Data, Error> {
   let isHydrated = false
 
   const instance = getCurrentInstance() as any
-  const vm = instance.proxy || instance // https://github.com/vuejs/composition-api/pull/520
+  const vm = instance?.proxy || instance // https://github.com/vuejs/composition-api/pull/520
+  if (!vm) {
+    throw new Error('Could not get current instance, check to make sure that `useSwrv` is declared in the top level of the setup function.')
+  }
+
   const IS_SERVER = vm.$isServer
   const isSsrHydration = Boolean(
     !IS_SERVER &&


Test patch:
diff --git a/tests/use-swrv.spec.tsx b/tests/use-swrv.spec.tsx
index 7502c3f..e06b7ca 100644
--- a/tests/use-swrv.spec.tsx
+++ b/tests/use-swrv.spec.tsx
@@ -1196,6 +1196,29 @@ describe('useSWRV - error', () => {
 
     done()
   })
+
+  it('should display friendly error message when swrv is not top level in setup', async done => {
+    const spy = jest.spyOn(console, 'error').mockImplementation(() => {})
+    const vm = new Vue({
+      template: '<button v-on:click="dontDoThis">bad idea</button>',
+      setup  () {
+        function dontDoThis () {
+          useSWRV(() => 'error-top-level', () => 'hello')
+        }
+
+        return {
+          dontDoThis
+        }
+      }
+    }).$mount()
+
+    vm.$el.click()
+
+    expect(spy).toHaveBeenCalledWith(expect.stringContaining('Could not get current instance, check to make sure that `useSwrv` is declared in the top level of the setup function.'))
+
+    spy.mockRestore()
+    done()
+  })
 })
 
 describe('useSWRV - window events', () => {