@ttoss/graphql-api-server 0.11.15 → 0.11.17

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 ADDED
@@ -0,0 +1,84 @@
1
+ /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
+ Object.defineProperty(exports, Symbol.toStringTag, {
3
+ value: 'Module'
4
+ });
5
+ let _ttoss_http_server = require("@ttoss/http-server");
6
+ let _ttoss_graphql_api = require("@ttoss/graphql-api");
7
+ let _ttoss_auth_core_amazon_cognito = require("@ttoss/auth-core/amazon-cognito");
8
+ let graphql_yoga = require("graphql-yoga");
9
+
10
+ //#region src/index.ts
11
+ const createServer = ({
12
+ authenticationType,
13
+ userPoolConfig,
14
+ graphiql,
15
+ cors: corsOptions,
16
+ ...buildSchemaInput
17
+ }) => {
18
+ const app = new _ttoss_http_server.App();
19
+ app.use((0, _ttoss_http_server.cors)(corsOptions));
20
+ /**
21
+ * https://the-guild.dev/graphql/yoga-server/docs/integrations/integration-with-koa
22
+ */
23
+ const yoga = (0, graphql_yoga.createYoga)({
24
+ schema: (0, _ttoss_graphql_api.buildSchema)(buildSchemaInput),
25
+ graphiql,
26
+ landingPage: false,
27
+ logging: false,
28
+ /**
29
+ * Disable CORS, as it's handled by Koa middleware
30
+ */
31
+ cors: false
32
+ });
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
+ })();
43
+ app.use(async (ctx, next) => {
44
+ /**
45
+ * Check if the request is for the GraphQL endpoint.
46
+ * If not, pass it to the next middleware.
47
+ */
48
+ if (ctx.path !== "/graphql") return next();
49
+ /**
50
+ * If the request is not a GraphiQL request, verify the JWT token, else
51
+ * set Unauthorized status code and return.
52
+ */
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 {
59
+ ctx.status = 401;
60
+ ctx.body = "Unauthorized";
61
+ return;
62
+ }
63
+ /**
64
+ * https://the-guild.dev/graphql/yoga-server/docs/integrations/integration-with-koa
65
+ */
66
+ const response = await yoga.handleNodeRequestAndResponse(ctx.req, ctx.res, ctx);
67
+ /**
68
+ * Set status code
69
+ */
70
+ ctx.status = response.status;
71
+ /**
72
+ * Set headers
73
+ */
74
+ response.headers.forEach((value, key) => {
75
+ ctx.append(key, value);
76
+ });
77
+ ctx.body = response.body;
78
+ });
79
+ return app;
80
+ };
81
+
82
+ //#endregion
83
+ exports.Router = _ttoss_http_server.Router;
84
+ exports.createServer = createServer;
@@ -0,0 +1,26 @@
1
+
2
+ import { App, Router, cors } from "@ttoss/http-server";
3
+ import { BuildSchemaInput } from "@ttoss/graphql-api";
4
+
5
+ //#region src/index.d.ts
6
+ type AuthenticationType = 'AMAZON_COGNITO_USER_POOLS';
7
+ type CreateServerInput = {
8
+ graphiql?: boolean;
9
+ authenticationType?: AuthenticationType;
10
+ userPoolConfig?: {
11
+ userPoolId: string;
12
+ tokenUse?: 'access' | 'id';
13
+ clientId: string;
14
+ };
15
+ cors?: cors.Options;
16
+ } & BuildSchemaInput;
17
+ type ServerContext = App.Context;
18
+ declare const createServer: ({
19
+ authenticationType,
20
+ userPoolConfig,
21
+ graphiql,
22
+ cors: corsOptions,
23
+ ...buildSchemaInput
24
+ }: CreateServerInput) => App;
25
+ //#endregion
26
+ export { AuthenticationType, CreateServerInput, Router, ServerContext, createServer };
@@ -0,0 +1,26 @@
1
+
2
+ import { App, Router, cors } from "@ttoss/http-server";
3
+ import { BuildSchemaInput } from "@ttoss/graphql-api";
4
+
5
+ //#region src/index.d.ts
6
+ type AuthenticationType = 'AMAZON_COGNITO_USER_POOLS';
7
+ type CreateServerInput = {
8
+ graphiql?: boolean;
9
+ authenticationType?: AuthenticationType;
10
+ userPoolConfig?: {
11
+ userPoolId: string;
12
+ tokenUse?: 'access' | 'id';
13
+ clientId: string;
14
+ };
15
+ cors?: cors.Options;
16
+ } & BuildSchemaInput;
17
+ type ServerContext = App.Context;
18
+ declare const createServer: ({
19
+ authenticationType,
20
+ userPoolConfig,
21
+ graphiql,
22
+ cors: corsOptions,
23
+ ...buildSchemaInput
24
+ }: CreateServerInput) => App;
25
+ //#endregion
26
+ export { AuthenticationType, CreateServerInput, Router, ServerContext, createServer };
@@ -1,16 +1,11 @@
1
1
  /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
