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/main.js CHANGED
@@ -47,6 +47,135 @@ var Configuration = class {
47
47
  }
48
48
  };
49
49
  var DefaultConfig = new Configuration();
50
+ var BaseAPI = class {
51
+ constructor(configuration = DefaultConfig) {
52
+ this.configuration = configuration;
53
+ this.fetchApi = async (url, init) => {
54
+ let fetchParams = { url, init };
55
+ for (const middleware of this.middleware) {
56
+ if (middleware.pre) {
57
+ fetchParams = await middleware.pre({
58
+ fetch: this.fetchApi,
59
+ ...fetchParams
60
+ }) || fetchParams;
61
+ }
62
+ }
63
+ let response = void 0;
64
+ try {
65
+ response = await (this.configuration.fetchApi || fetch)(fetchParams.url, fetchParams.init);
66
+ } catch (e) {
67
+ for (const middleware of this.middleware) {
68
+ if (middleware.onError) {
69
+ response = await middleware.onError({
70
+ fetch: this.fetchApi,
71
+ url: fetchParams.url,
72
+ init: fetchParams.init,
73
+ error: e,
74
+ response: response ? response.clone() : void 0
75
+ }) || response;
76
+ }
77
+ }
78
+ if (response === void 0) {
79
+ if (e instanceof Error) {
80
+ throw new FetchError(e, "The request failed and the interceptors did not return an alternative response");
81
+ } else {
82
+ throw e;
83
+ }
84
+ }
85
+ }
86
+ for (const middleware of this.middleware) {
87
+ if (middleware.post) {
88
+ response = await middleware.post({
89
+ fetch: this.fetchApi,
90
+ url: fetchParams.url,
91
+ init: fetchParams.init,
92
+ response: response.clone()
93
+ }) || response;
94
+ }
95
+ }
96
+ return response;
97
+ };
98
+ this.middleware = configuration.middleware;
99
+ }
100
+ withMiddleware(...middlewares) {
101
+ const next = this.clone();
102
+ next.middleware = next.middleware.concat(...middlewares);
103
+ return next;
104
+ }
105
+ withPreMiddleware(...preMiddlewares) {
106
+ const middlewares = preMiddlewares.map((pre) => ({ pre }));
107
+ return this.withMiddleware(...middlewares);
108
+ }
109
+ withPostMiddleware(...postMiddlewares) {
110
+ const middlewares = postMiddlewares.map((post) => ({ post }));
111
+ return this.withMiddleware(...middlewares);
112
+ }
113
+ async request(context, initOverrides) {
114
+ const { url, init } = await this.createFetchParams(context, initOverrides);
115
+ const response = await this.fetchApi(url, init);
116
+ if (response && (response.status >= 200 && response.status < 300)) {
117
+ return response;
118
+ }
119
+ throw new ResponseError(response, "Response returned an error code");
120
+ }
121
+ async createFetchParams(context, initOverrides) {
122
+ let url = this.configuration.basePath + context.path;
123
+ if (context.query !== void 0 && Object.keys(context.query).length !== 0) {
124
+ url += "?" + this.configuration.queryParamsStringify(context.query);
125
+ }
126
+ const headers = Object.assign({}, this.configuration.headers, context.headers);
127
+ Object.keys(headers).forEach((key) => headers[key] === void 0 ? delete headers[key] : {});
128
+ const initOverrideFn = typeof initOverrides === "function" ? initOverrides : async () => initOverrides;
129
+ const initParams = {
130
+ method: context.method,
131
+ headers,
132
+ body: context.body,
133
+ credentials: this.configuration.credentials
134
+ };
135
+ const overriddenInit = {
136
+ ...initParams,
137
+ ...await initOverrideFn({
138
+ init: initParams,
139
+ context
140
+ })
141
+ };
142
+ const init = {
143
+ ...overriddenInit,
144
+ body: isFormData(overriddenInit.body) || overriddenInit.body instanceof URLSearchParams || isBlob(overriddenInit.body) ? overriddenInit.body : JSON.stringify(overriddenInit.body)
145
+ };
146
+ return { url, init };
147
+ }
148
+ /**
149
+ * Create a shallow clone of `this` by constructing a new instance
150
+ * and then shallow cloning data members.
151
+ */
152
+ clone() {
153
+ const constructor = this.constructor;
154
+ const next = new constructor(this.configuration);
155
+ next.middleware = this.middleware.slice();
156
+ return next;
157
+ }
158
+ };
159
+ function isBlob(value) {
160
+ return typeof Blob !== "undefined" && value instanceof Blob;
161
+ }
162
+ function isFormData(value) {
163
+ return typeof FormData !== "undefined" && value instanceof FormData;
164
+ }
165
+ var ResponseError = class extends Error {
166
+ constructor(response, msg) {
167
+ super(msg);
168
+ this.response = response;
169
+ this.name = "ResponseError";
170
+ }
171
+ };
172
+ var FetchError = class extends Error {
173
+ constructor(cause, msg) {
174
+ super(msg);
175
+ this.cause = cause;
176
+ this.name = "FetchError";
177
+ }
178
+ };
50
179
  function querystring(params, prefix = "") {
51
180
  return Object.keys(params).map((key) => querystringSingleKey(key, params[key], prefix)).filter((part) => part.length > 0).join("&");
52
181
  }
