@ttoss/aws-appsync-nodejs 1.2.0 → 1.3.1

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/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # @ttoss/aws-appsync-nodejs
2
+
3
+ This package implements a AWS AppSync client for Node.js. We've followed the [AWS Amplify](https://docs.amplify.aws/lib/graphqlapi/graphql-from-nodejs/q/platform/js/) example to create this package.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ yarn add @ttoss/aws-appsync-nodejs
9
+ ```
10
+
11
+ ## Quickstart
12
+
13
+ ```typescript
14
+ import { appSyncClient } from '@ttoss/aws-appsync-nodejs';
15
+
16
+ appSyncClient.setConfig({
17
+ apiEndpoint: 'https://xxxxxx.appsync-api.us-east-1.amazonaws.com/graphql',
18
+ apiKey: 'da2-xxxxxxxxxxxxxxxxxxxxxxxxxx',
19
+ });
20
+
21
+ const query = /* GraphQL */ `
22
+ query user($id: ID!) {
23
+ user(id: $id) {
24
+ id
25
+ name
26
+ }
27
+ }
28
+ `;
29
+
30
+ appSyncClient.query(query, { id: '1' }).then((result) => {
31
+ console.log(result);
32
+ });
33
+ ```
34
+
35
+ ## Config
36
+
37
+ You need to configure the client with `apiEndpoint` (required), `apiKey` (optional) and `awsCredentials` (optional).
38
+
39
+ 1. If you don't provide `apiKey` or `awsCredentials`, the client will try to use the AWS credentials from the environment variables of your system—local computer, AWS Lambda, EC2.
40
+
41
+ ```typescript
42
+ appSyncClient.setConfig({
43
+ apiEndpoint: 'https://xxxxxx.appsync-api.us-east-1.amazonaws.com/graphql',
44
+ });
45
+ ```
46
+
47
+ 2. If you provide `apiKey`, the client will use the API key to authenticate.
48
+
49
+ ```typescript
50
+ appSyncClient.setConfig({
51
+ apiEndpoint: 'https://xxxxxx.appsync-api.us-east-1.amazonaws.com/graphql',
52
+ apiKey: 'da2-xxxxxxxxxxxxxxxxxxxxxxxxxx',
53
+ });
54
+ ```
55
+
56
+ 3. If you provide `awsCredentials`, the client will use the credentials to authenticate.
57
+
58
+ ```typescript
59
+ appSyncClient.setConfig({
60
+ apiEndpoint,
61
+ awsCredentials: {
62
+ accessKeyId: // access key id,
63
+ secretAccessKey: // secret access key,
64
+ sessionToken: // optional session token,
65
+ },
66
+ });
67
+ ```
package/dist/esm/index.js CHANGED
@@ -1,7 +1,11 @@
1
1
  /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
2
 
3
3
  // src/index.ts
4
+ import { HttpRequest } from "@aws-sdk/protocol-http";
4
5
  import { Request, default as fetch } from "node-fetch";
6
+ import { Sha256 } from "@aws-crypto/sha256-js";
7
+ import { SignatureV4 } from "@aws-sdk/signature-v4";
8
+ import { defaultProvider } from "@aws-sdk/credential-provider-node";
5
9
  var _config;
6
10
  var setConfig = (config) => {
7
11
  _config = config;
@@ -23,14 +27,46 @@ var queryWithApiKey = async (query2, variables) => {
23
27
  const body = await response.json();
24
28
  return body;
25
29
  };
30
+ var getRegionFromEndpoint = (endpoint) => {
31
+ const matcher = /\.[a-z]+-[a-z]+-[0-9]\./;
32
+ const regexResponse = endpoint.match(matcher);
33
+ let region = "";
34
+ if (regexResponse) {
35
+ region = regexResponse[0].replace(/\./g, "");
36
+ }
37
+ return region;
38
+ };
39
+ var queryWithCredentials = async (query2, variables) => {
40
+ const { apiEndpoint, awsCredentials } = _config;
41
+ const endpoint = new URL(apiEndpoint);
42
+ const region = getRegionFromEndpoint(apiEndpoint);
43
+ const signer = new SignatureV4({
44
+ credentials: awsCredentials || defaultProvider(),
45
+ region,
46
+ service: "appsync",
47
+ sha256: Sha256
48
+ });
49
+ const requestToBeSigned = new HttpRequest({
50
+ method: "POST",
51
+ headers: {
52
+ "Content-Type": "application/json",
53
+ host: endpoint.host
54
+ },
55
+ hostname: endpoint.host,
56
+ body: JSON.stringify({ query: query2, variables }),
57
+ path: endpoint.pathname
58
+ });
59
+ const signed = await signer.sign(requestToBeSigned);
60
+ const request = new Request(endpoint, signed);
61
+ const response = await fetch(request);
62
+ const body = await response.json();
63
+ return body;
64
+ };
26
65
  var query = async (query2, variables) => {
27
66
  if (_config.apiKey) {
28
67
  return queryWithApiKey(query2, variables);
29
68
  }
30
- if (_config.awsCredentials) {
31
- return { data: null };
32
- }
33
- throw new Error("No API key or credentials set");
69
+ return queryWithCredentials(query2, variables);
34
70
  };
35
71
  var appSyncClient = {
36
72
  query,
package/dist/index.d.ts CHANGED
@@ -1,9 +1,16 @@
1
- declare type Config = {
1
+ import { AwsCredentialIdentity } from '@aws-sdk/types';
2
+
3
+ /**
4
+ * Implementation of the AppSync client for NodeJS, based on the AWS Amplify
5
+ * documentation: https://docs.amplify.aws/lib/graphqlapi/graphql-from-nodejs/q/platform/js/
6
+ */
7
+
8
+ type Config = {
2
9
  apiEndpoint: string;
3
10
  apiKey?: string;
4
- awsCredentials?: any;
11
+ awsCredentials?: AwsCredentialIdentity;
5
12
  };
6
- declare type Query = (query: string, variables: Record<string, unknown>) => Promise<{
13
+ type Query = (query: string, variables: Record<string, unknown>) => Promise<{
7
14
  data: Record<string, unknown> | null;
8
15
  errors?: {
9
16
  message: string;
package/dist/index.js CHANGED
@@ -30,7 +30,11 @@ __export(src_exports, {
30
30
  appSyncClient: () => appSyncClient
31
31
  });
32
32
  module.exports = __toCommonJS(src_exports);
33
+ var import_protocol_http = require("@aws-sdk/protocol-http");
33
34
  var import_node_fetch = __toESM(require("node-fetch"));
35
+ var import_sha256_js = require("@aws-crypto/sha256-js");
36
+ var import_signature_v4 = require("@aws-sdk/signature-v4");
37
+ var import_credential_provider_node = require("@aws-sdk/credential-provider-node");
34
38
  var _config;
35
39
  var setConfig = (config) => {
36
40
  _config = config;
@@ -52,14 +56,46 @@ var queryWithApiKey = async (query2, variables) => {
52
56
  const body = await response.json();
53
57
  return body;
54
58
  };
59
+ var getRegionFromEndpoint = (endpoint) => {
60
+ const matcher = /\.[a-z]+-[a-z]+-[0-9]\./;
61
+ const regexResponse = endpoint.match(matcher);
62
+ let region = "";
63
+ if (regexResponse) {
64
+ region = regexResponse[0].replace(/\./g, "");
65
+ }
66
+ return region;
67
+ };
68
+ var queryWithCredentials = async (query2, variables) => {
69
+ const { apiEndpoint, awsCredentials } = _config;
70
+ const endpoint = new URL(apiEndpoint);
71
+ const region = getRegionFromEndpoint(apiEndpoint);
72
+ const signer = new import_signature_v4.SignatureV4({
73
+ credentials: awsCredentials || (0, import_credential_provider_node.defaultProvider)(),
74
+ region,
75
+ service: "appsync",
76
+ sha256: import_sha256_js.Sha256
77
+ });
78
+ const requestToBeSigned = new import_protocol_http.HttpRequest({
79
+ method: "POST",
80
+ headers: {
81
+ "Content-Type": "application/json",
82
+ host: endpoint.host
83
+ },
84
+ hostname: endpoint.host,
85
+ body: JSON.stringify({ query: query2, variables }),
86
+ path: endpoint.pathname
87
+ });
88
+ const signed = await signer.sign(requestToBeSigned);
89
+ const request = new import_node_fetch.Request(endpoint, signed);
90
+ const response = await (0, import_node_fetch.default)(request);
91
+ const body = await response.json();
92
+ return body;
93
+ };
55
94
  var query = async (query2, variables) => {
56
95
  if (_config.apiKey) {
57
96
  return queryWithApiKey(query2, variables);
58
97
  }
59
- if (_config.awsCredentials) {
60
- return { data: null };
61
- }
62
- throw new Error("No API key or credentials set");
98
+ return queryWithCredentials(query2, variables);
63
99
  };
64
100
  var appSyncClient = {
65
101
  query,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/aws-appsync-nodejs",
3
- "version": "1.2.0",
3
+ "version": "1.3.1",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "author": "ttoss",
@@ -12,14 +12,24 @@
12
12
  "dist/"
13
13
  ],
14
14
  "scripts": {
15
- "build": "tsup"
15
+ "build": "tsup",
16
+ "test": "jest"
16
17
  },
17
18
  "dependencies": {
19
+ "@aws-crypto/sha256-js": "^2.0.2",
20
+ "@aws-sdk/credential-provider-node": "^3.231.0",
21
+ "@aws-sdk/protocol-http": "^3.226.0",
22
+ "@aws-sdk/signature-v4": "^3.226.0",
18
23
  "node-fetch": "2"
19
24
  },
25
+ "devDependencies": {
26
+ "@ttoss/config": "^1.26.0",
27
+ "@types/jest": "^29.2.4",
28
+ "jest": "^29.3.1"
29
+ },
20
30
  "keywords": [],
21
31
  "publishConfig": {
22
32
  "access": "public"
23
33
  },
24
- "gitHead": "88b98945c469cdfdece6b12ce41243403ad2c242"
34
+ "gitHead": "f7fcb1caa22adf045f0ec7d15a678bfe48a3913d"
25
35
  }