Skip to content

Commit db70f7b

Browse files
rsmogurastocaaro
andauthored
fix: (14277) Unable to override options when calling events.connect and subscribe (#14278)
* fix: (14277) Unable to override options when calling `events.connect` and `subscribe` This fixes issue where overriding options are not applied to methods`events.connect` and `subscribe`. fixes: 14277 * refactor: Adopt more explicit auth assignment, extend to cover apiKeys and fix types * fix: Add support for both post and publish --------- Co-authored-by: Aaron S. <94858815+stocaaro@users.noreply.github.com> Co-authored-by: Aaron S <stocaaro@stocad.com>
1 parent afe0ec6 commit db70f7b

File tree

3 files changed

+72
-32
lines changed

3 files changed

+72
-32
lines changed

packages/api-graphql/__tests__/events.test.ts

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ describe('Events client', () => {
100100
});
101101
});
102102

103-
const authModes: GraphQLAuthMode[] = [
104-
'apiKey',
105-
'userPool',
106-
'oidc',
107-
'iam',
108-
'lambda',
109-
'none',
103+
const authModeConfigs: { authMode: GraphQLAuthMode, apiKey?: string, authToken?: string }[] = [
104+
{ authMode: 'apiKey', apiKey: 'testAPIKey' },
105+
{ authMode: 'userPool', authToken: 'userPoolToken' },
106+
{ authMode: 'oidc', authToken: 'oidcToken' },
107+
{ authMode: 'iam', authToken: 'iamToken' },
108+
{ authMode: 'lambda', authToken: 'lambdaToken' },
109+
{ authMode: 'none' },
110110
];
111111

112112
describe('channel', () => {
@@ -124,12 +124,16 @@ describe('Events client', () => {
124124
mockProvider = AppSyncEventProvider;
125125
});
126126

127-
for (const authMode of authModes) {
128-
test(`auth override: ${authMode}`, async () => {
129-
await events.connect('/', { authMode });
127+
for (const authConfig of authModeConfigs) {
128+
const {authMode: authenticationType, ...config} = authConfig
129+
test(`connect auth override: ${authConfig.authMode}`, async () => {
130+
const channel = await events.connect('/', authConfig);
130131

131132
expect(mockProvider.connect).toHaveBeenCalledWith(
132-
expect.objectContaining({ authenticationType: authMode }),
133+
expect.objectContaining({
134+
authenticationType,
135+
...config
136+
}),
133137
);
134138
});
135139
}
@@ -153,20 +157,24 @@ describe('Events client', () => {
153157
mockProvider = AppSyncEventProvider;
154158
});
155159

156-
for (const authMode of authModes) {
157-
test(`auth override: ${authMode}`, async () => {
158-
const channel = await events.connect('/');
160+
for (const authConfig of authModeConfigs) {
161+
const {authMode: authenticationType, ...config} = authConfig
159162

160-
channel.subscribe(
163+
test(`subscription auth override: ${authConfig.authMode}`, async () => {
164+
const channel = await events.connect('/');
165+
channel.subscribe(
161166
{
162167
next: data => void data,
163168
error: error => void error,
164169
},
165-
{ authMode },
166-
);
167-
168-
expect(mockSubscribeObservable).toHaveBeenCalledWith(
169-
expect.objectContaining({ authenticationType: authMode }),
170+
authConfig
171+
)
172+
173+
expect(mockProvider.subscribe).toHaveBeenCalledWith(
174+
expect.objectContaining({
175+
authenticationType,
176+
...config
177+
}),
170178
);
171179
});
172180
}
@@ -195,14 +203,21 @@ describe('Events client', () => {
195203
mockProvider = AppSyncEventProvider;
196204
});
197205

198-
for (const authMode of authModes) {
199-
test(`auth override: ${authMode}`, async () => {
200-
const channel = await events.connect('/');
206+
for (const authConfig of authModeConfigs) {
207+
const {authMode: authenticationType, ...config} = authConfig
201208

202-
channel.publish({ some: 'data' }, { authMode });
209+
test(`publish auth override: ${authConfig.authMode}`, async () => {
210+
const channel = await events.connect('/');
211+
channel.publish(
212+
"Test message",
213+
authConfig
214+
)
203215

204216
expect(mockProvider.publish).toHaveBeenCalledWith(
205-
expect.objectContaining({ authenticationType: authMode }),
217+
expect.objectContaining({
218+
authenticationType,
219+
...config
220+
}),
206221
);
207222
});
208223
}
@@ -230,16 +245,19 @@ describe('Events client', () => {
230245
);
231246
});
232247

233-
for (const authMode of authModes) {
234-
test(`auth override: ${authMode}`, async () => {
235-
await events.post('/', { test: 'data' }, { authMode });
248+
for (const authConfig of authModeConfigs) {
249+
const {authMode: authenticationType, ...config} = authConfig
250+
251+
test(`auth override: ${authenticationType}`, async () => {
252+
await events.post('/', { test: 'data' }, authConfig);
236253

237254
expect(mockReq).toHaveBeenCalledWith(
238255
Amplify,
239256
expect.objectContaining({
240257
query: '/',
241258
variables: ['{"test":"data"}'],
242-
authenticationType: authMode,
259+
authenticationType,
260+
...config
243261
}),
244262
{},
245263
abortController,

packages/api-graphql/src/internals/events/index.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { configure, normalizeAuth, serializeEvents } from './utils';
1212
import type {
1313
EventsChannel,
1414
EventsOptions,
15+
ProviderOptions,
1516
PublishResponse,
1617
PublishedEvent,
1718
SubscriptionObserver,
@@ -44,12 +45,14 @@ async function connect(
4445
channel: string,
4546
options?: EventsOptions,
4647
): Promise<EventsChannel> {
47-
const providerOptions = configure();
48+
const providerOptions: ProviderOptions = configure();
4849

4950
providerOptions.authenticationType = normalizeAuth(
5051
options?.authMode,
5152
providerOptions.authenticationType,
5253
);
54+
providerOptions.apiKey = options?.apiKey || providerOptions.apiKey;
55+
providerOptions.authToken = options?.authToken || providerOptions.authToken;
5356

5457
await eventProvider.connect(providerOptions);
5558

@@ -70,6 +73,9 @@ async function connect(
7073
subOptions?.authMode,
7174
subscribeOptions.authenticationType,
7275
);
76+
subscribeOptions.apiKey = subOptions?.apiKey || subscribeOptions.apiKey;
77+
subscribeOptions.authToken =
78+
subOptions?.authToken || subscribeOptions.authToken;
7379

7480
_subscription = eventProvider
7581
.subscribe(subscribeOptions)
@@ -94,6 +100,9 @@ async function connect(
94100
pubOptions?.authMode,
95101
publishOptions.authenticationType,
96102
);
103+
publishOptions.apiKey = pubOptions?.apiKey || publishOptions.apiKey;
104+
publishOptions.authToken =
105+
pubOptions?.authToken || publishOptions.authToken;
97106

98107
return eventProvider.publish(publishOptions);
99108
};
@@ -141,11 +150,13 @@ async function post(
141150
event: DocumentType | DocumentType[],
142151
options?: EventsOptions,
143152
): Promise<void | PublishedEvent[]> {
144-
const providerOptions = configure();
153+
const providerOptions: ProviderOptions = configure();
145154
providerOptions.authenticationType = normalizeAuth(
146155
options?.authMode,
147156
providerOptions.authenticationType,
148157
);
158+
providerOptions.apiKey = options?.apiKey || providerOptions.apiKey;
159+
providerOptions.authToken = options?.authToken || providerOptions.authToken;
149160

150161
// trailing slash required in publish
151162
const normalizedChannelName = channel[0] === '/' ? channel : `/${channel}`;
@@ -154,7 +165,6 @@ async function post(
154165
...providerOptions,
155166
query: normalizedChannelName,
156167
variables: serializeEvents(event),
157-
authToken: options?.authToken,
158168
};
159169

160170
const abortController = new AbortController();

packages/api-graphql/src/internals/events/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export type ResolvedGraphQLAuthModes = Exclude<GraphQLAuthMode, 'identityPool'>;
8484
export interface EventsOptions {
8585
authMode?: GraphQLAuthMode;
8686
authToken?: string;
87+
apiKey?: string;
8788
}
8889

8990
export interface PublishedEvent {
@@ -95,3 +96,14 @@ export interface PublishResponse {
9596
failed: PublishedEvent[];
9697
successful: PublishedEvent[];
9798
}
99+
100+
interface EventsConfigure {
101+
appSyncGraphqlEndpoint: string;
102+
region?: string;
103+
authenticationType: ResolvedGraphQLAuthModes;
104+
apiKey?: string;
105+
}
106+
107+
export type ProviderOptions = EventsConfigure & {
108+
authToken?: string;
109+
};

0 commit comments

Comments
 (0)