@@ -965,12 +1094,13 @@ var AppStoreConnectAPI = class {
965
1094
  }
966
1095
  /**
967
1096
  * Returns the current bearer token, generating a new one if necessary.
1097
+ * The token will be refreshed if it's close to expiration (within 2 minutes).
968
1098
  */
969
1099
  async getConfiguration() {
970
1100
  const latestBearerTokenDuration = this.bearerTokenGeneratedAt ? Date.now() - this.bearerTokenGeneratedAt : 0;
971
1101
  const expirationDurationMs = 1e3 * (this.options.expirationDuration ?? DEFAULT_EXPIRATION_DURATION_SECONDS);
972
- const hasExpired = latestBearerTokenDuration > expirationDurationMs;
973
- if (!this.configuration || hasExpired) {
1102
+ const shouldRefresh = !this.configuration || latestBearerTokenDuration > expirationDurationMs || latestBearerTokenDuration > expirationDurationMs - 2 * 60 * 1e3;
1103
+ if (shouldRefresh) {
974
1104
  await this.genConfiguration();
975
1105
  }
976
1106
  return this.configuration;
@@ -983,6 +1113,39 @@ var AppStoreConnectAPI = class {
983
1113
  async create(apiClass) {
984
1114
  return new apiClass(await this.getConfiguration());
985
1115
  }
1116
+ /**
1117
+ * Makes an API request.
1118
+ * @param context - The request context for the API request.
1119
+ * @param initOverrides - (Optional) The request options to use for the API request.
1120
+ * @returns The response from the API request.
1121
+ */
1122
+ async request(context, initOverrides) {
1123
+ if (!context.url && !context.path) {
1124
+ throw new Error("No url or path provided");
1125
+ }
1126
+ const _context = { ...context };
1127
+ if (!_context.path && _context.url) {
1128
+ if (_context.url.startsWith(BASE_PATH)) {
1129
+ _context.path = _context.url.replace(BASE_PATH, "");
1130
+ } else {
1131
+ throw new Error("url must start with " + BASE_PATH);
1132
+ }
1133
+ }
1134
+ if (!_context.method) {
1135
+ _context.method = "GET";
1136
+ }
1137
+ delete _context.url;
1138
+ const globalApi = new GlobalAPI(await this.getConfiguration());
1139
+ return await globalApi.request(_context, initOverrides);
1140
+ }
1141
+ };
1142
+ var GlobalAPI = class extends BaseAPI {
1143
+ constructor(configuration) {
1144
+ super(configuration);
1145
+ }
1146
+ request(context, initOverrides) {
1147
+ return super.request(context, initOverrides);
1148
+ }
986
1149
  };
987
1150
  export {
988
1151
  AppStoreConnectAPI