jinrai 1.1.1 → 1.1.3

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 (58) hide show
  1. package/index.ts +4 -0
  2. package/lib/bin/bin.js +13 -9
  3. package/lib/index.d.ts +2 -0
  4. package/lib/index.js +1 -0
  5. package/lib/src/bin/agent/agent.d.ts +1 -0
  6. package/lib/src/bin/agent/agent.js +3 -0
  7. package/lib/src/bin/playwright/pageCollector.d.ts +2 -1
  8. package/lib/src/bin/playwright/templates.d.ts +2 -6
  9. package/lib/src/bin/routes/Parser.d.ts +6 -1
  10. package/lib/src/bin/routes/Parser.js +5 -0
  11. package/lib/src/bin/routes/getRoutes.d.ts +3 -2
  12. package/lib/src/front/server-state/DataProxy.d.ts +2 -1
  13. package/lib/src/front/server-state/DataProxy.js +119 -60
  14. package/lib/src/front/server-state/SSR.d.ts +2 -0
  15. package/lib/src/front/server-state/SSR.js +16 -3
  16. package/lib/src/front/server-state/real.js +12 -1
  17. package/lib/src/front/server-state/serverStates.d.ts +19 -5
  18. package/lib/src/front/server-state/serverStates.js +29 -15
  19. package/lib/src/front/server-state/useServerState.d.ts +3 -12
  20. package/lib/src/front/server-state/useServerState.js +12 -12
  21. package/lib/src/front/url/search/useSearch.js +1 -1
  22. package/lib/src/front/url/search/useSearchValue.d.ts +11 -5
  23. package/lib/src/front/url/search/useSearchValue.js +15 -7
  24. package/lib/src/front/wrapper/Custom.d.ts +3 -3
  25. package/lib/src/front/wrapper/Custom.js +11 -2
  26. package/lib/vite/plugin.js +18 -149
  27. package/package.json +7 -1
  28. package/src/bin/agent/agent.ts +1 -0
  29. package/src/bin/playwright/pageCollector.ts +6 -3
  30. package/src/bin/playwright/templates.ts +5 -9
  31. package/src/bin/routes/Parser.ts +12 -5
  32. package/src/bin/routes/getRoutes.ts +4 -3
  33. package/src/front/server-state/DataProxy.ts +136 -61
  34. package/src/front/server-state/SSR.ts +18 -2
  35. package/src/front/server-state/real.ts +16 -1
  36. package/src/front/server-state/serverStates.ts +54 -20
  37. package/src/front/server-state/useServerState.ts +17 -26
  38. package/src/front/url/search/useSearch.ts +1 -1
  39. package/src/front/url/search/useSearchValue.ts +29 -13
  40. package/src/front/wrapper/Custom.tsx +20 -4
  41. package/tests/data-proxy/create-dataproxy.test.ts +116 -0
  42. package/tests/{custom.test.ts → parse/custom.test.ts} +2 -2
  43. package/tests/{parse.test.ts → parse/parse.test.ts} +7 -7
  44. package/vite/plugin.ts +21 -15
  45. /package/tests/{content → parse/content}/1.html +0 -0
  46. /package/tests/{content → parse/content}/1_result.json +0 -0
  47. /package/tests/{content → parse/content}/2.html +0 -0
  48. /package/tests/{content → parse/content}/2_result.json +0 -0
  49. /package/tests/{content → parse/content}/3.html +0 -0
  50. /package/tests/{content → parse/content}/3_result.json +0 -0
  51. /package/tests/{content → parse/content}/4.html +0 -0
  52. /package/tests/{content → parse/content}/4_result.json +0 -0
  53. /package/tests/{content → parse/content}/custom.html +0 -0
  54. /package/tests/{content → parse/content}/custom.json +0 -0
  55. /package/tests/{content → parse/content}/index.html +0 -0
  56. /package/tests/{content → parse/content}/index.json +0 -0
  57. /package/tests/{content → parse/content}/index_with_templates.json +0 -0
  58. /package/tests/{content → parse/content}/templates.json +0 -0
package/index.ts CHANGED
@@ -1,6 +1,10 @@
1
+ export type { ServerKey } from "./src/front/server-state/useServerState"
2
+
1
3
  export { useServerState } from "./src/front/server-state/useServerState"