- var __defProp = Object.defineProperty;
3
- var __name = (target, value) => __defProp(target, "name", {
4
- value,
5
- configurable: true
6
- });
7
-
8
- // src/index.ts
9
2
  import { App, Router, cors } from "@ttoss/http-server";
10
3
  import { buildSchema } from "@ttoss/graphql-api";
11
4
  import { CognitoJwtVerifier } from "@ttoss/auth-core/amazon-cognito";
12
5
  import { createYoga } from "graphql-yoga";
13
- var createServer = /* @__PURE__ */__name(({
6
+
7
+ //#region src/index.ts
8
+ const createServer = ({
14
9
  authenticationType,
15
10
  userPoolConfig,
16
11
  graphiql,
@@ -19,6 +14,9 @@ var createServer = /* @__PURE__ */__name(({
19
14
  }) => {
20
15
  const app = new App();
21
16
  app.use(cors(corsOptions));
17
+ /**
18
+ * https://the-guild.dev/graphql/yoga-server/docs/integrations/integration-with-koa
19
+ */
22
20
  const yoga = createYoga({
23
21
  schema: buildSchema(buildSchemaInput),
24
22
  graphiql,
@@ -31,9 +29,7 @@ var createServer = /* @__PURE__ */__name(({
31
29
  });
32
30
  const jwtVerifier = (() => {
33
31
  if (authenticationType === "AMAZON_COGNITO_USER_POOLS") {
34
- if (!userPoolConfig) {
35
- throw new Error("userPoolConfig is required when using AMAZON_COGNITO_USER_POOLS authenticationType");
36
- }
32
+ if (!userPoolConfig) throw new Error("userPoolConfig is required when using AMAZON_COGNITO_USER_POOLS authenticationType");
37
33
  return CognitoJwtVerifier.create({
38
34
  tokenUse: "access",
39
35
  ...userPoolConfig
@@ -42,30 +38,43 @@ var createServer = /* @__PURE__ */__name(({
42
38
  return null;
43
39
  })();
44
40
  app.use(async (ctx, next) => {
45
- if (ctx.path !== "/graphql") {
46
- return next();
47
- }
48
- const isGraphiqlRequest = ctx.headers.accept?.includes("text/html") && graphiql;
49
- if (!isGraphiqlRequest) {
50
- try {
51
- if (authenticationType === "AMAZON_COGNITO_USER_POOLS" && jwtVerifier) {
52
- const token = ctx.headers.authorization?.replace("Bearer ", "");
53
- const identity = await jwtVerifier.verify(token || "");
54
- ctx.identity = identity;
55
- }
56
- } catch {
57
- ctx.status = 401;
58
- ctx.body = "Unauthorized";
59
- return;
41
+ /**
42
+ * Check if the request is for the GraphQL endpoint.
43
+ * If not, pass it to the next middleware.
44
+ */
45
+ if (ctx.path !== "/graphql") return next();
46
+ /**
47
+ * If the request is not a GraphiQL request, verify the JWT token, else
48
+ * set Unauthorized status code and return.
49
+ */
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 || "");
60
54
  }
55
+ } catch {
56
+ ctx.status = 401;
57
+ ctx.body = "Unauthorized";
58
+ return;
61
59
  }
60
+ /**
61
+ * https://the-guild.dev/graphql/yoga-server/docs/integrations/integration-with-koa
62
+ */
62
63
  const response = await yoga.handleNodeRequestAndResponse(ctx.req, ctx.res, ctx);
64
+ /**
65
+ * Set status code
66
+ */
63
67
  ctx.status = response.status;
68
+ /**
69
+ * Set headers
70
+ */
64
71
  response.headers.forEach((value, key) => {
65
72
  ctx.append(key, value);
66
73
  });
67
74
  ctx.body = response.body;
68
75
  });
69
76
  return app;
70
- }, "createServer");
77
+ };
78
+
79
+ //#endregion
71
80
  export { Router, createServer };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/graphql-api-server",
3
- "version": "0.11.15",
3
+ "version": "0.11.17",
4
4
  "description": "GraphQL API Server",
5
5
  "keywords": [
6
6
  "api",
@@ -21,8 +21,8 @@
21
21
  "type": "module",
22
22
  "exports": {
23
23
  ".": {
24
- "import": "./dist/esm/index.js",
25
- "types": "./dist/index.d.ts"
24
+ "import": "./dist/index.mjs",
25
+ "types": "./dist/index.d.mts"
26
26
  }
27
27
  },
28
28
  "files": [
@@ -30,28 +30,28 @@
30
30
  ],
31
31
  "dependencies": {
32
32
  "graphql-yoga": "^5.10.4",
33
- "@ttoss/http-server": "^0.5.13",
34
- "@ttoss/auth-core": "^0.4.12"
33
+ "@ttoss/auth-core": "^0.4.14",
34
+ "@ttoss/http-server": "^0.5.15"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@types/supertest": "^6.0.2",
38
38
  "graphql": "^16.11.0",
39
39
  "jest": "^30.3.0",
40
40
  "supertest": "^7.2.2",
41
- "tsup": "^8.5.1",
42
- "@ttoss/config": "^1.37.12",
43
- "@ttoss/graphql-api": "^0.10.2"
41
+ "tsdown": "^0.22.0",
42
+ "@ttoss/config": "^1.37.13",
43
+ "@ttoss/graphql-api": "^0.10.4"
44
44
  },
45
45
  "peerDependencies": {
46
46
  "graphql": "^16.6.0",
47
- "@ttoss/graphql-api": "^0.10.2"
47
+ "@ttoss/graphql-api": "^0.10.4"
48
48
  },
49
49
  "publishConfig": {
50
50
  "access": "public",
51
51
  "provenance": true
52
52
  },
53
53
  "scripts": {
54
- "build": "tsup",
54
+ "build": "tsdown",
55
55
  "test": "jest --projects tests/unit"
56
56
  }
57
57
  }
package/dist/index.d.ts DELETED
@@ -1,19 +0,0 @@
1
- import { cors, App } from '@ttoss/http-server';
2
- export { Router } from '@ttoss/http-server';
3
- import { BuildSchemaInput } from '@ttoss/graphql-api';
4
-
5
- type AuthenticationType = 'AMAZON_COGNITO_USER_POOLS';
6
- type CreateServerInput = {
7
- graphiql?: boolean;
8
- authenticationType?: AuthenticationType;
9
- userPoolConfig?: {
10
- userPoolId: string;
11
- tokenUse?: 'access' | 'id';
12
- clientId: string;
13
- };
14
- cors?: cors.Options;
15
- } & BuildSchemaInput;
16
- type ServerContext = App.Context;
17
- declare const createServer: ({ authenticationType, userPoolConfig, graphiql, cors: corsOptions, ...buildSchemaInput }: CreateServerInput) => App;
18
-
19
- export { type AuthenticationType, type CreateServerInput, type ServerContext, createServer };