naystack 1.4.16 → 1.4.20

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.
Files changed (87) hide show
  1. package/dist/auth/email/client.cjs.js +27 -12
  2. package/dist/auth/email/client.esm.js +27 -12
  3. package/dist/auth/email/index.cjs.js +32 -15
  4. package/dist/auth/email/index.esm.js +32 -15
  5. package/dist/auth/email/routes/delete.cjs.js +14 -4
  6. package/dist/auth/email/routes/delete.esm.js +14 -4
  7. package/dist/auth/email/routes/get.cjs.js +12 -2
  8. package/dist/auth/email/routes/get.esm.js +12 -2
  9. package/dist/auth/email/routes/post.cjs.js +17 -6
  10. package/dist/auth/email/routes/post.esm.js +17 -6
  11. package/dist/auth/email/routes/put.cjs.js +12 -4
  12. package/dist/auth/email/routes/put.esm.js +12 -4
  13. package/dist/auth/email/token.cjs.js +9 -2
  14. package/dist/auth/email/token.esm.js +9 -2
  15. package/dist/auth/email/types.d.mts +1 -7
  16. package/dist/auth/email/types.d.ts +1 -7
  17. package/dist/auth/email/utils.cjs.js +16 -5
  18. package/dist/auth/email/utils.d.mts +2 -2
  19. package/dist/auth/email/utils.d.ts +2 -2
  20. package/dist/auth/email/utils.esm.js +16 -5
  21. package/dist/auth/google/get.cjs.js +15 -7
  22. package/dist/auth/google/get.d.mts +1 -3
  23. package/dist/auth/google/get.d.ts +1 -3
  24. package/dist/auth/google/get.esm.js +15 -7
  25. package/dist/auth/google/index.cjs.js +15 -7
  26. package/dist/auth/google/index.d.mts +0 -5
  27. package/dist/auth/google/index.d.ts +0 -5
  28. package/dist/auth/google/index.esm.js +15 -7
  29. package/dist/auth/index.cjs.js +47 -31
  30. package/dist/auth/index.esm.js +47 -31
  31. package/dist/auth/instagram/client.cjs.js +12 -1
  32. package/dist/auth/instagram/client.d.mts +1 -1
  33. package/dist/auth/instagram/client.d.ts +1 -1
  34. package/dist/auth/instagram/client.esm.js +10 -1
  35. package/dist/auth/instagram/index.cjs.js +16 -9
  36. package/dist/auth/instagram/index.d.mts +0 -2
  37. package/dist/auth/instagram/index.d.ts +0 -2
  38. package/dist/auth/instagram/index.esm.js +16 -9
  39. package/dist/auth/instagram/route.cjs.js +16 -9
  40. package/dist/auth/instagram/route.d.mts +1 -1
  41. package/dist/auth/instagram/route.d.ts +1 -1
  42. package/dist/auth/instagram/route.esm.js +16 -9
  43. package/dist/auth/instagram/utils.cjs.js +10 -1
  44. package/dist/auth/instagram/utils.esm.js +8 -1
  45. package/dist/client/index.cjs.js +8 -1
  46. package/dist/client/index.esm.js +8 -1
  47. package/dist/client/seo.cjs.js +10 -1
  48. package/dist/client/seo.esm.js +8 -1
  49. package/dist/env.cjs.js +57 -0
  50. package/dist/env.d.mts +23 -0
  51. package/dist/env.d.ts +23 -0
  52. package/dist/env.esm.js +31 -0
  53. package/dist/file/client.cjs.js +10 -1
  54. package/dist/file/client.esm.js +10 -1
  55. package/dist/file/index.cjs.js +40 -28
  56. package/dist/file/index.d.mts +0 -2
  57. package/dist/file/index.d.ts +0 -2
  58. package/dist/file/index.esm.js +40 -28
  59. package/dist/file/put.cjs.js +23 -11
  60. package/dist/file/put.d.mts +0 -2
  61. package/dist/file/put.d.ts +0 -2
  62. package/dist/file/put.esm.js +23 -11
  63. package/dist/file/setup.cjs.js +40 -28
  64. package/dist/file/setup.d.mts +0 -7
  65. package/dist/file/setup.d.ts +0 -7
  66. package/dist/file/setup.esm.js +40 -28
  67. package/dist/file/utils.cjs.js +28 -17
  68. package/dist/file/utils.d.mts +6 -10
  69. package/dist/file/utils.d.ts +6 -10
  70. package/dist/file/utils.esm.js +28 -17
  71. package/dist/graphql/client.cjs.js +15 -3
  72. package/dist/graphql/client.d.mts +1 -1
  73. package/dist/graphql/client.d.ts +1 -1
  74. package/dist/graphql/client.esm.js +15 -3
  75. package/dist/graphql/index.cjs.js +65 -3
  76. package/dist/graphql/index.d.mts +1 -0
  77. package/dist/graphql/index.d.ts +1 -0
  78. package/dist/graphql/index.esm.js +65 -3
  79. package/dist/graphql/init.cjs.js +11 -2
  80. package/dist/graphql/init.esm.js +11 -2
  81. package/dist/graphql/server.cjs.js +10 -1
  82. package/dist/graphql/server.esm.js +10 -1
  83. package/dist/graphql/utils.cjs.js +54 -1
  84. package/dist/graphql/utils.d.mts +10 -3
  85. package/dist/graphql/utils.d.ts +10 -3
  86. package/dist/graphql/utils.esm.js +54 -1
  87. package/package.json +1 -1
