@sqlrooms/room-store 0.19.2 → 0.21.0

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.
@@ -48,4 +48,17 @@ export declare function createRoomStore<PC, RS extends RoomState<PC>>(stateCreat
48
48
  roomStore: StoreApi<RS>;
49
49
  useRoomStore: <T>(selector: (state: RS) => T) => T;
50
50
  };
51
+ /**
52
+ * Factory to create a room store creator with custom params.
53
+ *
54
+ * @template PC - Room config type
55
+ * @template RS - Room state type
56
+ * @param stateCreatorFactory - A function that takes params and returns a Zustand state creator
57
+ * @returns An object with createRoomStore(params) and useRoomStore(selector)
58
+ *
59
+ */
60
+ export declare function createRoomStoreCreator<TState extends RoomState<any>>(): <TFactory extends (...args: any[]) => StateCreator<TState>>(stateCreatorFactory: TFactory) => {
61
+ createRoomStore: (...args: Parameters<TFactory>) => StoreApi<TState>;
62
+ useRoomStore: <T>(selector: (state: TState) => T) => T;
63
+ };
51
64
  //# sourceMappingURL=RoomStore.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"RoomStore.d.ts","sourceRoot":"","sources":["../src/RoomStore.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,YAAY,EAAE,QAAQ,EAAc,MAAM,SAAS,CAAC;AAG5D,MAAM,MAAM,SAAS,CAAC,EAAE,IAAI,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC,CAAC;AAEpD,MAAM,MAAM,cAAc,CAAC,EAAE,IAAI;IAC/B,eAAe,EAAE,EAAE,GAAG,SAAS,CAAC;IAChC,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;IAC5C,gBAAgB,EAAE,CAAC,SAAS,EAAE,OAAO,EAAE,cAAc,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;CAC1E,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG;IACzB,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC9B,OAAO,EAAE,M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BaseRoomStore } from './RoomStateProvider';
