@mearie/react 0.1.0 → 0.2.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.
package/dist/index.cjs CHANGED
@@ -1,34 +1,8 @@
1
- //#region rolldown:runtime
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __copyProps = (to, from, except, desc) => {
9
- if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
- key = keys[i];
11
- if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
- get: ((k) => from[k]).bind(null, key),
13
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
- });
15
- }
16
- return to;
17
- };
18
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
- value: mod,
20
- enumerable: true
21
- }) : target, mod));
22
-
23
- //#endregion
24
- let __mearie_core = require("@mearie/core");
25
- __mearie_core = __toESM(__mearie_core);
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ let _mearie_core = require("@mearie/core");
26
3
  let react = require("react");
27
- react = __toESM(react);
28
4
  let react_jsx_runtime = require("react/jsx-runtime");
29
- react_jsx_runtime = __toESM(react_jsx_runtime);
30
- let __mearie_core_stream = require("@mearie/core/stream");
31
- __mearie_core_stream = __toESM(__mearie_core_stream);
5
+ let _mearie_core_stream = require("@mearie/core/stream");
32
6
 
33
7
  //#region src/client-provider.tsx
34
8
  const ClientContext = (0, react.createContext)(null);
@@ -46,22 +20,24 @@ const useClient = () => {
46
20
 
47
21
  //#endregion
48
22
  //#region src/use-query.ts
49
- const useQuery = (query, ...[variables, options]) => {
23
+ const useQuery = ((query, variables, options) => {
50
24
  const client = useClient();
51
- const [data, setData] = (0, react.useState)();
52
- const [loading, setLoading] = (0, react.useState)(!options?.skip);
25
+ const [data, setData] = (0, react.useState)(options?.initialData);
26
+ const [loading, setLoading] = (0, react.useState)(!options?.skip && !options?.initialData);
53
27
  const [error, setError] = (0, react.useState)();
54
28
  const unsubscribe = (0, react.useRef)(null);
55
- const stableVariables = (0, react.useMemo)(() => (0, __mearie_core.stringify)(variables), [variables]);
29
+ const initialized = (0, react.useRef)(false);
30
+ const stableVariables = (0, react.useMemo)(() => (0, _mearie_core.stringify)(variables), [variables]);
56
31
  const stableOptions = (0, react.useMemo)(() => options, [options?.skip]);
57
32
  const execute = (0, react.useCallback)(() => {
58
33
  unsubscribe.current?.();
59
34
  if (stableOptions?.skip) return;
60
- setLoading(true);
35
+ if (!initialized.current && options?.initialData) setLoading(true);
36
+ initialized.current = true;
61
37
  setError(void 0);
62
- unsubscribe.current = (0, __mearie_core_stream.pipe)(client.executeQuery(query, variables, stableOptions), (0, __mearie_core_stream.subscribe)({ next: (result) => {
38
+ unsubscribe.current = (0, _mearie_core_stream.pipe)(client.executeQuery(query, variables, stableOptions), (0, _mearie_core_stream.subscribe)({ next: (result) => {
63
39
  if (result.errors && result.errors.length > 0) {
64
- setError(new __mearie_core.AggregatedError(result.errors));
40
+ setError(new _mearie_core.AggregatedError(result.errors));
65
41
  setLoading(false);
66
42
  } else {
67
43
  setData(result.data);
@@ -85,7 +61,7 @@ const useQuery = (query, ...[variables, options]) => {
85
61
  error,
86
62
  refetch: execute
87
63
  };
88
- };
64
+ });
89
65
 
90
66
  //#endregion
91
67
  //#region src/use-subscription.ts
@@ -95,7 +71,7 @@ const useSubscription = (subscription, ...[variables, options]) => {
95
71
  const [loading, setLoading] = (0, react.useState)(!options?.skip);
96
72
  const [error, setError] = (0, react.useState)();
97
73
  const unsubscribe = (0, react.useRef)(null);
98
- const stableVariables = (0, react.useMemo)(() => (0, __mearie_core.stringify)(variables), [variables]);
74
+ const stableVariables = (0, react.useMemo)(() => (0, _mearie_core.stringify)(variables), [variables]);
99
75
  const stableOptions = (0, react.useMemo)(() => options, [
100
76
  options?.skip,
101
77
  options?.onData,
@@ -106,9 +82,9 @@ const useSubscription = (subscription, ...[variables, options]) => {
106
82
  if (stableOptions?.skip) return;
107
83
  setLoading(true);
108
84
  setError(void 0);
109
- unsubscribe.current = (0, __mearie_core_stream.pipe)(client.executeSubscription(subscription, variables, stableOptions), (0, __mearie_core_stream.subscribe)({ next: (result) => {
85
+ unsubscribe.current = (0, _mearie_core_stream.pipe)(client.executeSubscription(subscription, variables, stableOptions), (0, _mearie_core_stream.subscribe)({ next: (result) => {
110
86
  if (result.errors && result.errors.length > 0) {
111
- const err = new __mearie_core.AggregatedError(result.errors);
87
+ const err = new _mearie_core.AggregatedError(result.errors);
112
88
  setError(err);
113
89
  setLoading(false);
114
90
  stableOptions?.onError?.(err);
@@ -148,9 +124,9 @@ const useMutation = (mutation) => {
148
124
  setLoading(true);
149
125
  setError(void 0);
150
126
  try {
151
- const result = await (0, __mearie_core_stream.pipe)(client.executeMutation(mutation, variables, options), __mearie_core_stream.collect);
127
+ const result = await (0, _mearie_core_stream.pipe)(client.executeMutation(mutation, variables, options), (0, _mearie_core_stream.take)(1), _mearie_core_stream.collect);
152
128
  if (result.errors && result.errors.length > 0) {
153
- const err = new __mearie_core.AggregatedError(result.errors);
129
+ const err = new _mearie_core.AggregatedError(result.errors);
154
130
  setError(err);
155
131
  setLoading(false);
156
132
  throw err;
@@ -159,7 +135,7 @@ const useMutation = (mutation) => {
159
135
  setLoading(false);
160
136
  return result.data;
161
137
  } catch (err) {
162
- if (err instanceof __mearie_core.AggregatedError) setError(err);
138
+ if (err instanceof _mearie_core.AggregatedError) setError(err);
163
139
  setLoading(false);
164
140
  throw err;
165
141
  }
@@ -172,12 +148,16 @@ const useMutation = (mutation) => {
172
148
 
173
149
  //#endregion
174
150
  //#region src/use-fragment.ts
175
- const useFragment = (fragment, fragmentRef, options) => {
151
+ const useFragment = ((fragment, fragmentRef, options) => {
176
152
  const client = useClient();
177
153
  const dataRef = (0, react.useRef)(void 0);
178
154
  const subscribe_ = (0, react.useCallback)((onChange) => {
179
- return (0, __mearie_core_stream.pipe)(client.executeFragment(fragment, fragmentRef, options), (0, __mearie_core_stream.subscribe)({ next: (result) => {
180
- if (result.errors && result.errors.length > 0) throw new __mearie_core.AggregatedError(result.errors);
155
+ if (fragmentRef == null) {
156
+ dataRef.current = null;
157
+ return () => {};
158
+ }
159
+ return (0, _mearie_core_stream.pipe)(client.executeFragment(fragment, fragmentRef, options), (0, _mearie_core_stream.subscribe)({ next: (result) => {
160
+ if (result.errors && result.errors.length > 0) throw new _mearie_core.AggregatedError(result.errors);
181
161
  dataRef.current = result.data;
182
162
  onChange();
183
163
  } }));
@@ -188,9 +168,10 @@ const useFragment = (fragment, fragmentRef, options) => {
188
168
  options
189
169
  ]);
190
170
  const snapshot = (0, react.useCallback)(() => {
171
+ if (fragmentRef == null) return null;
191
172
  if (dataRef.current === void 0) {
192
- const result = (0, __mearie_core_stream.pipe)(client.executeFragment(fragment, fragmentRef, options), __mearie_core_stream.peek);
193
- if (result.errors && result.errors.length > 0) throw new __mearie_core.AggregatedError(result.errors);
173
+ const result = (0, _mearie_core_stream.pipe)(client.executeFragment(fragment, fragmentRef, options), _mearie_core_stream.peek);
174
+ if (result.errors && result.errors.length > 0) throw new _mearie_core.AggregatedError(result.errors);
194
175
  dataRef.current = result.data;
195
176
  }
196
177
  return dataRef.current;
@@ -204,7 +185,7 @@ const useFragment = (fragment, fragmentRef, options) => {
204
185
  return { get data() {
205
186
  return data;
206
187
  } };
207
- };
188
+ });
208
189
 
209
190
  //#endregion
210
191
  exports.ClientProvider = ClientProvider;
@@ -213,9 +194,9 @@ exports.useFragment = useFragment;
213
194
  exports.useMutation = useMutation;
214
195
  exports.useQuery = useQuery;
215
196
  exports.useSubscription = useSubscription;
216
- Object.keys(__mearie_core).forEach(function (k) {
197
+ Object.keys(_mearie_core).forEach(function (k) {
217
198
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
218
199
  enumerable: true,
219
- get: function () { return __mearie_core[k]; }
200
+ get: function () { return _mearie_core[k]; }
220
201
  });
221
202
  });
package/dist/index.d.cts CHANGED
@@ -14,6 +14,9 @@ declare const ClientProvider: <TMeta extends SchemaMeta = SchemaMeta>({
14
14
  declare const useClient: <TMeta extends SchemaMeta = SchemaMeta>() => Client<TMeta>;
15
15
  //#endregion
16
16
  //#region src/use-query.d.ts
17
+ type UseQueryOptions<T extends Artifact<'query'> = Artifact<'query'>> = QueryOptions<T> & {
18
+ skip?: boolean;
19
+ };
17
20
  type Query<T extends Artifact<'query'>> = {
18
21
  data: undefined;
19
22
  loading: true;
@@ -30,10 +33,29 @@ type Query<T extends Artifact<'query'>> = {
30
33
  error: AggregatedError;
31
34
  refetch: () => void;
32
35
  };
33
- type UseQueryOptions = QueryOptions & {
34
- skip?: boolean;
36
+ type DefinedQuery<T extends Artifact<'query'>> = {
37
+ data: DataOf<T>;
38
+ loading: true;
39
+ error: undefined;
40
+ refetch: () => void;
41
+ } | {
42
+ data: DataOf<T>;
43
+ loading: false;
44
+ error: undefined;
45
+ refetch: () => void;
46
+ } | {
47
+ data: DataOf<T>;
48
+ loading: false;
49
+ error: AggregatedError;
50
+ refetch: () => void;
51
+ };
52
+ type UseQueryFn = {
53
+ <T extends Artifact<'query'>>(query: T, variables: VariablesOf<T> | undefined, options: UseQueryOptions<T> & {
54
+ initialData: DataOf<T>;
55
+ }): DefinedQuery<T>;
56
+ <T extends Artifact<'query'>>(query: T, ...[variables, options]: VariablesOf<T> extends Record<string, never> ? [undefined?, UseQueryOptions<T>?] : [VariablesOf<T>, UseQueryOptions<T>?]): Query<T>;
35
57
  };
36
- declare const useQuery: <T extends Artifact<"query">>(query: T, ...[variables, options]: VariablesOf<T> extends Record<string, never> ? [undefined?, UseQueryOptions?] : [VariablesOf<T>, UseQueryOptions?]) => Query<T>;
58
+ declare const useQuery: UseQueryFn;
37
59
  //#endregion
38
60
  //#region src/use-subscription.d.ts
39
61
  type Subscription<T extends Artifact<'subscription'>> = {
@@ -79,6 +101,17 @@ type UseFragmentOptions = FragmentOptions;
79
101
  type Fragment<T extends Artifact<'fragment'>> = {
80
102
  data: DataOf<T>;
81
103
  };
82
- declare const useFragment: <T extends Artifact<"fragment">>(fragment: T, fragmentRef: FragmentRefs<T["name"]>, options?: UseFragmentOptions) => Fragment<T>;
104
+ type FragmentList<T extends Artifact<'fragment'>> = {
105
+ data: DataOf<T>[];
106
+ };
107
+ type OptionalFragment<T extends Artifact<'fragment'>> = {
108
+ data: DataOf<T> | null;
109
+ };
110
+ type UseFragmentFn = {
111
+ <T extends Artifact<'fragment'>>(fragment: T, fragmentRef: FragmentRefs<T['name']>[], options?: UseFragmentOptions): FragmentList<T>;
112
+ <T extends Artifact<'fragment'>>(fragment: T, fragmentRef: FragmentRefs<T['name']>, options?: UseFragmentOptions): Fragment<T>;
113
+ <T extends Artifact<'fragment'>>(fragment: T, fragmentRef: FragmentRefs<T['name']> | null | undefined, options?: UseFragmentOptions): OptionalFragment<T>;
114
+ };
115
+ declare const useFragment: UseFragmentFn;
83
116
  //#endregion
84
- export { ClientProvider, type Fragment, type Mutation, type Query, type Subscription, type UseFragmentOptions, type UseMutationOptions, type UseQueryOptions, type UseSubscriptionOptions, useClient, useFragment, useMutation, useQuery, useSubscription };
117
+ export { ClientProvider, type DefinedQuery, type Fragment, type FragmentList, type Mutation, type OptionalFragment, type Query, type Subscription, type UseFragmentOptions, type UseMutationOptions, type UseQueryOptions, type UseSubscriptionOptions, useClient, useFragment, useMutation, useQuery, useSubscription };
@@ -14,6 +14,9 @@ declare const ClientProvider: <TMeta extends SchemaMeta = SchemaMeta>({
14
14
  declare const useClient: <TMeta extends SchemaMeta = SchemaMeta>() => Client<TMeta>;
15
15
  //#endregion
16
16
  //#region src/use-query.d.ts
17
+ type UseQueryOptions<T extends Artifact<'query'> = Artifact<'query'>> = QueryOptions<T> & {
18
+ skip?: boolean;
19
+ };
17
20
  type Query<T extends Artifact<'query'>> = {
18
21
  data: undefined;
19
22
  loading: true;
@@ -30,10 +33,29 @@ type Query<T extends Artifact<'query'>> = {
30
33
  error: AggregatedError;
31
34
  refetch: () => void;
32
35
  };
33
- type UseQueryOptions = QueryOptions & {
34
- skip?: boolean;
36
+ type DefinedQuery<T extends Artifact<'query'>> = {
37
+ data: DataOf<T>;
38
+ loading: true;
39
+ error: undefined;
40
+ refetch: () => void;
41
+ } | {
42
+ data: DataOf<T>;
43
+ loading: false;
44
+ error: undefined;
45
+ refetch: () => void;
46
+ } | {
47
+ data: DataOf<T>;
48
+ loading: false;
49
+ error: AggregatedError;
50
+ refetch: () => void;
51
+ };
52
+ type UseQueryFn = {
53
+ <T extends Artifact<'query'>>(query: T, variables: VariablesOf<T> | undefined, options: UseQueryOptions<T> & {
54
+ initialData: DataOf<T>;
55
+ }): DefinedQuery<T>;
56
+ <T extends Artifact<'query'>>(query: T, ...[variables, options]: VariablesOf<T> extends Record<string, never> ? [undefined?, UseQueryOptions<T>?] : [VariablesOf<T>, UseQueryOptions<T>?]): Query<T>;
35
57
  };
36
- declare const useQuery: <T extends Artifact<"query">>(query: T, ...[variables, options]: VariablesOf<T> extends Record<string, never> ? [undefined?, UseQueryOptions?] : [VariablesOf<T>, UseQueryOptions?]) => Query<T>;
58
+ declare const useQuery: UseQueryFn;
37
59
  //#endregion
38
60
  //#region src/use-subscription.d.ts
39
61
  type Subscription<T extends Artifact<'subscription'>> = {
@@ -79,6 +101,17 @@ type UseFragmentOptions = FragmentOptions;
79
101
  type Fragment<T extends Artifact<'fragment'>> = {
80
102
  data: DataOf<T>;
81
103
  };
82
- declare const useFragment: <T extends Artifact<"fragment">>(fragment: T, fragmentRef: FragmentRefs<T["name"]>, options?: UseFragmentOptions) => Fragment<T>;
104
+ type FragmentList<T extends Artifact<'fragment'>> = {
105
+ data: DataOf<T>[];
106
+ };
107
+ type OptionalFragment<T extends Artifact<'fragment'>> = {
108
+ data: DataOf<T> | null;
109
+ };
110
+ type UseFragmentFn = {
111
+ <T extends Artifact<'fragment'>>(fragment: T, fragmentRef: FragmentRefs<T['name']>[], options?: UseFragmentOptions): FragmentList<T>;
112
+ <T extends Artifact<'fragment'>>(fragment: T, fragmentRef: FragmentRefs<T['name']>, options?: UseFragmentOptions): Fragment<T>;
113
+ <T extends Artifact<'fragment'>>(fragment: T, fragmentRef: FragmentRefs<T['name']> | null | undefined, options?: UseFragmentOptions): OptionalFragment<T>;
114
+ };
115
+ declare const useFragment: UseFragmentFn;
83
116
  //#endregion
84
- export { ClientProvider, type Fragment, type Mutation, type Query, type Subscription, type UseFragmentOptions, type UseMutationOptions, type UseQueryOptions, type UseSubscriptionOptions, useClient, useFragment, useMutation, useQuery, useSubscription };
117
+ export { ClientProvider, type DefinedQuery, type Fragment, type FragmentList, type Mutation, type OptionalFragment, type Query, type Subscription, type UseFragmentOptions, type UseMutationOptions, type UseQueryOptions, type UseSubscriptionOptions, useClient, useFragment, useMutation, useQuery, useSubscription };
@@ -1,7 +1,7 @@
1
1
  import { AggregatedError, stringify } from "@mearie/core";
2
2
  import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
3
3
  import { jsx } from "react/jsx-runtime";
4
- import { collect, peek, pipe, subscribe } from "@mearie/core/stream";
4
+ import { collect, peek, pipe, subscribe, take } from "@mearie/core/stream";
5
5
 
6
6
  export * from "@mearie/core"
7
7
 
@@ -21,18 +21,20 @@ const useClient = () => {
21
21
 
22
22
  //#endregion
23
23
  //#region src/use-query.ts
24
- const useQuery = (query, ...[variables, options]) => {
24
+ const useQuery = ((query, variables, options) => {
25
25
  const client = useClient();
26
- const [data, setData] = useState();
27
- const [loading, setLoading] = useState(!options?.skip);
26
+ const [data, setData] = useState(options?.initialData);
27
+ const [loading, setLoading] = useState(!options?.skip && !options?.initialData);
28
28
  const [error, setError] = useState();
29
29
  const unsubscribe = useRef(null);
30
+ const initialized = useRef(false);
30
31
  const stableVariables = useMemo(() => stringify(variables), [variables]);
31
32
  const stableOptions = useMemo(() => options, [options?.skip]);
32
33
  const execute = useCallback(() => {
33
34
  unsubscribe.current?.();
34
35
  if (stableOptions?.skip) return;
35
- setLoading(true);
36
+ if (!initialized.current && options?.initialData) setLoading(true);
37
+ initialized.current = true;
36
38
  setError(void 0);
37
39
  unsubscribe.current = pipe(client.executeQuery(query, variables, stableOptions), subscribe({ next: (result) => {
38
40
  if (result.errors && result.errors.length > 0) {
@@ -60,7 +62,7 @@ const useQuery = (query, ...[variables, options]) => {
60
62
  error,
61
63
  refetch: execute
62
64
  };
63
- };
65
+ });
64
66
 
65
67
  //#endregion
66
68
  //#region src/use-subscription.ts
@@ -123,7 +125,7 @@ const useMutation = (mutation) => {
123
125
  setLoading(true);
124
126
  setError(void 0);
125
127
  try {
126
- const result = await pipe(client.executeMutation(mutation, variables, options), collect);
128
+ const result = await pipe(client.executeMutation(mutation, variables, options), take(1), collect);
127
129
  if (result.errors && result.errors.length > 0) {
128
130
  const err = new AggregatedError(result.errors);
129
131
  setError(err);
@@ -147,10 +149,14 @@ const useMutation = (mutation) => {
147
149
 
148
150
  //#endregion
149
151
  //#region src/use-fragment.ts
150
- const useFragment = (fragment, fragmentRef, options) => {
152
+ const useFragment = ((fragment, fragmentRef, options) => {
151
153
  const client = useClient();
152
154
  const dataRef = useRef(void 0);
153
155
  const subscribe_ = useCallback((onChange) => {
156
+ if (fragmentRef == null) {
157
+ dataRef.current = null;
158
+ return () => {};
159
+ }
154
160
  return pipe(client.executeFragment(fragment, fragmentRef, options), subscribe({ next: (result) => {
155
161
  if (result.errors && result.errors.length > 0) throw new AggregatedError(result.errors);
156
162
  dataRef.current = result.data;
@@ -163,6 +169,7 @@ const useFragment = (fragment, fragmentRef, options) => {
163
169
  options
164
170
  ]);
165
171
  const snapshot = useCallback(() => {
172
+ if (fragmentRef == null) return null;
166
173
  if (dataRef.current === void 0) {
167
174
  const result = pipe(client.executeFragment(fragment, fragmentRef, options), peek);
168
175
  if (result.errors && result.errors.length > 0) throw new AggregatedError(result.errors);
@@ -179,7 +186,7 @@ const useFragment = (fragment, fragmentRef, options) => {
179
186
  return { get data() {
180
187
  return data;
181
188
  } };
182
- };
189
+ });
183
190
 
184
191
  //#endregion
185
192
  export { ClientProvider, useClient, useFragment, useMutation, useQuery, useSubscription };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mearie/react",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Type-safe, zero-overhead GraphQL client",
5
5
  "keywords": [
6
6
  "graphql",
@@ -40,12 +40,12 @@
40
40
  "README.md"
41
41
  ],
42
42
  "dependencies": {
43
- "@mearie/core": "0.1.0"
43
+ "@mearie/core": "0.2.0"
44
44
  },
45
45
  "devDependencies": {
46
- "@types/react": "^19.2.2",
47
- "react": "^19.2.0",
48
- "tsdown": "^0.15.12",
46
+ "@types/react": "^19.2.14",
47
+ "react": "^19.2.4",
48
+ "tsdown": "^0.20.3",
49
49
  "typescript": "^5.9.3"
50
50
  },
51
51
  "peerDependencies": {