naystack 1.2.15 → 1.2.16

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.
@@ -220,7 +220,14 @@ function getEmailAuthRoutes(options) {
220
220
  POST: getPostRoute(options),
221
221
  PUT: getPutRoute(options),
222
222
  DELETE: getDeleteRoute(options),
223
- getUserIdFromRequest: (req) => getUserContext(options.refreshKey, options.signingKey, req)
223
+ getContext: (req) => {
224
+ const ids = getUserContext(options.refreshKey, options.signingKey, req);
225
+ if (!ids) return { userId: null };
226
+ if (ids.refreshUserID) {
227
+ return { userId: ids.refreshUserID, isRefreshID: true };
228
+ }
229
+ return { userId: ids.accessUserId };
230
+ }
224
231
  };
225
232
  }
226
233
  // Annotate the CommonJS export names for ESM import in node:
@@ -14,10 +14,16 @@ declare function getEmailAuthRoutes(options: InitRoutesOptions): {
14
14
  accessToken: string | undefined;
15
15
  refreshToken: string | undefined;
16
16
  }>>;
17
- getUserIdFromRequest: (req: NextRequest) => {
18
- refreshUserID?: number;
19
- accessUserId?: number;
20
- } | null;
17
+ getContext: (req: NextRequest) => {
18
+ userId: null;
19
+ isRefreshID?: undefined;
20
+ } | {
21
+ userId: number;
22
+ isRefreshID: boolean;
23
+ } | {
24
+ userId: number | undefined;
25
+ isRefreshID?: undefined;
26
+ };
21
27
  };
22
28
 
23
29
  export { getEmailAuthRoutes };
@@ -14,10 +14,16 @@ declare function getEmailAuthRoutes(options: InitRoutesOptions): {
14
14
  accessToken: string | undefined;
15
15
  refreshToken: string | undefined;
16
16
  }>>;
17
- getUserIdFromRequest: (req: NextRequest) => {
18
- refreshUserID?: number;
19
- accessUserId?: number;
20
- } | null;
17
+ getContext: (req: NextRequest) => {
18
+ userId: null;
19
+ isRefreshID?: undefined;
20
+ } | {
21
+ userId: number;
22
+ isRefreshID: boolean;
23
+ } | {
24
+ userId: number | undefined;
25
+ isRefreshID?: undefined;
26
+ };
21
27
  };
22
28
 
23
29
  export { getEmailAuthRoutes };
@@ -194,7 +194,14 @@ function getEmailAuthRoutes(options) {
194
194
  POST: getPostRoute(options),
195
195
  PUT: getPutRoute(options),
196
196
  DELETE: getDeleteRoute(options),
197
- getUserIdFromRequest: (req) => getUserContext(options.refreshKey, options.signingKey, req)
197
+ getContext: (req) => {
198
+ const ids = getUserContext(options.refreshKey, options.signingKey, req);
199
+ if (!ids) return { userId: null };
200
+ if (ids.refreshUserID) {
201
+ return { userId: ids.refreshUserID, isRefreshID: true };
202
+ }
203
+ return { userId: ids.accessUserId };
204
+ }
198
205
  };
199
206
  }
200
207
  export {
@@ -234,7 +234,14 @@ function getEmailAuthRoutes(options) {
234
234
  POST: getPostRoute(options),
235
235
  PUT: getPutRoute(options),
236
236
  DELETE: getDeleteRoute(options),
237
- getUserIdFromRequest: (req) => getUserContext(options.refreshKey, options.signingKey, req)
237
+ getContext: (req) => {
238
+ const ids = getUserContext(options.refreshKey, options.signingKey, req);
239
+ if (!ids) return { userId: null };
240
+ if (ids.refreshUserID) {
241
+ return { userId: ids.refreshUserID, isRefreshID: true };
242
+ }
243
+ return { userId: ids.accessUserId };
244
+ }
238
245
  };
239
246
  }
240
247
 
@@ -206,7 +206,14 @@ function getEmailAuthRoutes(options) {
206
206
  POST: getPostRoute(options),
207
207
  PUT: getPutRoute(options),
208
208
  DELETE: getDeleteRoute(options),
209
- getUserIdFromRequest: (req) => getUserContext(options.refreshKey, options.signingKey, req)
209
+ getContext: (req) => {
210
+ const ids = getUserContext(options.refreshKey, options.signingKey, req);
211
+ if (!ids) return { userId: null };
212
+ if (ids.refreshUserID) {
213
+ return { userId: ids.refreshUserID, isRefreshID: true };
214
+ }
215
+ return { userId: ids.accessUserId };
216
+ }
210
217
  };
