naystack 1.8.1 → 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.
- package/dist/graphql/clear-store.cjs.js +47 -0
- package/dist/graphql/clear-store.d.mts +16 -0
- package/dist/graphql/clear-store.d.ts +16 -0
- package/dist/graphql/clear-store.esm.js +23 -0
- package/dist/graphql/client.cjs.js +38 -16
- package/dist/graphql/client.d.mts +7 -3
- package/dist/graphql/client.d.ts +7 -3
- package/dist/graphql/client.esm.js +31 -9
- package/dist/graphql/next.cjs.js +23 -4
- package/dist/graphql/next.esm.js +20 -1
- package/dist/graphql/utils.d.mts +8 -1
- package/dist/graphql/utils.d.ts +8 -1
- package/package.json +1 -1
|
@@ -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
|
|
41
|
-
var
|
|
42
|
-
var
|
|
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
|
|
97
|
-
cache: new
|
|
98
|
-
link: new
|
|
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
|
-
|
|
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;
|
|
@@ -115,12 +136,13 @@ var tokenContext = (token) => {
|
|
|
115
136
|
credentials: `omit`
|
|
116
137
|
};
|
|
117
138
|
};
|
|
118
|
-
function useAuthQuery(query, variables) {
|
|
119
|
-
const [fetch, result] = (0,
|
|
120
|
-
fetchPolicy: "no-cache"
|
|
139
|
+
function useAuthQuery(query, variables, options) {
|
|
140
|
+
const [fetch, result] = (0, import_client3.useLazyQuery)(query, {
|
|
141
|
+
fetchPolicy: "no-cache",
|
|
142
|
+
...options
|
|
121
143
|
});
|
|
122
|
-
const prevVarsRef = (0,
|
|
123
|
-
(0,
|
|
144
|
+
const prevVarsRef = (0, import_react2.useRef)(null);
|
|
145
|
+
(0, import_react2.useEffect)(() => {
|
|
124
146
|
const serialized = JSON.stringify(variables);
|
|
125
147
|
if (variables && prevVarsRef.current !== serialized) {
|
|
126
148
|
prevVarsRef.current = serialized;
|
|
@@ -133,7 +155,7 @@ function useAuthQuery(query, variables) {
|
|
|
133
155
|
});
|
|
134
156
|
}
|
|
135
157
|
}, [fetch, variables]);
|
|
136
|
-
const reFetch = (0,
|
|
158
|
+
const reFetch = (0, import_react2.useCallback)(
|
|
137
159
|
(input) => fetch({
|
|
138
160
|
// @ts-expect-error -- to allow dynamic props
|
|
139
161
|
variables: { input },
|
|
@@ -146,9 +168,9 @@ function useAuthQuery(query, variables) {
|
|
|
146
168
|
return [reFetch, result];
|
|
147
169
|
}
|
|
148
170
|
function useAuthMutation(mutation, options) {
|
|
149
|
-
const token = (0,
|
|
150
|
-
const [mutate, result] = (0,
|
|
151
|
-
const method = (0,
|
|
171
|
+
const token = (0, import_client4.useToken)();
|
|
172
|
+
const [mutate, result] = (0, import_client3.useMutation)(mutation, options);
|
|
173
|
+
const method = (0, import_react2.useCallback)(
|
|
152
174
|
(input) => mutate({
|
|
153
175
|
// @ts-expect-error -- to allow dynamic props
|
|
154
176
|
variables: { input },
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as _apollo_client from '@apollo/client';
|
|
2
|
-
import { InMemoryCacheConfig, OperationVariables, MutationHookOptions, ApolloClient } from '@apollo/client';
|
|
2
|
+
import { InMemoryCacheConfig, OperationVariables, LazyQueryHookOptions, MutationHookOptions, ApolloClient } from '@apollo/client';
|
|
3
3
|
import { TypedDocumentNode } from '@graphql-typed-document-node/core';
|
|
4
4
|
import React__default, { PropsWithChildren } from 'react';
|
|
5
5
|
|
|
@@ -25,10 +25,14 @@ 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
|
-
*
|
|
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.
|
|
29
32
|
*
|
|
30
33
|
* @param query - A `TypedDocumentNode` for the query (e.g. from codegen or a `gql` template).
|
|
31
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.
|
|
35
|
+
* @param options - Optional Apollo `LazyQueryHookOptions` (e.g. `fetchPolicy`, `notifyOnNetworkStatusChange`). Merged into the underlying `useLazyQuery`.
|
|
32
36
|
* @returns Tuple: `[refetch, result]`.
|
|
33
37
|
* - `refetch(input)` — runs the query again with the given input (wrapped as `variables.input`).
|
|
34
38
|
* - `result` — `{ data, loading, error, hasAuth }` from Apollo. `hasAuth` is `true` when an auth token is available.
|
|
@@ -58,7 +62,7 @@ declare const tokenContext: (token?: string | null) => {
|
|
|
58
62
|
*
|
|
59
63
|
* @category GraphQL
|
|
60
64
|
*/
|
|
61
|
-
declare function useAuthQuery<T, V extends OperationVariables>(query: TypedDocumentNode<T, V>, variables?: V["input"]): readonly [(input?: V["input"]) => Promise<_apollo_client.QueryResult<T, V>>, _apollo_client.QueryResult<T, V>];
|
|
65
|
+
declare function useAuthQuery<T, V extends OperationVariables>(query: TypedDocumentNode<T, V>, variables?: V["input"], options?: LazyQueryHookOptions<T, V>): readonly [(input?: V["input"]) => Promise<_apollo_client.QueryResult<T, V>>, _apollo_client.QueryResult<T, V>];
|
|
62
66
|
/**
|
|
63
67
|
* Hook to run a GraphQL mutation with the current user's token. Returns a function you call with the mutation input.
|
|
64
68
|
*
|
package/dist/graphql/client.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as _apollo_client from '@apollo/client';
|
|
2
|
-
import { InMemoryCacheConfig, OperationVariables, MutationHookOptions, ApolloClient } from '@apollo/client';
|
|
2
|
+
import { InMemoryCacheConfig, OperationVariables, LazyQueryHookOptions, MutationHookOptions, ApolloClient } from '@apollo/client';
|
|
3
3
|
import { TypedDocumentNode } from '@graphql-typed-document-node/core';
|
|
4
4
|
import React__default, { PropsWithChildren } from 'react';
|
|
5
5
|
|
|
@@ -25,10 +25,14 @@ 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
|
-
*
|
|
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.
|
|
29
32
|
*
|
|
30
33
|
* @param query - A `TypedDocumentNode` for the query (e.g. from codegen or a `gql` template).
|
|
31
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.
|
|
35
|
+
* @param options - Optional Apollo `LazyQueryHookOptions` (e.g. `fetchPolicy`, `notifyOnNetworkStatusChange`). Merged into the underlying `useLazyQuery`.
|
|
32
36
|
* @returns Tuple: `[refetch, result]`.
|
|
33
37
|
* - `refetch(input)` — runs the query again with the given input (wrapped as `variables.input`).
|
|
34
38
|
* - `result` — `{ data, loading, error, hasAuth }` from Apollo. `hasAuth` is `true` when an auth token is available.
|
|
@@ -58,7 +62,7 @@ declare const tokenContext: (token?: string | null) => {
|
|
|
58
62
|
*
|
|
59
63
|
* @category GraphQL
|
|
60
64
|
*/
|
|
61
|
-
declare function useAuthQuery<T, V extends OperationVariables>(query: TypedDocumentNode<T, V>, variables?: V["input"]): readonly [(input?: V["input"]) => Promise<_apollo_client.QueryResult<T, V>>, _apollo_client.QueryResult<T, V>];
|
|
65
|
+
declare function useAuthQuery<T, V extends OperationVariables>(query: TypedDocumentNode<T, V>, variables?: V["input"], options?: LazyQueryHookOptions<T, V>): readonly [(input?: V["input"]) => Promise<_apollo_client.QueryResult<T, V>>, _apollo_client.QueryResult<T, V>];
|
|
62
66
|
/**
|
|
63
67
|
* Hook to run a GraphQL mutation with the current user's token. Returns a function you call with the mutation input.
|
|
64
68
|
*
|
|
@@ -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
|
-
|
|
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;
|
|
@@ -90,12 +111,13 @@ var tokenContext = (token) => {
|
|
|
90
111
|
credentials: `omit`
|
|
91
112
|
};
|
|
92
113
|
};
|
|
93
|
-
function useAuthQuery(query, variables) {
|
|
114
|
+
function useAuthQuery(query, variables, options) {
|
|
94
115
|
const [fetch, result] = useLazyQuery(query, {
|
|
95
|
-
fetchPolicy: "no-cache"
|
|
116
|
+
fetchPolicy: "no-cache",
|
|
117
|
+
...options
|
|
96
118
|
});
|
|
97
|
-
const prevVarsRef =
|
|
98
|
-
|
|
119
|
+
const prevVarsRef = useRef2(null);
|
|
120
|
+
useEffect2(() => {
|
|
99
121
|
const serialized = JSON.stringify(variables);
|
|
100
122
|
if (variables && prevVarsRef.current !== serialized) {
|
|
101
123
|
prevVarsRef.current = serialized;
|
|
@@ -121,7 +143,7 @@ function useAuthQuery(query, variables) {
|
|
|
121
143
|
return [reFetch, result];
|
|
122
144
|
}
|
|
123
145
|
function useAuthMutation(mutation, options) {
|
|
124
|
-
const token =
|
|
146
|
+
const token = useToken2();
|
|
125
147
|
const [mutate, result] = useMutation(mutation, options);
|
|
126
148
|
const method = useCallback(
|
|
127
149
|
(input) => mutate({
|
package/dist/graphql/next.cjs.js
CHANGED
|
@@ -34,9 +34,9 @@ __export(next_exports, {
|
|
|
34
34
|
ApolloWrapper: () => ApolloWrapper
|
|
35
35
|
});
|
|
36
36
|
module.exports = __toCommonJS(next_exports);
|
|
37
|
-
var
|
|
37
|
+
var import_client3 = require("@apollo/client");
|
|
38
38
|
var import_client_integration_nextjs = require("@apollo/client-integration-nextjs");
|
|
39
|
-
var
|
|
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
|
|
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__ */
|
|
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 = {
|
package/dist/graphql/next.esm.js
CHANGED
|
@@ -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/dist/graphql/utils.d.mts
CHANGED
|
@@ -34,8 +34,15 @@ type Promisify<T> = T | Promise<T>;
|
|
|
34
34
|
* there's no refresh cookie the only way to keep that promise is to not call the
|
|
35
35
|
* resolver at all — hence the added `null`, which surfaces the logged-out case in
|
|
36
36
|
* the types instead of passing a null `userId` through the cast.
|
|
37
|
+
*
|
|
38
|
+
* `[IsAuth] extends [true]` (not `IsAuth extends true`) is deliberate: `field()`
|
|
39
|
+
* has no `= false` default and can't reverse-infer `IsAuth` from a `ctx: Context`
|
|
40
|
+
* signature, so a non-authorized field infers `IsAuth = boolean`. A naked
|
|
41
|
+
* conditional would distribute over `true | false` and union `null` into every
|
|
42
|
+
* public field's return. The tuple wrapper stops the distribution, so only a
|
|
43
|
+
* literal `true` adds `null`.
|
|
37
44
|
*/
|
|
38
|
-
type AuthCallReturn<IsAuth extends boolean, R> = IsAuth extends true ? Awaited<R> | null : Awaited<R>;
|
|
45
|
+
type AuthCallReturn<IsAuth extends boolean, R> = [IsAuth] extends [true] ? Awaited<R> | null : Awaited<R>;
|
|
39
46
|
/** Base for query/field definition (output, input, options). */
|
|
40
47
|
interface BaseDefinition<T, U, IsAuth extends boolean = false, OutputNullable extends boolean = false, InputNullable extends boolean = false> {
|
|
41
48
|
output: T;
|
package/dist/graphql/utils.d.ts
CHANGED
|
@@ -34,8 +34,15 @@ type Promisify<T> = T | Promise<T>;
|
|
|
34
34
|
* there's no refresh cookie the only way to keep that promise is to not call the
|
|
35
35
|
* resolver at all — hence the added `null`, which surfaces the logged-out case in
|
|
36
36
|
* the types instead of passing a null `userId` through the cast.
|
|
37
|
+
*
|
|
38
|
+
* `[IsAuth] extends [true]` (not `IsAuth extends true`) is deliberate: `field()`
|
|
39
|
+
* has no `= false` default and can't reverse-infer `IsAuth` from a `ctx: Context`
|
|
40
|
+
* signature, so a non-authorized field infers `IsAuth = boolean`. A naked
|
|
41
|
+
* conditional would distribute over `true | false` and union `null` into every
|
|
42
|
+
* public field's return. The tuple wrapper stops the distribution, so only a
|
|
43
|
+
* literal `true` adds `null`.
|
|
37
44
|
*/
|
|
38
|
-
type AuthCallReturn<IsAuth extends boolean, R> = IsAuth extends true ? Awaited<R> | null : Awaited<R>;
|
|
45
|
+
type AuthCallReturn<IsAuth extends boolean, R> = [IsAuth] extends [true] ? Awaited<R> | null : Awaited<R>;
|
|
39
46
|
/** Base for query/field definition (output, input, options). */
|
|
40
47
|
interface BaseDefinition<T, U, IsAuth extends boolean = false, OutputNullable extends boolean = false, InputNullable extends boolean = false> {
|
|
41
48
|
output: T;
|