@snugdesk/whatsapp-widget 0.1.7 → 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,7 +25,13 @@ npm install @snugdesk/core
25
25
  npm install @snugdesk/whatsapp-widget
26
26
  ```
27
27
 
28
- The widget bundle brings its own runtime dependencies (`aws-amplify`, `@aws-amplify/api-graphql`, `@aws-sdk/client-s3`, `@ctrl/ngx-emoji-mart`, `ngx-avatars`, `ngx-infinite-scroll`, `ngx-skeleton-loader`, `moment-timezone`, `libphonenumber-js`, `sort-nested-json`, `uuid`, …). No extra manual installs are needed unless you want to pin specific versions in your workspace.
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
+
30
+ ```bash
31
+ npm install aws-amplify@^5 @aws-amplify/api-graphql@^3
32
+ ```
33
+
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`, …).
29
35
 
30
36
  ---
31
37
 
@@ -166,6 +172,16 @@ Ensure you render the widget only after you have a fresh token to avoid the comp
166
172
 
167
173
  ---
168
174
 
175
+ ## 🧩 Amplify Configuration Behavior
176
+
177
+ The widget can run in two modes without any extra setup from third parties:
178
+
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
+ - 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`).
182
+
183
+ ---
184
+
169
185
  ## 🎨 Assets & Styling
170
186
 
171
187
  The library bundles icons, background artwork, and shared CSS under `@snugdesk/whatsapp-widget/assets`. To make them available during your build, add the folder to the Angular CLI asset list:
@@ -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,6 +142,12 @@ class AppSyncGraphqlService {
142
142
  if (this.configured) {
143
143
  return;
144
144
  }
145
+ const hostConfig = this.getHostConfig();
146
+ if (hostConfig.useHost) {
147
+ this.authMode = this.normalizeAuthMode(hostConfig.authMode) ?? GRAPHQL_AUTH_MODE.API_KEY;
148
+ this.configured = true;
149
+ return;
150
+ }
145
151
  if (!this.config?.endpoint || !this.config?.region) {
146
152
  throw new Error('AppSync GraphQL configuration is missing.');
147
153
  }
@@ -150,12 +156,12 @@ class AppSyncGraphqlService {
150
156
  aws_project_region: region,
151
157
  aws_appsync_graphqlEndpoint: endpoint,
152
158
  aws_appsync_region: region,
153
- aws_appsync_authenticationType: 'AWS_LAMBDA',
159
+ aws_appsync_authenticationType: GRAPHQL_AUTH_MODE.AWS_LAMBDA,
154
160
  API: {
155
161
  GraphQL: {
156
162
  endpoint,
157
163
  region,
158
- defaultAuthMode: 'AWS_LAMBDA',
164
+ defaultAuthMode: GRAPHQL_AUTH_MODE.AWS_LAMBDA,
159
165
  graphql_headers: async () => this.buildAuthHeaders(),
160
166
  functionAuthProvider: async () => ({
161
167
  token: this.getToken(),
@@ -163,8 +169,37 @@ class AppSyncGraphqlService {
163
169
  },
164
170
  },
165
171
  });
172
+ this.authMode = GRAPHQL_AUTH_MODE.AWS_LAMBDA;
166
173
  this.configured = true;
167
174
  }
175
+ getHostConfig() {
176
+ const existing = Amplify.getConfig?.() ?? {};
177
+ const existingEndpoint = existing?.API?.GraphQL?.endpoint ?? existing?.API?.GraphQL?.graphql_endpoint ?? existing?.aws_appsync_graphqlEndpoint;
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
+ }
202
+ }
168
203
  buildAuthHeaders() {
169
204
  const token = this.getToken();
170
205
  if (!token) {