211
218
  }
212
219
 
@@ -579,11 +579,11 @@ async function initGraphQLServer({
579
579
  authChecker,
580
580
  resolvers,
581
581
  plugins,
582
- context
582
+ getContext
583
583
  }) {
584
584
  const { typeDefs, resolvers: builtResolvers } = await (0, import_type_graphql.buildTypeDefsAndResolvers)({
585
585
  validate: true,
586
- authChecker,
586
+ authChecker: authChecker || (({ context }) => !!context.userId),
587
587
  resolvers
588
588
  });
589
589
  const server = new import_server.ApolloServer({
@@ -593,10 +593,7 @@ async function initGraphQLServer({
593
593
  process.env.NODE_ENV === "production" ? (0, import_default.ApolloServerPluginLandingPageProductionDefault)() : (0, import_default.ApolloServerPluginLandingPageLocalDefault)(),
594
594
  {
595
595
  async requestDidStart({ request, contextValue }) {
596
- if (
597
- // eslint-disable-next-line
598
- contextValue.onlyQuery && !request.query?.startsWith("query")
599
- )
596
+ if (contextValue.isRefreshID && !request.query?.startsWith("query"))
600
597
  contextValue.userId = null;
601
598
  }
602
599
  },
@@ -606,12 +603,11 @@ async function initGraphQLServer({
606
603
  status400ForVariableCoercionErrors: true
607
604
  });
608
605
  const handler = (0, import_next.startServerAndCreateNextHandler)(server, {
609
- context
606
+ context: getContext
610
607
  });
611
608
  return {
612
609
  GET: (request) => handler(request),
613
- POST: (request) => handler(request),
614
- context
610
+ POST: (request) => handler(request)
615
611
  };
616
612
  }
617
613
 
@@ -566,11 +566,11 @@ async function initGraphQLServer({
566
566
  authChecker,
567
567
  resolvers,
568
568
  plugins,
569
- context
569
+ getContext
570
570
  }) {
571
571
  const { typeDefs, resolvers: builtResolvers } = await buildTypeDefsAndResolvers({
572
572
  validate: true,
573
- authChecker,
573
+ authChecker: authChecker || (({ context }) => !!context.userId),
574
574
  resolvers
575
575
  });
576
576
  const server = new ApolloServer({
@@ -580,10 +580,7 @@ async function initGraphQLServer({
580
580
  process.env.NODE_ENV === "production" ? ApolloServerPluginLandingPageProductionDefault() : ApolloServerPluginLandingPageLocalDefault(),
581
581
  {
582
582
  async requestDidStart({ request, contextValue }) {
583
- if (
584
- // eslint-disable-next-line
585
- contextValue.onlyQuery && !request.query?.startsWith("query")
586
- )
583
+ if (contextValue.isRefreshID && !request.query?.startsWith("query"))
587
584
  contextValue.userId = null;
588
585
  }
589
586
  },
@@ -593,12 +590,11 @@ async function initGraphQLServer({
593
590
  status400ForVariableCoercionErrors: true
594
591
  });
595
592
  const handler = startServerAndCreateNextHandler(server, {
596
- context
593
+ context: getContext
597
594
  });
598
595
  return {
599
596
  GET: (request) => handler(request),
600
- POST: (request) => handler(request),
601
- context
597
+ POST: (request) => handler(request)
602
598
  };
603
599
  }
604
600
 
@@ -32,11 +32,11 @@ async function initGraphQLServer({
32
32
  authChecker,
33
33
  resolvers,
34
34
  plugins,
35
- context
35
+ getContext
36
36
  }) {
37
37
  const { typeDefs, resolvers: builtResolvers } = await (0, import_type_graphql.buildTypeDefsAndResolvers)({
38
38
  validate: true,
39
- authChecker,
39
+ authChecker: authChecker || (({ context }) => !!context.userId),
40
40
  resolvers
41
41
  });
42
42
  const server = new import_server.ApolloServer({
@@ -46,10 +46,7 @@ async function initGraphQLServer({
46
46
  process.env.NODE_ENV === "production" ? (0, import_default.ApolloServerPluginLandingPageProductionDefault)() : (0, import_default.ApolloServerPluginLandingPageLocalDefault)(),
47
47
  {
48
48
  async requestDidStart({ request, contextValue }) {
49
- if (
50
- // eslint-disable-next-line
51
- contextValue.onlyQuery && !request.query?.startsWith("query")
52
- )
49
+ if (contextValue.isRefreshID && !request.query?.startsWith("query"))
53
50
  contextValue.userId = null;
54
51
  }
55
52
  },
@@ -59,12 +56,11 @@ async function initGraphQLServer({
59
56
  status400ForVariableCoercionErrors: true
60
57
  });
61
58
  const handler = (0, import_next.startServerAndCreateNextHandler)(server, {
62
- context
59
+ context: getContext
63
60
  });
64
61
  return {
65
62
  GET: (request) => handler(request),
66
- POST: (request) => handler(request),
67
- context
63
+ POST: (request) => handler(request)
68
64
  };
69
65
  }
70
66
  // Annotate the CommonJS export names for ESM import in node:
@@ -2,15 +2,14 @@ import { ApolloServerPlugin } from '@apollo/server';
2
2
  import { NextRequest } from 'next/server';
3
3
  import { AuthChecker, NonEmptyArray } from 'type-graphql';
4
4
 
5
- declare function initGraphQLServer({ authChecker, resolvers, plugins, context, }: {
5
+ declare function initGraphQLServer({ authChecker, resolvers, plugins, getContext, }: {
6
6
  authChecker?: AuthChecker<any>;
7
7
  resolvers: NonEmptyArray<Function>;
8
8
  plugins?: ApolloServerPlugin[];
9
- context?: (req: NextRequest) => Promise<any>;
9
+ getContext?: (req: NextRequest) => Promise<any>;
10
10
  }): Promise<{
11
11
  GET: (request: NextRequest) => Promise<Response>;
12
12
  POST: (request: NextRequest) => Promise<Response>;
13
- context: ((req: NextRequest) => Promise<any>) | undefined;
14
13
  }>;
15
14
 
16
15
  export { initGraphQLServer };
@@ -2,15 +2,14 @@ import { ApolloServerPlugin } from '@apollo/server';
2
2
  import { NextRequest } from 'next/server';
3
3
  import { AuthChecker, NonEmptyArray } from 'type-graphql';
4
4
 
5
- declare function initGraphQLServer({ authChecker, resolvers, plugins, context, }: {
5
+ declare function initGraphQLServer({ authChecker, resolvers, plugins, getContext, }: {
6
6
  authChecker?: AuthChecker<any>;
7
7
  resolvers: NonEmptyArray<Function>;
8
8
  plugins?: ApolloServerPlugin[];
9
- context?: (req: NextRequest) => Promise<any>;
9
+ getContext?: (req: NextRequest) => Promise<any>;
10
10
  }): Promise<{
11
11
  GET: (request: NextRequest) => Promise<Response>;
12
12
  POST: (request: NextRequest) => Promise<Response>;
13
- context: ((req: NextRequest) => Promise<any>) | undefined;
14
13
  }>;
15
14
 
16
15
  export { initGraphQLServer };
@@ -13,11 +13,11 @@ async function initGraphQLServer({
13
13
  authChecker,
14
14
  resolvers,
15
15
  plugins,
16
- context
16
+ getContext
17
17
  }) {
18
18
  const { typeDefs, resolvers: builtResolvers } = await buildTypeDefsAndResolvers({
19
19
  validate: true,
20
- authChecker,
20
+ authChecker: authChecker || (({ context }) => !!context.userId),
21
21
  resolvers
22
22
  });
23
23
  const server = new ApolloServer({
@@ -27,10 +27,7 @@ async function initGraphQLServer({
27
27
  process.env.NODE_ENV === "production" ? ApolloServerPluginLandingPageProductionDefault() : ApolloServerPluginLandingPageLocalDefault(),
28
28
  {
29
29
  async requestDidStart({ request, contextValue }) {
30
- if (
31
- // eslint-disable-next-line
32
- contextValue.onlyQuery && !request.query?.startsWith("query")
33
- )
30
+ if (contextValue.isRefreshID && !request.query?.startsWith("query"))
34
31
  contextValue.userId = null;
35
32
  }
36
33
  },
@@ -40,12 +37,11 @@ async function initGraphQLServer({
40
37
  status400ForVariableCoercionErrors: true
41
38
  });
42
39
  const handler = startServerAndCreateNextHandler(server, {
43
- context
40
+ context: getContext
44
41
  });
45
42
  return {
46
43
  GET: (request) => handler(request),
47
- POST: (request) => handler(request),
48
- context
44
+ POST: (request) => handler(request)
49
45
  };
50
46
  }
51
47
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "naystack",
3
- "version": "1.2.15",
3
+ "version": "1.2.16",
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",