Skip to content

Commit f498160

Browse files
committed
feat!: upgrade to vue 3
resolves #498
1 parent 36c9b5b commit f498160

File tree

10 files changed

+114
-253
lines changed

10 files changed

+114
-253
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<h1 align="center">🟢 vue-sanity</h1>
2-
<p align="center">Sanity integration for VueJS</p>
2+
<p align="center">Sanity integration for Vue</p>
33

44
<p align="center">
55
<a href="https://npmjs.com/package/vue-sanity">
@@ -34,8 +34,6 @@
3434

3535
## Quick Start
3636

37-
> If you are using Vue 2, then this project requires you be on at least Vue 2.7.
38-
3937
First install `vue-sanity`:
4038

4139
```bash

package.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"test:types": "tsc --noEmit"
4545
},
4646
"peerDependencies": {
47-
"vue": "^2.7.0 || >=3.0.0-rc.0"
47+
"vue": "^3.0.0"
4848
},
4949
"dependencies": {
5050
"@sanity/client": "^6.21.3",
@@ -59,13 +59,13 @@
5959
"@typescript-eslint/parser": "8.3.0",
6060
"@vitest/coverage-v8": "2.0.5",
6161
"@vue/composition-api": "1.7.2",
62-
"@vue/test-utils": "1.3.6",
62+
"@vue/test-utils": "2.4.6",
6363
"bumpp": "^9.5.2",
6464
"conventional-changelog-conventionalcommits": "8.0.0",
6565
"eslint": "9.9.1",
6666
"expect-type": "0.20.0",
6767
"flush-promises": "1.0.2",
68-
"happy-dom": "15.4.2",
68+
"happy-dom": "15.5.0",
6969
"husky": "9.1.5",
7070
"lint-staged": "15.2.9",
7171
"memory-fs": "0.5.0",
@@ -74,9 +74,7 @@
7474
"unbuild": "2.0.0",
7575
"vite": "5.4.2",
7676
"vitest": "2.0.5",
77-
"vue": "2.7.16",
78-
"vue-server-renderer": "2.7.16",
79-
"vue-template-compiler": "2.7.16"
77+
"vue": "3.4.38"
8078
},
8179
"resolutions": {
8280
"minimist": ">=1.2.8"

pnpm-lock.yaml

Lines changed: 84 additions & 207 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cache.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import process from 'node:process'
22
import type { Ref } from 'vue'
3-
import { computed, getCurrentInstance, isRef, onServerPrefetch, reactive, set, unref, watch } from 'vue'
3+
import { computed, getCurrentInstance, isRef, onServerPrefetch, reactive, unref, useSSRContext, watch } from 'vue'
44

55
/**
66
* Cached data, status of fetch, timestamp of last fetch, error
77
*/
8-
type CacheEntry<T> = [T, FetchStatus, number, any, Promise<T>]
8+
type CacheEntry<T> = [T, FetchStatus, number, any, Promise<T> | null]
99

1010
const cache = reactive<Record<string, CacheEntry<any>>>({})
1111

@@ -19,7 +19,7 @@ export function ensureInstance() {
1919
export function getServerInstance() {
2020
const instance = getCurrentInstance()
2121

22-
if (process.env.VITE_SSG || instance?.proxy.$isServer)
22+
if (process.env.VITE_SSG || (import.meta as any).env?.SSR || (import.meta as any).server)
2323
return instance?.proxy
2424
return false
2525
}
@@ -83,7 +83,7 @@ export function useCache<T, K = null>(
8383
status = 'initialised',
8484
time = new Date().getTime(),
8585
}: SetCacheOptions<T, K>) {
86-
set(cache, key, [value, status, time, error])
86+
cache[key] = [value, status, time, error, null]
8787
}
8888

8989
const serverInstance = getServerInstance()
@@ -123,11 +123,7 @@ export function useCache<T, K = null>(
123123
if (!(key in cache))
124124
initialiseCache({ key, value, status })
125125

126-
set(cache[key], 0, value)
127-
set(cache[key], 1, status)
128-
set(cache[key], 2, new Date().getTime())
129-
set(cache[key], 3, error)
130-
set(cache[key], 4, promise)
126+
cache[key] = [value, status, new Date().getTime(), error, promise]
131127
}
132128

133129
function fetch(query = unref(key), force?: boolean) {
@@ -164,7 +160,7 @@ export function useCache<T, K = null>(
164160
}
165161

166162
if (enableSSR && serverInstance) {
167-
const ctx = serverInstance.$ssrContext
163+
const ctx = useSSRContext()
168164
if (ctx) {
169165
if (ctx.nuxt && !ctx.nuxt.vsanity) {
170166
ctx.nuxt.vsanity = {}

test/__snapshots__/ssr.spec.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`ssr > cache fetches data correctly on SSR 1`] = `"<div data-server-rendered="true">data: value, status: server loaded</div>"`;
3+
exports[`ssr > cache fetches data correctly on SSR 1`] = `"<div>data: value, status: server loaded</div>"`;

test/helpers/mount.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
import Vue, { h } from 'vue'
2-
import type { Data, SetupFunction } from 'vue'
1+
import { h } from 'vue'
32
import { mount } from '@vue/test-utils'
43
import flushPromises from 'flush-promises'
54

6-
Vue.config.productionTip = false
7-
Vue.config.devtools = false
8-
9-
export async function runInSetup<T extends SetupFunction<Data, Data>, D extends SetupFunction<Data, Data>>(setup: T, child?: D) {
10-
let result
5+
export async function runInSetup<T extends () => unknown, D extends () => unknown>(setup: T, child?: D) {
6+
let result: T & D
117
mount({
128
setup() {
139
result = (setup as any)() || {}
@@ -23,5 +19,5 @@ export async function runInSetup<T extends SetupFunction<Data, Data>, D extends
2319

2420
await flushPromises()
2521

26-
return result as ReturnType<T> & ReturnType<D>
22+
return result! as ReturnType<T> & ReturnType<D>
2723
}

test/image.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const config = {
1212
apiVersion: '2021-03-25',
1313
} satisfies ClientConfig
1414

15-
;(globalThis.console.error as any) = vi.fn()
15+
;(globalThis.console.warn as any) = vi.fn()
1616

1717
describe('image builder', () => {
1818
const image = {
@@ -37,7 +37,7 @@ describe('image builder', () => {
3737
})
3838

3939
expect(error).toBeDefined()
40-
expect(console.error).toBeCalled()
40+
expect(console.warn).toBeCalled()
4141
})
4242

4343
it('errors when run outside of setup', async () => {

test/index.spec.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
import Vue from 'vue'
2-
31
import { beforeEach, describe, expect, it, vi } from 'vitest'
42
import { createClient } from '@sanity/client'
53

64
import { useSanityClient } from '../src'
75
import { runInSetup } from './helpers/mount'
86

9-
Vue.config.productionTip = false
10-
Vue.config.devtools = false
11-
127
vi.mock('@sanity/client')
138
;(globalThis.console.error as any) = vi.fn()
149

test/query.spec.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const config = {
1616
}
1717

1818
const mockFetch = vi.fn(async (key: string) => `return value-${key}`)
19-
;(globalThis.console.error as any) = vi.fn()
19+
;(globalThis.console.warn as any) = vi.fn()
2020

2121
const mockUnsubscribe = vi.fn()
2222
const mockSubscribe = vi.fn((callback: (result: any) => void) => {
@@ -39,7 +39,7 @@ vi.mock('@sanity/client', () => {
3939

4040
beforeEach(() => {
4141
createClient.mockClear()
42-
;(globalThis.console.error as any).mockClear()
42+
;(globalThis.console.warn as any).mockClear()
4343
mockListen.mockClear()
4444
mockSubscribe.mockClear()
4545
mockUnsubscribe.mockClear()
@@ -58,14 +58,18 @@ describe('fetcher', () => {
5858
expect(error).toBeDefined()
5959
})
6060
it('errors when client is not injected', async () => {
61+
let error
6162
await runInSetup(() => {
6263
try {
6364
useSanityFetcher(() => `my-error`)
6465
}
65-
catch {}
66+
catch (e) {
67+
error = e
68+
}
6669
})
6770

68-
expect(console.error).toBeCalled()
71+
expect(console.warn).toBeCalled()
72+
expect(error).toBeDefined()
6973
})
7074

7175
it('allows default options to be set', async () => {

test/ssr.spec.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
/** @vitest-environment node */
2-
import Vue, { h, ref } from 'vue'
3-
import { createRenderer } from 'vue-server-renderer'
2+
import { createApp, h, ref } from 'vue'
3+
import { renderToString } from 'vue/server-renderer'
44

55
import { describe, expect, it } from 'vitest'
66

77
import { useCache } from '../src/cache'
88
import { fetcher } from './helpers/utils'
99

10-
Vue.config.productionTip = false
11-
Vue.config.devtools = false
12-
1310
describe('ssr', () => {
1411
it('cache fetches data correctly on SSR', async () => {
15-
const app = new Vue({
12+
const app = createApp({
1613
setup() {
1714
const { data, status } = useCache(ref('key'), () => fetcher('value'))
1815

1916
return () =>
2017
h('div', {}, [`data: ${data.value}, status: ${status.value}`])
2118
},
2219
})
23-
const html = await createRenderer().renderToString(app)
20+
const html = await renderToString(app)
2421
expect(html).toMatchSnapshot()
2522
})
2623
}, 10000)

0 commit comments

Comments
 (0)