naystack 1.1.10-beta.7 → 1.1.11

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,126 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/graphql/index.ts
21
+ var graphql_exports = {};
22
+ __export(graphql_exports, {
23
+ initGraphQLServer: () => initGraphQLServer
24
+ });
25
+ module.exports = __toCommonJS(graphql_exports);
26
+
27
+ // src/graphql/init.ts
28
+ var import_reflect_metadata = require("reflect-metadata");
29
+ var import_server3 = require("@apollo/server");
30
+ var import_default = require("@apollo/server/plugin/landingPage/default");
31
+ var import_next = require("@as-integrations/next");
32
+ var import_type_graphql = require("type-graphql");
33
+
34
+ // src/graphql/context.ts
35
+ var import_jsonwebtoken2 = require("jsonwebtoken");
36
+
37
+ // src/auth/email/token.ts
38
+ var import_bcryptjs = require("bcryptjs");
39
+ var import_jsonwebtoken = require("jsonwebtoken");
40
+ var import_server = require("next/server");
41
+ function getUserIdFromRefreshToken(refreshKey, refreshToken) {
42
+ if (refreshToken)
43
+ try {
44
+ const decoded = (0, import_jsonwebtoken.verify)(refreshToken, refreshKey);
45
+ if (typeof decoded !== "string" && typeof decoded.id === "number")
46
+ return decoded.id;
47
+ } catch (e) {
48
+ if (!(e instanceof import_jsonwebtoken.JsonWebTokenError)) console.error(e, "errors");
49
+ return null;
50
+ }
51
+ return null;
52
+ }
53
+
54
+ // src/auth/utils/errors.ts
55
+ var import_server2 = require("next/server");
56
+ function handleError(status, message, onError) {
57
+ const res = onError?.({ status, message });
58
+ if (res) return res;
59
+ return new import_server2.NextResponse(message, { status });
60
+ }
61
+
62
+ // src/graphql/context.ts
63
+ var getContext = (refreshKey, signingKey) => async (req) => {
64
+ const bearer = req.headers.get("authorization");
65
+ const isMobile = req.headers.get("x-platform-is-mobile");
66
+ if (!bearer) {
67
+ if (isMobile) return { userId: null };
68
+ const refresh = req.cookies.get("refresh")?.value;
69
+ const userId = getUserIdFromRefreshToken(refreshKey, refresh);
70
+ if (userId) return { userId, onlyQuery: true };
71
+ return { userId: null };
72
+ }
73
+ const token = bearer.slice(7);
74
+ try {
75
+ const res = (0, import_jsonwebtoken2.verify)(token, signingKey);
76
+ return { userId: typeof res !== "string" ? res.id : null };
77
+ } catch {
78
+ if (isMobile) return handleError(401, "Refresh token is invalid.");
79
+ }
80
+ return { userId: null };
81
+ };
82
+
83
+ // src/graphql/init.ts
84
+ async function initGraphQLServer({
85
+ authChecker,
86
+ resolvers,
87
+ plugins,
88
+ refreshKey,
89
+ signingKey
90
+ }) {
91
+ const { typeDefs, resolvers: builtResolvers } = await (0, import_type_graphql.buildTypeDefsAndResolvers)({
92
+ validate: true,
93
+ authChecker,
94
+ resolvers
95
+ });
96
+ const server = new import_server3.ApolloServer({
97
+ typeDefs,
98
+ resolvers: builtResolvers,
99
+ plugins: [
100
+ process.env.NODE_ENV === "production" ? (0, import_default.ApolloServerPluginLandingPageProductionDefault)() : (0, import_default.ApolloServerPluginLandingPageLocalDefault)(),
101
+ {
102
+ async requestDidStart({ request, contextValue }) {
103
+ if (
104
+ // eslint-disable-next-line
105
+ contextValue.onlyQuery && !request.query?.startsWith("query")
106
+ )
107
+ contextValue.userId = null;
108
+ }
109
+ },
110
+ ...plugins || []
111
+ ],
112
+ introspection: process.env.NODE_ENV !== "production",
113
+ status400ForVariableCoercionErrors: true
114
+ });
115
+ const handler = (0, import_next.startServerAndCreateNextHandler)(server, {
116
+ context: getContext(refreshKey, signingKey)
117
+ });
118
+ return {
119
+ GET: (request) => handler(request),
120
+ POST: (request) => handler(request)
121
+ };
122
+ }
123
+ // Annotate the CommonJS export names for ESM import in node:
124
+ 0 && (module.exports = {
125
+ initGraphQLServer
126
+ });
@@ -0,0 +1,23 @@
1
+ import { ApolloServerPlugin } from '@apollo/server';
2
+ import { NextRequest } from 'next/server';
3
+ import { AuthChecker, NonEmptyArray } from 'type-graphql';
4
+
5
+ declare function initGraphQLServer({ authChecker, resolvers, plugins, refreshKey, signingKey, }: {
6
+ authChecker?: AuthChecker<any>;
7
+ resolvers: NonEmptyArray<Function>;
8
+ plugins?: ApolloServerPlugin[];
9
+ refreshKey: string;
10
+ signingKey: string;
11
+ }): Promise<{
12
+ GET: (request: NextRequest) => Promise<Response>;
13
+ POST: (request: NextRequest) => Promise<Response>;
14
+ }>;
15
+
16
+ interface Context {
17
+ userId: number | null;
18
+ }
19
+ interface AuthorizedContext {
20
+ userId: number;
21
+ }
22
+
23
+ export { type AuthorizedContext, type Context, initGraphQLServer };
@@ -0,0 +1,23 @@
1
+ import { ApolloServerPlugin } from '@apollo/server';
2
+ import { NextRequest } from 'next/server';
3
+ import { AuthChecker, NonEmptyArray } from 'type-graphql';
4
+
5
+ declare function initGraphQLServer({ authChecker, resolvers, plugins, refreshKey, signingKey, }: {
6
+ authChecker?: AuthChecker<any>;
7
+ resolvers: NonEmptyArray<Function>;
8
+ plugins?: ApolloServerPlugin[];
9
+ refreshKey: string;
10
+ signingKey: string;
11
+ }): Promise<{
12
+ GET: (request: NextRequest) => Promise<Response>;
13
+ POST: (request: NextRequest) => Promise<Response>;
14
+ }>;
15
+
16
+ interface Context {
17
+ userId: number | null;
18
+ }
19
+ interface AuthorizedContext {
20
+ userId: number;
21
+ }
22
+
23
+ export { type AuthorizedContext, type Context, initGraphQLServer };
@@ -0,0 +1,104 @@
1
+ // src/graphql/init.ts
2
+ import "reflect-metadata";
3
+ import { ApolloServer } from "@apollo/server";
4
+ import {
5
+ ApolloServerPluginLandingPageLocalDefault,
6
+ ApolloServerPluginLandingPageProductionDefault
7
+ } from "@apollo/server/plugin/landingPage/default";
8
+ import { startServerAndCreateNextHandler } from "@as-integrations/next";
9
+ import {
10
+ buildTypeDefsAndResolvers
11
+ } from "type-graphql";
12
+
13
+ // src/graphql/context.ts
14
+ import { verify as verify2 } from "jsonwebtoken";
15
+
16
+ // src/auth/email/token.ts
17
+ import { compare } from "bcryptjs";
18
+ import { JsonWebTokenError, sign, verify } from "jsonwebtoken";
19
+ import { NextResponse } from "next/server";
20
+ function getUserIdFromRefreshToken(refreshKey, refreshToken) {
21
+ if (refreshToken)
22
+ try {
23
+ const decoded = verify(refreshToken, refreshKey);
24
+ if (typeof decoded !== "string" && typeof decoded.id === "number")
25
+ return decoded.id;
26
+ } catch (e) {
27
+ if (!(e instanceof JsonWebTokenError)) console.error(e, "errors");
28
+ return null;
29
+ }
30
+ return null;
31
+ }
32
+
33
+ // src/auth/utils/errors.ts
34
+ import { NextResponse as NextResponse2 } from "next/server";
35
+ function handleError(status, message, onError) {
36
+ const res = onError?.({ status, message });
37
+ if (res) return res;
38
+ return new NextResponse2(message, { status });
39
+ }
40
+
41
+ // src/graphql/context.ts
42
+ var getContext = (refreshKey, signingKey) => async (req) => {
43
+ const bearer = req.headers.get("authorization");
44
+ const isMobile = req.headers.get("x-platform-is-mobile");
45
+ if (!bearer) {
46
+ if (isMobile) return { userId: null };
47
+ const refresh = req.cookies.get("refresh")?.value;
48
+ const userId = getUserIdFromRefreshToken(refreshKey, refresh);
49
+ if (userId) return { userId, onlyQuery: true };
50
+ return { userId: null };
51
+ }
52
+ const token = bearer.slice(7);
53
+ try {
54
+ const res = verify2(token, signingKey);
55
+ return { userId: typeof res !== "string" ? res.id : null };
56
+ } catch {
57
+ if (isMobile) return handleError(401, "Refresh token is invalid.");
58
+ }
59
+ return { userId: null };
60
+ };
61
+
62
+ // src/graphql/init.ts
63
+ async function initGraphQLServer({
64
+ authChecker,
65
+ resolvers,
66
+ plugins,
67
+ refreshKey,
68
+ signingKey
69
+ }) {
70
+ const { typeDefs, resolvers: builtResolvers } = await buildTypeDefsAndResolvers({
71
+ validate: true,
72
+ authChecker,
73
+ resolvers
74
+ });
75
+ const server = new ApolloServer({
76
+ typeDefs,
77
+ resolvers: builtResolvers,
78
+ plugins: [
79
+ process.env.NODE_ENV === "production" ? ApolloServerPluginLandingPageProductionDefault() : ApolloServerPluginLandingPageLocalDefault(),
80
+ {
81
+ async requestDidStart({ request, contextValue }) {
82
+ if (
83
+ // eslint-disable-next-line
84
+ contextValue.onlyQuery && !request.query?.startsWith("query")
85
+ )
86
+ contextValue.userId = null;
87
+ }
88
+ },
89
+ ...plugins || []
90
+ ],
91
+ introspection: process.env.NODE_ENV !== "production",
92
+ status400ForVariableCoercionErrors: true
93
+ });
94
+ const handler = startServerAndCreateNextHandler(server, {
95
+ context: getContext(refreshKey, signingKey)
96
+ });
97
+ return {
98
+ GET: (request) => handler(request),
99
+ POST: (request) => handler(request)
100
+ };
101
+ }
102
+ export {
103
+ initGraphQLServer
104
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "naystack",
3
- "version": "1.1.10-beta.7",
3
+ "version": "1.1.11",
4
4
  "description": "A stack built with tight Next + Drizzle + GraphQL",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",
@@ -22,6 +22,11 @@
22
22
  "types": "./dist/socials/index.d.ts",
23
23
  "import": "./dist/socials/index.esm.js",
24
24
  "require": "./dist/socials/index.cjs.js"
25
+ },
26
+ "./graphql": {
27
+ "types": "./dist/graphql/index.d.ts",
28
+ "import": "./dist/graphql/index.esm.js",
29
+ "require": "./dist/graphql/index.cjs.js"
25
30
  }
26
31
  },
27
32
  "keywords": [
@@ -44,11 +49,14 @@
44
49
  "author": "Abhinay Pandey <abhinaypandey02@gmail.com>",
45
50
  "license": "ISC",
46
51
  "dependencies": {
52
+ "@apollo/server": "^4.12.0",
53
+ "@as-integrations/next": "^3.2.0",
47
54
  "@vercel/functions": "^3.1.0",
48
55
  "bcryptjs": "^3.0.2",
49
56
  "drizzle-orm": "^0.44.5",
50
57
  "jsonwebtoken": "^9.0.2",
51
58
  "next": "^15.5.3",
59
+ "reflect-metadata": "^0.2.2",
52
60
  "type-graphql": "2.0.0-rc.2"
53
61
  },
54
62
  "devDependencies": {