@@ -562,6 +562,15 @@ import { startServerAndCreateNextHandler } from "@as-integrations/next";
562
562
  import {
563
563
  buildTypeDefsAndResolvers
564
564
  } from "type-graphql";
565
+
566
+ // src/env.ts
567
+ function getEnv(key, skipCheck) {
568
+ const value = process.env[key];
569
+ if (!skipCheck && !value) throw new Error(`${key} is not defined`);
570
+ return value;
571
+ }
572
+
573
+ // src/graphql/init.ts
565
574
  async function initGraphQLServer({
566
575
  authChecker,
567
576
  resolvers,
@@ -577,7 +586,7 @@ async function initGraphQLServer({
577
586
  typeDefs,
578
587
  resolvers: builtResolvers,
579
588
  plugins: [
580
- process.env.NODE_ENV === "production" ? ApolloServerPluginLandingPageProductionDefault() : ApolloServerPluginLandingPageLocalDefault(),
589
+ getEnv("NODE_ENV" /* NODE_ENV */, true) === "production" ? ApolloServerPluginLandingPageProductionDefault() : ApolloServerPluginLandingPageLocalDefault(),
581
590
  {
582
591
  async requestDidStart({ request, contextValue }) {
583
592
  if (contextValue.isRefreshID && !request.query?.startsWith("query"))
@@ -586,7 +595,7 @@ async function initGraphQLServer({
586
595
  },
587
596
  ...plugins || []
588
597
  ],
589
- introspection: process.env.NODE_ENV !== "production",
598
+ introspection: getEnv("NODE_ENV" /* NODE_ENV */, true) !== "production",
590
599
  status400ForVariableCoercionErrors: true
591
600
  });
592
601
  const handler = startServerAndCreateNextHandler(server, {
@@ -599,6 +608,8 @@ async function initGraphQLServer({
599
608
  }
600
609
 
601
610
  // src/graphql/utils.ts
611
+ import { cacheLife, cacheTag } from "next/cache";
612
+ import { cookies as cookies2 } from "next/headers";
602
613
  import {
603
614
  Arg,
604
615
  Authorized,
@@ -609,8 +620,59 @@ import {
609
620
  Resolver,
610
621
  Root
611
622
  } from "type-graphql";
623
+
624
+ // src/auth/constants.ts
625
+ var REFRESH_COOKIE_NAME = "refresh";
626
+
627
+ // src/auth/email/token.ts
628
+ import { compare } from "bcryptjs";
629
+ import { JsonWebTokenError, sign, verify } from "jsonwebtoken";
630
+ import { cookies } from "next/headers";
631
+ import { redirect } from "next/navigation";
632
+ import { NextResponse } from "next/server";
633
+ function getUserIdFromRefreshToken(refreshKey, refreshToken) {
634
+ if (refreshToken)
635
+ try {
636
+ const decoded = verify(refreshToken, refreshKey);
637
+ if (typeof decoded !== "string" && typeof decoded.id === "number")
638
+ return decoded.id;
639
+ } catch (e) {
640
+ if (!(e instanceof JsonWebTokenError)) console.error(e, "errors");
641
+ return null;
642
+ }
643
+ return null;
644
+ }
645
+
646
+ // src/graphql/utils.ts
612
647
  function query(fn, options) {
613
- return { ...options, fn };
648
+ return { ...options, fn, call: getCaller(fn, options) };
649
+ }
650
+ var getUserId = async () => {
651
+ const Cookie = await cookies2();
652
+ const refresh = Cookie.get(REFRESH_COOKIE_NAME)?.value;
653
+ return refresh ? getUserIdFromRefreshToken(refresh) : null;
654
+ };
655
+ function getCaller(fn, options) {
656
+ return async (data, config) => {
657
+ if (config?.revalidate) {
658
+ const cachedFn = (data2, config2) => {
659
+ "use cache";
660
+ if (config2?.revalidate) cacheLife(config2.revalidate);
661
+ if (config2?.tags) cacheTag(...config2.tags);
662
+ const ctx2 = {
663
+ userId: null,
664
+ isRefreshID: true
665
+ };
666
+ return fn(ctx2, data2);
667
+ };
668
+ return cachedFn(data, config);
669
+ }
670
+ const ctx = {
671
+ userId: options.authorized ? await getUserId() : null,
672
+ isRefreshID: true
673
+ };
674
+ return fn(ctx, data);
675
+ };
614
676
  }
615
677
  function field(fn, options) {
616
678
  return { ...options, fn };
@@ -28,6 +28,15 @@ var import_server = require("@apollo/server");
28
28
  var import_default = require("@apollo/server/plugin/landingPage/default");
29
29
  var import_next = require("@as-integrations/next");
30
30
  var import_type_graphql = require("type-graphql");
31
+
32
+ // src/env.ts
33
+ function getEnv(key, skipCheck) {
34
+ const value = process.env[key];
35
+ if (!skipCheck && !value) throw new Error(`${key} is not defined`);
36
+ return value;
37
+ }
38
+
39
+ // src/graphql/init.ts
31
40
  async function initGraphQLServer({
32
41
  authChecker,
33
42
  resolvers,
@@ -43,7 +52,7 @@ async function initGraphQLServer({
43
52
  typeDefs,
44
53
  resolvers: builtResolvers,
45
54
  plugins: [
46
- process.env.NODE_ENV === "production" ? (0, import_default.ApolloServerPluginLandingPageProductionDefault)() : (0, import_default.ApolloServerPluginLandingPageLocalDefault)(),
55
+ getEnv("NODE_ENV" /* NODE_ENV */, true) === "production" ? (0, import_default.ApolloServerPluginLandingPageProductionDefault)() : (0, import_default.ApolloServerPluginLandingPageLocalDefault)(),
47
56
  {
48
57
  async requestDidStart({ request, contextValue }) {
49
58
  if (contextValue.isRefreshID && !request.query?.startsWith("query"))
@@ -52,7 +61,7 @@ async function initGraphQLServer({
52
61
  },
53
62
  ...plugins || []
54
63
  ],
55
- introspection: process.env.NODE_ENV !== "production",
64
+ introspection: getEnv("NODE_ENV" /* NODE_ENV */, true) !== "production",
56
65
  status400ForVariableCoercionErrors: true
57
66
  });
58
67
  const handler = (0, import_next.startServerAndCreateNextHandler)(server, {
@@ -9,6 +9,15 @@ import { startServerAndCreateNextHandler } from "@as-integrations/next";
9
9
  import {
10
10
  buildTypeDefsAndResolvers
11
11
  } from "type-graphql";
12
+
13
+ // src/env.ts
14
+ function getEnv(key, skipCheck) {
15
+ const value = process.env[key];
16
+ if (!skipCheck && !value) throw new Error(`${key} is not defined`);
17
+ return value;
18
+ }
19
+
20
+ // src/graphql/init.ts
12
21
  async function initGraphQLServer({
13
22
  authChecker,
14
23
  resolvers,
@@ -24,7 +33,7 @@ async function initGraphQLServer({
24
33
  typeDefs,
25
34
  resolvers: builtResolvers,
26
35
  plugins: [
27
- process.env.NODE_ENV === "production" ? ApolloServerPluginLandingPageProductionDefault() : ApolloServerPluginLandingPageLocalDefault(),
36
+ getEnv("NODE_ENV" /* NODE_ENV */, true) === "production" ? ApolloServerPluginLandingPageProductionDefault() : ApolloServerPluginLandingPageLocalDefault(),
28
37
  {
29
38
  async requestDidStart({ request, contextValue }) {
30
39
  if (contextValue.isRefreshID && !request.query?.startsWith("query"))
@@ -33,7 +42,7 @@ async function initGraphQLServer({
33
42
  },
34
43
  ...plugins || []
35
44
  ],
36
- introspection: process.env.NODE_ENV !== "production",
45
+ introspection: getEnv("NODE_ENV" /* NODE_ENV */, true) !== "production",
37
46
  status400ForVariableCoercionErrors: true
38
47
  });
39
48
  const handler = startServerAndCreateNextHandler(server, {
@@ -28,6 +28,15 @@ var import_client = require("@apollo/client");
28
28
  var import_client_integration_nextjs = require("@apollo/client-integration-nextjs");
29
29
  var import_headers = require("next/headers");
30
30
  var import_react = require("react");
31
+
32
+ // src/env.ts
33
+ function getEnv(key, skipCheck) {
34
+ const value = process.env[key];
35
+ if (!skipCheck && !value) throw new Error(`${key} is not defined`);
36
+ return value;
37
+ }
38
+
39
+ // src/graphql/server.tsx
31
40
  function Injector({
32
41
  fetch,
33
42
  Component,
@@ -47,7 +56,7 @@ var { query: gqlQuery } = (0, import_client_integration_nextjs.registerApolloCli
47
56
  return new import_client.ApolloClient({
48
57
  cache: new import_client.InMemoryCache(),
49
58
  link: new import_client.HttpLink({
50
- uri: process.env.NEXT_PUBLIC_GRAPHQL_ENDPOINT
59
+ uri: getEnv("NEXT_PUBLIC_GRAPHQL_ENDPOINT" /* NEXT_PUBLIC_GRAPHQL_ENDPOINT */)
51
60
  })
52
61
  });
53
62
  });
@@ -7,6 +7,15 @@ import {
7
7
  import { registerApolloClient } from "@apollo/client-integration-nextjs";
8
8
  import { cookies } from "next/headers";
9
9
  import { Suspense } from "react";
10
+
11
+ // src/env.ts
12
+ function getEnv(key, skipCheck) {
13
+ const value = process.env[key];
14
+ if (!skipCheck && !value) throw new Error(`${key} is not defined`);
15
+ return value;
16
+ }
17
+
18
+ // src/graphql/server.tsx
10
19
  function Injector({
11
20
  fetch,
12
21
  Component,
@@ -26,7 +35,7 @@ var { query: gqlQuery } = registerApolloClient(() => {
26
35
  return new ApolloClient({
27
36
  cache: new InMemoryCache(),
28
37
  link: new HttpLink({
29
- uri: process.env.NEXT_PUBLIC_GRAPHQL_ENDPOINT
38
+ uri: getEnv("NEXT_PUBLIC_GRAPHQL_ENDPOINT" /* NEXT_PUBLIC_GRAPHQL_ENDPOINT */)
30
39
  })
31
40
  });
32
41
  });
@@ -34,9 +34,62 @@ __export(utils_exports, {
34
34
  query: () => query
35
35
  });
36
36
  module.exports = __toCommonJS(utils_exports);
37
+ var import_cache = require("next/cache");
38
+ var import_headers2 = require("next/headers");
37
39
  var import_type_graphql = require("type-graphql");
40
+
41
+ // src/auth/constants.ts
42
+ var REFRESH_COOKIE_NAME = "refresh";
43
+
44
+ // src/auth/email/token.ts
45
+ var import_bcryptjs = require("bcryptjs");
46
+ var import_jsonwebtoken = require("jsonwebtoken");
47
+ var import_headers = require("next/headers");
48
+ var import_navigation = require("next/navigation");
49
+ var import_server = require("next/server");
50
+ function getUserIdFromRefreshToken(refreshKey, refreshToken) {
51
+ if (refreshToken)
52
+ try {
53
+ const decoded = (0, import_jsonwebtoken.verify)(refreshToken, refreshKey);
54
+ if (typeof decoded !== "string" && typeof decoded.id === "number")
55
+ return decoded.id;
56
+ } catch (e) {
57
+ if (!(e instanceof import_jsonwebtoken.JsonWebTokenError)) console.error(e, "errors");
58
+ return null;
59
+ }
60
+ return null;
61
+ }
62
+
63
+ // src/graphql/utils.ts
38
64
  function query(fn, options) {
39
- return { ...options, fn };
65
+ return { ...options, fn, call: getCaller(fn, options) };
66
+ }
67
+ var getUserId = async () => {
68
+ const Cookie = await (0, import_headers2.cookies)();
69
+ const refresh = Cookie.get(REFRESH_COOKIE_NAME)?.value;
70
+ return refresh ? getUserIdFromRefreshToken(refresh) : null;
71
+ };
72
+ function getCaller(fn, options) {
73
+ return async (data, config) => {
74
+ if (config?.revalidate) {
75
+ const cachedFn = (data2, config2) => {
76
+ "use cache";
77
+ if (config2?.revalidate) (0, import_cache.cacheLife)(config2.revalidate);
78
+ if (config2?.tags) (0, import_cache.cacheTag)(...config2.tags);
79
+ const ctx2 = {
80
+ userId: null,
81
+ isRefreshID: true
82
+ };
83
+ return fn(ctx2, data2);
84
+ };
85
+ return cachedFn(data, config);
86
+ }
87
+ const ctx = {
88
+ userId: options.authorized ? await getUserId() : null,
89
+ isRefreshID: true
90
+ };
91
+ return fn(ctx, data);
92
+ };
40
93
  }
41
94
  function field(fn, options) {
42
95
  return { ...options, fn };
@@ -1,4 +1,5 @@
1
1
  import { GraphQLScalarType } from 'graphql';
2
+ import { cacheLife } from 'next/cache';
2
3
  import { Query, Arg, ClassType } from 'type-graphql';
3
4
  import { AuthorizedContext, Context } from './types.mjs';
4
5
 
@@ -15,6 +16,7 @@ type NullableOptions<T, X extends boolean> = T & {
15
16
  type ParsedGQLType<T, MergeNullUndefined extends boolean> = T extends StringConstructor ? string : T extends NumberConstructor ? number : T extends BooleanConstructor ? boolean : T extends GraphQLScalarType<infer U> ? U : T extends ClassType<infer U> ? MergeNullUndefined extends true ? NormalizeNullUndefined<U> : U : T extends Record<infer K, string | number> ? T[K] : void;
16
17
  type ParsedGQLTypeWithArray<T, MergeNullUndefined extends boolean> = T extends Array<infer U> ? ParsedGQLType<U, MergeNullUndefined>[] : ParsedGQLType<T, MergeNullUndefined>;
17
18
  type ParsedGQLTypeWithNullability<T, IsNullable extends boolean, MergeNullUndefined extends boolean> = IsNullable extends true ? ParsedGQLTypeWithArray<T, MergeNullUndefined> | null | undefined : ParsedGQLTypeWithArray<T, MergeNullUndefined>;
19
+ type Promisify<T> = T | Promise<T>;
18
20
  interface BaseDefinition<T, U, IsAuth extends boolean = false, OutputNullable extends boolean = false, InputNullable extends boolean = false> {
19
21
  output: T;
20
22
  outputOptions?: NullableOptions<ReturnOptions, OutputNullable>;
@@ -23,12 +25,17 @@ interface BaseDefinition<T, U, IsAuth extends boolean = false, OutputNullable ex
23
25
  authorized?: IsAuth;
24
26
  }
25
27
  interface QueryDefinition<T, U, IsAuth extends boolean = false, OutputNullable extends boolean = false, InputNullable extends boolean = false> extends BaseDefinition<T, U, IsAuth, OutputNullable, InputNullable> {
26
- fn: (ctx: IsAuth extends true ? AuthorizedContext : Context, data: ParsedGQLTypeWithNullability<U, InputNullable, false>) => Promise<ParsedGQLTypeWithNullability<T, OutputNullable, true>> | ParsedGQLTypeWithNullability<T, OutputNullable, true>;
28
+ fn: (ctx: IsAuth extends true ? AuthorizedContext : Context, data: ParsedGQLTypeWithNullability<U, InputNullable, false>) => Promisify<ParsedGQLTypeWithNullability<T, OutputNullable, true>>;
29
+ call: (data: ParsedGQLTypeWithNullability<U, InputNullable, false>, config?: CallerConfig<IsAuth>) => Promisify<ParsedGQLTypeWithNullability<T, OutputNullable, true>>;
27
30
  mutation?: boolean;
28
31
  }
29
- declare function query<T, U, IsAuth extends boolean = false, OutputNullable extends boolean = false, InputNullable extends boolean = false>(fn: QueryDefinition<T, U, IsAuth, OutputNullable, InputNullable>["fn"], options: Omit<QueryDefinition<T, U, IsAuth, OutputNullable, InputNullable>, "fn">): QueryDefinition<T, U, IsAuth, OutputNullable, InputNullable>;
32
+ type CallerConfig<IsAuth extends boolean> = {
33
+ revalidate?: IsAuth extends true ? never : Parameters<typeof cacheLife>[0];
34
+ tags?: IsAuth extends true ? never : string[];
35
+ };
36
+ declare function query<T, U, IsAuth extends boolean = false, OutputNullable extends boolean = false, InputNullable extends boolean = false>(fn: QueryDefinition<T, U, IsAuth, OutputNullable, InputNullable>["fn"], options: Omit<QueryDefinition<T, U, IsAuth, OutputNullable, InputNullable>, "fn" | "call">): QueryDefinition<T, U, IsAuth, OutputNullable, InputNullable>;
30
37
  interface FieldResolverDefinition<T, U, Root, IsAuth extends boolean = false, OutputNullable extends boolean = false, InputNullable extends boolean = false> extends BaseDefinition<T, U, IsAuth, OutputNullable, InputNullable> {
31
- fn: (root: Root, ctx: IsAuth extends true ? AuthorizedContext : Context, data: ParsedGQLTypeWithNullability<U, InputNullable, false>) => Promise<ParsedGQLTypeWithNullability<T, OutputNullable, true>> | ParsedGQLTypeWithNullability<T, OutputNullable, true>;
38
+ fn: (root: Root, ctx: IsAuth extends true ? AuthorizedContext : Context, data: ParsedGQLTypeWithNullability<U, InputNullable, false>) => Promisify<ParsedGQLTypeWithNullability<T, OutputNullable, true>>;
32
39
  }
33
40
  declare function field<T, U, IsAuth extends boolean, Root, OutputNullable extends boolean = false, InputNullable extends boolean = false>(fn: FieldResolverDefinition<T, U, Root, IsAuth, OutputNullable, InputNullable>["fn"], options: Omit<FieldResolverDefinition<T, U, Root, IsAuth, OutputNullable, InputNullable>, "fn">): FieldResolverDefinition<T, U, Root, IsAuth, OutputNullable, InputNullable>;
34
41
  declare function QueryLibrary<T extends Record<string, QueryDefinition<any, any, any, any, any>>>(queries: T): {
@@ -1,4 +1,5 @@
1
1
  import { GraphQLScalarType } from 'graphql';
2
+ import { cacheLife } from 'next/cache';
2
3
  import { Query, Arg, ClassType } from 'type-graphql';
3
4
  import { AuthorizedContext, Context } from './types.js';
4
5
 
@@ -15,6 +16,7 @@ type NullableOptions<T, X extends boolean> = T & {
15
16
  type ParsedGQLType<T, MergeNullUndefined extends boolean> = T extends StringConstructor ? string : T extends NumberConstructor ? number : T extends BooleanConstructor ? boolean : T extends GraphQLScalarType<infer U> ? U : T extends ClassType<infer U> ? MergeNullUndefined extends true ? NormalizeNullUndefined<U> : U : T extends Record<infer K, string | number> ? T[K] : void;
16
17
  type ParsedGQLTypeWithArray<T, MergeNullUndefined extends boolean> = T extends Array<infer U> ? ParsedGQLType<U, MergeNullUndefined>[] : ParsedGQLType<T, MergeNullUndefined>;
17
18
  type ParsedGQLTypeWithNullability<T, IsNullable extends boolean, MergeNullUndefined extends boolean> = IsNullable extends true ? ParsedGQLTypeWithArray<T, MergeNullUndefined> | null | undefined : ParsedGQLTypeWithArray<T, MergeNullUndefined>;
19
+ type Promisify<T> = T | Promise<T>;
18
20
  interface BaseDefinition<T, U, IsAuth extends boolean = false, OutputNullable extends boolean = false, InputNullable extends boolean = false> {
19
21
  output: T;
20
22
  outputOptions?: NullableOptions<ReturnOptions, OutputNullable>;
@@ -23,12 +25,17 @@ interface BaseDefinition<T, U, IsAuth extends boolean = false, OutputNullable ex
23
25
  authorized?: IsAuth;
24
26
  }
25
27
  interface QueryDefinition<T, U, IsAuth extends boolean = false, OutputNullable extends boolean = false, InputNullable extends boolean = false> extends BaseDefinition<T, U, IsAuth, OutputNullable, InputNullable> {
26
- fn: (ctx: IsAuth extends true ? AuthorizedContext : Context, data: ParsedGQLTypeWithNullability<U, InputNullable, false>) => Promise<ParsedGQLTypeWithNullability<T, OutputNullable, true>> | ParsedGQLTypeWithNullability<T, OutputNullable, true>;
28
+ fn: (ctx: IsAuth extends true ? AuthorizedContext : Context, data: ParsedGQLTypeWithNullability<U, InputNullable, false>) => Promisify<ParsedGQLTypeWithNullability<T, OutputNullable, true>>;
29
+ call: (data: ParsedGQLTypeWithNullability<U, InputNullable, false>, config?: CallerConfig<IsAuth>) => Promisify<ParsedGQLTypeWithNullability<T, OutputNullable, true>>;
27
30
  mutation?: boolean;
28
31
  }
29
- declare function query<T, U, IsAuth extends boolean = false, OutputNullable extends boolean = false, InputNullable extends boolean = false>(fn: QueryDefinition<T, U, IsAuth, OutputNullable, InputNullable>["fn"], options: Omit<QueryDefinition<T, U, IsAuth, OutputNullable, InputNullable>, "fn">): QueryDefinition<T, U, IsAuth, OutputNullable, InputNullable>;
32
+ type CallerConfig<IsAuth extends boolean> = {
33
+ revalidate?: IsAuth extends true ? never : Parameters<typeof cacheLife>[0];
34
+ tags?: IsAuth extends true ? never : string[];
35
+ };
36
+ declare function query<T, U, IsAuth extends boolean = false, OutputNullable extends boolean = false, InputNullable extends boolean = false>(fn: QueryDefinition<T, U, IsAuth, OutputNullable, InputNullable>["fn"], options: Omit<QueryDefinition<T, U, IsAuth, OutputNullable, InputNullable>, "fn" | "call">): QueryDefinition<T, U, IsAuth, OutputNullable, InputNullable>;
30
37
  interface FieldResolverDefinition<T, U, Root, IsAuth extends boolean = false, OutputNullable extends boolean = false, InputNullable extends boolean = false> extends BaseDefinition<T, U, IsAuth, OutputNullable, InputNullable> {
31
- fn: (root: Root, ctx: IsAuth extends true ? AuthorizedContext : Context, data: ParsedGQLTypeWithNullability<U, InputNullable, false>) => Promise<ParsedGQLTypeWithNullability<T, OutputNullable, true>> | ParsedGQLTypeWithNullability<T, OutputNullable, true>;
38
+ fn: (root: Root, ctx: IsAuth extends true ? AuthorizedContext : Context, data: ParsedGQLTypeWithNullability<U, InputNullable, false>) => Promisify<ParsedGQLTypeWithNullability<T, OutputNullable, true>>;
32
39
  }
33
40
  declare function field<T, U, IsAuth extends boolean, Root, OutputNullable extends boolean = false, InputNullable extends boolean = false>(fn: FieldResolverDefinition<T, U, Root, IsAuth, OutputNullable, InputNullable>["fn"], options: Omit<FieldResolverDefinition<T, U, Root, IsAuth, OutputNullable, InputNullable>, "fn">): FieldResolverDefinition<T, U, Root, IsAuth, OutputNullable, InputNullable>;
34
41
  declare function QueryLibrary<T extends Record<string, QueryDefinition<any, any, any, any, any>>>(queries: T): {
@@ -10,6 +10,8 @@ var __decorateClass = (decorators, target, key, kind) => {
10
10
  };
11
11
 
12
12
  // src/graphql/utils.ts
13
+ import { cacheLife, cacheTag } from "next/cache";
14
+ import { cookies as cookies2 } from "next/headers";
13
15
  import {
14
16
  Arg,
15
17
  Authorized,
@@ -20,8 +22,59 @@ import {
20
22
  Resolver,
21
23
  Root
22
24
  } from "type-graphql";
25
+
26
+ // src/auth/constants.ts
27
+ var REFRESH_COOKIE_NAME = "refresh";
28
+
29
+ // src/auth/email/token.ts
30
+ import { compare } from "bcryptjs";
31
+ import { JsonWebTokenError, sign, verify } from "jsonwebtoken";
32
+ import { cookies } from "next/headers";
33
+ import { redirect } from "next/navigation";
34
+ import { NextResponse } from "next/server";
35
+ function getUserIdFromRefreshToken(refreshKey, refreshToken) {
36
+ if (refreshToken)
37
+ try {
38
+ const decoded = verify(refreshToken, refreshKey);
39
+ if (typeof decoded !== "string" && typeof decoded.id === "number")
40
+ return decoded.id;
41
+ } catch (e) {
42
+ if (!(e instanceof JsonWebTokenError)) console.error(e, "errors");
43
+ return null;
44
+ }
45
+ return null;
46
+ }
47
+
48
+ // src/graphql/utils.ts
23
49
  function query(fn, options) {
24
- return { ...options, fn };
50
+ return { ...options, fn, call: getCaller(fn, options) };
51
+ }
52
+ var getUserId = async () => {
53
+ const Cookie = await cookies2();
54
+ const refresh = Cookie.get(REFRESH_COOKIE_NAME)?.value;
55
+ return refresh ? getUserIdFromRefreshToken(refresh) : null;
56
+ };
57
+ function getCaller(fn, options) {
58
+ return async (data, config) => {
59
+ if (config?.revalidate) {
60
+ const cachedFn = (data2, config2) => {
61
+ "use cache";
62
+ if (config2?.revalidate) cacheLife(config2.revalidate);
63
+ if (config2?.tags) cacheTag(...config2.tags);
64
+ const ctx2 = {
65
+ userId: null,
66
+ isRefreshID: true
67
+ };
68
+ return fn(ctx2, data2);
69
+ };
70
+ return cachedFn(data, config);
71
+ }
72
+ const ctx = {
73
+ userId: options.authorized ? await getUserId() : null,
74
+ isRefreshID: true
75
+ };
76
+ return fn(ctx, data);
77
+ };
25
78
  }
26
79
  function field(fn, options) {
27
80
  return { ...options, fn };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "naystack",
3
- "version": "1.4.16",
3
+ "version": "1.4.20",
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",