@ttoss/aws-appsync-nodejs 1.7.4 → 1.8.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 CHANGED
@@ -5,7 +5,7 @@ This package implements a AWS AppSync client for Node.js. We've followed the [AW
5
5
  ## Installation
6
6
 
7
7
  ```bash
8
- yarn add @ttoss/aws-appsync-nodejs
8
+ pnpm add @ttoss/aws-appsync-nodejs
9
9
  ```
10
10
 
11
11
  ## Quickstart
@@ -14,7 +14,7 @@ yarn add @ttoss/aws-appsync-nodejs
14
14
  import { appSyncClient } from '@ttoss/aws-appsync-nodejs';
15
15
 
16
16
  appSyncClient.setConfig({
17
- apiEndpoint: 'https://xxxxxx.appsync-api.us-east-1.amazonaws.com/graphql',
17
+ endpoint: 'https://xxxxxx.appsync-api.us-east-1.amazonaws.com/graphql',
18
18
  apiKey: 'da2-xxxxxxxxxxxxxxxxxxxxxxxxxx',
19
19
  });
20
20
 
@@ -34,13 +34,13 @@ appSyncClient.query(query, { id: '1' }).then((result) => {
34
34
 
35
35
  ## Config
36
36
 
37
- You need to configure the client with `apiEndpoint` (required), `apiKey` (optional) and `awsCredentials` (optional).
37
+ You need to configure the client with `endpoint` (required), `apiKey` (optional) and `credentials` (optional).
38
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.
39
+ 1. If you don't provide `apiKey` or `credentials`, the client will try to use the AWS credentials from the environment variables of your system—local computer, AWS Lambda, EC2.
40
40
 
41
41
  ```typescript
42
42
  appSyncClient.setConfig({
43
- apiEndpoint: 'https://xxxxxx.appsync-api.us-east-1.amazonaws.com/graphql',
43
+ endpoint: 'https://xxxxxx.appsync-api.us-east-1.amazonaws.com/graphql',
44
44
  });
45
45
  ```
46
46
 
@@ -48,17 +48,31 @@ appSyncClient.setConfig({
48
48
 
49
49
  ```typescript
50
50
  appSyncClient.setConfig({
51
- apiEndpoint: 'https://xxxxxx.appsync-api.us-east-1.amazonaws.com/graphql',
51
+ endpoint: 'https://xxxxxx.appsync-api.us-east-1.amazonaws.com/graphql',
52
52
  apiKey: 'da2-xxxxxxxxxxxxxxxxxxxxxxxxxx',
53
53
  });
54
54
  ```
55
55
 
56
- 3. If you provide `awsCredentials`, the client will use the credentials to authenticate.
56
+ 3. If you provide `credentials`, the client will use the credentials to authenticate.
57
57
 
58
58
  ```typescript
59
59
  appSyncClient.setConfig({
60
- apiEndpoint,
61
- awsCredentials: {
60
+ endpoint,
61
+ credentials: {
62
+ accessKeyId: // access key id,
63
+ secretAccessKey: // secret access key,
64
+ sessionToken: // optional session token,
65
+ },
66
+ });
67
+ ```
68
+
69
+ If you provide the default endpoint (`https://xxxxxx.appsync-api.us-east-1.amazonaws.com/graphql`), the client will retrieve the region from the endpoint. If you provide the endpoint with the custom domain (`https://custom-domain.com`), you need to provide the region as well.
70
+
71
+ ```typescript
72
+ appSyncClient.setConfig({
73
+ endpoint: 'https://custom-domain.com',
74
+ region: 'us-east-1',
75
+ credentials: {
62
76
  accessKeyId: // access key id,
63
77
  secretAccessKey: // secret access key,
64
78
  sessionToken: // optional session token,
package/dist/esm/index.js CHANGED
@@ -1,9 +1,9 @@
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
+ import { HttpRequest } from "@smithy/protocol-http";
5
5
  import { Sha256 } from "@aws-crypto/sha256-js";
6
- import { SignatureV4 } from "@aws-sdk/signature-v4";
6
+ import { SignatureV4 } from "@smithy/signature-v4";
7
7
  import { defaultProvider } from "@aws-sdk/credential-provider-node";
8
8
  var _config;
9
9
  var setConfig = config => {
@@ -11,7 +11,7 @@ var setConfig = config => {
11
11
  };
12
12
  var queryWithApiKey = async (query2, variables) => {
13
13
  const {
14
- apiEndpoint,
14
+ endpoint,
15
15
  apiKey
16
16
  } = _config;
17
17
  if (!apiKey) {
@@ -27,7 +27,7 @@ var queryWithApiKey = async (query2, variables) => {
27
27
  variables
28
28
  })
29
29
  };
30
- const request = new Request(apiEndpoint, options);
30
+ const request = new Request(endpoint, options);
31
31
  const response = await fetch(request);
32
32
  const body = await response.json();
33
33
  return body;
@@ -42,14 +42,10 @@ var getRegionFromEndpoint = endpoint => {
42
42
  return region;
43
43
  };
44
44
  var queryWithCredentials = async (query2, variables) => {
45
- const {
46
- apiEndpoint,
47
- awsCredentials
48
- } = _config;
49
- const endpoint = new URL(apiEndpoint);
50
- const region = getRegionFromEndpoint(apiEndpoint);
45
+ const endpoint = new URL(_config.endpoint);
46
+ const region = _config.region || getRegionFromEndpoint(_config.endpoint);
51
47
  const signer = new SignatureV4({
52
- credentials: awsCredentials || defaultProvider(),
48
+ credentials: _config.credentials || defaultProvider(),
53
49
  region,
54
50
  service: "appsync",
55
51
  sha256: Sha256
@@ -65,7 +61,8 @@ var queryWithCredentials = async (query2, variables) => {
65
61
  query: query2,
66
62
  variables
67
63
  }),
68
- path: endpoint.pathname
64
+ path: endpoint.pathname,
65
+ protocol: endpoint.protocol
69
66
  });
70
67
  const signed = await signer.sign(requestToBeSigned);
71
68
  const request = new Request(endpoint, signed);
package/dist/index.d.mts CHANGED
@@ -6,11 +6,12 @@ import { AwsCredentialIdentity } from '@aws-sdk/types';
6
6
  */
7
7
 
8
8
  type Config = {
9
- apiEndpoint: string;
9
+ endpoint: string;
10
+ region?: string;
10
11
  apiKey?: string;
11
- awsCredentials?: AwsCredentialIdentity;
12
+ credentials?: AwsCredentialIdentity;
12
13
  };
13
- type Query = (query: string, variables: Record<string, unknown>) => Promise<{
14
+ type Query = (query: string, variables?: Record<string, unknown>) => Promise<{
14
15
  data: Record<string, unknown> | null;
15
16
  errors?: {
16
17
  message: string;
package/dist/index.d.ts CHANGED
@@ -6,11 +6,12 @@ import { AwsCredentialIdentity } from '@aws-sdk/types';
6
6
  */
7
7
 
8
8
  type Config = {
9
- apiEndpoint: string;
9
+ endpoint: string;
10
+ region?: string;
10
11
  apiKey?: string;
11
- awsCredentials?: AwsCredentialIdentity;
12
+ credentials?: AwsCredentialIdentity;
12
13
  };
13
- type Query = (query: string, variables: Record<string, unknown>) => Promise<{
14
+ type Query = (query: string, variables?: Record<string, unknown>) => Promise<{
14
15
  data: Record<string, unknown> | null;
15
16
  errors?: {
16
17
  message: string;
package/dist/index.js CHANGED
@@ -30,9 +30,9 @@ __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
+ var import_protocol_http = require("@smithy/protocol-http");
34
34
  var import_sha256_js = require("@aws-crypto/sha256-js");
35
- var import_signature_v4 = require("@aws-sdk/signature-v4");
35
+ var import_signature_v4 = require("@smithy/signature-v4");
36
36
  var import_credential_provider_node = require("@aws-sdk/credential-provider-node");
37
37
  var _config;
38
38
  var setConfig = config => {
@@ -40,7 +40,7 @@ var setConfig = config => {
40
40
  };
41
41
  var queryWithApiKey = async (query2, variables) => {
42
42
  const {
43
- apiEndpoint,
43
+ endpoint,
44
44
  apiKey
45
45
  } = _config;
46
46
  if (!apiKey) {
@@ -56,7 +56,7 @@ var queryWithApiKey = async (query2, variables) => {
56
56
  variables
57
57
  })
58
58
  };
59
- const request = new Request(apiEndpoint, options);
59
+ const request = new Request(endpoint, options);
60
60
  const response = await fetch(request);
61
61
  const body = await response.json();
62
62
  return body;
@@ -71,14 +71,10 @@ var getRegionFromEndpoint = endpoint => {
71
71
  return region;
72
72
  };
73
73
  var queryWithCredentials = async (query2, variables) => {
74
- const {
75
- apiEndpoint,
76
- awsCredentials
77
- } = _config;
78
- const endpoint = new URL(apiEndpoint);
79
- const region = getRegionFromEndpoint(apiEndpoint);
74
+ const endpoint = new URL(_config.endpoint);
75
+ const region = _config.region || getRegionFromEndpoint(_config.endpoint);
80
76
  const signer = new import_signature_v4.SignatureV4({
81
- credentials: awsCredentials || (0, import_credential_provider_node.defaultProvider)(),
77
+ credentials: _config.credentials || (0, import_credential_provider_node.defaultProvider)(),
82
78
  region,
83
79
  service: "appsync",
84
80
  sha256: import_sha256_js.Sha256
@@ -94,7 +90,8 @@ var queryWithCredentials = async (query2, variables) => {
94
90
  query: query2,
95
91
  variables
96
92
  }),
97
- path: endpoint.pathname
93
+ path: endpoint.pathname,
94
+ protocol: endpoint.protocol
98
95
  });
99
96
  const signed = await signer.sign(requestToBeSigned);
100
97
  const request = new Request(endpoint, signed);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/aws-appsync-nodejs",
3
- "version": "1.7.4",
3
+ "version": "1.8.1",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "author": "ttoss",
@@ -12,22 +12,29 @@
12
12
  "url": "https://github.com/ttoss/ttoss.git",
13
13
  "directory": "packages/aws-appsync-nodejs"
14
14
  },
15
- "main": "dist/index.js",
15
+ "exports": {
16
+ ".": {
17
+ "import": "./dist/esm/index.js",
18
+ "require": "./dist/index.js",
19
+ "types": "./dist/index.d.ts"
20
+ }
21
+ },
16
22
  "files": [
17
- "dist/"
23
+ "dist/",
24
+ "src/"
18
25
  ],
19
26
  "dependencies": {
20
27
  "@aws-crypto/sha256-js": "^5.2.0",
21
- "@aws-sdk/credential-provider-node": "^3.460.0",
22
- "@aws-sdk/protocol-http": "^3.370.0",
23
- "@aws-sdk/signature-v4": "^3.370.0"
28
+ "@aws-sdk/credential-provider-node": "^3.503.1",
29
+ "@smithy/protocol-http": "^3.1.1",
30
+ "@smithy/signature-v4": "^2.1.1"
24
31
  },
25
32
  "devDependencies": {
26
- "@aws-sdk/types": "^3.460.0",
27
- "@types/jest": "^29.5.11",
33
+ "@aws-sdk/types": "^3.502.0",
34
+ "@types/jest": "^29.5.12",
28
35
  "jest": "^29.7.0",
29
- "tsup": "^8.0.1",
30
- "@ttoss/config": "^1.31.4"
36
+ "tsup": "^8.0.2",
37
+ "@ttoss/config": "^1.31.5"
31
38
  },
32
39
  "keywords": [],
33
40
  "engines": {
package/src/index.ts ADDED
@@ -0,0 +1,120 @@
1
+ /**
2
+ * Implementation of the AppSync client for NodeJS, based on the AWS Amplify
3
+ * documentation: https://docs.amplify.aws/lib/graphqlapi/graphql-from-nodejs/q/platform/js/
4
+ */
5
+ import { AwsCredentialIdentity } from '@aws-sdk/types';
6
+ import { HttpRequest } from '@smithy/protocol-http';
7
+ import { Sha256 } from '@aws-crypto/sha256-js';
8
+ import { SignatureV4 } from '@smithy/signature-v4';
9
+ import { defaultProvider } from '@aws-sdk/credential-provider-node';
10
+
11
+ export type Config = {
12
+ endpoint: string;
13
+ region?: string;
14
+ apiKey?: string;
15
+ credentials?: AwsCredentialIdentity;
16
+ };
17
+
18
+ let _config: Config;
19
+
20
+ const setConfig = (config: Config) => {
21
+ _config = config;
22
+ };
23
+
24
+ export type Query = (
25
+ query: string,
26
+ variables?: Record<string, unknown>
27
+ ) => Promise<{
28
+ data: Record<string, unknown> | null;
29
+ errors?: {
30
+ message: string;
31
+ path: string | null;
32
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
33
+ locations: any[];
34
+ }[];
35
+ }>;
36
+
37
+ const queryWithApiKey: Query = async (query, variables) => {
38
+ const { endpoint, apiKey } = _config;
39
+
40
+ if (!apiKey) {
41
+ throw new Error('No API Key set');
42
+ }
43
+
44
+ const options: RequestInit = {
45
+ method: 'POST',
46
+ headers: {
47
+ 'x-api-key': apiKey,
48
+ },
49
+ body: JSON.stringify({ query, variables }),
50
+ };
51
+
52
+ const request = new Request(endpoint, options);
53
+
54
+ const response = await fetch(request);
55
+
56
+ const body = await response.json();
57
+
58
+ return body;
59
+ };
60
+
61
+ const getRegionFromEndpoint = (endpoint: string): string => {
62
+ const matcher = /\.[a-z]+-[a-z]+-[0-9]\./;
63
+ const regexResponse = endpoint.match(matcher);
64
+ let region = '';
65
+ if (regexResponse) {
66
+ region = regexResponse[0].replace(/\./g, '');
67
+ }
68
+ return region;
69
+ };
70
+
71
+ const queryWithCredentials: Query = async (query, variables) => {
72
+ const endpoint = new URL(_config.endpoint);
73
+
74
+ const region = _config.region || getRegionFromEndpoint(_config.endpoint);
75
+
76
+ const signer = new SignatureV4({
77
+ credentials: _config.credentials || defaultProvider(),
78
+ region,
79
+ service: 'appsync',
80
+ sha256: Sha256,
81
+ });
82
+
83
+ const requestToBeSigned = new HttpRequest({
84
+ method: 'POST',
85
+ headers: {
86
+ 'Content-Type': 'application/json',
87
+ host: endpoint.host,
88
+ },
89
+ hostname: endpoint.host,
90
+ body: JSON.stringify({ query, variables }),
91
+ path: endpoint.pathname,
92
+ protocol: endpoint.protocol,
93
+ });
94
+
95
+ const signed = await signer.sign(requestToBeSigned);
96
+
97
+ const request = new Request(endpoint, signed);
98
+
99
+ const response = await fetch(request);
100
+
101
+ const body = await response.json();
102
+
103
+ return body;
104
+ };
105
+
106
+ const query: Query = async (query, variables) => {
107
+ if (_config.apiKey) {
108
+ return queryWithApiKey(query, variables);
109
+ }
110
+
111
+ return queryWithCredentials(query, variables);
112
+ };
113
+
114
+ export const appSyncClient = {
115
+ query,
116
+ setConfig,
117
+ get config() {
118
+ return _config;
119
+ },
120
+ };