@ttoss/graphql-api-server 0.15.6 → 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 +50 -22
- package/dist/index.d.cts +6 -13
- package/dist/index.d.mts +6 -13
- package/dist/index.mjs +44 -21
- package/package.json +12 -12
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
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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)
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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,14 +96,17 @@ var createServer = ({
|
|
|
71
96
|
/**
|
|
72
97
|
* Set headers
|
|
73
98
|
*/
|
|
74
|
-
response.headers.
|
|
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;
|
|
80
103
|
};
|
|
81
104
|
|
|
82
105
|
//#endregion
|
|
83
|
-
exports
|
|
106
|
+
Object.defineProperty(exports, "Router", {
|
|
107
|
+
enumerable: true,
|
|
108
|
+
get: function () {
|
|
109
|
+
return _ttoss_http_server.Router;
|
|
110
|
+
}
|
|
111
|
+
});
|
|
84
112
|
exports.createServer = createServer;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
|
|
2
|
-
import { App, Router, cors } from "@ttoss/http-server";
|
|
3
2
|
import { BuildSchemaInput } from "@ttoss/graphql-api";
|
|
4
|
-
|
|
3
|
+
import { App, Router, cors } from "@ttoss/http-server";
|
|
5
4
|
//#region src/index.d.ts
|
|
6
|
-
type AuthenticationType = 'AMAZON_COGNITO_USER_POOLS';
|
|
7
|
-
type CreateServerInput = {
|
|
5
|
+
export type AuthenticationType = 'AMAZON_COGNITO_USER_POOLS';
|
|
6
|
+
export type CreateServerInput = {
|
|
8
7
|
graphiql?: boolean;
|
|
9
8
|
authenticationType?: AuthenticationType;
|
|
10
9
|
userPoolConfig?: {
|
|
@@ -14,13 +13,7 @@ type CreateServerInput = {
|
|
|
14
13
|
};
|
|
15
14
|
cors?: cors.Options;
|
|
16
15
|
} & BuildSchemaInput;
|
|
17
|
-
type ServerContext = App.Context;
|
|
18
|
-
declare const createServer: ({
|
|
19
|
-
authenticationType,
|
|
20
|
-
userPoolConfig,
|
|
21
|
-
graphiql,
|
|
22
|
-
cors: corsOptions,
|
|
23
|
-
...buildSchemaInput
|
|
24
|
-
}: CreateServerInput) => App;
|
|
16
|
+
export type ServerContext = App.Context;
|
|
17
|
+
export declare const createServer: ({ authenticationType, userPoolConfig, graphiql, cors: corsOptions, ...buildSchemaInput }: CreateServerInput) => App;
|
|
25
18
|
//#endregion
|
|
26
|
-
export {
|
|
19
|
+
export { Router };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
|
|
2
|
-
import { App, Router, cors } from "@ttoss/http-server";
|
|
3
2
|
import { BuildSchemaInput } from "@ttoss/graphql-api";
|
|
4
|
-
|
|
3
|
+
import { App, Router, cors } from "@ttoss/http-server";
|
|
5
4
|
//#region src/index.d.ts
|
|
6
|
-
type AuthenticationType = 'AMAZON_COGNITO_USER_POOLS';
|
|
7
|
-
type CreateServerInput = {
|
|
5
|
+
export type AuthenticationType = 'AMAZON_COGNITO_USER_POOLS';
|
|
6
|
+
export type CreateServerInput = {
|
|
8
7
|
graphiql?: boolean;
|
|
9
8
|
authenticationType?: AuthenticationType;
|
|
10
9
|
userPoolConfig?: {
|
|
@@ -14,13 +13,7 @@ type CreateServerInput = {
|
|
|
14
13
|
};
|
|
15
14
|
cors?: cors.Options;
|
|
16
15
|
} & BuildSchemaInput;
|
|
17
|
-
type ServerContext = App.Context;
|
|
18
|
-
declare const createServer: ({
|
|
19
|
-
authenticationType,
|
|
20
|
-
userPoolConfig,
|
|
21
|
-
graphiql,
|
|
22
|
-
cors: corsOptions,
|
|
23
|
-
...buildSchemaInput
|
|
24
|
-
}: CreateServerInput) => App;
|
|
16
|
+
export type ServerContext = App.Context;
|
|
17
|
+
export declare const createServer: ({ authenticationType, userPoolConfig, graphiql, cors: corsOptions, ...buildSchemaInput }: CreateServerInput) => App;
|
|
25
18
|
//#endregion
|
|
26
|
-
export {
|
|
19
|
+
export { Router };
|
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
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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)
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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.
|
|
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.
|
|
3
|
+
"version": "0.17.0",
|
|
4
4
|
"description": "GraphQL API Server",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"api",
|
|
@@ -29,22 +29,22 @@
|
|
|
29
29
|
"dist"
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"graphql-yoga": "^5.
|
|
33
|
-
"@ttoss/auth-core": "^0.
|
|
34
|
-
"@ttoss/http-server": "^0.
|
|
32
|
+
"graphql-yoga": "^5.24.4",
|
|
33
|
+
"@ttoss/auth-core": "^0.20.0",
|
|
34
|
+
"@ttoss/http-server": "^0.14.1"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@types/supertest": "^
|
|
38
|
-
"graphql": "^
|
|
39
|
-
"jest": "^30.
|
|
40
|
-
"supertest": "^7.
|
|
41
|
-
"tsdown": "^0.
|
|
42
|
-
"@ttoss/config": "^
|
|
43
|
-
"@ttoss/graphql-api": "^0.
|
|
37
|
+
"@types/supertest": "^7.2.1",
|
|
38
|
+
"graphql": "^17.0.2",
|
|
39
|
+
"jest": "^30.5.2",
|
|
40
|
+
"supertest": "^7.3.1",
|
|
41
|
+
"tsdown": "^0.23.1",
|
|
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.
|
|
47
|
+
"@ttoss/graphql-api": "^0.15.0"
|
|
48
48
|
},
|
|
49
49
|
"publishConfig": {
|
|
50
50
|
"access": "public",
|