Skip to content

Commit fb3e3a8

Browse files
committed
feat: add optional AmplifyContext overloads to all category APIs
1 parent 93fce80 commit fb3e3a8

22 files changed

Lines changed: 546 additions & 87 deletions

File tree

packages/analytics/src/providers/kinesis-firehose/apis/flushEvents.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { AmplifyContext } from '@aws-amplify/core';
21
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
32
// SPDX-License-Identifier: Apache-2.0
43

5-
import { AnalyticsAction } from '@aws-amplify/core/internals/utils';
6-
import { ConsoleLogger } from '@aws-amplify/core';
4+
import { AmplifyContext, ConsoleLogger } from '@aws-amplify/core';
5+
import {
6+
AnalyticsAction,
7+
resolveCtxArgs,
8+
} from '@aws-amplify/core/internals/utils';
79

810
import { getEventBuffer, resolveConfig } from '../utils';
911
import {
@@ -20,7 +22,10 @@ const logger = new ConsoleLogger('KinesisFirehose');
2022
* This API will make a best-effort attempt to flush events from the buffer. Events recorded immediately after invoking
2123
* this API may not be included in the flush.
2224
*/
23-
export const flushEvents = (ctx: AmplifyContext) => {
25+
export function flushEvents(): void;
26+
export function flushEvents(ctx: AmplifyContext): void;
27+
export function flushEvents(...args: any[]): void {
28+
const [ctx] = resolveCtxArgs<undefined>(args);
2429
const { region, flushSize, flushInterval, bufferSize, resendLimit } =
2530
resolveConfig(ctx);
2631
resolveCredentials(ctx)
@@ -40,4 +45,4 @@ export const flushEvents = (ctx: AmplifyContext) => {
4045
.catch(e => {
4146
logger.warn('Failed to flush events.', e);
4247
});
43-
};
48+
}

packages/analytics/src/providers/kinesis-firehose/apis/record.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { AmplifyContext } from '@aws-amplify/core';
21
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
32
// SPDX-License-Identifier: Apache-2.0
43

4+
import { AmplifyContext, ConsoleLogger } from '@aws-amplify/core';
5+
import {
6+
AnalyticsAction,
7+
resolveCtxArgs,
8+
} from '@aws-amplify/core/internals/utils';
59
import { fromUtf8 } from '@smithy/util-utf8';
6-
import { AnalyticsAction } from '@aws-amplify/core/internals/utils';
7-
import { ConsoleLogger } from '@aws-amplify/core';
810

911
import { RecordInput } from '../types';
1012
import { getEventBuffer, resolveConfig } from '../utils';
@@ -34,7 +36,12 @@ const logger = new ConsoleLogger('KinesisFirehose');
3436
* });
3537
* ```
3638
*/
37-
export const record = (ctx: AmplifyContext, { streamName, data }: RecordInput): void => {
39+
export function record(input: RecordInput): void;
40+
export function record(ctx: AmplifyContext, input: RecordInput): void;
41+
export function record(...args: any[]): void {
42+
const [ctx, input] = resolveCtxArgs<RecordInput>(args);
43+
const { streamName, data } = input;
44+
3845
if (!isAnalyticsEnabled()) {
3946
logger.debug('Analytics is disabled, event will not be recorded.');
4047

@@ -69,4 +76,4 @@ export const record = (ctx: AmplifyContext, { streamName, data }: RecordInput):
6976
.catch(e => {
7077
logger.warn('Failed to record event.', e);
7178
});
72-
};
79+
}

packages/analytics/src/providers/kinesis/apis/flushEvents.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { AmplifyContext } from '@aws-amplify/core';
21
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
32
// SPDX-License-Identifier: Apache-2.0
43

5-
import { AnalyticsAction } from '@aws-amplify/core/internals/utils';
6-
import { ConsoleLogger } from '@aws-amplify/core';
4+
import { AmplifyContext, ConsoleLogger } from '@aws-amplify/core';
5+
import {
6+
AnalyticsAction,
7+
resolveCtxArgs,
8+
} from '@aws-amplify/core/internals/utils';
79

810
import { resolveConfig } from '../utils/resolveConfig';
911
import {
@@ -21,7 +23,10 @@ const logger = new ConsoleLogger('Kinesis');
2123
* This API will make a best-effort attempt to flush events from the buffer. Events recorded immediately after invoking
2224
* this API may not be included in the flush.
2325
*/
24-
export const flushEvents = (ctx: AmplifyContext) => {
26+
export function flushEvents(): void;
27+
export function flushEvents(ctx: AmplifyContext): void;
28+
export function flushEvents(...args: any[]): void {
29+
const [ctx] = resolveCtxArgs<undefined>(args);
2530
const { region, flushSize, flushInterval, bufferSize, resendLimit } =
2631
resolveConfig(ctx);
2732
resolveCredentials(ctx)
@@ -41,4 +46,4 @@ export const flushEvents = (ctx: AmplifyContext) => {
4146
.catch(e => {
4247
logger.warn('Failed to flush events.', e);
4348
});
44-
};
49+
}

packages/analytics/src/providers/kinesis/apis/record.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { AmplifyContext } from '@aws-amplify/core';
21
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
32
// SPDX-License-Identifier: Apache-2.0
43

4+
import { AmplifyContext, ConsoleLogger } from '@aws-amplify/core';
5+
import {
6+
AnalyticsAction,
7+
resolveCtxArgs,
8+
} from '@aws-amplify/core/internals/utils';
59
import { fromUtf8 } from '@smithy/util-utf8';
6-
import { AnalyticsAction } from '@aws-amplify/core/internals/utils';
7-
import { ConsoleLogger } from '@aws-amplify/core';
810

911
import { RecordInput } from '../types';
1012
import { getEventBuffer } from '../utils/getEventBuffer';
@@ -39,11 +41,12 @@ const logger = new ConsoleLogger('Kinesis');
3941
*
4042
* @returns void
4143
*/
42-
export const record = (ctx: AmplifyContext, {
43-
streamName,
44-
partitionKey,
45-
data,
46-
}: RecordInput): void => {
44+
export function record(input: RecordInput): void;
45+
export function record(ctx: AmplifyContext, input: RecordInput): void;
46+
export function record(...args: any[]): void {
47+
const [ctx, input] = resolveCtxArgs<RecordInput>(args);
48+
const { streamName, partitionKey, data } = input;
49+
4750
if (!isAnalyticsEnabled()) {
4851
logger.debug('Analytics is disabled, event will not be recorded.');
4952

@@ -80,4 +83,4 @@ export const record = (ctx: AmplifyContext, {
8083
// An error occured while fetching credentials or persisting the event to the buffer
8184
logger.warn('Failed to record event.', e);
8285
});
83-
};
86+
}

packages/analytics/src/providers/pinpoint/apis/configureAutoTrack.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
import { AmplifyContext } from '@aws-amplify/core';
5+
import { resolveCtxArgs } from '@aws-amplify/core/internals/utils';
46
import { UpdateEndpointException } from '@aws-amplify/core/internals/providers/pinpoint';
57

68
import { AnalyticsValidationErrorCode } from '../../../errors';
@@ -15,7 +17,6 @@ import {
1517
} from '../../../utils';
1618
import { ConfigureAutoTrackInput } from '../types';
1719

18-
import { AmplifyContext } from '@aws-amplify/core';
1920
import { record } from './record';
2021

2122
// Configured Tracker instances for Pinpoint
@@ -47,9 +48,15 @@ const emitTrackingEvent = (ctx: AmplifyContext,
4748
* @throws validation: {@link AnalyticsValidationErrorCode} - Thrown when the provided parameters or library
4849
* configuration is incorrect.
4950
*/
50-
export const configureAutoTrack = (ctx: AmplifyContext, input: ConfigureAutoTrackInput): void => {
51+
export function configureAutoTrack(input: ConfigureAutoTrackInput): void;
52+
export function configureAutoTrack(
53+
ctx: AmplifyContext,
54+
input: ConfigureAutoTrackInput,
55+
): void;
56+
export function configureAutoTrack(...args: any[]): void {
57+
const [ctx, input] = resolveCtxArgs<ConfigureAutoTrackInput>(args);
5158
validateTrackerConfiguration(input);
5259

5360
// Initialize or update this provider's trackers
5461
updateProviderTrackers(input, emitTrackingEvent.bind(null, ctx), configuredTrackers);
55-
};
62+
}

packages/analytics/src/providers/pinpoint/apis/flushEvents.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { AmplifyContext } from '@aws-amplify/core';
21
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
32
// SPDX-License-Identifier: Apache-2.0
43

4+
import { AmplifyContext, ConsoleLogger } from '@aws-amplify/core';
5+
import {
6+
AnalyticsAction,
7+
resolveCtxArgs,
8+
} from '@aws-amplify/core/internals/utils';
59
import { flushEvents as flushEventsCore } from '@aws-amplify/core/internals/providers/pinpoint';
6-
import { AnalyticsAction } from '@aws-amplify/core/internals/utils';
7-
import { ConsoleLogger } from '@aws-amplify/core';
810

911
import { resolveConfig, resolveCredentials } from '../utils';
1012
import { getAnalyticsUserAgentString } from '../../../utils';
@@ -20,7 +22,10 @@ const logger = new ConsoleLogger('Analytics');
2022
* This API will make a best-effort attempt to flush events from the buffer. Events recorded immediately after invoking
2123
* this API may not be included in the flush.
2224
*/
23-
export const flushEvents = (ctx: AmplifyContext) => {
25+
export function flushEvents(): void;
26+
export function flushEvents(ctx: AmplifyContext): void;
27+
export function flushEvents(...args: any[]): void {
28+
const [ctx] = resolveCtxArgs<undefined>(args);
2429
const { appId, region, bufferSize, flushSize, flushInterval, resendLimit } =
2530
resolveConfig(ctx);
2631
resolveCredentials(ctx)
@@ -40,4 +45,4 @@ export const flushEvents = (ctx: AmplifyContext) => {
4045
.catch(e => {
4146
logger.warn('Failed to flush events', e);
4247
});
43-
};
48+
}

packages/analytics/src/providers/pinpoint/apis/identifyUser.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import { AmplifyContext } from '@aws-amplify/core';
21
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
32
// SPDX-License-Identifier: Apache-2.0
43

5-
import { AnalyticsAction } from '@aws-amplify/core/internals/utils';
4+
import { AmplifyContext } from '@aws-amplify/core';
5+
import {
6+
AnalyticsAction,
7+
resolveCtxArgs,
8+
} from '@aws-amplify/core/internals/utils';
69
import {
710
UpdateEndpointException,
811
updateEndpoint,
@@ -60,11 +63,14 @@ import { resolveConfig, resolveCredentials } from '../utils';
6063
* }
6164
* });
6265
*/
63-
export const identifyUser = async (ctx: AmplifyContext, {
64-
userId,
65-
userProfile,
66-
options,
67-
}: IdentifyUserInput): Promise<void> => {
66+
export async function identifyUser(input: IdentifyUserInput): Promise<void>;
67+
export async function identifyUser(
68+
ctx: AmplifyContext,
69+
input: IdentifyUserInput,
70+
): Promise<void>;
71+
export async function identifyUser(...args: any[]): Promise<void> {
72+
const [ctx, input] = resolveCtxArgs<IdentifyUserInput>(args);
73+
const { userId, userProfile, options } = input;
6874
const { credentials, identityId } = await resolveCredentials(ctx);
6975
const { appId, region } = resolveConfig(ctx);
7076
const { userAttributes } = options ?? {};
@@ -79,4 +85,4 @@ export const identifyUser = async (ctx: AmplifyContext, {
7985
userProfile,
8086
userAgentValue: getAnalyticsUserAgentString(AnalyticsAction.IdentifyUser),
8187
});
82-
};
88+
}

packages/analytics/src/providers/pinpoint/apis/record.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { AmplifyContext } from '@aws-amplify/core';
21
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
32
// SPDX-License-Identifier: Apache-2.0
43

5-
import { ConsoleLogger, Hub } from '@aws-amplify/core';
4+
import { AmplifyContext, ConsoleLogger, Hub } from '@aws-amplify/core';
65
import {
76
AMPLIFY_SYMBOL,
87
AnalyticsAction,
8+
resolveCtxArgs,
99
} from '@aws-amplify/core/internals/utils';
1010
import { record as recordCore } from '@aws-amplify/core/internals/providers/pinpoint';
1111

@@ -52,7 +52,10 @@ const logger = new ConsoleLogger('Analytics');
5252
* })
5353
* ```
5454
*/
55-
export const record = (ctx: AmplifyContext, input: RecordInput): void => {
55+
export function record(input: RecordInput): void;
56+
export function record(ctx: AmplifyContext, input: RecordInput): void;
57+
export function record(...args: any[]): void {
58+
const [ctx, input] = resolveCtxArgs<RecordInput>(args);
5659
const { appId, region, bufferSize, flushSize, flushInterval, resendLimit } =
5760
resolveConfig(ctx);
5861

@@ -90,4 +93,4 @@ export const record = (ctx: AmplifyContext, input: RecordInput): void => {
9093
// An error occured while fetching credentials or persisting the event to the buffer
9194
logger.warn('Failed to record event.', e);
9295
});
93-
};
96+
}