2
+ import { createStore, useStore } from 'zustand';
4
3
  export function createRoomSlice(props) {
5
4
  const { config: initialConfig, room: roomStateProps, ...restState } = props;
6
5
  const initialRoomState = {
@@ -67,19 +66,42 @@ export function createBaseSlice(sliceCreator) {
67
66
  * @returns The room store and a hook for accessing the room store
68
67
  */
69
68
  export function createRoomStore(stateCreator) {
70
- const roomStore = createStore((set, get, store) => ({
71
- ...stateCreator(set, get, store),
72
- }));
73
- if (typeof window !== 'undefined') {
74
- roomStore.getState().room.initialize();
75
- }
76
- else {
77
- console.warn('Skipping room store initialization. Room store should be only used on client.');
78
- }
69
+ const factory = createRoomStoreCreator();
70
+ const storeCreator = factory(() => stateCreator);
71
+ const roomStore = storeCreator.createRoomStore();
79
72
  function useRoomStore(selector) {
80
- // @ts-ignore TODO fix typing
81
- return useBaseRoomStore(selector);
73
+ return storeCreator.useRoomStore(selector);
82
74
  }
83
75
  return { roomStore, useRoomStore };
84
76
  }
77
+ /**
78
+ * Factory to create a room store creator with custom params.
79
+ *
80
+ * @template PC - Room config type
81
+ * @template RS - Room state type
82
+ * @param stateCreatorFactory - A function that takes params and returns a Zustand state creator
83
+ * @returns An object with createRoomStore(params) and useRoomStore(selector)
84
+ *
85
+ */
86
+ export function createRoomStoreCreator() {
87
+ return function (stateCreatorFactory) {
88
+ let store;
89
+ function createRoomStore(...args) {
90
+ store = createStore(stateCreatorFactory(...args));
91
+ if (typeof window !== 'undefined') {
92
+ store.getState().room.initialize();
93
+ }
94
+ else {
95
+ console.warn('Skipping room store initialization. Room store should be only used on client.');
96
+ }
97
+ return store;
98
+ }
99
+ function useRoomStore(selector) {
100
+ if (!store)
101
+ throw new Error('Room store not initialized. Call createRoomStore first.');
102
+ return useStore(store, selector);
103
+ }
104
+ return { createRoomStore, useRoomStore };
105
+ };
106
+ }
85
107
  //# sourceMappingURL=RoomStore.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"RoomStore.js","sourceRoot":"","sources":["../src/Ro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AAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAC7B,YAA8B;IAE9B,MAAM,SAAS,GAAG,WAAW,CAAK,CAAC,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC;QACtD,GAAG,YAAY,CAAC,GAAG,EAAE,GAAG,EAAE,KAAK,CAAC;KACjC,CAAC,CAAC,CAAC;IAEJ,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;QAClC,SAAS,CAAC,QAAQ,EAAE,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC;IACzC,CAAC;SAAM,CAAC;QACN,OAAO,CAAC,IAAI,CACV,+EAA+E,CAChF,CAAC;IACJ,CAAC;IAED,SAAS,YAAY,CAAI,QAA0B;QACjD,6BAA6B;QAC7B,OAAO,gBAAgB,CAAC,QAA4B,CAAC,CAAC;IACxD,CAAC;IACD,OAAO,EAAC,SAAS,EAAE,YAAY,EAAC,CAAC;AACnC,CAAC","sourcesContent":["import {castDraft, produce} from 'immer';\nimport {StateCreator, StoreApi, createStore} from 'zustand';\nimport {useBaseRoomStore} from './RoomStateProvider';\n\nexport type RoomStore<PC> = StoreApi<RoomState<PC>>;\n\nexport type RoomStateProps<PC> = {\n lastSavedConfig: PC | undefined;\n tasksProgress: Record<string, TaskProgress>;\n captureException: (exception: unknown, captureContext?: unknown) => void;\n};\n\nexport type TaskProgress = {\n progress?: number | undefined;\n message: string;\n};\n\nexport type RoomStateActions<PC> = {\n initialize: () => Promise<void>;\n\n /**\n * Set the room config.\n * @param config - The room config to set.\n */\n setRoomConfig: (config: PC) => void;\n /**\n * Set the last saved room config. This can be used to check if the room has unsaved changes.\n * @param config - The room config to set.\n */\n setLastSavedConfig: (config: PC) => void;\n /**\n * Check if the room has unsaved changes.\n * @returns True if the room has unsaved changes, false otherwise.\n */\n hasUnsavedChanges(): boolean; // since last save\n\n setTaskProgress: (id: string, taskProgress: TaskProgress | undefined) => void;\n getLoadingProgress: () => TaskProgress | undefined;\n};\n\nexport type RoomState<PC> = {\n config: PC;\n room: RoomStateProps<PC> & RoomStateActions<PC>;\n};\n\nexport function createRoomSlice<PC>(props: {\n config: PC;\n room: Partial<Omit<RoomStateProps<PC>, 'config'>>;\n}): StateCreator<RoomState<PC>> {\n const {config: initialConfig, room: roomStateProps, ...restState} = props;\n const initialRoomState: RoomStateProps<PC> = {\n ...roomStateProps,\n lastSavedConfig: undefined,\n tasksProgress: {},\n captureException: (exception: unknown) => {\n console.error(exception);\n },\n };\n const slice: StateCreator<RoomState<PC>> = (set, get) => {\n const roomState: RoomState<PC> = {\n config: initialConfig,\n room: {\n ...initialRoomState,\n initialize: async () => {\n // To be overridden by the room shell\n },\n\n setRoomConfig: (config) =>\n set((state) =>\n produce(state, (draft) => {\n draft.config = castDraft(config);\n }),\n ),\n setLastSavedConfig: (config) =>\n set((state) =>\n produce(state, (draft) => {\n draft.room.lastSavedConfig = castDraft(config);\n }),\n ),\n\n hasUnsavedChanges: () => {\n const {lastSavedConfig} = get().room;\n const {config} = get();\n return config !== lastSavedConfig;\n },\n\n /** Returns the progress of the last task */\n getLoadingProgress() {\n const {tasksProgress} = get().room;\n const keys = Object.keys(tasksProgress);\n const lastKey = keys[keys.length - 1];\n if (lastKey) {\n return tasksProgress[lastKey];\n }\n return undefined;\n },\n\n setTaskProgress(id, taskProgress) {\n set((state) =>\n produce(state, (draft) => {\n if (taskProgress) {\n draft.room.tasksProgress[id] = taskProgress;\n } else {\n delete draft.room.tasksProgress[id];\n }\n }),\n );\n },\n },\n ...restState,\n };\n\n return roomState;\n };\n\n return slice;\n}\n\nexport function createBaseSlice<PC, S>(\n sliceCreator: (...args: Parameters<StateCreator<S & RoomState<PC>>>) => S,\n): StateCreator<S> {\n return (set, get, store) =>\n sliceCreator(\n set,\n get as () => S & RoomState<PC>,\n store as StoreApi<S & RoomState<PC>>,\n );\n}\n\n/**\n * Create a room store with custom fields and methods\n * @param initialState - The initial state and config for the room\n * @param sliceCreators - The slices to add to the room store\n * @returns The room store and a hook for accessing the room store\n */\nexport function createRoomStore<PC, RS extends RoomState<PC>>(\n stateCreator: StateCreator<RS>,\n) {\n const roomStore = createStore<RS>((set, get, store) => ({\n ...stateCreator(set, get, store),\n }));\n\n if (typeof window !== 'undefined') {\n roomStore.getState().room.initialize();\n } else {\n console.warn(\n 'Skipping room store initialization. Room store should be only used on client.',\n );\n }\n\n function useRoomStore<T>(selector: (state: RS) => T): T {\n // @ts-ignore TODO fix typing\n return useBaseRoomStore(selector as (state: RS) => T);\n }\n return {roomStore, useRoomStore};\n}\n"]}
1
+ {"version":3,"file":"RoomStore.js","sourceRoot":"","sources":["../src/RoomStore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAE,OAAO,EAAC,MAAM,OAAO,CAAC;AACzC,OAAO,EAAyB,WAAW,EAAE,QAAQ,EAAC,MAAM,SAAS,CAAC;AA4CtE,MAAM,UAAU,eAAe,CAAK,KAGnC;IACC,MAAM,EAAC,MAAM,EAAE,aAAa,EAAE,IAAI,EAAE,cAAc,EAAE,GAAG,SAAS,EAAC,GAAG,KAAK,CAAC;IAC1E,MAAM,gBAAgB,GAAuB;QAC3C,GAAG,cAAc;QACjB,eAAe,EAAE,SAAS;QAC1B,aAAa,EAAE,EAAE;QACjB,gBAAgB,EAAE,CAAC,SAAkB,EAAE,EAAE;YACvC,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;QAC3B,CAAC;KACF,CAAC;IACF,MAAM,KAAK,GAAgC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;QACtD,MAAM,SAAS,GAAkB;YAC/B,MAAM,EAAE,aAAa;YACrB,IAAI,EAAE;gBACJ,GAAG,gBAAgB;gBACnB,UAAU,EAAE,KAAK,IAAI,EAAE;oBACrB,qCAAqC;gBACvC,CAAC;gBAED,aAAa,EAAE,CAAC,MAAM,EAAE,EAAE,CACxB,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CACZ,OAAO,CAAC,KAAK,EAAE,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AAI,CAAC,KAAK;gBACR,MAAM,IAAI,KAAK,CACb,yDAAyD,CAC1D,CAAC;YACJ,OAAO,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;QACnC,CAAC;QAED,OAAO,EAAC,eAAe,EAAE,YAAY,EAAC,CAAC;IACzC,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import {castDraft, produce} from 'immer';\nimport {StateCreator, StoreApi, createStore, useStore} from 'zustand';\nimport {useBaseRoomStore} from './RoomStateProvider';\n\nexport type RoomStore<PC> = StoreApi<RoomState<PC>>;\n\nexport type RoomStateProps<PC> = {\n lastSavedConfig: PC | undefined;\n tasksProgress: Record<string, TaskProgress>;\n captureException: (exception: unknown, captureContext?: unknown) => void;\n};\n\nexport type TaskProgress = {\n progress?: number | undefined;\n message: string;\n};\n\nexport type RoomStateActions<PC> = {\n initialize: () => Promise<void>;\n\n /**\n * Set the room config.\n * @param config - The room config to set.\n */\n setRoomConfig: (config: PC) => void;\n /**\n * Set the last saved room config. This can be used to check if the room has unsaved changes.\n * @param config - The room config to set.\n */\n setLastSavedConfig: (config: PC) => void;\n /**\n * Check if the room has unsaved changes.\n * @returns True if the room has unsaved changes, false otherwise.\n */\n hasUnsavedChanges(): boolean; // since last save\n\n setTaskProgress: (id: string, taskProgress: TaskProgress | undefined) => void;\n getLoadingProgress: () => TaskProgress | undefined;\n};\n\nexport type RoomState<PC> = {\n config: PC;\n room: RoomStateProps<PC> & RoomStateActions<PC>;\n};\n\nexport function createRoomSlice<PC>(props: {\n config: PC;\n room: Partial<Omit<RoomStateProps<PC>, 'config'>>;\n}): StateCreator<RoomState<PC>> {\n const {config: initialConfig, room: roomStateProps, ...restState} = props;\n const initialRoomState: RoomStateProps<PC> = {\n ...roomStateProps,\n lastSavedConfig: undefined,\n tasksProgress: {},\n captureException: (exception: unknown) => {\n console.error(exception);\n },\n };\n const slice: StateCreator<RoomState<PC>> = (set, get) => {\n const roomState: RoomState<PC> = {\n config: initialConfig,\n room: {\n ...initialRoomState,\n initialize: async () => {\n // To be overridden by the room shell\n },\n\n setRoomConfig: (config) =>\n set((state) =>\n produce(state, (draft) => {\n draft.config = castDraft(config);\n }),\n ),\n setLastSavedConfig: (config) =>\n set((state) =>\n produce(state, (draft) => {\n draft.room.lastSavedConfig = castDraft(config);\n }),\n ),\n\n hasUnsavedChanges: () => {\n const {lastSavedConfig} = get().room;\n const {config} = get();\n return config !== lastSavedConfig;\n },\n\n /** Returns the progress of the last task */\n getLoadingProgress() {\n const {tasksProgress} = get().room;\n const keys = Object.keys(tasksProgress);\n const lastKey = keys[keys.length - 1];\n if (lastKey) {\n return tasksProgress[lastKey];\n }\n return undefined;\n },\n\n setTaskProgress(id, taskProgress) {\n set((state) =>\n produce(state, (draft) => {\n if (taskProgress) {\n draft.room.tasksProgress[id] = taskProgress;\n } else {\n delete draft.room.tasksProgress[id];\n }\n }),\n );\n },\n },\n ...restState,\n };\n\n return roomState;\n };\n\n return slice;\n}\n\nexport function createBaseSlice<PC, S>(\n sliceCreator: (...args: Parameters<StateCreator<S & RoomState<PC>>>) => S,\n): StateCreator<S> {\n return (set, get, store) =>\n sliceCreator(\n set,\n get as () => S & RoomState<PC>,\n store as StoreApi<S & RoomState<PC>>,\n );\n}\n\n/**\n * Create a room store with custom fields and methods\n * @param initialState - The initial state and config for the room\n * @param sliceCreators - The slices to add to the room store\n * @returns The room store and a hook for accessing the room store\n */\nexport function createRoomStore<PC, RS extends RoomState<PC>>(\n stateCreator: StateCreator<RS>,\n) {\n const factory = createRoomStoreCreator<RS>();\n const storeCreator = factory(() => stateCreator);\n const roomStore = storeCreator.createRoomStore();\n\n function useRoomStore<T>(selector: (state: RS) => T): T {\n return storeCreator.useRoomStore(selector);\n }\n\n return {roomStore, useRoomStore};\n}\n\n/**\n * Factory to create a room store creator with custom params.\n *\n * @template PC - Room config type\n * @template RS - Room state type\n * @param stateCreatorFactory - A function that takes params and returns a Zustand state creator\n * @returns An object with createRoomStore(params) and useRoomStore(selector)\n *\n */\nexport function createRoomStoreCreator<TState extends RoomState<any>>() {\n return function <TFactory extends (...args: any[]) => StateCreator<TState>>(\n stateCreatorFactory: TFactory,\n ): {\n createRoomStore: (...args: Parameters<TFactory>) => StoreApi<TState>;\n useRoomStore: <T>(selector: (state: TState) => T) => T;\n } {\n let store: StoreApi<TState> | undefined;\n\n function createRoomStore(...args: Parameters<TFactory>): StoreApi<TState> {\n store = createStore(stateCreatorFactory(...args));\n if (typeof window !== 'undefined') {\n store.getState().room.initialize();\n } else {\n console.warn(\n 'Skipping room store initialization. Room store should be only used on client.',\n );\n }\n return store;\n }\n\n function useRoomStore<T>(selector: (state: TState) => T): T {\n if (!store)\n throw new Error(\n 'Room store not initialized. Call createRoomStore first.',\n );\n return useStore(store, selector);\n }\n\n return {createRoomStore, useRoomStore};\n };\n}\n"]}
package/dist/index.d.ts CHANGED
@@ -3,6 +3,6 @@
3
3
  * @packageDocumentation
4
4
  */
5
5
  export { RoomStateContext, RoomStateProvider, useBaseRoomStore, type RoomStateProviderProps, } from './RoomStateProvider';
6
- export { createRoomStore, createRoomSlice, type RoomStateActions, type RoomStateProps, type RoomState, type RoomStore, type TaskProgress, createBaseSlice, } from './RoomStore';
6
+ export { createRoomStore, createRoomStoreCreator, createRoomSlice, type RoomStateActions, type RoomStateProps, type RoomState, type RoomStore, type TaskProgress, createBaseSlice, } from './RoomStore';
7
7
  export * from '@sqlrooms/room-config';
8
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EACjB,gBAAgB,EAChB,KAAK,sBAAsB,GAC5B,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EACL,eAAe,EACf,eAAe,EACf,KAAK,gBAAgB,EACrB,KAAK,cAAc,EACnB,KAAK,SAAS,EACd,KAAK,SAAS,EACd,KAAK,YAAY,EACjB,eAAe,GAChB,MAAM,aAAa,CAAC;AAErB,cAAc,uBAAuB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EACjB,gBAAgB,EAChB,KAAK,sBAAsB,GAC5B,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EACL,eAAe,EACf,sBAAsB,EACtB,eAAe,EACf,KAAK,gBAAgB,EACrB,KAAK,cAAc,EACnB,KAAK,SAAS,EACd,KAAK,SAAS,EACd,KAAK,YAAY,EACjB,eAAe,GAChB,MAAM,aAAa,CAAC;AAErB,cAAc,uBAAuB,CAAC"}
package/dist/index.js CHANGED
@@ -3,6 +3,6 @@
3
3
  * @packageDocumentation
4
4
  */
5
5
  export { RoomStateContext, RoomStateProvider, useBaseRoomStore, } from './RoomStateProvider';
6
- export { createRoomStore, createRoomSlice, createBaseSlice, } from './RoomStore';
6
+ export { createRoomStore, createRoomStoreCreator, createRoomSlice, createBaseSlice, } from './RoomStore';
7
7
  export * from '@sqlrooms/room-config';
8
8
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EACjB,gBAAgB,GAEjB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EACL,eAAe,EACf,eAAe,EAMf,eAAe,GAChB,MAAM,aAAa,CAAC;AAErB,cAAc,uBAAuB,CAAC","sourcesContent":["/**\n * {@include ../README.md}\n * @packageDocumentation\n */\n\nexport {\n RoomStateContext,\n RoomStateProvider,\n useBaseRoomStore,\n type RoomStateProviderProps,\n} from './RoomStateProvider';\n\nexport {\n createRoomStore,\n createRoomSlice,\n type RoomStateActions,\n type RoomStateProps,\n type RoomState,\n type RoomStore,\n type TaskProgress,\n createBaseSlice,\n} from './RoomStore';\n\nexport * from '@sqlrooms/room-config';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EACjB,gBAAgB,GAEjB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EACL,eAAe,EACf,sBAAsB,EACtB,eAAe,EAMf,eAAe,GAChB,MAAM,aAAa,CAAC;AAErB,cAAc,uBAAuB,CAAC","sourcesContent":["/**\n * {@include ../README.md}\n * @packageDocumentation\n */\n\nexport {\n RoomStateContext,\n RoomStateProvider,\n useBaseRoomStore,\n type RoomStateProviderProps,\n} from './RoomStateProvider';\n\nexport {\n createRoomStore,\n createRoomStoreCreator,\n createRoomSlice,\n type RoomStateActions,\n type RoomStateProps,\n type RoomState,\n type RoomStore,\n type TaskProgress,\n createBaseSlice,\n} from './RoomStore';\n\nexport * from '@sqlrooms/room-config';\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sqlrooms/room-store",
3
- "version": "0.19.2",
3
+ "version": "0.21.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "module": "dist/index.js",
@@ -18,7 +18,7 @@
18
18
  "access": "public"
19
19
  },
20
20
  "dependencies": {
21
- "@sqlrooms/room-config": "0.19.2",
21
+ "@sqlrooms/room-config": "0.21.0",
22
22
  "immer": "^10.1.1",
23
23
  "zod": "^3.25.57",
24
24
  "zustand": "^5.0.5"
@@ -33,5 +33,5 @@
33
33
  "typecheck": "tsc --noEmit",
34
34
  "typedoc": "typedoc"
35
35
  },
36
- "gitHead": "92b70398a085ebbf622c4f18e44b1ef60139096e"
36
+ "gitHead": "8411bc915049f20dbcd48a60493907ddda8b7b38"
37
37
  }