@ttoss/graphql-api-server 0.16.0 → 0.17.0

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/index.cjs CHANGED
@@ -2,12 +2,44 @@
2
2
  Object.defineProperty(exports, Symbol.toStringTag, {
3
3
  value: 'Module'
4
4
  });
5
- let _ttoss_http_server = require("@ttoss/http-server");
6
- let _ttoss_graphql_api = require("@ttoss/graphql-api");
7
5
  let _ttoss_auth_core_amazon_cognito = require("@ttoss/auth-core/amazon-cognito");
6
+ let _ttoss_graphql_api = require("@ttoss/graphql-api");
7
+ let _ttoss_http_server = require("@ttoss/http-server");
8
8
  let graphql_yoga = require("graphql-yoga");
9
9
 
10
10
  //#region src/index.ts
11
+ var createJwtVerifier = ({
12
+ authenticationType,
13
+ userPoolConfig
14
+ }) => {
15
+ if (authenticationType === "AMAZON_COGNITO_USER_POOLS") {
16
+ if (!userPoolConfig) throw new Error("userPoolConfig is required when using AMAZON_COGNITO_USER_POOLS authenticationType");
17
+ return _ttoss_auth_core_amazon_cognito.CognitoJwtVerifier.create({
18
+ tokenUse: "access",
19
+ ...userPoolConfig
20
+ });
21
+ }
22
+ return null;
23
+ };
24
+ /**
25
+ * Verify the JWT token and set `ctx.identity`. Returns `false` if the token
26
+ * is invalid.
27
+ */
28
+ var authenticate = async ({
29
+ ctx,
30
+ authenticationType,
31
+ jwtVerifier
32
+ }) => {
33
+ try {
34
+ if (authenticationType === "AMAZON_COGNITO_USER_POOLS" && jwtVerifier) {
35
+ const token = ctx.headers.authorization?.replace("Bearer ", "");
36
+ ctx.identity = await jwtVerifier.verify(token || "");
37
+ }
38
+ return true;
39
+ } catch {
40
+ return false;
41
+ }
42
+ };
11
43
  var createServer = ({
12
44
  authenticationType,
13
45
  userPoolConfig,
@@ -30,16 +62,10 @@ var createServer = ({
30
62
  */
31
63
  cors: false
32
64
  });
33
- const jwtVerifier = (() => {
34
- if (authenticationType === "AMAZON_COGNITO_USER_POOLS") {
35
- if (!userPoolConfig) throw new Error("userPoolConfig is required when using AMAZON_COGNITO_USER_POOLS authenticationType");
36
- return _ttoss_auth_core_amazon_cognito.CognitoJwtVerifier.create({
37
- tokenUse: "access",
38
- ...userPoolConfig
39
- });
40
- }
41
- return null;
42
- })();
65
+ const jwtVerifier = createJwtVerifier({
66
+ authenticationType,
67
+ userPoolConfig
68
+ });
43
69
  app.use(async (ctx, next) => {
44
70
  /**
45
71
  * Check if the request is for the GraphQL endpoint.
@@ -50,12 +76,11 @@ var createServer = ({
50
76
  * If the request is not a GraphiQL request, verify the JWT token, else
51
77
  * set Unauthorized status code and return.
52
78
  */
53
- if (!(ctx.headers.accept?.includes("text/html") && graphiql)) try {
54
- if (authenticationType === "AMAZON_COGNITO_USER_POOLS" && jwtVerifier) {
55
- const token = ctx.headers.authorization?.replace("Bearer ", "");
56
- ctx.identity = await jwtVerifier.verify(token || "");
57
- }
58
- } catch {
79
+ if (!(ctx.headers.accept?.includes("text/html") && graphiql) && !(await authenticate({
80
+ ctx,
81
+ authenticationType,
82
+ jwtVerifier
83
+ }))) {
59
84
  ctx.status = 401;
60
85
  ctx.body = "Unauthorized";
61
86
  return;
@@ -71,9 +96,7 @@ var createServer = ({
71
96
  /**
72
97
  * Set headers
73
98
  */
74
- response.headers.forEach((value, key) => {
75
- ctx.append(key, value);
76
- });
99
+ for (const [key, value] of response.headers.entries()) ctx.append(key, value);
77
100
  ctx.body = response.body;
78
101
  });
79
102
  return app;
package/dist/index.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
 
2
- import { App, Router, cors } from "@ttoss/http-server";
3
2
  import { BuildSchemaInput } from "@ttoss/graphql-api";
3
+ import { App, Router, cors } from "@ttoss/http-server";
4
4
  //#region src/index.d.ts
5
5
  export type AuthenticationType = 'AMAZON_COGNITO_USER_POOLS';
6
6
  export type CreateServerInput = {
package/dist/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
 
2
- import { App, Router, cors } from "@ttoss/http-server";
3
2
  import { BuildSchemaInput } from "@ttoss/graphql-api";
3
+ import { App, Router, cors } from "@ttoss/http-server";
4
4
  //#region src/index.d.ts
5
5
  export type AuthenticationType = 'AMAZON_COGNITO_USER_POOLS';
6
6
  export type CreateServerInput = {
package/dist/index.mjs CHANGED
@@ -1,10 +1,42 @@
1
1
  /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
- import { App, Router, cors } from "@ttoss/http-server";
3
- import { buildSchema } from "@ttoss/graphql-api";
4
2
  import { CognitoJwtVerifier } from "@ttoss/auth-core/amazon-cognito";
3
+ import { buildSchema } from "@ttoss/graphql-api";
4
+ import { App, Router, cors } from "@ttoss/http-server";
5
5
  import { createYoga } from "graphql-yoga";
6
6
 
7
7
  //#region src/index.ts
8
+ var createJwtVerifier = ({
9
+ authenticationType,
10
+ userPoolConfig
11
+ }) => {
12
+ if (authenticationType === "AMAZON_COGNITO_USER_POOLS") {
13
+ if (!userPoolConfig) throw new Error("userPoolConfig is required when using AMAZON_COGNITO_USER_POOLS authenticationType");
14
+ return CognitoJwtVerifier.create({
15
+ tokenUse: "access",
16
+ ...userPoolConfig
17
+ });
18
+ }
19
+ return null;
20
+ };
21
+ /**
22
+ * Verify the JWT token and set `ctx.identity`. Returns `false` if the token
23
+ * is invalid.
24
+ */
25
+ var authenticate = async ({
26
+ ctx,
27
+ authenticationType,
28
+ jwtVerifier
29
+ }) => {
30
+ try {
31
+ if (authenticationType === "AMAZON_COGNITO_USER_POOLS" && jwtVerifier) {
32
+ const token = ctx.headers.authorization?.replace("Bearer ", "");
33
+ ctx.identity = await jwtVerifier.verify(token || "");
34
+ }
35
+ return true;
36
+ } catch {
37
+ return false;
38
+ }
39
+ };
8
40
  var createServer = ({
9
41
  authenticationType,
10
42
  userPoolConfig,
@@ -27,16 +59,10 @@ var createServer = ({
27
59
  */
28
60
  cors: false
29
61
  });
30
- const jwtVerifier = (() => {
31
- if (authenticationType === "AMAZON_COGNITO_USER_POOLS") {
32
- if (!userPoolConfig) throw new Error("userPoolConfig is required when using AMAZON_COGNITO_USER_POOLS authenticationType");
33
- return CognitoJwtVerifier.create({
34
- tokenUse: "access",
35
- ...userPoolConfig
36
- });
37
- }
38
- return null;
39
- })();
62
+ const jwtVerifier = createJwtVerifier({
63
+ authenticationType,
64
+ userPoolConfig
65
+ });
40
66
  app.use(async (ctx, next) => {
41
67
  /**
42
68
  * Check if the request is for the GraphQL endpoint.
@@ -47,12 +73,11 @@ var createServer = ({
47
73
  * If the request is not a GraphiQL request, verify the JWT token, else
48
74
  * set Unauthorized status code and return.
49
75
  */
50
- if (!(ctx.headers.accept?.includes("text/html") && graphiql)) try {
51
- if (authenticationType === "AMAZON_COGNITO_USER_POOLS" && jwtVerifier) {
52
- const token = ctx.headers.authorization?.replace("Bearer ", "");
53
- ctx.identity = await jwtVerifier.verify(token || "");
54
- }
55
- } catch {
76
+ if (!(ctx.headers.accept?.includes("text/html") && graphiql) && !(await authenticate({
77
+ ctx,
78
+ authenticationType,
79
+ jwtVerifier
80
+ }))) {
56
81
  ctx.status = 401;
57
82
  ctx.body = "Unauthorized";
58
83
  return;
@@ -68,9 +93,7 @@ var createServer = ({
68
93
  /**
69
94
  * Set headers
70
95
  */
71
- response.headers.forEach((value, key) => {
72
- ctx.append(key, value);
73
- });
96
+ for (const [key, value] of response.headers.entries()) ctx.append(key, value);
74
97
  ctx.body = response.body;
75
98
  });
76
99
  return app;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/graphql-api-server",
3
- "version": "0.16.0",
3
+ "version": "0.17.0",
4
4
  "description": "GraphQL API Server",
5
5
  "keywords": [
6
6
  "api",
@@ -30,8 +30,8 @@
30
30
  ],
31
31
  "dependencies": {
32
32
  "graphql-yoga": "^5.24.4",
33
- "@ttoss/http-server": "^0.14.0",
34
- "@ttoss/auth-core": "^0.19.0"
33
+ "@ttoss/auth-core": "^0.20.0",
34
+ "@ttoss/http-server": "^0.14.1"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@types/supertest": "^7.2.1",
@@ -39,12 +39,12 @@
39
39
  "jest": "^30.5.2",
40
40
  "supertest": "^7.3.1",
41
41
  "tsdown": "^0.23.1",
42
- "@ttoss/config": "^2.0.0",
43
- "@ttoss/graphql-api": "^0.14.0"
42
+ "@ttoss/config": "^3.0.0",
43
+ "@ttoss/graphql-api": "^0.15.0"
44
44
  },
45
45
  "peerDependencies": {
46
46
  "graphql": "^16.6.0",
47
- "@ttoss/graphql-api": "^0.14.0"
47
+ "@ttoss/graphql-api": "^0.15.0"
48
48
  },
49
49
  "publishConfig": {
50
50
  "access": "public",