packages/api-rest/src/apis/index.ts

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
import { AmplifyContext } from '@aws-amplify/core';
5+
import { resolveCtxArgs } from '@aws-amplify/core/internals/utils';
56

67
import {
78
DeleteInput,
@@ -67,7 +68,13 @@ import {
6768
* }
6869
* ```
6970
*/
70-
export const get = (ctx: AmplifyContext, input: GetInput): GetOperation => commonGet(ctx, input);
71+
export function get(input: GetInput): GetOperation;
72+
export function get(ctx: AmplifyContext, input: GetInput): GetOperation;
73+
export function get(...args: any[]): GetOperation {
74+
const [ctx, input] = resolveCtxArgs<GetInput>(args);
75+
76+
return commonGet(ctx, input);
77+
}
7178

7279
/**
7380
* POST HTTP request
@@ -108,8 +115,13 @@ export const get = (ctx: AmplifyContext, input: GetInput): GetOperation => commo
108115
* }
109116
* ```
110117
*/
111-
export const post = (ctx: AmplifyContext, input: PostInput): PostOperation =>
112-
commonPost(ctx, input);
118+
export function post(input: PostInput): PostOperation;
119+
export function post(ctx: AmplifyContext, input: PostInput): PostOperation;
120+
export function post(...args: any[]): PostOperation {
121+
const [ctx, input] = resolveCtxArgs<PostInput>(args);
122+
123+
return commonPost(ctx, input);
124+
}
113125

114126
/**
115127
* PUT HTTP request
@@ -149,7 +161,13 @@ export const post = (ctx: AmplifyContext, input: PostInput): PostOperation =>
149161
* }
150162
* ```
151163
*/
152-
export const put = (ctx: AmplifyContext, input: PutInput): PutOperation => commonPut(ctx, input);
164+
export function put(input: PutInput): PutOperation;
165+
export function put(ctx: AmplifyContext, input: PutInput): PutOperation;
166+
export function put(...args: any[]): PutOperation {
167+
const [ctx, input] = resolveCtxArgs<PutInput>(args);
168+
169+
return commonPut(ctx, input);
170+
}
153171

154172
/**
155173
* DELETE HTTP request
@@ -171,8 +189,13 @@ export const put = (ctx: AmplifyContext, input: PutInput): PutOperation => commo
171189
* }).response;
172190
* ```
173191
*/
174-
export const del = (ctx: AmplifyContext, input: DeleteInput): DeleteOperation =>
175-
commonDel(ctx, input);
192+
export function del(input: DeleteInput): DeleteOperation;
193+
export function del(ctx: AmplifyContext, input: DeleteInput): DeleteOperation;
194+
export function del(...args: any[]): DeleteOperation {
195+
const [ctx, input] = resolveCtxArgs<DeleteInput>(args);
196+
197+
return commonDel(ctx, input);
198+
}
176199

177200
/**
178201
* HEAD HTTP request
@@ -195,8 +218,13 @@ export const del = (ctx: AmplifyContext, input: DeleteInput): DeleteOperation =>
195218
* ```
196219
*
197220
*/
198-
export const head = (ctx: AmplifyContext, input: HeadInput): HeadOperation =>
199-
commonHead(ctx, input);
221+
export function head(input: HeadInput): HeadOperation;
222+
export function head(ctx: AmplifyContext, input: HeadInput): HeadOperation;
223+
export function head(...args: any[]): HeadOperation {
224+
const [ctx, input] = resolveCtxArgs<HeadInput>(args);
225+
226+
return commonHead(ctx, input);
227+
}
200228

201229
/**
202230
* PATCH HTTP request
@@ -237,5 +265,10 @@ export const head = (ctx: AmplifyContext, input: HeadInput): HeadOperation =>
237265
* }
238266
* ```
239267
*/
240-
export const patch = (ctx: AmplifyContext, input: PatchInput): PatchOperation =>
241-
commonPatch(ctx, input);
268+
export function patch(input: PatchInput): PatchOperation;
269+
export function patch(ctx: AmplifyContext, input: PatchInput): PatchOperation;
270+
export function patch(...args: any[]): PatchOperation {
271+
const [ctx, input] = resolveCtxArgs<PatchInput>(args);
272+
273+
return commonPatch(ctx, input);
274+
}

0 commit comments

Comments
 (0)