@ttoss/relay-amplify 0.2.4 → 0.3.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/esm/index.js CHANGED
@@ -1,17 +1,46 @@
1
1
  /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
2
 
3
3
  // src/index.ts
4
- import { API, graphqlOperation } from "aws-amplify";
4
+ import { API, Auth, graphqlOperation } from "aws-amplify";
5
5
  import {
6
6
  Environment,
7
7
  Network,
8
8
  RecordSource,
9
9
  Store
10
10
  } from "relay-runtime";
11
+
12
+ // src/encodeCredentials.ts
13
+ var encodeCredentials = (credentials) => {
14
+ return Buffer.from(
15
+ JSON.stringify({
16
+ accessKeyId: credentials.accessKeyId,
17
+ secretAccessKey: credentials.secretAccessKey,
18
+ sessionToken: credentials.sessionToken
19
+ })
20
+ ).toString("base64");
21
+ };
22
+ var decodeCredentials = (credentials) => {
23
+ return JSON.parse(Buffer.from(credentials, "base64").toString("utf8"));
24
+ };
25
+
26
+ // src/index.ts
11
27
  var fetchQuery = async (operation, variables) => {
28
+ let credentials;
29
+ try {
30
+ const currentCredentials = await Auth.currentCredentials();
31
+ credentials = encodeCredentials(currentCredentials);
32
+ } catch (err) {
33
+ console.error(err?.message);
34
+ credentials = void 0;
35
+ }
12
36
  try {
37
+ const headers = {};
38
+ if (credentials) {
39
+ headers["x-credentials"] = credentials;
40
+ }
13
41
  const response = await API.graphql(
14
- graphqlOperation(operation.text, variables)
42
+ graphqlOperation(operation.text, variables),
43
+ headers
15
44
  );
16
45
  return response;
17
46
  } catch (error) {
@@ -29,5 +58,7 @@ var createEnvironment = ({ storeOptions }) => {
29
58
  };
30
59
  export {
31
60
  createEnvironment,
61
+ decodeCredentials,
62
+ encodeCredentials,
32
63
  fetchQuery
33
64
  };
package/dist/index.d.ts CHANGED
@@ -1,8 +1,19 @@
1
1
  import { FetchFunction, Environment } from 'relay-runtime';
2
2
 
3
+ interface ICredentials {
4
+ accessKeyId: string;
5
+ sessionToken: string;
6
+ secretAccessKey: string;
7
+ identityId: string;
8
+ authenticated: boolean;
9
+ expiration?: Date;
10
+ }
11
+ declare const encodeCredentials: (credentials: ICredentials) => string;
12
+ declare const decodeCredentials: (credentials: string) => any;
13
+
3
14
  declare const fetchQuery: FetchFunction;
4
15
  declare const createEnvironment: ({ storeOptions }: {
5
16
  storeOptions?: any;
6
17
  }) => Environment;
7
18
 
8
- export { createEnvironment, fetchQuery };
19
+ export { ICredentials, createEnvironment, decodeCredentials, encodeCredentials, fetchQuery };
package/dist/index.js CHANGED
@@ -22,15 +22,46 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
22
22
  var src_exports = {};
23
23
  __export(src_exports, {
24
24
  createEnvironment: () => createEnvironment,
25
+ decodeCredentials: () => decodeCredentials,
26
+ encodeCredentials: () => encodeCredentials,
25
27
  fetchQuery: () => fetchQuery
26
28
  });
27
29
  module.exports = __toCommonJS(src_exports);
28
30
  var import_aws_amplify = require("aws-amplify");
29
31
  var import_relay_runtime = require("relay-runtime");
32
+
33
+ // src/encodeCredentials.ts
34
+ var encodeCredentials = (credentials) => {
35
+ return Buffer.from(
36
+ JSON.stringify({
37
+ accessKeyId: credentials.accessKeyId,
38
+ secretAccessKey: credentials.secretAccessKey,
39
+ sessionToken: credentials.sessionToken
40
+ })
41
+ ).toString("base64");
42
+ };
43
+ var decodeCredentials = (credentials) => {
44
+ return JSON.parse(Buffer.from(credentials, "base64").toString("utf8"));
45
+ };
46
+
47
+ // src/index.ts
30
48
  var fetchQuery = async (operation, variables) => {
49
+ let credentials;
50
+ try {
51
+ const currentCredentials = await import_aws_amplify.Auth.currentCredentials();
52
+ credentials = encodeCredentials(currentCredentials);
53
+ } catch (err) {
54
+ console.error(err?.message);
55
+ credentials = void 0;
56
+ }
31
57
  try {
58
+ const headers = {};
59
+ if (credentials) {
60
+ headers["x-credentials"] = credentials;
61
+ }
32
62
  const response = await import_aws_amplify.API.graphql(
33
- (0, import_aws_amplify.graphqlOperation)(operation.text, variables)
63
+ (0, import_aws_amplify.graphqlOperation)(operation.text, variables),
64
+ headers
34
65
  );
35
66
  return response;
36
67
  } catch (error) {
@@ -49,5 +80,7 @@ var createEnvironment = ({ storeOptions }) => {
49
80
  // Annotate the CommonJS export names for ESM import in node:
50
81
  0 && (module.exports = {
51
82
  createEnvironment,
83
+ decodeCredentials,
84
+ encodeCredentials,
52
85
  fetchQuery
53
86
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/relay-amplify",
3
- "version": "0.2.4",
3
+ "version": "0.3.0",
4
4
  "description": "",
5
5
  "license": "UNLICENSED",
6
6
  "author": "ttoss",
@@ -17,6 +17,7 @@
17
17
  "build": "tsup",
18
18
  "test": "jest"
19
19
  },
20
+ "sideEffects": false,
20
21
  "typings": "dist/index.d.ts",
21
22
  "peerDependencies": {
22
23
  "aws-amplify": "^5.0.7",
@@ -31,5 +32,5 @@
31
32
  "publishConfig": {
32
33
  "access": "public"
33
34
  },
34
- "gitHead": "8ba4361ab9cf6c0ab275ee771a371d4c2ba8cebe"
35
+ "gitHead": "ff8bed71bb39e2e7d7385f8bb4a1bfda3553cb5c"
35
36
  }
@@ -0,0 +1,22 @@
1
+ export interface ICredentials {
2
+ accessKeyId: string;
3
+ sessionToken: string;
4
+ secretAccessKey: string;
5
+ identityId: string;
6
+ authenticated: boolean;
7
+ expiration?: Date;
8
+ }
9
+
10
+ export const encodeCredentials = (credentials: ICredentials) => {
11
+ return Buffer.from(
12
+ JSON.stringify({
13
+ accessKeyId: credentials.accessKeyId,
14
+ secretAccessKey: credentials.secretAccessKey,
15
+ sessionToken: credentials.sessionToken,
16
+ })
17
+ ).toString('base64');
18
+ };
19
+
20
+ export const decodeCredentials = (credentials: string) => {
21
+ return JSON.parse(Buffer.from(credentials, 'base64').toString('utf8'));
22
+ };
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { API, graphqlOperation } from 'aws-amplify';
1
+ import { API, Auth, graphqlOperation } from 'aws-amplify';
2
2
  import {
3
3
  Environment,
4
4
  FetchFunction,
@@ -6,12 +6,39 @@ import {
6
6
  RecordSource,
7
7
  Store,
8
8
  } from 'relay-runtime';
9
+ import { encodeCredentials } from './encodeCredentials';
10
+
11
+ export {
12
+ encodeCredentials,
13
+ decodeCredentials,
14
+ ICredentials,
15
+ } from './encodeCredentials';
9
16
 
10
17
  export const fetchQuery: FetchFunction = async (operation, variables) => {
18
+ let credentials: string | undefined;
19
+
20
+ try {
21
+ const currentCredentials = await Auth.currentCredentials();
22
+
23
+ credentials = encodeCredentials(currentCredentials);
24
+ } catch (err: any) {
25
+ // eslint-disable-next-line no-console
26
+ console.error(err?.message);
27
+ credentials = undefined;
28
+ }
29
+
11
30
  try {
31
+ const headers: { [key: string]: string } = {};
32
+
33
+ if (credentials) {
34
+ headers['x-credentials'] = credentials;
35
+ }
36
+
12
37
  const response = await API.graphql(
13
- graphqlOperation(operation.text, variables)
38
+ graphqlOperation(operation.text, variables),
39
+ headers
14
40
  );
41
+
15
42
  return response as any;
16
43
  } catch (error: any) {
17
44
  if (error.errors && error.errors.length > 0) {