-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): store and fetch user settings from backend (#5939)
* feat(core): fetch structure tool settings from backend (#5901) * feat(core): store recent search history in backend (#5940) * feat(core): fetch inspect mode from backend (#5938) * chore: update studio version for production-ready cellar (#5993) * fix: prevent key value store from firing setKey for each open subscription (#5997) * fix: update tests to match api (#6000) * fix: update tests to match api * fix: comment out flaky tests for now * chore: remove unused resolve logic
- Loading branch information
1 parent
8e63552
commit ecb3495
Showing
16 changed files
with
331 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 24 additions & 14 deletions
38
packages/sanity/src/core/store/key-value/KeyValueStore.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 17 additions & 11 deletions
28
packages/sanity/src/core/store/key-value/backends/memory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,26 @@ | ||
import {type Observable, of as observableOf} from 'rxjs' | ||
|
||
import {type Backend} from './types' | ||
import {type Backend, type KeyValuePair} from './types' | ||
|
||
const DB = Object.create(null) | ||
|
||
const get = (key: string, defValue: unknown): Observable<unknown> => | ||
observableOf(key in DB ? DB[key] : defValue) | ||
|
||
const set = (key: string, nextValue: unknown): Observable<unknown> => { | ||
if (typeof nextValue === 'undefined' || nextValue === null) { | ||
delete DB[key] | ||
} else { | ||
DB[key] = nextValue | ||
} | ||
const getKey = (key: string): Observable<unknown> => observableOf(key in DB ? DB[key] : null) | ||
|
||
const setKey = (key: string, nextValue: unknown): Observable<unknown> => { | ||
DB[key] = nextValue | ||
return observableOf(nextValue) | ||
} | ||
|
||
export const memoryBackend: Backend = {get, set} | ||
const getKeys = (keys: string[]): Observable<unknown[]> => { | ||
return observableOf(keys.map((key, i) => (key in DB ? DB[key] : null))) | ||
} | ||
|
||
const setKeys = (keyValuePairs: KeyValuePair[]): Observable<unknown[]> => { | ||
keyValuePairs.forEach((pair) => { | ||
DB[pair.key] = pair.value | ||
}) | ||
|
||
return observableOf(keyValuePairs.map((pair) => pair.value)) | ||
} | ||
|
||
export const memoryBackend: Backend = {getKey, setKey, getKeys, setKeys} |
This file was deleted.
Oops, something went wrong.
89 changes: 89 additions & 0 deletions
89
packages/sanity/src/core/store/key-value/backends/server.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import {type SanityClient} from '@sanity/client' | ||
import DataLoader from 'dataloader' | ||
import {catchError, from, map, of} from 'rxjs' | ||
|
||
import {DEFAULT_STUDIO_CLIENT_OPTIONS} from '../../../studioClient' | ||
import {type KeyValueStoreValue} from '../types' | ||
import {type Backend, type KeyValuePair} from './types' | ||
|
||
/** @internal */ | ||
export interface ServerBackendOptions { | ||
client: SanityClient | ||
} | ||
|
||
/** | ||
* One of serveral possible backends for KeyValueStore. This backend uses the | ||
* Sanity client to store and retrieve key-value pairs from the /users/me/keyvalue endpoint. | ||
* @internal | ||
*/ | ||
export function serverBackend({client: _client}: ServerBackendOptions): Backend { | ||
const client = _client.withConfig(DEFAULT_STUDIO_CLIENT_OPTIONS) | ||
|
||
const keyValueLoader = new DataLoader<string, KeyValueStoreValue | null>(async (keys) => { | ||
const value = await client | ||
.request<KeyValuePair[]>({ | ||
uri: `/users/me/keyvalue/${keys.join(',')}`, | ||
withCredentials: true, | ||
}) | ||
.catch((error) => { | ||
console.error('Error fetching data:', error) | ||
return Array(keys.length).fill(null) | ||
}) | ||
|
||
const keyValuePairs = value.reduce( | ||
(acc, next) => { | ||
if (next?.key) { | ||
acc[next.key] = next.value | ||
} | ||
return acc | ||
}, | ||
{} as Record<string, KeyValueStoreValue | null>, | ||
) | ||
|
||
const result = keys.map((key) => keyValuePairs[key] || null) | ||
return result | ||
}) | ||
|
||
const getKeys = (keys: string[]) => { | ||
return from(keyValueLoader.loadMany(keys)) | ||
} | ||
|
||
const setKeys = (keyValuePairs: KeyValuePair[]) => { | ||
return from( | ||
client.request<KeyValuePair[]>({ | ||
method: 'PUT', | ||
uri: `/users/me/keyvalue`, | ||
body: keyValuePairs, | ||
withCredentials: true, | ||
}), | ||
).pipe( | ||
map((response) => { | ||
return response.map((pair) => { | ||
keyValueLoader.clear(pair.key) | ||
keyValueLoader.prime(pair.key, pair.value) | ||
|
||
return pair.value | ||
}) | ||
}), | ||
catchError((error) => { | ||
console.error('Error setting data:', error) | ||
return of(Array(keyValuePairs.length).fill(null)) | ||
}), | ||
) | ||
} | ||
|
||
const getKey = (key: string) => { | ||
return getKeys([key]).pipe(map((values) => values[0])) | ||
} | ||
|
||
const setKey = (key: string, nextValue: unknown) => { | ||
return setKeys([{key, value: nextValue as KeyValueStoreValue}]).pipe(map((values) => values[0])) | ||
} | ||
|
||
return { | ||
getKey, | ||
setKey, | ||
getKeys, | ||
setKeys, | ||
} | ||
} |
13 changes: 11 additions & 2 deletions
13
packages/sanity/src/core/store/key-value/backends/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
import {type Observable} from 'rxjs' | ||
|
||
import {type KeyValueStoreValue} from '../types' | ||
|
||
export interface KeyValuePair { | ||
key: string | ||
value: KeyValueStoreValue | null | ||
} | ||
|
||
export interface Backend { | ||
get: (key: string, defValue: unknown) => Observable<unknown> | ||
set: (key: string, nextValue: unknown) => Observable<unknown> | ||
getKey: (key: string) => Observable<unknown> | ||
setKey: (key: string, nextValue: unknown) => Observable<unknown> | ||
getKeys: (keys: string[]) => Observable<unknown[]> | ||
setKeys: (keyValuePairs: KeyValuePair[]) => Observable<unknown[]> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.