2
4
  export { real } from "./src/front/server-state/real"
3
5
  export { useParamsIndex } from "./src/front/url/params/useParamsIndex"
4
6
  export { useSearchValue, useSearchArray } from "./src/front/url/search/useSearchValue"
5
7
  export { useSearch } from "./src/front/url/search/useSearch"
6
8
  export { Custom } from "./src/front/wrapper/Custom"
9
+
10
+ export { stringifyInput } from "./src/front/server-state/SSR"
package/lib/bin/bin.js CHANGED
@@ -28,6 +28,7 @@ const normalizeHtmlWhitespace = (html) => {
28
28
  .trim();
29
29
  };
30
30
 
31
+ const SPLIT = "@#UNIQ_SPLITTER#@";
31
32
  class Parser {
32
33
  options;
33
34
  openVar = "{{";
@@ -95,12 +96,12 @@ class Parser {
95
96
  }
96
97
  }
97
98
  createCustomElement(parent, value) {
98
- const [name, ...props] = value.trimStart().split("|");
99
- value = props.join("|");
99
+ const [customProps, exampleProps, children] = value.split(SPLIT);
100
+ const custom = JSON.parse(customProps);
100
101
  parent.push({
101
102
  type: "custom",
102
- name,
103
- props: value,
103
+ name: custom.name,
104
+ props: custom.props,
104
105
  });
105
106
  }
106
107
  createElement(parent, value, isVarible) {
@@ -144,7 +145,7 @@ const getRoutesAndTemplates = (pages, normalize = true, templates = true) => {
144
145
  id,
145
146
  content,
146
147
  mask,
147
- requests: template.input,
148
+ state: template.state,
148
149
  });
149
150
  }
