naystack 1.8.2 → 1.8.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.
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ "use client";
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/graphql/clear-store.tsx
22
+ var clear_store_exports = {};
23
+ __export(clear_store_exports, {
24
+ ClearStoreOnLogout: () => ClearStoreOnLogout
25
+ });
26
+ module.exports = __toCommonJS(clear_store_exports);
27
+ var import_client = require("@apollo/client");
28
+ var import_client2 = require("naystack/auth/client");
29
+ var import_react = require("react");
30
+ function ClearStoreOnLogout() {
31
+ const client = (0, import_client.useApolloClient)();
32
+ const token = (0, import_client2.useToken)();
33
+ const hadToken = (0, import_react.useRef)(false);
34
+ (0, import_react.useEffect)(() => {
35
+ if (token) {
36
+ hadToken.current = true;
37
+ } else if (token === null && hadToken.current) {
38
+ hadToken.current = false;
39
+ void client.clearStore();
40
+ }
41
+ }, [client, token]);
42
+ return null;
43
+ }
44
+ // Annotate the CommonJS export names for ESM import in node:
45
+ 0 && (module.exports = {
46
+ ClearStoreOnLogout
47
+ });
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Clears the Apollo cache on logout so a subsequent login can't read the
3
+ * previous user's cached data. Mount inside an `ApolloProvider` — both the
4
+ * native (`ApolloWrapper` in `graphql/client`) and Next (`graphql/next`)
5
+ * wrappers render it, so any query that opts into a cache-reading `fetchPolicy`
6
+ * is safe across account switches without per-call-site cleanup.
7
+ *
8
+ * Keyed on the token transitioning to `null` (logout / session loss). Same-user
9
+ * access-token refreshes go string→string and never pass through `null`, so the
10
+ * cache survives them; only a real logout empties the store. Renders nothing.
11
+ *
12
+ * @category GraphQL
13
+ */
14
+ declare function ClearStoreOnLogout(): null;
15
+
16
+ export { ClearStoreOnLogout };
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Clears the Apollo cache on logout so a subsequent login can't read the
3
+ * previous user's cached data. Mount inside an `ApolloProvider` — both the
4
+ * native (`ApolloWrapper` in `graphql/client`) and Next (`graphql/next`)
5
+ * wrappers render it, so any query that opts into a cache-reading `fetchPolicy`
6
+ * is safe across account switches without per-call-site cleanup.
7
+ *
8
+ * Keyed on the token transitioning to `null` (logout / session loss). Same-user
9
+ * access-token refreshes go string→string and never pass through `null`, so the
10
+ * cache survives them; only a real logout empties the store. Renders nothing.
11
+ *
12
+ * @category GraphQL
13
+ */
14
+ declare function ClearStoreOnLogout(): null;
15
+
16
+ export { ClearStoreOnLogout };
@@ -0,0 +1,23 @@
1
+ "use client";
2
+
3
+ // src/graphql/clear-store.tsx
4
+ import { useApolloClient } from "@apollo/client";
5
+ import { useToken } from "naystack/auth/client";
6
+ import { useEffect, useRef } from "react";
7
+ function ClearStoreOnLogout() {
8
+ const client = useApolloClient();
9
+ const token = useToken();
10
+ const hadToken = useRef(false);
11
+ useEffect(() => {
12
+ if (token) {
13
+ hadToken.current = true;
14
+ } else if (token === null && hadToken.current) {
15
+ hadToken.current = false;
16
+ void client.clearStore();
17
+ }
18
+ }, [client, token]);
19
+ return null;
20
+ }
21
+ export {
22
+ ClearStoreOnLogout
23
+ };
@@ -37,9 +37,9 @@ __export(client_exports, {
37
37
  useAuthQuery: () => useAuthQuery
38
38
  });
39
39
  module.exports = __toCommonJS(client_exports);
40
- var import_client = require("@apollo/client");
41
- var import_client2 = require("naystack/auth/client");
42
- var import_react = __toESM(require("react"));
40
+ var import_client3 = require("@apollo/client");
41
+ var import_client4 = require("naystack/auth/client");
42
+ var import_react2 = __toESM(require("react"));
43
43
 
44
44
  // src/env.ts
45
45
  var EXTRA_ENV = globalThis.__NAYSTACK_ENV__ || (globalThis.__NAYSTACK_ENV__ = {});
@@ -91,11 +91,30 @@ function getEnv(key, skipCheck) {
91
91
  return value;
92
92
  }
93
93
 
