@ttoss/graphql-api-server 0.6.10 → 0.6.12

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/graphql-api-server",
3
- "version": "0.6.10",
3
+ "version": "0.6.12",
4
4
  "description": "GraphQL API Server",
5
5
  "author": "ttoss",
6
6
  "contributors": [
@@ -28,11 +28,11 @@
28
28
  "@koa/router": "^12.0.1",
29
29
  "graphql-yoga": "^5.1.1",
30
30
  "koa": "^2.15.0",
31
- "@ttoss/auth-core": "^0.1.9"
31
+ "@ttoss/auth-core": "^0.1.11"
32
32
  },
33
33
  "peerDependencies": {
34
34
  "graphql": "^16.6.0",
35
- "@ttoss/graphql-api": "^0.7.3"
35
+ "@ttoss/graphql-api": "^0.7.5"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@types/koa": "^2.14.0",
@@ -42,9 +42,9 @@
42
42
  "graphql": "^16.8.1",
43
43
  "jest": "^29.7.0",
44
44
  "supertest": "^6.3.4",
45
- "tsup": "^8.2.4",
46
- "@ttoss/graphql-api": "^0.7.3",
47
- "@ttoss/config": "^1.32.9"
45
+ "tsup": "^8.3.0",
46
+ "@ttoss/config": "^1.33.0",
47
+ "@ttoss/graphql-api": "^0.7.5"
48
48
  },