150
151
  return {
@@ -194,12 +195,15 @@ class Task {
194
195
 
195
196
  const pageCollector = async (page) => {
196
197
  const state = await page.evaluate(() => {
197
- return window.__page_requests;
198
+ const state = Object.fromEntries(window.$exportServerStates);
199
+ return state;
198
200
  });
199
201
  const root = await page.locator("#root").innerHTML();
200
- return { state, root };
202
+ return { root, state };
201
203
  };
202
204
 
205
+ const JinraiAgent = "____JINRAI_AGENT____";
206
+
203
207
  const getRawPageData = async (url, pages, test = false, debug = false) => {
204
208
  const task = new Task();
205
209
  task.next("Router analysis", "yellow", spinners.dotsCircle);
@@ -207,7 +211,7 @@ const getRawPageData = async (url, pages, test = false, debug = false) => {
207
211
  const browser = await chromium.launch({ headless: !debug, devtools: true, channel: "chrome" });
208
212
  // const test_browser = await chromium.launch({ headless: true, channel: "chrome" })
209
213
  const context = await browser.newContext({
210
- userAgent: "____JINRAI_CLIENT____",
214
+ userAgent: JinraiAgent,
211
215
  locale: "ru-RU",
212
216
  });
213
217
  for await (const [id, mask] of pages.entries()) {
@@ -233,7 +237,7 @@ const getRawPageData = async (url, pages, test = false, debug = false) => {
233
237
  page.close();
234
238
  result.push({
235
239
  id,
236
- input: state,
240
+ state,
237
241
  mask,
238
242
  root,
239
243
  test: testRoot,
package/lib/index.d.ts CHANGED
@@ -1,6 +1,8 @@
1
+ export type { ServerKey } from "./src/front/server-state/useServerState";
1
2
  export { useServerState } from "./src/front/server-state/useServerState";
2
3
  export { real } from "./src/front/server-state/real";
3
4
  export { useParamsIndex } from "./src/front/url/params/useParamsIndex";
4
5
  export { useSearchValue, useSearchArray } from "./src/front/url/search/useSearchValue";
5
6
  export { useSearch } from "./src/front/url/search/useSearch";
6
7
  export { Custom } from "./src/front/wrapper/Custom";
8
+ export { stringifyInput } from "./src/front/server-state/SSR";
package/lib/index.js CHANGED
@@ -4,3 +4,4 @@ export { useParamsIndex } from './src/front/url/params/useParamsIndex.js';
4
4
  export { useSearchArray, useSearchValue } from './src/front/url/search/useSearchValue.js';
5
5
  export { useSearch } from './src/front/url/search/useSearch.js';
6
6
  export { Custom } from './src/front/wrapper/Custom.js';
7
+ export { stringifyInput } from './src/front/server-state/SSR.js';
@@ -0,0 +1 @@
1
+ export declare const JinraiAgent = "____JINRAI_AGENT____";
@@ -0,0 +1,3 @@
1
+ const JinraiAgent = "____JINRAI_AGENT____";
2
+
3
+ export { JinraiAgent };
@@ -1,5 +1,6 @@
1
1
  import { Page } from "playwright";
2
+ import { ServerStateMap } from "../../front/server-state/useServerState";
2
3
  export declare const pageCollector: (page: Page) => Promise<{
3
- state: any;
4
+ state: ServerStateMap;
4
5
  root: string;
5
6
  }>;
@@ -1,13 +1,9 @@
1
- export type input = {
2
- method: string;
3
- url: string;
4
- input: object;
5
- };
1
+ import { ServerValue } from "../../front/server-state/serverStates";
6
2
  export interface PageData {
7
3
  id: number;
8
4
  mask: string;
9
5
  root: string;
10
- input: input[];
6
+ state: Record<string, ServerValue>;
11
7
  test?: string;
12
8
  }
13
9
  export declare const getRawPageData: (url: string, pages: string[], test?: boolean, debug?: boolean) => Promise<PageData[]>;
@@ -1,3 +1,8 @@
1
+ export declare const SPLIT = "@#UNIQ_SPLITTER#@";
2
+ interface CustomElement {
3
+ name: string;
4
+ props: object;
5
+ }
1
6
  interface ParserOptions {
2
7
  templates?: boolean;
3
8
  normalize?: boolean;
@@ -19,7 +24,7 @@ interface ValueElement {
19
24
  interface CustomElement {
20
25
  type: "custom";
21
26
  name: string;
22
- props: string;
27
+ props: object;
23
28
  }
24
29
  export declare class Parser {
25
30
  options?: ParserOptions;
@@ -0,0 +1,5 @@
1
+ import 'node:crypto';
2
+
3
+ const SPLIT = "@#UNIQ_SPLITTER#@";
4
+
5
+ export { SPLIT };
@@ -1,9 +1,10 @@
1
- import { input, PageData } from "../playwright/templates";
1
+ import { ServerStateMap } from "../../front/server-state/useServerState";
2
+ import { PageData } from "../playwright/templates";
2
3
  import { Element } from "./Parser";
3
4
  interface Route {
4
5
  id: number;
5
6
  mask: string;
6
- requests: input[];
7
+ state: ServerStateMap;
7
8
  content: Element[];
8
9
  }
9
10
  export declare const getRoutesAndTemplates: (pages: PageData[], normalize?: boolean, templates?: boolean) => {
@@ -4,5 +4,6 @@ export interface DataProxy {
4
4
  getValue: () => any;
5
5
  }
6
6
  export declare const sources: Map<string, any>;
7
- declare const createDataProxy: (data: any, path?: string) => DataProxy;
7
+ type WithDataProxy<T> = T & DataProxy;
8
+ declare function createDataProxy<T>(data: T, path?: string): WithDataProxy<T>;
8
9
  export default createDataProxy;
@@ -13,92 +13,151 @@ const getTarget = (data, path) => {
13
13
  case "undefined":
14
14
  case "symbol":
15
15
  // эти типы можно просто завернуть
16
- return { value: data };
16
+ return { $__ROOT__: data };
17
17
  default:
18
18
  return () => `{{${path}}}`;
19
19
  }
20
20
  };
21
- const createDataProxy = (data, path = "") => {
21
+ function createDataProxy(data, path = "") {
22
22
  if (path.endsWith("@"))
23
23
  sources.set(path.slice(0, -1), data);
24
24
  return new Proxy(getTarget(data, path), {
25
- get: (_, prop) => {
26
- // if (typeof prop == "symbol") return data[prop]
27
- if (!(typeof data == "object" && data !== null && prop in data))
28
- // DEV TOOLS
25
+ get: (_target, prop) => {
26
+ if (ssr.exportToJV) {
27
+ return {
28
+ $JV: {
29
+ key: path + "/" + String(prop),
30
+ type: "proxy",
31
+ def: data,
32
+ },
33
+ };
34
+ }
35
+ // ---------------------------
36
+ // 1. Обработка символов
37
+ // ---------------------------
38
+ if (typeof prop === "symbol") {
29
39
  switch (prop) {
30
- // @ts-ignore
31
40
  case Symbol.toPrimitive:
32
41
  return (hint) => {
33
42
  console.log("PROXYDATA", hint);
34
43
  return `{{${path}}}`;
35
44
  };
36
- // @ts-ignore
37
45
  case Symbol.toStringTag:
38
46
  return "Object";
39
- // @ts-ignore
40
47
  case Symbol.iterator:
41
- return data[Symbol.iterator];
42
- case "$$typeof":
43
- case "type":
48
+ if (typeof data === "object" && data !== null && Symbol.iterator in data) {
49
+ return data[Symbol.iterator];
50
+ }
44
51
  return undefined;
45
- case "_debugInfo":
46
- return {
47
- note: `State From Request (${path})`,
48
- kind: typeof data,
49
- timestamp: Date.now(),
50
- preview: data,
51
- };
52
52
  }
53
- // SELF
54
- if (prop.startsWith("$"))
53
+ }
54
+ // ---------------------------
55
+ // 2. DEV tools
56
+ // ---------------------------
57
+ if (!(typeof data === "object" && data !== null && prop in data)) {
58
+ if (prop === "$$typeof" || prop === "type") {
59
+ return undefined;
60
+ }
61
+ if (prop === "_debugInfo") {
62
+ return {
63
+ note: `State From Request (${path})`,
64
+ kind: typeof data,
65
+ timestamp: Date.now(),
66
+ preview: data,
67
+ };
68
+ }
69
+ }
70
+ // ---------------------------
71
+ // 3. SELF: $key → returns function
72
+ // ---------------------------
73
+ if (typeof prop === "string" && prop.startsWith("$")) {
55
74
  return (key) => `{{${path + "/" + key}${"\\" + prop}}}`;
56
- // TYPES
75
+ }
76
+ // ---------------------------
77
+ // 4. Types special cases
78
+ // ---------------------------
57
79
  switch (typeof data) {
58
80
  case "string":
59
- switch (prop) {
60
- case "length":
61
- case "entries":
62
- return undefined;
63
- }
81
+ if (prop === "length" || prop === "entries")
82
+ return undefined;
83
+ break;
64
84
  case "number":
65
- switch (prop) {
66
- case "@@iterator":
67
- return undefined;
68
- }
85
+ if (prop === "@@iterator")
86
+ return undefined;
87
+ break;
69
88
  default:
70
- switch (prop) {
71
- case "then":
72
- return undefined;
73
- }
89
+ if (prop === "then")
90
+ return undefined;
74
91
  }
75
- // OTHER
76
- switch (prop) {
77
- case "find":
78
- return data[prop];
79
- case "map":
80
- case "forEach":
81
- return (callback) => React.createElement("loopwrapper", null, [
82
- `ArrayDataKey=${path}|`,
83
- Object.entries(data)
84
- .slice(0, 1)
85
- .map(([key, itm]) => callback(createDataProxy(itm, `${path}/[ITEM=${key}]`))),
86
- ]);
87
- case "getValue":
88
- return () => data;
89
- case "toJSON":
90
- return () => {
91
- console.log("dataproxy toJSON", path, data);
92
- return ssr.exportParams ? `{{${path}}}` : data;
93
- };
94
- default:
95
- if (data && (typeof data[prop] == "object" || Array.isArray(data[prop]))) {
96
- return createDataProxy(data[prop], path + "/" + prop);
97
- }
98
- return `{{${path + "/" + prop}}}`;
92
+ // ---------------------------
93
+ // 5. Array-like handlers
94
+ // ---------------------------
95
+ if (prop === "find") {
96
+ return data[prop];
97
+ }
98
+ if (prop === "length") {
99
+ if (Array.isArray(data)) {
100
+ return data.length;
101
+ }
102
+ }
103
+ if (prop === "map" || prop === "forEach") {
104
+ return (callback) => React.createElement("loopwrapper", null, [
105
+ `ArrayDataKey=${path}|`,
106
+ Object.entries(data)
107
+ .slice(0, 1)
108
+ .map(([key, itm]) => callback(createDataProxy(itm, `${path}/[ITEM=${key}]`))),
109
+ ]);
110
+ }
111
+ // ---------------------------
112
+ // 6. getValue
113
+ // ---------------------------
114
+ if (prop === "getValue") {
115
+ return () => data;
99
116
  }
117
+ // ---------------------------
118
+ // 7. toJSON
119
+ // ---------------------------
120
+ if (prop === "toJSON") {
121
+ return () => {
122
+ console.log("dataproxy toJSON", path, data);
123
+ return ssr.exportParams
124
+ ? `{{${path}}}`
125
+ : {
126
+ $JV: {
127
+ key: path,
128
+ type: "proxy",
129
+ def: data,
130
+ separator: "",
131
+ },
132
+ };
133
+ };
134
+ }
135
+ // ---------------------------
136
+ // 8. Nested object
137
+ // ---------------------------
138
+ const value = data[prop];
139
+ if (!ssr.exportParams) {
140
+ return value;
141
+ }
142
+ if (value && (typeof value === "object" || Array.isArray(value))) {
143
+ return createDataProxy(value, path + "/" + String(prop));
144
+ }
145
+ // if (ssr.exportParams) {
146
+ // if (typeof value == "string") {
147
+ // const key = `{{${path + "/" + String(prop)}}}`
148
+ // const jv = getJinraiValue(key, "proxyValue", "", value)
149
+ // return value.bindSource(jv)
150
+ // }
151
+ // return key
152
+ // } else {
153
+ // return value
154
+ // }
155
+ // ---------------------------
156
+ // 9. Final primitive fallback
157
+ // ---------------------------
158
+ return `{{${path + "/" + String(prop)}}}`;
100
159
  },
101
160
  });
102
- };
161
+ }
103
162
 
104
163
  export { createDataProxy as default, sources };
@@ -1,4 +1,6 @@
1
1
  export declare const ssr: {
2
2
  current: boolean;
3
3
  exportParams: boolean;
4
+ exportToJV: boolean;
4
5
  };
6
+ export declare const stringifyInput: (input: any) => string;
@@ -1,6 +1,19 @@
1
+ import { JinraiAgent } from '../../bin/agent/agent.js';
2
+
1
3
  const ssr = {
2
- current: navigator.userAgent == "____JINRAI_CLIENT____",
3
- exportParams: false,
4
+ current: navigator.userAgent == JinraiAgent,
5
+ exportParams: true,
6
+ exportToJV: false,
7
+ };
8
+ if (window != undefined) {
9
+ // @ts-ignore
10
+ window.__ssr = ssr;
11
+ }
12
+ const stringifyInput = (input) => {
13
+ ssr.exportParams = false;
14
+ const result = JSON.stringify(input);
15
+ ssr.exportParams = true;
16
+ return result;
4
17
  };
5
18
 
6
- export { ssr };
19
+ export { ssr, stringifyInput };
@@ -1,5 +1,6 @@
1
1
  import { ssr } from './SSR.js';
2
2
  import { sources } from './DataProxy.js';
3
+ import { getJinraiValue } from '../url/search/useSearchValue.js';
3
4
 
4
5
  function real(value) {
5
6
  if (!ssr.current)
@@ -8,7 +9,11 @@ function real(value) {
8
9
  case "number":
9
10
  return value;
10
11
  case "string":
11
- return value.startsWith("{{") && value.endsWith("}}") ? getArrayByPath(value.slice(2, -2)) : value;
12
+ if (value.startsWith("{{") && value.endsWith("}}")) {
13
+ const result = getArrayByPath(value.slice(2, -2));
14
+ return wrapSource(result, getJinraiValue(value, "proxy", "", result));
15
+ }
16
+ return value;
12
17
  case "object":
13
18
  // @ts-ignore
14
19
  if (value && typeof value.getValue === "function") {
@@ -20,6 +25,12 @@ function real(value) {
20
25
  return value;
21
26
  }
22
27
  }
28
+ const wrapSource = (value, source) => {
29
+ if (typeof value == "string") {
30
+ return value.bindSource(source);
31
+ }
32
+ return value;
33
+ };
23
34
  const getArrayByPath = (path) => {
24
35
  const [sourceIndex, requestPath] = path.split("@", 2);
25
36
  const keys = requestPath.split("/");
@@ -1,5 +1,19 @@
1
- import { ServerStateOptions } from "./useServerState";
2
- export type ServerValue = any;
3
- export declare const serverStates: Map<string, string>;
4
- export declare const getServerValue: (key: string, def?: ServerValue) => any[];
5
- export declare const setServerValue: (key: string, value: ServerValue, options?: ServerStateOptions) => import("./DataProxy").DataProxy;
1
+ import { ServerKey } from "./useServerState";
2
+ interface Request {
3
+ method: string;
4
+ url: string;
5
+ input: any;
6
+ }
7
+ export type ServerValue = {
8
+ value: any;
9
+ options?: {
10
+ source: {
11
+ request?: Request;
12
+ };
13
+ };
14
+ key: ServerKey;
15
+ };
16
+ export declare const serverStates: Map<string, ServerValue>;
17
+ export declare const getServerValue: (key?: ServerKey, def?: ServerValue["value"], options?: ServerValue["options"]) => any[];
18
+ export declare const setServerValue: (key: ServerKey, value: ServerValue["value"], options?: ServerValue["options"]) => any;
19
+ export {};
@@ -1,29 +1,43 @@
1
1
  import createDataProxy from './DataProxy.js';
2
- import { ssr } from './SSR.js';
3
2
 
3
+ // @ts-ignore
4
+ const initialState = { ...(window?.__appc__?.state ?? {}) };
5
+ // @ts-ignore
6
+ const serverErrors = [...(window?.__appc__?.errors ?? [])];
7
+ if (serverErrors.length) {
8
+ console.error("SERVER:", serverErrors);
9
+ }
10
+ if (window != undefined) {
11
+ // @ts-ignore
12
+ delete window.__appc__;
13
+ }
4
14
  const serverStates = new Map();
5
15
  if (window != undefined) {
16
+ console.log("init $exportServerStates");
6
17
  // @ts-ignore
7
- window.__serverExportStates__ = serverStates;
18
+ window.$exportServerStates = serverStates;
8
19
  }
9
- const getServerValue = (key, def) => {
10
- if (key == "") {
20
+ const getIdent = (key) => {
21
+ return Array.isArray(key) ? key.join("-") : key;
22
+ };
23
+ const getServerValue = (key, def, options) => {
24
+ if (key == undefined) {
11
25
  return [def, false];
12
26
  }
13
- // @ts-ignore
14
- if (window != undefined && window?.__serverInitialStates__ && key in window.__serverInitialStates__) {
15
- // @ts-ignore
16
- return [window.__serverInitialStates__[key], true];
27
+ const ident = getIdent(key);
28
+ serverStates.set(ident, { options, value: !options?.source ? def : undefined, key });
29
+ if (ident in initialState) {
30
+ const result = initialState[ident];
31
+ // delete initialState[ident]
32
+ console.log("HAS", ident, result);
33
+ return ["data" in result ? result.data : result, true];
17
34
  }
18
- return [serverStates.get(key) ?? def, false];
35
+ return [def, false];
19
36
  };
20
37
  const setServerValue = (key, value, options) => {
21
- ssr.exportParams = true;
22
- const json = JSON.stringify({ value, options });
23
- ssr.exportParams = false;
24
- console.log(key, json);
25
- serverStates.set(key, json);
26
- return createDataProxy(value, `${key}@`);
38
+ const ident = getIdent(key);
39
+ serverStates.set(ident, { options, value: !options?.source ? value : undefined, key });
40
+ return createDataProxy(value, `${ident}@`);
27
41
  };
28
42
 
29
43
  export { getServerValue, serverStates, setServerValue };
@@ -1,14 +1,5 @@
1
1
  import { Dispatch, SetStateAction } from "react";
2
2
  import { ServerValue } from "./serverStates";
3
- interface Request {
4
- method: string;
5
- url: string;
6
- input: any;
7
- }
8
- export interface ServerStateOptions {
9
- source: {
10
- request?: Request;
11
- };
12
- }
13
- export declare const useServerState: <T extends ServerValue>(serverKey: string, initialValue: T, options?: ServerStateOptions) => [T, Dispatch<SetStateAction<T>>, () => boolean];
14
- export {};
3
+ export type ServerStateMap = Record<string, ServerValue>;
4
+ export type ServerKey = string | string[];
5
+ export declare const useServerState: <T extends ServerValue["value"]>(serverKey: ServerKey | undefined, initialValue: T, options?: ServerValue["options"]) => [T, Dispatch<SetStateAction<T>>, boolean];
@@ -1,28 +1,28 @@
1
- import { useState } from 'react';
1
+ import { useState, useRef } from 'react';
2
2
  import { ssr } from './SSR.js';
3
3
  import { getServerValue, setServerValue } from './serverStates.js';
4
4
 
5
5
  const useServerState = (serverKey, initialValue, options) => {
6
- const [serverValue, isInitOnServer] = getServerValue(serverKey, initialValue);
6
+ const [serverValue, isInitOnServer] = getServerValue(serverKey, initialValue, options);
7
7
  const [value, setStateValue] = useState(serverValue);
8
- const [isInit, setIsInit] = useState(isInitOnServer);
8
+ useRef(isInitOnServer);
9
9
  const setValue = (value) => {
10
10
  setStateValue((prev) => {
11
11
  const result = value instanceof Function ? value(prev) : value;
12
- if (serverKey && ssr.current) {
12
+ if (serverKey != undefined && ssr.current) {
13
13
  return setServerValue(serverKey, result, options);
14
14
  }
15
15
  return result;
16
16
  });
17
17
  };
18
- const initOnServer = () => {
19
- if (isInit) {
20
- setIsInit(false);
21
- return true;
22
- }
23
- return false;
24
- };
25
- return [value, setValue, initOnServer];
18
+ // const initOnServer = () => {
19
+ // if (isInit.current) {
20
+ // isInit.current = false
21
+ // return true
22
+ // }
23
+ // return false
24
+ // }
25
+ return [value, setValue, isInitOnServer];
26
26
  };
27
27
 
28
28
  export { useServerState };
@@ -7,7 +7,7 @@ const useSearch = () => {
7
7
  const { deps } = useContext(JinraiContext);
8
8
  const value = useMemo(() => location.search.substring(1), deps ? deps : []);
9
9
  const stableValue = useMemo(() => {
10
- return ssr.current ? value.bindSource(getJinraiValue("", "search", "", "")) : value;
10
+ return ssr.current ? value.bindSource(getJinraiValue("", "searchFull", "", "")) : value;
11
11
  }, [value]);
12
12
  return stableValue;
13
13
  };
@@ -1,16 +1,22 @@
1
1
  import { UseQueryStateReturn } from "nuqs";
2
+ export interface JinraiValue {
3
+ key: string;
4
+ type: "searchArray" | "searchString" | "proxy" | "searchFull" | "paramsIndex";
5
+ separator: string;
6
+ def: any;
7
+ }
2
8
  declare global {
3
9
  interface String {
4
- source?: string;
10
+ $JV?: JinraiValue;
5
11
  toJSON: () => string;
6
- bindSource: (source: string) => string;
12
+ bindSource: (source: JinraiValue) => string;
7
13
  }
8
14
  interface Array<T> {
9
- source?: string;
15
+ $JV?: JinraiValue;
10
16
  toJSON(): any;
11
- bindSource(source: string): T[];
17
+ bindSource(source: JinraiValue): T[];
12
18
  }
13
19
  }
14
20
  export declare const useSearchValue: (key: string, defaultValue: string) => UseQueryStateReturn<string, string>;
15
21
  export declare const useSearchArray: (key: string, defaultValue?: string[], separator?: string) => UseQueryStateReturn<string[], string[]>;
16
- export declare const getJinraiValue: (key: string, type: string, separator: string, def: any) => string;
22
+ export declare const getJinraiValue: (key: string, type: JinraiValue["type"], separator: string, def: any) => JinraiValue;