naystack 1.5.26 → 1.5.27

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.
@@ -44,6 +44,9 @@ __export(client_exports, {
44
44
  module.exports = __toCommonJS(client_exports);
45
45
  var import_react = __toESM(require("react"));
46
46
 
47
+ // src/auth/constants.ts
48
+ var REFRESH_COOKIE_NAME = "refresh";
49
+
47
50
  // src/env.ts
48
51
  var getEnvValue = (key) => {
49
52
  switch (key) {
@@ -95,19 +98,33 @@ function getEnv(key, skipCheck) {
95
98
 
96
99
  // src/auth/email/client.tsx
97
100
  var TokenContext = (0, import_react.createContext)({
98
- token: null,
101
+ token: void 0,
99
102
  setToken: () => null
100
103
  });
101
- var AuthWrapper = ({ children }) => {
102
- const [token, setToken] = (0, import_react.useState)(null);
104
+ var AuthWrapper = ({
105
+ children,
106
+ onTokenUpdate
107
+ }) => {
108
+ const [token, setToken] = (0, import_react.useState)();
109
+ (0, import_react.useEffect)(() => {
110
+ if (onTokenUpdate && token !== void 0) {
111
+ onTokenUpdate(token);
112
+ }
113
+ }, [token]);
103
114
  return /* @__PURE__ */ import_react.default.createElement(TokenContext.Provider, { value: { token, setToken } }, children);
104
115
  };
105
- function useAuthFetch() {
116
+ function useAuthFetch(getRefreshToken) {
106
117
  const setToken = useSetToken();
107
- (0, import_react.useEffect)(() => {
118
+ const fetchToken = async () => {
108
119
  fetch(getEnv("NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT" /* NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT */), {
109
- credentials: "include"
120
+ credentials: "include",
121
+ body: getRefreshToken ? JSON.stringify({
122
+ [REFRESH_COOKIE_NAME]: await getRefreshToken()
123
+ }) : void 0
110
124
  }).then((res) => res.json()).then((data) => setToken(data.accessToken));
125
+ };
126
+ (0, import_react.useEffect)(() => {
127
+ fetchToken();
111
128
  }, []);
112
129
  }
113
130
  function AuthApply({ data }) {
@@ -5,8 +5,8 @@ import React__default, { Dispatch, SetStateAction } from 'react';
5
5
  * @category Auth
6
6
  */
7
7
  declare const TokenContext: React__default.Context<{
8
- token: string | null;
9
- setToken: Dispatch<SetStateAction<string | null>>;
8
+ token: string | null | undefined;
9
+ setToken: Dispatch<SetStateAction<string | null | undefined>>;
10
10
  }>;
11
11
  /**
12
12
  * Provider that fetches the current access token from your auth endpoint and exposes it via TokenContext.
@@ -39,10 +39,11 @@ declare const TokenContext: React__default.Context<{
39
39
  *
40
40
  * @category Auth
41
41
  */
42
- declare const AuthWrapper: ({ children }: {
42
+ declare const AuthWrapper: ({ children, onTokenUpdate, }: {
43
43
  children: React__default.ReactNode;
44
+ onTokenUpdate?: (token: string | null) => void;
44
45
  }) => React__default.JSX.Element;
45
- declare function useAuthFetch(): void;
46
+ declare function useAuthFetch(getRefreshToken?: () => Promise<string>): void;
46
47
  declare function AuthApply({ data }: {
47
48
  data?: string | null;
48
49
  }): null;
@@ -73,7 +74,7 @@ declare function AuthApply({ data }: {
73
74
  *
74
75
  * @category Auth
75
76
  */
76
- declare function useToken(): string | null;
77
+ declare function useToken(): string | null | undefined;
77
78
  /**
78
79
  * Returns the setter for the access token in TokenContext. Use to update token after login/signup or clear it on logout.
79
80
  * Must be used inside `AuthWrapper`. Typically you won't need this directly — use `useLogin`, `useSignUp`, and `useLogout` instead.
@@ -82,7 +83,7 @@ declare function useToken(): string | null;
82
83
  *
83
84
  * @category Auth
84
85
  */
85
- declare function useSetToken(): React__default.Dispatch<React__default.SetStateAction<string | null>>;
86
+ declare function useSetToken(): React__default.Dispatch<React__default.SetStateAction<string | null | undefined>>;
86
87
  /**
87
88
  * Returns a sign-up function that POSTs to the auth endpoint with credentials. On success, the response's
88
89
  * `accessToken` is stored and the token context updates automatically.
@@ -5,8 +5,8 @@ import React__default, { Dispatch, SetStateAction } from 'react';
5
5
  * @category Auth
6
6
  */
7
7
  declare const TokenContext: React__default.Context<{
8
- token: string | null;
9
- setToken: Dispatch<SetStateAction<string | null>>;
8
+ token: string | null | undefined;
9
+ setToken: Dispatch<SetStateAction<string | null | undefined>>;
10
10
  }>;
11
11
  /**
12
12
  * Provider that fetches the current access token from your auth endpoint and exposes it via TokenContext.
@@ -39,10 +39,11 @@ declare const TokenContext: React__default.Context<{
39
39
  *
40
40
  * @category Auth
41
41
  */
42
- declare const AuthWrapper: ({ children }: {
42
+ declare const AuthWrapper: ({ children, onTokenUpdate, }: {
43
43
  children: React__default.ReactNode;
44
+ onTokenUpdate?: (token: string | null) => void;
44
45
  }) => React__default.JSX.Element;
45
- declare function useAuthFetch(): void;
46
+ declare function useAuthFetch(getRefreshToken?: () => Promise<string>): void;
46
47
  declare function AuthApply({ data }: {
47
48
  data?: string | null;
48
49
  }): null;
@@ -73,7 +74,7 @@ declare function AuthApply({ data }: {
73
74
  *
74
75
  * @category Auth
75
76
  */
76
- declare function useToken(): string | null;
77
+ declare function useToken(): string | null | undefined;
77
78
  /**
78
79
  * Returns the setter for the access token in TokenContext. Use to update token after login/signup or clear it on logout.
79
80
  * Must be used inside `AuthWrapper`. Typically you won't need this directly — use `useLogin`, `useSignUp`, and `useLogout` instead.
@@ -82,7 +83,7 @@ declare function useToken(): string | null;
82
83
  *
83
84
  * @category Auth
84
85
  */
85
- declare function useSetToken(): React__default.Dispatch<React__default.SetStateAction<string | null>>;
86
+ declare function useSetToken(): React__default.Dispatch<React__default.SetStateAction<string | null | undefined>>;
86
87
  /**
87
88
  * Returns a sign-up function that POSTs to the auth endpoint with credentials. On success, the response's
88
89
  * `accessToken` is stored and the token context updates automatically.
@@ -9,6 +9,9 @@ import React, {
9
9
  useState
10
10
  } from "react";
11
11
 
12
+ // src/auth/constants.ts
13
+ var REFRESH_COOKIE_NAME = "refresh";
14
+
12
15
  // src/env.ts
13
16
  var getEnvValue = (key) => {
14
17
  switch (key) {
@@ -60,19 +63,33 @@ function getEnv(key, skipCheck) {
60
63
 
61
64
  // src/auth/email/client.tsx
62
65
  var TokenContext = createContext({
63
- token: null,
66
+ token: void 0,
64
67
  setToken: () => null
65
68
  });
66
- var AuthWrapper = ({ children }) => {
67
- const [token, setToken] = useState(null);
69
+ var AuthWrapper = ({
70
+ children,
71
+ onTokenUpdate
72
+ }) => {
73
+ const [token, setToken] = useState();
74
+ useEffect(() => {
75
+ if (onTokenUpdate && token !== void 0) {
76
+ onTokenUpdate(token);
77
+ }
78
+ }, [token]);
68
79
  return /* @__PURE__ */ React.createElement(TokenContext.Provider, { value: { token, setToken } }, children);
69
80
  };
70
- function useAuthFetch() {
81
+ function useAuthFetch(getRefreshToken) {
71
82
  const setToken = useSetToken();
72
- useEffect(() => {
83
+ const fetchToken = async () => {
73
84
  fetch(getEnv("NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT" /* NEXT_PUBLIC_EMAIL_AUTH_ENDPOINT */), {
74
- credentials: "include"
85
+ credentials: "include",
86
+ body: getRefreshToken ? JSON.stringify({
87
+ [REFRESH_COOKIE_NAME]: await getRefreshToken()
88
+ }) : void 0
75
89
  }).then((res) => res.json()).then((data) => setToken(data.accessToken));
90
+ };
91
+ useEffect(() => {
92
+ fetchToken();
76
93
  }, []);
77
94
  }
78
95
  function AuthApply({ data }) {
@@ -243,7 +243,9 @@ var getDeleteRoute = (options) => async (req) => {
243
243
  // src/auth/email/routes/get.ts
244
244
  var getGetRoute = (options) => async (req) => {
245
245
  const refresh = req.cookies.get(REFRESH_COOKIE_NAME)?.value;
246
- const userID = getUserIdFromRefreshToken(refresh);
246
+ const requestBody = refresh ? null : await req.json();
247
+ const bodyRefresh = requestBody?.[REFRESH_COOKIE_NAME];
248
+ const userID = getUserIdFromRefreshToken(refresh || bodyRefresh);
247
249
  if (userID) {
248
250
  if (options.onRefresh) {
249
251
  const body = await req.json();
@@ -206,7 +206,9 @@ var getDeleteRoute = (options) => async (req) => {
206
206
  // src/auth/email/routes/get.ts
207
207
  var getGetRoute = (options) => async (req) => {
208
208
  const refresh = req.cookies.get(REFRESH_COOKIE_NAME)?.value;
209
- const userID = getUserIdFromRefreshToken(refresh);
209
+ const requestBody = refresh ? null : await req.json();
210
+ const bodyRefresh = requestBody?.[REFRESH_COOKIE_NAME];
211
+ const userID = getUserIdFromRefreshToken(refresh || bodyRefresh);
210
212
  if (userID) {
211
213
  if (options.onRefresh) {
212
214
  const body = await req.json();
@@ -124,7 +124,9 @@ function getUserIdFromRefreshToken(refreshToken) {
124
124
  // src/auth/email/routes/get.ts
125
125
  var getGetRoute = (options) => async (req) => {
126
126
  const refresh = req.cookies.get(REFRESH_COOKIE_NAME)?.value;
127
- const userID = getUserIdFromRefreshToken(refresh);
127
+ const requestBody = refresh ? null : await req.json();
128
+ const bodyRefresh = requestBody?.[REFRESH_COOKIE_NAME];
129
+ const userID = getUserIdFromRefreshToken(refresh || bodyRefresh);
128
130
  if (userID) {
129
131
  if (options.onRefresh) {
130
132
  const body = await req.json();
@@ -98,7 +98,9 @@ function getUserIdFromRefreshToken(refreshToken) {
98
98
  // src/auth/email/routes/get.ts
99
99
  var getGetRoute = (options) => async (req) => {
100
100
  const refresh = req.cookies.get(REFRESH_COOKIE_NAME)?.value;
101
- const userID = getUserIdFromRefreshToken(refresh);
101
+ const requestBody = refresh ? null : await req.json();
102
+ const bodyRefresh = requestBody?.[REFRESH_COOKIE_NAME];
103
+ const userID = getUserIdFromRefreshToken(refresh || bodyRefresh);
102
104
  if (userID) {
103
105
  if (options.onRefresh) {
104
106
  const body = await req.json();
@@ -261,7 +261,9 @@ var getDeleteRoute = (options) => async (req) => {
261
261
  // src/auth/email/routes/get.ts
262
262
  var getGetRoute = (options) => async (req) => {
263
263
  const refresh = req.cookies.get(REFRESH_COOKIE_NAME)?.value;
264
- const userID = getUserIdFromRefreshToken(refresh);
264
+ const requestBody = refresh ? null : await req.json();
265
+ const bodyRefresh = requestBody?.[REFRESH_COOKIE_NAME];
266
+ const userID = getUserIdFromRefreshToken(refresh || bodyRefresh);
265
267
  if (userID) {
266
268
  if (options.onRefresh) {
267
269
  const body = await req.json();
@@ -218,7 +218,9 @@ var getDeleteRoute = (options) => async (req) => {
218
218
  // src/auth/email/routes/get.ts
219
219
  var getGetRoute = (options) => async (req) => {
220
220
  const refresh = req.cookies.get(REFRESH_COOKIE_NAME)?.value;
221
- const userID = getUserIdFromRefreshToken(refresh);
221
+ const requestBody = refresh ? null : await req.json();
222
+ const bodyRefresh = requestBody?.[REFRESH_COOKIE_NAME];
223
+ const userID = getUserIdFromRefreshToken(refresh || bodyRefresh);
222
224
  if (userID) {
223
225
  if (options.onRefresh) {
224
226
  const body = await req.json();
@@ -32,6 +32,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
32
32
  var client_exports = {};
33
33
  __export(client_exports, {
34
34
  ApolloWrapper: () => ApolloWrapper,
35
+ ApolloWrapperNext: () => ApolloWrapperNext,
35
36
  tokenContext: () => tokenContext,
36
37
  useAuthMutation: () => useAuthMutation,
37
38
  useAuthQuery: () => useAuthQuery
@@ -92,19 +93,33 @@ function getEnv(key, skipCheck) {
92
93
  }
93
94
 
94
95
  // src/graphql/client.tsx
96
+ function makeClient(cacheConfig) {
97
+ return new import_client_integration_nextjs.ApolloClient({
98
+ cache: new import_client_integration_nextjs.InMemoryCache(cacheConfig),
99
+ link: new import_client.HttpLink({
100
+ uri: getEnv("NEXT_PUBLIC_GRAPHQL_ENDPOINT" /* NEXT_PUBLIC_GRAPHQL_ENDPOINT */)
101
+ })
102
+ });
103
+ }
104
+ var ApolloWrapperNext = ({
105
+ children,
106
+ cacheConfig
107
+ }) => {
108
+ return /* @__PURE__ */ import_react.default.createElement(import_client_integration_nextjs.ApolloNextAppProvider, { makeClient: () => makeClient(cacheConfig) }, children);
109
+ };
110
+ function makeClientBasic(cacheConfig) {
111
+ return new import_client.ApolloClient({
112
+ cache: new import_client.InMemoryCache(cacheConfig),
113
+ link: new import_client.HttpLink({
114
+ uri: getEnv("NEXT_PUBLIC_GRAPHQL_ENDPOINT" /* NEXT_PUBLIC_GRAPHQL_ENDPOINT */)
115
+ })
116
+ });
117
+ }
95
118
  var ApolloWrapper = ({
96
119
  children,
97
120
  cacheConfig
98
121
  }) => {
99
- function makeClient() {
100
- return new import_client_integration_nextjs.ApolloClient({
101
- cache: new import_client_integration_nextjs.InMemoryCache(cacheConfig),
102
- link: new import_client.HttpLink({
103
- uri: getEnv("NEXT_PUBLIC_GRAPHQL_ENDPOINT" /* NEXT_PUBLIC_GRAPHQL_ENDPOINT */)
104
- })
105
- });
106
- }
107
- return /* @__PURE__ */ import_react.default.createElement(import_client_integration_nextjs.ApolloNextAppProvider, { makeClient }, children);
122
+ return /* @__PURE__ */ import_react.default.createElement(import_client.ApolloProvider, { client: makeClientBasic(cacheConfig) }, children);
108
123
  };
109
124
  var tokenContext = (token) => {
110
125
  if (!token) return void 0;
@@ -157,6 +172,7 @@ function useAuthMutation(mutation, options) {
157
172
  // Annotate the CommonJS export names for ESM import in node:
158
173
  0 && (module.exports = {
159
174
  ApolloWrapper,
175
+ ApolloWrapperNext,
160
176
  tokenContext,
161
177
  useAuthMutation,
162
178
  useAuthQuery
@@ -3,38 +3,9 @@ import { InMemoryCacheConfig, OperationVariables, MutationHookOptions } from '@a
3
3
  import { TypedDocumentNode } from '@graphql-typed-document-node/core';
4
4
  import React__default, { PropsWithChildren } from 'react';
5
5
 
6
- /**
7
- * Apollo Client provider for Next.js. Wrap your app (or a subtree) so client components can run GraphQL queries and mutations.
8
- * The GraphQL endpoint is read from `NEXT_PUBLIC_GRAPHQL_ENDPOINT` env var.
9
- *
10
- * Must be placed **inside** `AuthWrapper` (since `useAuthQuery` / `useAuthMutation` depend on the auth token).
11
- *
12
- * @param props - Component props.
13
- * @param props.children - React children (your app or page content).
14
- * @param props.cacheConfig - Optional `InMemoryCache` config (e.g. `typePolicies`, `addTypename`). Passed to Apollo's `InMemoryCache`.
15
- * @returns Provider component that supplies Apollo Client to the tree.
16
- *
17
- * @example
18
- * ```tsx
19
- * // app/layout.tsx
20
- * import { AuthWrapper } from "naystack/auth/email/client";
21
- * import { ApolloWrapper } from "naystack/graphql/client";
22
- *
23
- * export default function RootLayout({ children }: { children: React.ReactNode }) {
24
- * return (
25
- * <html lang="en">
26
- * <body>
27
- * <AuthWrapper>
28
- * <ApolloWrapper>{children}</ApolloWrapper>
29
- * </AuthWrapper>
30
- * </body>
31
- * </html>
32
- * );
33
- * }
34
- * ```
35
- *
36
- * @category GraphQL
37
- */
6
+ declare const ApolloWrapperNext: ({ children, cacheConfig, }: PropsWithChildren<{
7
+ cacheConfig?: InMemoryCacheConfig;
8
+ }>) => React__default.JSX.Element;
38
9
  declare const ApolloWrapper: ({ children, cacheConfig, }: PropsWithChildren<{
39
10
  cacheConfig?: InMemoryCacheConfig;
40
11
  }>) => React__default.JSX.Element;
@@ -128,4 +99,4 @@ declare function useAuthQuery<T, V extends OperationVariables>(query: TypedDocum
128
99
  */
129
100
  declare function useAuthMutation<T, V extends OperationVariables>(mutation: TypedDocumentNode<T, V>, options?: MutationHookOptions<T, V>): readonly [(input?: V["input"]) => Promise<_apollo_client.FetchResult<T>>, _apollo_client.MutationResult<T>];
130
101
 
131
- export { ApolloWrapper, tokenContext, useAuthMutation, useAuthQuery };
102
+ export { ApolloWrapper, ApolloWrapperNext, tokenContext, useAuthMutation, useAuthQuery };
@@ -3,38 +3,9 @@ import { InMemoryCacheConfig, OperationVariables, MutationHookOptions } from '@a
3
3
  import { TypedDocumentNode } from '@graphql-typed-document-node/core';
4
4
  import React__default, { PropsWithChildren } from 'react';
5
5
 
6
- /**
7
- * Apollo Client provider for Next.js. Wrap your app (or a subtree) so client components can run GraphQL queries and mutations.
8
- * The GraphQL endpoint is read from `NEXT_PUBLIC_GRAPHQL_ENDPOINT` env var.
9
- *
10
- * Must be placed **inside** `AuthWrapper` (since `useAuthQuery` / `useAuthMutation` depend on the auth token).
11
- *
12
- * @param props - Component props.
13
- * @param props.children - React children (your app or page content).
14
- * @param props.cacheConfig - Optional `InMemoryCache` config (e.g. `typePolicies`, `addTypename`). Passed to Apollo's `InMemoryCache`.
15
- * @returns Provider component that supplies Apollo Client to the tree.
16
- *
17
- * @example
18
- * ```tsx
19
- * // app/layout.tsx
20
- * import { AuthWrapper } from "naystack/auth/email/client";
21
- * import { ApolloWrapper } from "naystack/graphql/client";
22
- *
23
- * export default function RootLayout({ children }: { children: React.ReactNode }) {
24
- * return (
25
- * <html lang="en">
26
- * <body>
27
- * <AuthWrapper>
28
- * <ApolloWrapper>{children}</ApolloWrapper>
29
- * </AuthWrapper>
30
- * </body>
31
- * </html>
32
- * );
33
- * }
34
- * ```
35
- *
36
- * @category GraphQL
37
- */
6
+ declare const ApolloWrapperNext: ({ children, cacheConfig, }: PropsWithChildren<{
7
+ cacheConfig?: InMemoryCacheConfig;
8
+ }>) => React__default.JSX.Element;
38
9
  declare const ApolloWrapper: ({ children, cacheConfig, }: PropsWithChildren<{
39
10
  cacheConfig?: InMemoryCacheConfig;
40
11
  }>) => React__default.JSX.Element;
@@ -128,4 +99,4 @@ declare function useAuthQuery<T, V extends OperationVariables>(query: TypedDocum
128
99
  */
129
100
  declare function useAuthMutation<T, V extends OperationVariables>(mutation: TypedDocumentNode<T, V>, options?: MutationHookOptions<T, V>): readonly [(input?: V["input"]) => Promise<_apollo_client.FetchResult<T>>, _apollo_client.MutationResult<T>];
130
101
 
131
- export { ApolloWrapper, tokenContext, useAuthMutation, useAuthQuery };
102
+ export { ApolloWrapper, ApolloWrapperNext, tokenContext, useAuthMutation, useAuthQuery };
@@ -2,7 +2,10 @@
2
2
 
3
3
  // src/graphql/client.tsx
4
4
  import {
5
+ ApolloClient as ApolloClientBasic,
6
+ ApolloProvider,
5
7
  HttpLink,
8
+ InMemoryCache as InMemoryCacheBasic,
6
9
  useLazyQuery,
7
10
  useMutation
8
11
  } from "@apollo/client";
@@ -68,19 +71,33 @@ function getEnv(key, skipCheck) {
68
71
  }
69
72
 
70
73
  // src/graphql/client.tsx
74
+ function makeClient(cacheConfig) {
75
+ return new ApolloClient({
76
+ cache: new InMemoryCache(cacheConfig),
77
+ link: new HttpLink({
78
+ uri: getEnv("NEXT_PUBLIC_GRAPHQL_ENDPOINT" /* NEXT_PUBLIC_GRAPHQL_ENDPOINT */)
79
+ })
80
+ });
81
+ }
82
+ var ApolloWrapperNext = ({
83
+ children,
84
+ cacheConfig
85
+ }) => {
86
+ return /* @__PURE__ */ React.createElement(ApolloNextAppProvider, { makeClient: () => makeClient(cacheConfig) }, children);
87
+ };
88
+ function makeClientBasic(cacheConfig) {
89
+ return new ApolloClientBasic({
90
+ cache: new InMemoryCacheBasic(cacheConfig),
91
+ link: new HttpLink({
92
+ uri: getEnv("NEXT_PUBLIC_GRAPHQL_ENDPOINT" /* NEXT_PUBLIC_GRAPHQL_ENDPOINT */)
93
+ })
94
+ });
95
+ }
71
96
  var ApolloWrapper = ({
72
97
  children,
73
98
  cacheConfig
74
99
  }) => {
75
- function makeClient() {
76
- return new ApolloClient({
77
- cache: new InMemoryCache(cacheConfig),
78
- link: new HttpLink({
79
- uri: getEnv("NEXT_PUBLIC_GRAPHQL_ENDPOINT" /* NEXT_PUBLIC_GRAPHQL_ENDPOINT */)
80
- })
81
- });
82
- }
83
- return /* @__PURE__ */ React.createElement(ApolloNextAppProvider, { makeClient }, children);
100
+ return /* @__PURE__ */ React.createElement(ApolloProvider, { client: makeClientBasic(cacheConfig) }, children);
84
101
  };
85
102
  var tokenContext = (token) => {
86
103
  if (!token) return void 0;
@@ -132,6 +149,7 @@ function useAuthMutation(mutation, options) {
132
149
  }
133
150
  export {
134
151
  ApolloWrapper,
152
+ ApolloWrapperNext,
135
153
  tokenContext,
136
154
  useAuthMutation,
137
155
  useAuthQuery
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "naystack",
3
- "version": "1.5.26",
3
+ "version": "1.5.27",
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",