94
+ // src/graphql/clear-store.tsx
95
+ var import_client = require("@apollo/client");
96
+ var import_client2 = require("naystack/auth/client");
97
+ var import_react = require("react");
98
+ function ClearStoreOnLogout() {
99
+ const client = (0, import_client.useApolloClient)();
100
+ const token = (0, import_client2.useToken)();
101
+ const hadToken = (0, import_react.useRef)(false);
102
+ (0, import_react.useEffect)(() => {
103
+ if (token) {
104
+ hadToken.current = true;
105
+ } else if (token === null && hadToken.current) {
106
+ hadToken.current = false;
107
+ void client.clearStore();
108
+ }
109
+ }, [client, token]);
110
+ return null;
111
+ }
112
+
94
113
  // src/graphql/client.tsx
95
114
  function makeClient(cacheConfig) {
96
- return new import_client.ApolloClient({
97
- cache: new import_client.InMemoryCache(cacheConfig),
98
- link: new import_client.HttpLink({
115
+ return new import_client3.ApolloClient({
116
+ cache: new import_client3.InMemoryCache(cacheConfig),
117
+ link: new import_client3.HttpLink({
99
118
  uri: getEnv("NEXT_PUBLIC_GRAPHQL_ENDPOINT" /* NEXT_PUBLIC_GRAPHQL_ENDPOINT */)
100
119
  })
101
120
  });
@@ -104,7 +123,9 @@ var ApolloWrapper = ({
104
123
  children,
105
124
  cacheConfig
106
125
  }) => {
107
- return /* @__PURE__ */ import_react.default.createElement(import_client.ApolloProvider, { client: makeClient(cacheConfig) }, children);
126
+ const clientRef = (0, import_react2.useRef)(null);
127
+ if (!clientRef.current) clientRef.current = makeClient(cacheConfig);
128
+ return /* @__PURE__ */ import_react2.default.createElement(import_client3.ApolloProvider, { client: clientRef.current }, /* @__PURE__ */ import_react2.default.createElement(ClearStoreOnLogout, null), children);
108
129
  };
109
130
  var tokenContext = (token) => {
110
131
  if (!token) return void 0;
@@ -116,12 +137,12 @@ var tokenContext = (token) => {
116
137
  };
117
138
  };
118
139
  function useAuthQuery(query, variables, options) {
119
- const [fetch, result] = (0, import_client.useLazyQuery)(query, {
140
+ const [fetch, result] = (0, import_client3.useLazyQuery)(query, {
120
141
  fetchPolicy: "no-cache",
121
142
  ...options
122
143
  });
123
- const prevVarsRef = (0, import_react.useRef)(null);
124
- (0, import_react.useEffect)(() => {
144
+ const prevVarsRef = (0, import_react2.useRef)(null);
145
+ (0, import_react2.useEffect)(() => {
125
146
  const serialized = JSON.stringify(variables);
126
147
  if (variables && prevVarsRef.current !== serialized) {
127
148
  prevVarsRef.current = serialized;
@@ -134,7 +155,7 @@ function useAuthQuery(query, variables, options) {
134
155
  });
135
156
  }
136
157
  }, [fetch, variables]);
137
- const reFetch = (0, import_react.useCallback)(
158
+ const reFetch = (0, import_react2.useCallback)(
138
159
  (input) => fetch({
139
160
  // @ts-expect-error -- to allow dynamic props
140
161
  variables: { input },
@@ -147,9 +168,9 @@ function useAuthQuery(query, variables, options) {
147
168
  return [reFetch, result];
148
169
  }
149
170
  function useAuthMutation(mutation, options) {
150
- const token = (0, import_client2.useToken)();
151
- const [mutate, result] = (0, import_client.useMutation)(mutation, options);
152
- const method = (0, import_react.useCallback)(
171
+ const token = (0, import_client4.useToken)();
172
+ const [mutate, result] = (0, import_client3.useMutation)(mutation, options);
173
+ const method = (0, import_react2.useCallback)(
153
174
  (input) => mutate({
154
175
  // @ts-expect-error -- to allow dynamic props
155
176
  variables: { input },
@@ -25,10 +25,10 @@ declare const tokenContext: (token?: string | null) => {
25
25
  * Hook to run a GraphQL query with the current user's token. The query auto-fires when both the token
26
26
  * and variables are available. Returns a refetch function and the Apollo query result.
27
27
  *
28
- * Uses Apollo's default `fetchPolicy` (`cache-first`) unless you override it via
29
- * `options`. Pass `{ fetchPolicy: "no-cache" }` for always-fresh, viewer-keyed data
30
- * (e.g. the current user), or `{ fetchPolicy: "cache-and-network" }` to show cached
31
- * data instantly while refetching.
28
+ * Defaults to `fetchPolicy: "no-cache"` (always fresh, safe for viewer-keyed data).
29
+ * Override per-query via `options` e.g. `{ fetchPolicy: "cache-first" }` for static
30
+ * reference data, or `{ fetchPolicy: "cache-and-network" }` to show cached data
31
+ * instantly while refetching.
32
32
  *
33
33
  * @param query - A `TypedDocumentNode` for the query (e.g. from codegen or a `gql` template).
34
34
  * @param variables - Optional initial variables (the `input` value). Automatically wrapped as `{ input: variables }` before sending. Query fires automatically when this and token are set; change to refetch.
@@ -25,10 +25,10 @@ declare const tokenContext: (token?: string | null) => {
25
25
  * Hook to run a GraphQL query with the current user's token. The query auto-fires when both the token
26
26
  * and variables are available. Returns a refetch function and the Apollo query result.
27
27
  *
28
- * Uses Apollo's default `fetchPolicy` (`cache-first`) unless you override it via
29
- * `options`. Pass `{ fetchPolicy: "no-cache" }` for always-fresh, viewer-keyed data
30
- * (e.g. the current user), or `{ fetchPolicy: "cache-and-network" }` to show cached
31
- * data instantly while refetching.
28
+ * Defaults to `fetchPolicy: "no-cache"` (always fresh, safe for viewer-keyed data).
29
+ * Override per-query via `options` e.g. `{ fetchPolicy: "cache-first" }` for static
30
+ * reference data, or `{ fetchPolicy: "cache-and-network" }` to show cached data
31
+ * instantly while refetching.
32
32
  *
33
33
  * @param query - A `TypedDocumentNode` for the query (e.g. from codegen or a `gql` template).
34
34
  * @param variables - Optional initial variables (the `input` value). Automatically wrapped as `{ input: variables }` before sending. Query fires automatically when this and token are set; change to refetch.
@@ -9,11 +9,11 @@ import {
9
9
  useLazyQuery,
10
10
  useMutation
11
11
  } from "@apollo/client";
12
- import { useToken } from "naystack/auth/client";
12
+ import { useToken as useToken2 } from "naystack/auth/client";
13
13
  import React, {
14
14
  useCallback,
15
- useEffect,
16
- useRef
15
+ useEffect as useEffect2,
16
+ useRef as useRef2
17
17
  } from "react";
18
18
 
19
19
  // src/env.ts
@@ -66,6 +66,25 @@ function getEnv(key, skipCheck) {
66
66
  return value;
67
67
  }
68
68
 
69
+ // src/graphql/clear-store.tsx
70
+ import { useApolloClient } from "@apollo/client";
71
+ import { useToken } from "naystack/auth/client";
72
+ import { useEffect, useRef } from "react";
73
+ function ClearStoreOnLogout() {
74
+ const client = useApolloClient();
75
+ const token = useToken();
76
+ const hadToken = useRef(false);
77
+ useEffect(() => {
78
+ if (token) {
79
+ hadToken.current = true;
80
+ } else if (token === null && hadToken.current) {
81
+ hadToken.current = false;
82
+ void client.clearStore();
83
+ }
84
+ }, [client, token]);
85
+ return null;
86
+ }
87
+
69
88
  // src/graphql/client.tsx
70
89
  function makeClient(cacheConfig) {
71
90
  return new ApolloClient({
@@ -79,7 +98,9 @@ var ApolloWrapper = ({
79
98
  children,
80
99
  cacheConfig
81
100
  }) => {
82
- return /* @__PURE__ */ React.createElement(ApolloProvider, { client: makeClient(cacheConfig) }, children);
101
+ const clientRef = useRef2(null);
102
+ if (!clientRef.current) clientRef.current = makeClient(cacheConfig);
103
+ return /* @__PURE__ */ React.createElement(ApolloProvider, { client: clientRef.current }, /* @__PURE__ */ React.createElement(ClearStoreOnLogout, null), children);
83
104
  };
84
105
  var tokenContext = (token) => {
85
106
  if (!token) return void 0;
@@ -95,8 +116,8 @@ function useAuthQuery(query, variables, options) {
95
116
  fetchPolicy: "no-cache",
96
117
  ...options
97
118
  });
98
- const prevVarsRef = useRef(null);
99
- useEffect(() => {
119
+ const prevVarsRef = useRef2(null);
120
+ useEffect2(() => {
100
121
  const serialized = JSON.stringify(variables);
101
122
  if (variables && prevVarsRef.current !== serialized) {
102
123
  prevVarsRef.current = serialized;
@@ -122,7 +143,7 @@ function useAuthQuery(query, variables, options) {
122
143
  return [reFetch, result];
123
144
  }
124
145
  function useAuthMutation(mutation, options) {
125
- const token = useToken();
146
+ const token = useToken2();
126
147
  const [mutate, result] = useMutation(mutation, options);
127
148
  const method = useCallback(
128
149
  (input) => mutate({
@@ -34,9 +34,9 @@ __export(next_exports, {
34
34
  ApolloWrapper: () => ApolloWrapper
35
35
  });
36
36
  module.exports = __toCommonJS(next_exports);
37
- var import_client = require("@apollo/client");
37
+ var import_client3 = require("@apollo/client");
38
38
  var import_client_integration_nextjs = require("@apollo/client-integration-nextjs");
39
- var import_react = __toESM(require("react"));
39
+ var import_react2 = __toESM(require("react"));
40
40
 
41
41
  // src/env.ts
42
42
  var EXTRA_ENV = globalThis.__NAYSTACK_ENV__ || (globalThis.__NAYSTACK_ENV__ = {});
@@ -88,11 +88,30 @@ function getEnv(key, skipCheck) {
88
88
  return value;
89
89
  }
90
90
 
91
+ // src/graphql/clear-store.tsx
92
+ var import_client = require("@apollo/client");
93
+ var import_client2 = require("naystack/auth/client");
94
+ var import_react = require("react");
95
+ function ClearStoreOnLogout() {
96
+ const client = (0, import_client.useApolloClient)();
97
+ const token = (0, import_client2.useToken)();
98
+ const hadToken = (0, import_react.useRef)(false);
99
+ (0, import_react.useEffect)(() => {
100
+ if (token) {
101
+ hadToken.current = true;
102
+ } else if (token === null && hadToken.current) {
103
+ hadToken.current = false;
104
+ void client.clearStore();
105
+ }
106
+ }, [client, token]);
107
+ return null;
108
+ }
109
+
91
110
  // src/graphql/next.tsx
92
111
  function makeClient(cacheConfig) {
93
112
  return new import_client_integration_nextjs.ApolloClient({
94
113
  cache: new import_client_integration_nextjs.InMemoryCache(cacheConfig),
95
- link: new import_client.HttpLink({
114
+ link: new import_client3.HttpLink({
96
115
  uri: getEnv("NEXT_PUBLIC_GRAPHQL_ENDPOINT" /* NEXT_PUBLIC_GRAPHQL_ENDPOINT */)
97
116
  })
98
117
  });
@@ -101,7 +120,7 @@ var ApolloWrapper = ({
101
120
  children,
102
121
  cacheConfig
103
122
  }) => {
104
- return /* @__PURE__ */ import_react.default.createElement(import_client_integration_nextjs.ApolloNextAppProvider, { makeClient: () => makeClient(cacheConfig) }, children);
123
+ return /* @__PURE__ */ import_react2.default.createElement(import_client_integration_nextjs.ApolloNextAppProvider, { makeClient: () => makeClient(cacheConfig) }, /* @__PURE__ */ import_react2.default.createElement(ClearStoreOnLogout, null), children);
105
124
  };
106
125
  // Annotate the CommonJS export names for ESM import in node:
107
126
  0 && (module.exports = {
@@ -59,6 +59,25 @@ function getEnv(key, skipCheck) {
59
59
  return value;
60
60
  }
61
61
 
62
+ // src/graphql/clear-store.tsx
63
+ import { useApolloClient } from "@apollo/client";
64
+ import { useToken } from "naystack/auth/client";
65
+ import { useEffect, useRef } from "react";
66
+ function ClearStoreOnLogout() {
67
+ const client = useApolloClient();
68
+ const token = useToken();
69
+ const hadToken = useRef(false);
70
+ useEffect(() => {
71
+ if (token) {
72
+ hadToken.current = true;
73
+ } else if (token === null && hadToken.current) {
74
+ hadToken.current = false;
75
+ void client.clearStore();
76
+ }
77
+ }, [client, token]);
78
+ return null;
79
+ }
80
+
62
81
  // src/graphql/next.tsx
63
82
  function makeClient(cacheConfig) {
64
83
  return new ApolloClient({
@@ -72,7 +91,7 @@ var ApolloWrapper = ({
72
91
  children,
73
92
  cacheConfig
74
93
  }) => {
75
- return /* @__PURE__ */ React.createElement(ApolloNextAppProvider, { makeClient: () => makeClient(cacheConfig) }, children);
94
+ return /* @__PURE__ */ React.createElement(ApolloNextAppProvider, { makeClient: () => makeClient(cacheConfig) }, /* @__PURE__ */ React.createElement(ClearStoreOnLogout, null), children);
76
95
  };
77
96
  export {
78
97
  ApolloWrapper
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "naystack",
3
- "version": "1.8.2",
3
+ "version": "1.8.3",
4
4
  "description": "A stack built with Next + GraphQL + S3 + Auth",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",