appstore-connect-sdk 1.2.1 → 1.3.1

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.
package/dist/cjs/main.js CHANGED
@@ -83,6 +83,135 @@ var Configuration = class {
83
83
  }
84
84
  };
85
85
  var DefaultConfig = new Configuration();
86
+ var BaseAPI = class {
87
+ constructor(configuration = DefaultConfig) {
88
+ this.configuration = configuration;
89
+ this.fetchApi = async (url, init) => {
90
+ let fetchParams = { url, init };
91
+ for (const middleware of this.middleware) {
92
+ if (middleware.pre) {
93
+ fetchParams = await middleware.pre({
94
+ fetch: this.fetchApi,
95
+ ...fetchParams
96
+ }) || fetchParams;
97
+ }
98
+ }
99
+ let response = void 0;
100
+ try {
101
+ response = await (this.configuration.fetchApi || fetch)(fetchParams.url, fetchParams.init);
102
+ } catch (e) {
103
+ for (const middleware of this.middleware) {
104
+ if (middleware.onError) {
105
+ response = await middleware.onError({
106
+ fetch: this.fetchApi,
107
+ url: fetchParams.url,
108
+ init: fetchParams.init,
109
+ error: e,
110
+ response: response ? response.clone() : void 0
111
+ }) || response;
112
+ }
113
+ }
114
+ if (response === void 0) {
115
+ if (e instanceof Error) {
116
+ throw new FetchError(e, "The request failed and the interceptors did not return an alternative response");
117
+ } else {
118
+ throw e;
119
+ }
120
+ }
121
+ }
122
+ for (const middleware of this.middleware) {
123
+ if (middleware.post) {
124
+ response = await middleware.post({
125
+ fetch: this.fetchApi,
126
+ url: fetchParams.url,
127
+ init: fetchParams.init,
128
+ response: response.clone()
129
+ }) || response;
130
+ }
131
+ }
132
+ return response;
133
+ };
134
+ this.middleware = configuration.middleware;
135
+ }
136
+ withMiddleware(...middlewares) {
137
+ const next = this.clone();
138
+ next.middleware = next.middleware.concat(...middlewares);
139
+ return next;
140
+ }
141
+ withPreMiddleware(...preMiddlewares) {
142
+ const middlewares = preMiddlewares.map((pre) => ({ pre }));
143
+ return this.withMiddleware(...middlewares);
144
+ }
145
+ withPostMiddleware(...postMiddlewares) {
146
+ const middlewares = postMiddlewares.map((post) => ({ post }));
147
+ return this.withMiddleware(...middlewares);
148
+ }
149
+ async request(context, initOverrides) {
150
+ const { url, init } = await this.createFetchParams(context, initOverrides);
151
+ const response = await this.fetchApi(url, init);
152
+ if (response && (response.status >= 200 && response.status < 300)) {
153
+ return response;
154
+ }
155
+ throw new ResponseError(response, "Response returned an error code");
156
+ }
157
+ async createFetchParams(context, initOverrides) {
158
+ let url = this.configuration.basePath + context.path;
159
+ if (context.query !== void 0 && Object.keys(context.query).length !== 0) {
160
+ url += "?" + this.configuration.queryParamsStringify(context.query);
161
+ }
162
+ const headers = Object.assign({}, this.configuration.headers, context.headers);
163
+ Object.keys(headers).forEach((key) => headers[key] === void 0 ? delete headers[key] : {});
164
+ const initOverrideFn = typeof initOverrides === "function" ? initOverrides : async () => initOverrides;
165
+ const initParams = {
166
+ method: context.method,
167
+ headers,
168
+ body: context.body,
169
+ credentials: this.configuration.credentials
170
+ };
171
+ const overriddenInit = {
172
+ ...initParams,
173
+ ...await initOverrideFn({
174
+ init: initParams,
175
+ context
176
+ })
177
+ };
178
+ const init = {
179
+ ...overriddenInit,
180
+ body: isFormData(overriddenInit.body) || overriddenInit.body instanceof URLSearchParams || isBlob(overriddenInit.body) ? overriddenInit.body : JSON.stringify(overriddenInit.body)
181
+ };
182
+ return { url, init };
183
+ }
184
+ /**
185
+ * Create a shallow clone of `this` by constructing a new instance
186
+ * and then shallow cloning data members.
187
+ */
188
+ clone() {
189
+ const constructor = this.constructor;
190
+ const next = new constructor(this.configuration);
191
+ next.middleware = this.middleware.slice();
192
+ return next;
193
+ }
194
+ };
195
+ function isBlob(value) {
196
+ return typeof Blob !== "undefined" && value instanceof Blob;
197
+ }
198
+ function isFormData(value) {
199
+ return typeof FormData !== "undefined" && value instanceof FormData;
200
+ }
201
+ var ResponseError = class extends Error {
202
+ constructor(response, msg) {
203
+ super(msg);
204
+ this.response = response;
205
+ this.name = "ResponseError";
206
+ }
207
+ };
208
+ var FetchError = class extends Error {
209
+ constructor(cause, msg) {
210
+ super(msg);
211
+ this.cause = cause;
212
+ this.name = "FetchError";
213
+ }
214
+ };
86
215
  function querystring(params, prefix = "") {
87
216
  return Object.keys(params).map((key) => querystringSingleKey(key, params[key], prefix)).filter((part) => part.length > 0).join("&");
88
217
  }
