Skip to content

Commit 26da5f5

Browse files
authored
Export unstable_serialize from SWR (#1337)
* export serialize from swr and swr-infinite * fix API * rename api
1 parent 20ace5a commit 26da5f5

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

infinite/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const getFirstPageKey = (getKey: KeyLoader<any>) => {
2222
return serialize(getKey ? getKey(0, null) : null)[0]
2323
}
2424

25-
export const getInfiniteKey = (getKey: KeyLoader<any>) => {
25+
export const unstable_serialize = (getKey: KeyLoader<any>) => {
2626
return INFINITE_PREFIX + getFirstPageKey(getKey)
2727
}
2828

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Core APIs
2-
export { SWRConfig, mutate, createCache } from './use-swr'
2+
export { SWRConfig, mutate, createCache, unstable_serialize } from './use-swr'
33

44
// useSWR
55
import useSWR from './use-swr'

src/use-swr.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,4 +650,6 @@ export const createCache = <Data = any>(
650650
}
651651
}
652652

653+
export const unstable_serialize = (key: Key) => serialize(key)[0]
654+
653655
export default withArgs<SWRHook>(useSWRHandler)

test/use-swr-infinite.test.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useEffect, useState } from 'react'
22
import { render, fireEvent, act, screen } from '@testing-library/react'
33
import { mutate, createCache, SWRConfig } from 'swr'
4-
import useSWRInfinite, { getInfiniteKey } from 'swr/infinite'
4+
import useSWRInfinite, { unstable_serialize } from 'swr/infinite'
55
import { sleep, createKey, createResponse } from './utils'
66

77
describe('useSWRInfinite', () => {
@@ -581,7 +581,7 @@ describe('useSWRInfinite', () => {
581581
await screen.findByText('data:')
582582
})
583583

584-
it('should mutate a cache with getInfiniteKey', async () => {
584+
it('should mutate a cache with `unstable_serialize`', async () => {
585585
let count = 0
586586
function Page() {
587587
const { data } = useSWRInfinite<string, string>(
@@ -596,20 +596,22 @@ describe('useSWRInfinite', () => {
596596

597597
await screen.findByText('data:page-test-12-0:1')
598598

599-
await act(() => mutate(getInfiniteKey(index => `page-test-12-${index}`)))
599+
await act(() =>
600+
mutate(unstable_serialize(index => `page-test-12-${index}`))
601+
)
600602
await screen.findByText('data:page-test-12-0:2')
601603

602604
await act(() =>
603605
mutate(
604-
getInfiniteKey(index => `page-test-12-${index}`),
606+
unstable_serialize(index => `page-test-12-${index}`),
605607
'local-mutation',
606608
false
607609
)
608610
)
609611
await screen.findByText('data:local-mutation')
610612
})
611613

612-
it('should mutate a cache with getInfiniteKey based on a current data', async () => {
614+
it('should mutate a cache with `unstable_serialize` based on a current data', async () => {
613615
const getKey = index => [`pagetest-13`, index]
614616
function Comp() {
615617
const { data, size, setSize } = useSWRInfinite<string, string>(
@@ -630,15 +632,15 @@ describe('useSWRInfinite', () => {
630632

631633
await act(() =>
632634
mutate(
633-
getInfiniteKey(getKey),
635+
unstable_serialize(getKey),
634636
data => data.map(value => `(edited)${value}`),
635637
false
636638
)
637639
)
638640
await screen.findByText('data:(edited)page 0, (edited)page 1,')
639641
})
640642

641-
it('should be able to use getInfiniteKey with a custom cache', async () => {
643+
it('should be able to use `unstable_serialize` with a custom cache', async () => {
642644
const key = 'page-test-14;'
643645
const customCache1 = new Map([[key, 'initial-cache']])
644646
const { cache, mutate: mutateCustomCache } = createCache(customCache1)
@@ -659,11 +661,11 @@ describe('useSWRInfinite', () => {
659661

660662
await screen.findByText('data:initial-cache')
661663

662-
await act(() => mutateCustomCache(getInfiniteKey(() => key)))
664+
await act(() => mutateCustomCache(unstable_serialize(() => key)))
663665
await screen.findByText('data:response data')
664666

665667
await act(() =>
666-
mutateCustomCache(getInfiniteKey(() => key), 'local-mutation', false)
668+
mutateCustomCache(unstable_serialize(() => key), 'local-mutation', false)
667669
)
668670
await screen.findByText('data:local-mutation')
669671
})

0 commit comments

Comments
 (0)