@snugdesk/whatsapp-widget 0.1.8 → 0.1.9

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.

Potentially problematic release.


This version of @snugdesk/whatsapp-widget might be problematic. Click here for more details.

package/README.md CHANGED
@@ -25,10 +25,10 @@ npm install @snugdesk/core
25
25
  npm install @snugdesk/whatsapp-widget
26
26
  ```
27
27
 
28
- The widget expects the host app to provide Amplify packages (peer dependencies). If your app does not already use Amplify, install them explicitly:
28
+ The widget expects the host app to provide Amplify packages (peer dependencies). This library supports Amplify v5 only. If your app does not already use Amplify, install them explicitly:
29
29
 
30
30
  ```bash
31
- npm install aws-amplify @aws-amplify/api-graphql
31
+ npm install aws-amplify@^5 @aws-amplify/api-graphql@^3
32
32
  ```
33
33
 
34
34
  Other runtime dependencies are bundled (`@aws-sdk/client-s3`, `@ctrl/ngx-emoji-mart`, `ngx-avatars`, `ngx-infinite-scroll`, `ngx-skeleton-loader`, `moment-timezone`, `libphonenumber-js`, `sort-nested-json`, `uuid`, …).
@@ -177,7 +177,8 @@ Ensure you render the widget only after you have a fresh token to avoid the comp
177
177
  The widget can run in two modes without any extra setup from third parties:
178
178
 
179
179
  - If the host already configured Amplify and its AppSync endpoint matches the widget default (`defaultAppSyncConfig.endpoint`), the widget will **reuse the host config** and **skip** `Amplify.configure`.
180
- - Otherwise, the widget will **configure Amplify itself** using its default configuration.
180
+ - The widget honors the host AppSync auth mode when it reuses the host config (for example, `API_KEY` vs `AWS_LAMBDA`).
181
+ - Otherwise, the widget will **configure Amplify itself** using its default configuration (`AWS_LAMBDA`).
181
182
 
182
183
  ---
183
184
 
@@ -23,6 +23,7 @@ import { MatSelectModule } from '@angular/material/select';
23
23
  import * as i1 from '@snugdesk/core';
24
24
  import { CustomValidators, CleanDeep, SnugdeskCoreModule, CustomPipesModule } from '@snugdesk/core';
25
25
  import { API, Amplify } from 'aws-amplify';
26
+ import { GRAPHQL_AUTH_MODE } from '@aws-amplify/api-graphql';
26
27
  import moment from 'moment-timezone';
27
28
  import { lastValueFrom, BehaviorSubject } from 'rxjs';
28
29
  import * as i1$1 from '@angular/common/http';
@@ -58,6 +59,7 @@ function provideAppSyncConfig(config) {
58
59
  class AppSyncGraphqlService {
59
60
  authenticationService;
60
61
  configured = false;
62
+ authMode = GRAPHQL_AUTH_MODE.AWS_LAMBDA;
61
63
  config;
62
64
  constructor(config, authenticationService) {
63
65
  this.authenticationService = authenticationService;
@@ -66,14 +68,13 @@ class AppSyncGraphqlService {
66
68
  }
67
69
  async query(statement, variables) {
68
70
  this.ensureConfigured();
69
- const token = this.getToken();
70
71
  // console.debug('token: ', token);
71
72
  try {
72
73
  return (await API.graphql({
73
74
  query: statement,
74
75
  variables,
75
- authMode: 'AWS_LAMBDA',
76
- authToken: token,
76
+ authMode: this.authMode,
77
+ ...(this.authMode === GRAPHQL_AUTH_MODE.AWS_LAMBDA ? { authToken: this.getToken() } : {}),
77
78
  }));
78
79
  }
79
80
  catch (err) {
@@ -105,13 +106,12 @@ class AppSyncGraphqlService {
105
106
  }
106
107
  subscribe(statement, variables) {
107
108
  this.ensureConfigured();
108
- const token = this.getToken();
109
109
  try {
110
110
  return API.graphql({
111
111
  query: statement,
112
112
  variables,
113
- authMode: 'AWS_LAMBDA',
114
- authToken: token,
113
+ authMode: this.authMode,
114
+ ...(this.authMode === GRAPHQL_AUTH_MODE.AWS_LAMBDA ? { authToken: this.getToken() } : {}),
115
115
  });
116
116
  }
117
117
  catch (err) {
@@ -142,7 +142,9 @@ class AppSyncGraphqlService {
142
142
  if (this.configured) {
143
143
  return;
144
144
  }
145
- if (this.shouldUseHostConfig()) {
145
+ const hostConfig = this.getHostConfig();
146
+ if (hostConfig.useHost) {
147
+ this.authMode = this.normalizeAuthMode(hostConfig.authMode) ?? GRAPHQL_AUTH_MODE.API_KEY;
146
148
  this.configured = true;
147
149
  return;
148
150
  }
@@ -154,12 +156,12 @@ class AppSyncGraphqlService {
154
156
  aws_project_region: region,
155
157
  aws_appsync_graphqlEndpoint: endpoint,
156
158
  aws_appsync_region: region,
157
- aws_appsync_authenticationType: 'AWS_LAMBDA',
159
+ aws_appsync_authenticationType: GRAPHQL_AUTH_MODE.AWS_LAMBDA,
158
160
  API: {
159
161
  GraphQL: {
160
162
  endpoint,
161
163
  region,
162
- defaultAuthMode: 'AWS_LAMBDA',
164
+ defaultAuthMode: GRAPHQL_AUTH_MODE.AWS_LAMBDA,
163
165
  graphql_headers: async () => this.buildAuthHeaders(),
164
166
  functionAuthProvider: async () => ({
165
167
  token: this.getToken(),
@@ -167,12 +169,36 @@ class AppSyncGraphqlService {
167
169
  },
168
170
  },
169
171
  });
172
+ this.authMode = GRAPHQL_AUTH_MODE.AWS_LAMBDA;
170
173
  this.configured = true;
171
174
  }
172
- shouldUseHostConfig() {
175
+ getHostConfig() {
173
176
  const existing = Amplify.getConfig?.() ?? {};
174
177
  const existingEndpoint = existing?.API?.GraphQL?.endpoint ?? existing?.API?.GraphQL?.graphql_endpoint ?? existing?.aws_appsync_graphqlEndpoint;
175
- return existingEndpoint === defaultAppSyncConfig.endpoint;
178
+ const existingAuthMode = existing?.API?.GraphQL?.defaultAuthMode ?? existing?.aws_appsync_authenticationType;
179
+ return {
180
+ useHost: existingEndpoint === defaultAppSyncConfig.endpoint,
181
+ authMode: existingAuthMode,
182
+ };
183
+ }
184
+ normalizeAuthMode(mode) {
185
+ if (!mode) {
186
+ return undefined;
187
+ }
188
+ switch (mode) {
189
+ case 'API_KEY':
190
+ return GRAPHQL_AUTH_MODE.API_KEY;
191
+ case 'AWS_IAM':
192
+ return GRAPHQL_AUTH_MODE.AWS_IAM;
193
+ case 'OPENID_CONNECT':
194
+ return GRAPHQL_AUTH_MODE.OPENID_CONNECT;
195
+ case 'AMAZON_COGNITO_USER_POOLS':
196
+ return GRAPHQL_AUTH_MODE.AMAZON_COGNITO_USER_POOLS;
197
+ case 'AWS_LAMBDA':
198
+ return GRAPHQL_AUTH_MODE.AWS_LAMBDA;
199
+ default:
200
+ return undefined;
201
+ }
176
202
  }
177
203
  buildAuthHeaders() {
178
204
  const token = this.getToken();