@@ -1001,12 +1130,13 @@ var AppStoreConnectAPI = class {
1001
1130
  }
1002
1131
  /**
1003
1132
  * Returns the current bearer token, generating a new one if necessary.
1133
+ * The token will be refreshed if it's close to expiration (within 2 minutes).
1004
1134
  */
1005
1135
  async getConfiguration() {
1006
1136
  const latestBearerTokenDuration = this.bearerTokenGeneratedAt ? Date.now() - this.bearerTokenGeneratedAt : 0;
1007
1137
  const expirationDurationMs = 1e3 * (this.options.expirationDuration ?? DEFAULT_EXPIRATION_DURATION_SECONDS);
1008
- const hasExpired = latestBearerTokenDuration > expirationDurationMs;
1009
- if (!this.configuration || hasExpired) {
1138
+ const shouldRefresh = !this.configuration || latestBearerTokenDuration > expirationDurationMs || latestBearerTokenDuration > expirationDurationMs - 2 * 60 * 1e3;
1139
+ if (shouldRefresh) {
1010
1140
  await this.genConfiguration();
1011
1141
  }
1012
1142
  return this.configuration;
@@ -1019,6 +1149,39 @@ var AppStoreConnectAPI = class {
1019
1149
  async create(apiClass) {
1020
1150
  return new apiClass(await this.getConfiguration());
1021
1151
  }
1152
+ /**
1153
+ * Makes an API request.
1154
+ * @param context - The request context for the API request.
1155
+ * @param initOverrides - (Optional) The request options to use for the API request.
1156
+ * @returns The response from the API request.
1157
+ */
1158
+ async request(context, initOverrides) {
1159
+ if (!context.url && !context.path) {
1160
+ throw new Error("No url or path provided");
1161
+ }
1162
+ const _context = { ...context };
1163
+ if (!_context.path && _context.url) {
1164
+ if (_context.url.startsWith(BASE_PATH)) {
1165
+ _context.path = _context.url.replace(BASE_PATH, "");
1166
+ } else {
1167
+ throw new Error("url must start with " + BASE_PATH);
1168
+ }
1169
+ }
1170
+ if (!_context.method) {
1171
+ _context.method = "GET";
1172
+ }
1173
+ delete _context.url;
1174
+ const globalApi = new GlobalAPI(await this.getConfiguration());
1175
+ return await globalApi.request(_context, initOverrides);
1176
+ }
1177
+ };
1178
+ var GlobalAPI = class extends BaseAPI {
1179
+ constructor(configuration) {
1180
+ super(configuration);
1181
+ }
1182
+ request(context, initOverrides) {
1183
+ return super.request(context, initOverrides);
1184
+ }
1022
1185
  };
1023
1186
  // Annotate the CommonJS export names for ESM import in node:
1024
1187
  0 && (module.exports = {