49
49
  "keywords": [
50
50
  "api",
package/dist/esm/index.js DELETED
@@ -1,70 +0,0 @@
1
- /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
-
3
- // src/index.ts
4
- import { buildSchema } from "@ttoss/graphql-api";
5
- import { CognitoJwtVerifier } from "@ttoss/auth-core/amazon-cognito";
6
- import { createYoga } from "graphql-yoga";
7
- import Koa from "koa";
8
- import Router from "@koa/router";
9
- import cors from "@koa/cors";
10
- var createServer = ({
11
- authenticationType,
12
- userPoolConfig,
13
- graphiql,
14
- cors: corsOptions,
15
- ...buildSchemaInput
16
- }) => {
17
- const app = new Koa();
18
- app.use(cors(corsOptions));
19
- const yoga = createYoga({
20
- schema: buildSchema(buildSchemaInput),
21
- graphiql,
22
- landingPage: false,
23
- logging: false,
24
- /**
25
- * Disable CORS, as it's handled by Koa middleware
26
- */
27
- cors: false
28
- });
29
- const jwtVerifier = (() => {
30
- if (authenticationType === "AMAZON_COGNITO_USER_POOLS") {
31
- if (!userPoolConfig) {
32
- throw new Error("userPoolConfig is required when using AMAZON_COGNITO_USER_POOLS authenticationType");
33
- }
34
- return CognitoJwtVerifier.create({
35
- tokenUse: "access",
36
- ...userPoolConfig
37
- });
38
- }
39
- return null;
40
- })();
41
- app.use(async (ctx, next) => {
42
- if (ctx.path !== "/graphql") {
43
- return next();
44
- }
45
- const isGraphiqlRequest = ctx.headers.accept?.includes("text/html") && graphiql;
46
- if (!isGraphiqlRequest) {
47
- try {
48
- if (authenticationType === "AMAZON_COGNITO_USER_POOLS" && jwtVerifier) {
49
- const token = ctx.headers.authorization?.replace("Bearer ", "");
50
- const identity = await jwtVerifier.verify(token || "");
51
- ctx.identity = identity;
52
- }
53
- } catch {
54
- ctx.status = 401;
55
- ctx.body = "Unauthorized";
56
- return;
57
- }
58
- }
59
- const response = await yoga.handleNodeRequest(ctx.req, ctx);
60
- ctx.status = response.status;
61
- for (const [key, value] of response.headers.entries()) {
62
- if (ctx.status != 401) {
63
- ctx.append(key, value);
64
- }
65
- }
66
- ctx.body = response.body;
67
- });
68
- return app;
69
- };
70
- export { Router, createServer };
package/dist/index.d.mts DELETED
@@ -1,19 +0,0 @@
1
- import { BuildSchemaInput } from '@ttoss/graphql-api';
2
- import Koa from 'koa';
3
- export { default as Router } from '@koa/router';
4
- import cors from '@koa/cors';
5
-
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
- declare const createServer: ({ authenticationType, userPoolConfig, graphiql, cors: corsOptions, ...buildSchemaInput }: CreateServerInput) => Koa;
18
-
19
- export { type AuthenticationType, type CreateServerInput, createServer };
package/dist/index.d.ts DELETED
@@ -1,19 +0,0 @@
1
- import { BuildSchemaInput } from '@ttoss/graphql-api';
2
- import Koa from 'koa';
3
- export { default as Router } from '@koa/router';
4
- import cors from '@koa/cors';
5
-
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
- declare const createServer: ({ authenticationType, userPoolConfig, graphiql, cors: corsOptions, ...buildSchemaInput }: CreateServerInput) => Koa;
18
-
19
- export { type AuthenticationType, type CreateServerInput, createServer };
package/dist/index.js DELETED
@@ -1,115 +0,0 @@
1
- /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
- "use strict";
3
-
4
- var __create = Object.create;
5
- var __defProp = Object.defineProperty;
6
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
- var __getOwnPropNames = Object.getOwnPropertyNames;
8
- var __getProtoOf = Object.getPrototypeOf;
9
- var __hasOwnProp = Object.prototype.hasOwnProperty;
10
- var __export = (target, all) => {
11
- for (var name in all) __defProp(target, name, {
12
- get: all[name],
13
- enumerable: true
14
- });
15
- };
16
- var __copyProps = (to, from, except, desc) => {
17
- if (from && typeof from === "object" || typeof from === "function") {
18
- for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
19
- get: () => from[key],
20
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
21
- });
22
- }
23
- return to;
24
- };
25
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
26
- // If the importer is in node compatibility mode or this is not an ESM
27
- // file that has been converted to a CommonJS file using a Babel-
28
- // compatible transform (i.e. "__esModule" has not been set), then set
29
- // "default" to the CommonJS "module.exports" for node compatibility.
30
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
31
- value: mod,
32
- enumerable: true
33
- }) : target, mod));
34
- var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
35
- value: true
36
- }), mod);
37
-
38
- // src/index.ts
39
- var src_exports = {};
40
- __export(src_exports, {
41
- Router: () => import_router.default,
42
- createServer: () => createServer
43
- });
44
- module.exports = __toCommonJS(src_exports);
45
- var import_graphql_api = require("@ttoss/graphql-api");
46
- var import_amazon_cognito = require("@ttoss/auth-core/amazon-cognito");
47
- var import_graphql_yoga = require("graphql-yoga");
48
- var import_koa = __toESM(require("koa"));
49
- var import_router = __toESM(require("@koa/router"));
50
- var import_cors = __toESM(require("@koa/cors"));
51
- var createServer = ({
52
- authenticationType,
53
- userPoolConfig,
54
- graphiql,
55
- cors: corsOptions,
56
- ...buildSchemaInput
57
- }) => {
58
- const app = new import_koa.default();
59
- app.use((0, import_cors.default)(corsOptions));
60
- const yoga = (0, import_graphql_yoga.createYoga)({
61
- schema: (0, import_graphql_api.buildSchema)(buildSchemaInput),
62
- graphiql,
63
- landingPage: false,
64
- logging: false,
65
- /**
66
- * Disable CORS, as it's handled by Koa middleware
67
- */
68
- cors: false
69
- });
70
- const jwtVerifier = (() => {
71
- if (authenticationType === "AMAZON_COGNITO_USER_POOLS") {
72
- if (!userPoolConfig) {
73
- throw new Error("userPoolConfig is required when using AMAZON_COGNITO_USER_POOLS authenticationType");
74
- }
75
- return import_amazon_cognito.CognitoJwtVerifier.create({
76
- tokenUse: "access",
77
- ...userPoolConfig
78
- });
79
- }
80
- return null;
81
- })();
82
- app.use(async (ctx, next) => {
83
- if (ctx.path !== "/graphql") {
84
- return next();
85
- }
86
- const isGraphiqlRequest = ctx.headers.accept?.includes("text/html") && graphiql;
87
- if (!isGraphiqlRequest) {
88
- try {
89
- if (authenticationType === "AMAZON_COGNITO_USER_POOLS" && jwtVerifier) {
90
- const token = ctx.headers.authorization?.replace("Bearer ", "");
91
- const identity = await jwtVerifier.verify(token || "");
92
- ctx.identity = identity;
93
- }
94
- } catch {
95
- ctx.status = 401;
96
- ctx.body = "Unauthorized";
97
- return;
98
- }
99
- }
100
- const response = await yoga.handleNodeRequest(ctx.req, ctx);
101
- ctx.status = response.status;
102
- for (const [key, value] of response.headers.entries()) {
103
- if (ctx.status != 401) {
104
- ctx.append(key, value);
105
- }
106
- }
107
- ctx.body = response.body;
108
- });
109
- return app;
110
- };
111
- // Annotate the CommonJS export names for ESM import in node:
112
- 0 && (module.exports = {
113
- Router,
114
- createServer
115
- });