@sanity/sdk 0.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/dist/index.d.ts +339 -0
  2. package/dist/index.js +492 -0
  3. package/dist/index.js.map +1 -0
  4. package/package.json +77 -0
  5. package/src/_exports/index.ts +39 -0
  6. package/src/auth/authStore.test.ts +296 -0
  7. package/src/auth/authStore.ts +125 -0
  8. package/src/auth/getAuthStore.test.ts +14 -0
  9. package/src/auth/getInternalAuthStore.ts +20 -0
  10. package/src/auth/internalAuthStore.test.ts +334 -0
  11. package/src/auth/internalAuthStore.ts +519 -0
  12. package/src/client/getClient.test.ts +41 -0
  13. package/src/client/getClient.ts +13 -0
  14. package/src/client/getSubscribableClient.test.ts +71 -0
  15. package/src/client/getSubscribableClient.ts +17 -0
  16. package/src/client/store/actions/getClientEvents.test.ts +95 -0
  17. package/src/client/store/actions/getClientEvents.ts +33 -0
  18. package/src/client/store/actions/getOrCreateClient.test.ts +56 -0
  19. package/src/client/store/actions/getOrCreateClient.ts +40 -0
  20. package/src/client/store/actions/receiveToken.test.ts +18 -0
  21. package/src/client/store/actions/receiveToken.ts +31 -0
  22. package/src/client/store/clientStore.test.ts +152 -0
  23. package/src/client/store/clientStore.ts +98 -0
  24. package/src/documentList/documentListStore.test.ts +575 -0
  25. package/src/documentList/documentListStore.ts +269 -0
  26. package/src/documents/.keep +0 -0
  27. package/src/instance/identity.test.ts +46 -0
  28. package/src/instance/identity.ts +28 -0
  29. package/src/instance/sanityInstance.test.ts +66 -0
  30. package/src/instance/sanityInstance.ts +64 -0
  31. package/src/instance/types.d.ts +29 -0
  32. package/src/schema/schemaStore.test.ts +30 -0
  33. package/src/schema/schemaStore.ts +32 -0
  34. package/src/store/createStore.test.ts +108 -0
  35. package/src/store/createStore.ts +106 -0
  36. package/src/tsdoc.json +39 -0
@@ -0,0 +1,106 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import {devtools} from 'zustand/middleware'
3
+ import {createStore as createZustandStore, type StoreApi} from 'zustand/vanilla'
4
+
5
+ import type {SanityInstance} from '../instance/types'
6
+
7
+ /**
8
+ * Creates a (vanilla) zustand store with the given initial state and actions.
9
+ * Only the actions will be returned - actions should be created to interact with it.
10
+ *
11
+ * The rationale for this is to encapsulate the internal state so only the store knows about it,
12
+ * making refactoring easier and more predictable. While this seems like good practice, I am unsure
13
+ * if it might get in our way once we start wanting to subscribe to finer piece of state or similar.
14
+ * I will leave it for now, and we can see if it becomes a problem.
15
+ *
16
+ * @param initialState - Initial state of the store
17
+ * @param actions - The actions available on the store
18
+ * @param options - Options for the store
19
+ * @returns The actions available on the store
20
+ * @internal
21
+ */
22
+ export function createStore<S, A extends StoreActionMap<S>>(
23
+ initialState: S,
24
+ actions: A,
25
+ {name, instance}: StoreOptions,
26
+ ): CurriedActions<S, A> {
27
+ const uniqueName = `${name}-${instance.identity.id}`
28
+ const store = createZustandStore<S>()(devtools(() => ({...initialState}), {name: uniqueName}))
29
+ const context = {store, instance}
30
+ return createCurriedActions(actions, context)
31
+ }
32
+
33
+ /**
34
+ * Context passed to store actions as first argument.
35
+ *
36
+ * @internal
37
+ */
38
+ export interface StoreActionContext<S> {
39
+ /**
40
+ * The store API, from zustand. Contains `getState()`, `setState()`, and `subscribe()`.
41
+ */
42
+ store: StoreApi<S>
43
+
44
+ /**
45
+ * The Sanity SDK instance associated with the store. Often used
46
+ */
47
+ instance: SanityInstance
48
+ }
49
+
50
+ /**
51
+ * An action that can be performed on a store.
52
+ *
53
+ * @param context - Store context. Contains the store API (`getState()`, `setState()`, `subscribe()`), as well as the SDK instance associated with it.
54
+ * @internal
55
+ */
56
+ export type StoreAction<S> = (context: StoreActionContext<S>, ...args: any[]) => any
57
+
58
+ /**
59
+ * A map of actions that can be performed on a store.
60
+ *
61
+ * @internal
62
+ */
63
+ export type StoreActionMap<S> = Record<string, StoreAction<S>>
64
+
65
+ /**
66
+ * Options for creating a store.
67
+ *
68
+ * @internal
69
+ */
70
+ export interface StoreOptions {
71
+ /**
72
+ * Name used for debugging
73
+ */
74
+ name: string
75
+
76
+ /**
77
+ * The Sanity SDK instance associated with the store
78
+ */
79
+ instance: SanityInstance
80
+ }
81
+
82
+ function createCurriedActions<S, A extends StoreActionMap<S>>(
83
+ actions: A,
84
+ store: StoreActionContext<S>,
85
+ ): CurriedActions<S, A> {
86
+ const curried: CurriedActions<S, A> = {} as CurriedActions<S, A>
87
+ return Object.entries(actions).reduce((acc, [key, action]) => {
88
+ const curriedAction = (...args: RestParameters<typeof action>) => action(store, ...args)
89
+ return {...acc, [key]: curriedAction}
90
+ }, curried)
91
+ }
92
+
93
+ type RestParameters<T extends (...args: any[]) => any> = T extends (
94
+ first: any,
95
+ ...rest: infer R
96
+ ) => any
97
+ ? R
98
+ : never
99
+
100
+ type ActionReturn<T extends (...args: any[]) => any> = ReturnType<T>
101
+
102
+ type CurriedActions<S, A extends StoreActionMap<S>> = {
103
+ [K in keyof A]: A[K] extends StoreAction<S>
104
+ ? (...args: RestParameters<A[K]>) => ActionReturn<A[K]>
105
+ : never
106
+ }
package/src/tsdoc.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "$schema": "https://developer.microsoft.com/json-schemas/tsdoc/v0/tsdoc.schema.json",
3
+ "tagDefinitions": [
4
+ {
5
+ "tagName": "@hidden",
6
+ "syntaxKind": "block",
7
+ "allowMultiple": true
8
+ },
9
+ {
10
+ "tagName": "@todo",
11
+ "syntaxKind": "block",
12
+ "allowMultiple": true
13
+ },
14
+ {
15
+ "tagName": "@thoughtLevel",
16
+ "syntaxKind": "block",
17
+ "allowMultiple": true
18
+ }
19
+ ],
20
+ "supportForTags": {
21
+ "@alpha": true,
22
+ "@beta": true,
23
+ "@defaultValue": true,
24
+ "@deprecated": true,
25
+ "@example": true,
26
+ "@experimental": true,
27
+ "@hidden": true,
28
+ "@internal": true,
29
+ "@link": true,
30
+ "@param": true,
31
+ "@public": true,
32
+ "@remarks": true,
33
+ "@returns": true,
34
+ "@see": true,
35
+ "@thoughtLevel": true,
36
+ "@throws": true,
37
+ "@todo": true
38
+ }
39
+ }