-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory.ts
More file actions
243 lines (219 loc) · 9.5 KB
/
Copy pathfactory.ts
File metadata and controls
243 lines (219 loc) · 9.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/**
* Copyright (c) 2026 Apple Inc. Licensed under MIT License.
*/
import axios, { AxiosInstance } from 'axios';
import { HttpAgent, HttpsAgent } from 'agentkeepalive';
import { AppleAdsApi } from './api';
import { EphemeralClientSecretProvider } from './auth/ephemeral-client-secret-provider';
import { AccessTokenFetcher } from './auth/access-token-fetcher';
import { InMemoryCachingAccessTokenProvider } from './auth/in-memory-caching-access-token-provider';
import { installAuthInterceptors } from './auth/interceptors';
import { AUTH_AUDIENCE } from './auth/constants';
import type { AccessTokenProvider, ClientSecretProvider } from './auth/types';
import { installErrorInterceptor } from './error-interceptor';
import {
installLoggingInterceptors,
DEFAULT_REQUEST_LOG_LEVEL,
DEFAULT_ERROR_LOG_LEVEL,
} from './logging';
import type { Logger, LogLevel } from './logging';
import { version } from '../package.json';
const USER_AGENT = `apple-ads-node-${version}`;
const DEFAULT_AUTH_BASE_URL = AUTH_AUDIENCE + '/';
export const DEFAULT_BASE_URL = 'https://api.ads.apple.com/v1/';
/**
* Default connection pool settings for the API axios instance.
* These do not apply to the auth axios instance, which uses a plain connection
* (no keep-alive) since token refreshes are too infrequent to benefit from pooling.
*/
const DEFAULT_MAX_SOCKETS = 50;
const DEFAULT_MAX_FREE_SOCKETS = 10;
export const DEFAULT_TIMEOUT_MS = 5_000;
/**
* Idle sockets are removed from the pool after this many milliseconds of inactivity.
* Prevents stale connection errors when the API server closes an idle keep-alive
* connection before the client attempts to reuse it.
*/
const DEFAULT_FREE_SOCKET_TIMEOUT_MS = 30_000;
interface CommonOptions {
/** Base URL for the Apple Ads API. Defaults to https://api.ads.apple.com/v1/. */
baseUrl?: string;
/** Timeout in milliseconds for API requests. Defaults to 5000ms. */
apiTimeout?: number;
/**
* Maximum number of open sockets per host for the API connection pool. Defaults to 50.
* Has no effect on the auth axios instance, which does not use connection pooling.
*/
maxSockets?: number;
/**
* Maximum number of idle keep-alive sockets per host for the API connection pool. Defaults to 10.
* Has no effect on the auth axios instance, which does not use connection pooling.
*/
maxFreeSockets?: number;
/**
* Idle sockets are evicted from the API connection pool after this many milliseconds.
* Defaults to 30000ms. Set this below the server's keep-alive idle timeout to avoid
* stale connection errors.
* Has no effect on the auth axios instance, which does not use connection pooling.
*/
freeSocketTimeout?: number;
/**
* Optional callback to customize the axios instance used for API calls.
* The factory creates the instance with keep-alive connection pooling enabled; this
* callback lets you add interceptors, headers, or override any settings.
*/
apiAxiosCustomizer?: (instance: AxiosInstance) => void;
/**
* Logger for HTTP request/response logging. Defaults to `console`.
* Pass `null` to disable logging entirely.
*/
logger?: Logger | null;
/**
* Log level for request start and successful responses. Defaults to 'info'.
* Applies to both API and auth axios instances.
*/
requestLogLevel?: LogLevel;
/**
* Log level for request errors. Defaults to 'error'.
* Applies to both API and auth axios instances.
*/
errorLogLevel?: LogLevel;
}
/** Full key-based auth: the factory constructs the JWT client secret internally. */
export interface KeyAuthOptions extends CommonOptions {
authMode: 'key';
clientId: string;
teamId: string;
keyId: string;
/** Path to the .pem private key file created for authentication with the Apple Ads Platform API. */
privateKeyPath: string;
/** Override the Apple auth base URL (default: https://appleid.apple.com/). */
authBaseUrl?: string;
/** Timeout in milliseconds for token requests to the auth server. Defaults to 5000ms. */
authTimeout?: number;
/** Optional callback to customize the axios instance used for token requests. */
authAxiosCustomizer?: (instance: AxiosInstance) => void;
}
/** Bring-your-own client secret: you supply a {@link ClientSecretProvider} implementation. */
export interface ClientSecretAuthOptions extends CommonOptions {
authMode: 'clientSecret';
clientId: string;
clientSecretProvider: ClientSecretProvider;
/** Override the Apple auth base URL (default: https://appleid.apple.com/). */
authBaseUrl?: string;
/** Timeout in milliseconds for token requests to the auth server. Defaults to 5000ms. */
authTimeout?: number;
/** Optional callback to customize the axios instance used for token requests. */
authAxiosCustomizer?: (instance: AxiosInstance) => void;
}
/** Bring-your-own token: you supply a fully configured {@link AccessTokenProvider}. */
export interface TokenAuthOptions extends CommonOptions {
authMode: 'token';
accessTokenProvider: AccessTokenProvider;
}
export type AppleAdsApiOptions = KeyAuthOptions | ClientSecretAuthOptions | TokenAuthOptions;
/**
* Creates an axios instance with keep-alive connection pooling for high-frequency API calls.
* Uses agentkeepalive to evict idle sockets before they go stale.
*/
function createPooledApiAxiosInstance(
timeout?: number,
maxSockets?: number,
maxFreeSockets?: number,
freeSocketTimeout?: number,
): AxiosInstance {
const agentOptions = {
keepAlive: true,
maxSockets: maxSockets ?? DEFAULT_MAX_SOCKETS,
maxFreeSockets: maxFreeSockets ?? DEFAULT_MAX_FREE_SOCKETS,
freeSocketTimeout: freeSocketTimeout ?? DEFAULT_FREE_SOCKET_TIMEOUT_MS,
};
return axios.create({
timeout,
httpAgent: new HttpAgent(agentOptions),
httpsAgent: new HttpsAgent(agentOptions),
});
}
/**
* Creates a plain axios instance without connection pooling for auth token requests.
* Keep-alive is intentionally disabled: token refreshes are too infrequent (typically
* once per hour) to benefit from pooling, and reusing a stale connection would cause
* ECONNRESET errors.
*/
function createAuthAxiosInstance(timeout?: number): AxiosInstance {
return axios.create({ timeout });
}
function installUserAgentInterceptor(instance: AxiosInstance): void {
instance.interceptors.request.use((config) => {
config.headers.set('User-Agent', USER_AGENT);
return config;
});
}
/**
* Creates a fully configured {@link AppleAdsApi} instance.
*
* - For `authMode: 'key'` and `authMode: 'clientSecret'`, this function is async
* because it fetches an initial access token before returning.
* - For `authMode: 'token'`, the provided {@link AccessTokenProvider} is used directly.
*
* The API axios instance is created with keep-alive connection pooling via agentkeepalive.
* The auth axios instance uses a plain connection (no pooling). Use the
* `apiAxiosCustomizer` / `authAxiosCustomizer` callbacks to add interceptors or override defaults.
*
* Logging is installed before the initial token fetch on the auth instance, so all token
* requests (including the first) are captured. On the API instance, logging is installed
* last (after auth interceptors) so that response logs reflect final outcomes rather than
* intermediate states such as 401 responses that are transparently retried.
*/
export async function createAppleAdsApi(options: AppleAdsApiOptions): Promise<AppleAdsApi> {
const logger: Logger | null = 'logger' in options
? (options.logger ?? null)
: console;
const requestLogLevel: LogLevel = options.requestLogLevel ?? DEFAULT_REQUEST_LOG_LEVEL;
const errorLogLevel: LogLevel = options.errorLogLevel ?? DEFAULT_ERROR_LOG_LEVEL;
const apiAxiosInstance = createPooledApiAxiosInstance(
options.apiTimeout ?? DEFAULT_TIMEOUT_MS,
options.maxSockets,
options.maxFreeSockets,
options.freeSocketTimeout,
);
options.apiAxiosCustomizer?.(apiAxiosInstance);
installUserAgentInterceptor(apiAxiosInstance);
let accessTokenProvider: AccessTokenProvider;
if (options.authMode === 'token') {
accessTokenProvider = options.accessTokenProvider;
} else {
const authAxiosInstance = createAuthAxiosInstance(options.authTimeout ?? DEFAULT_TIMEOUT_MS);
options.authAxiosCustomizer?.(authAxiosInstance);
if (logger) {
installLoggingInterceptors(authAxiosInstance, logger, requestLogLevel, errorLogLevel);
}
const clientSecretProvider: ClientSecretProvider =
options.authMode === 'key'
? await EphemeralClientSecretProvider.create(
options.privateKeyPath,
options.clientId,
options.teamId,
options.keyId,
)
: options.clientSecretProvider;
const fetcher = new AccessTokenFetcher(
options.authBaseUrl ?? DEFAULT_AUTH_BASE_URL,
options.clientId,
clientSecretProvider,
authAxiosInstance,
);
accessTokenProvider = await InMemoryCachingAccessTokenProvider.create(fetcher, logger);
}
installAuthInterceptors(apiAxiosInstance, accessTokenProvider, logger);
if (logger) {
installLoggingInterceptors(apiAxiosInstance, logger, requestLogLevel, errorLogLevel);
}
// Installed last so it runs outermost on the response-error path: the auth retry and
// logging interceptors above must see the raw axios error first. This converts the
// rejection that escapes the library into a flat, serializable ApiRequestError,
// stripping the circular references axios attaches via the ClientRequest/socket and
// the keep-alive agent.
installErrorInterceptor(apiAxiosInstance);
return new AppleAdsApi(options.baseUrl ?? DEFAULT_BASE_URL, apiAxiosInstance);
}