appstore-connect-sdk 0.0.6 → 0.0.8

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/README.md CHANGED
@@ -18,6 +18,12 @@ The `appstore-connect-sdk` module includes the following features:
18
18
  - [x] Support for custom network libraries for making requests, such as fetch/node-fetch/axios...
19
19
  - [x] Support for _all_ requests due to OpenAPI generated requests and entities
20
20
  - [x] Compatibility with both Node.js and **Deno** environments (_please note that certain features of the appstore-connect-sdk module that rely on the Node crypto module may not work as expected in Deno, as described in the "Deno Compatibility" section above_)
21
+ - [] ESM Support
22
+
23
+ ## Examples
24
+
25
+ - [Deno Example](https://github.com/isaced/appstore-connect-sdk/tree/main/deno_example)
26
+ - [Node Example](https://github.com/isaced/appstore-connect-sdk/tree/main/node_example)
21
27
 
22
28
  ## Installation
23
29
 
package/dist/auth.js CHANGED
@@ -7,12 +7,10 @@ exports.generateAuthToken = void 0;
7
7
  const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
8
8
  // Generates a JWT token for use with the App Store Connect API
9
9
  function generateAuthToken({ apiKeyId, issuerId, privateKey, expirationTime }) {
10
- // Default expiration time is 10 minutes (600 seconds)
11
- const EXPIRATION_TIME = expirationTime || 10 * 60;
12
10
  // Generate the token using the provided options
13
11
  const token = jsonwebtoken_1.default.sign({}, privateKey, {
14
12
  algorithm: "ES256",
15
- expiresIn: EXPIRATION_TIME,
13
+ expiresIn: expirationTime,
16
14
  issuer: issuerId,
17
15
  audience: "appstoreconnect-v1",
18
16
  header: {
@@ -24,8 +22,6 @@ function generateAuthToken({ apiKeyId, issuerId, privateKey, expirationTime }) {
24
22
  typ: "JWT",
25
23
  },
26
24
  });
27
- // Log the generated token to the console for debugging purposes
28
- console.log("Generated JWT Token: ", token);
29
25
  // Return the generated token
30
26
  return token;
31
27
  }
package/dist/main.d.ts CHANGED
@@ -24,6 +24,10 @@ interface AppStoreConnectAPIOptions {
24
24
  * A bearer token can be provided directly, which will be used instead of generating a new token
25
25
  */
26
26
  bearerToken?: string;
27
+ /**
28
+ * The time (in seconds) until the token expires (default 10 minutes)
29
+ */
30
+ expirationTime?: number;
27
31
  }
28
32
  /**
29
33
  * The App Store Connect SDK for Node.js is written in TypeScript and supports all APIs
@@ -32,8 +36,9 @@ interface AppStoreConnectAPIOptions {
32
36
  * https://github.com/isaced/appstore-connect-sdk
33
37
  */
34
38
  export default class AppStoreConnectAPI {
35
- private bearerToken?;
36
- configuration?: Configuration;
39
+ private bearerTokenGeneratedAt;
40
+ private options;
41
+ private configuration?;
37
42
  /**
38
43
  * Creates an instance of the AppStoreConnectAPI.
39
44
  * @param options - The configuration options for the API.
@@ -42,9 +47,22 @@ export default class AppStoreConnectAPI {
42
47
  * @param options.privateKey - The content of the private key used for generating JWT token.
43
48
  * @param options.fetchApi - (Optional) The FetchAPI implementation to use for API requests.
44
49
  * @param options.bearerToken - (Optional) A pre-generated bearer token to use for authentication.
50
+ * @param options.expirationTime - (Optional) The time (in seconds) until the token expires (default 10 minutes)
45
51
  * @throws {string} Will throw an error if no bearerToken or private key is provided
46
52
  */
47
53
  constructor(options: AppStoreConnectAPIOptions);
54
+ /**
55
+ * Generates a new bearer token.
56
+ */
57
+ genToken(): string;
58
+ /**
59
+ * Create a Configuration object with the authentication token and any provided FetchAPI implementation.
60
+ */
61
+ genConfiguration(): void;
62
+ /**
63
+ * Returns the current bearer token, generating a new one if necessary.
64
+ */
65
+ getConfiguration(): Configuration | undefined;
48
66
  /**
49
67
  * Calls the specified API class using the current Configuration object.
50
68
  * @param apiClass - The API class to instantiate.
package/dist/main.js CHANGED
@@ -17,30 +17,54 @@ class AppStoreConnectAPI {
17
17
  * @param options.privateKey - The content of the private key used for generating JWT token.
18
18
  * @param options.fetchApi - (Optional) The FetchAPI implementation to use for API requests.
19
19
  * @param options.bearerToken - (Optional) A pre-generated bearer token to use for authentication.
20
+ * @param options.expirationTime - (Optional) The time (in seconds) until the token expires (default 10 minutes)
20
21
  * @throws {string} Will throw an error if no bearerToken or private key is provided
21
22
  */
22
23
  constructor(options) {
23
- // Generate an authentication token using the provided options.
24
- if (options.bearerToken) {
25
- this.bearerToken = options.bearerToken;
24
+ this.bearerTokenGeneratedAt = 0;
25
+ options.expirationTime = options.expirationTime || 60 * 10;
26
+ this.options = options;
27
+ }
28
+ /**
29
+ * Generates a new bearer token.
30
+ */
31
+ genToken() {
32
+ if (this.options.bearerToken) {
33
+ return this.options.bearerToken;
26
34
  }
27
- else if (options.privateKeyId && options.issuerId && options.privateKey) {
28
- this.bearerToken = (0, auth_1.generateAuthToken)({
29
- apiKeyId: options.privateKeyId,
30
- issuerId: options.issuerId,
31
- privateKey: options.privateKey,
35
+ else if (this.options.privateKeyId && this.options.issuerId && this.options.privateKey) {
36
+ return (0, auth_1.generateAuthToken)({
37
+ apiKeyId: this.options.privateKeyId,
38
+ issuerId: this.options.issuerId,
39
+ privateKey: this.options.privateKey,
40
+ expirationTime: this.options.expirationTime,
32
41
  });
33
42
  }
34
43
  else {
35
44
  throw 'No bearerToken!!!';
36
45
  }
37
- // Create a Configuration object with the authentication token and any provided FetchAPI implementation.
46
+ }
47
+ /**
48
+ * Create a Configuration object with the authentication token and any provided FetchAPI implementation.
49
+ */
50
+ genConfiguration() {
38
51
  this.configuration = new openapi_1.Configuration({
39
52
  headers: {
40
- Authorization: `Bearer ${this.bearerToken}`,
53
+ Authorization: `Bearer ${this.genToken()}`,
41
54
  },
42
- fetchApi: options.fetchApi,
55
+ fetchApi: this.options.fetchApi,
43
56
  });
57
+ this.bearerTokenGeneratedAt = Date.now();
58
+ }
59
+ /**
60
+ * Returns the current bearer token, generating a new one if necessary.
61
+ */
62
+ getConfiguration() {
63
+ const hasExpired = this.bearerTokenGeneratedAt && (Date.now() - this.bearerTokenGeneratedAt) > (1000 * this.options.expirationTime);
64
+ if (!this.configuration || hasExpired) {
65
+ this.genConfiguration();
66
+ }
67
+ return this.configuration;
44
68
  }
45
69
  /**
46
70
  * Calls the specified API class using the current Configuration object.
@@ -48,7 +72,7 @@ class AppStoreConnectAPI {
48
72
  * @returns An instance of the specified API class.
49
73
  */
50
74
  call(apiClass) {
51
- return new apiClass(this.configuration);
75
+ return new apiClass(this.getConfiguration());
52
76
  }
53
77
  }
54
78
  exports.default = AppStoreConnectAPI;
@@ -49,9 +49,9 @@ function AppEventAttributesTerritorySchedulesInnerToJSON(value) {
49
49
  }
50
50
  return {
51
51
  'territories': value.territories,
52
- 'publishStart': value.publishStart === undefined ? undefined : (value.publishStart.toISOString()),
53
- 'eventStart': value.eventStart === undefined ? undefined : (value.eventStart.toISOString()),
54
- 'eventEnd': value.eventEnd === undefined ? undefined : (value.eventEnd.toISOString()),
52
+ 'publishStart': typeof value.publishStart === 'string' ? value.publishStart : (value.publishStart === undefined ? undefined : (value.publishStart.toISOString())),
53
+ 'eventStart': typeof value.eventStart === 'string' ? value.eventStart : (value.eventStart === undefined ? undefined : (value.eventStart.toISOString())),
54
+ 'eventEnd': typeof value.eventEnd === 'string' ? value.eventEnd : (value.eventEnd === undefined ? undefined : (value.eventEnd.toISOString())),
55
55
  };
56
56
  }
57
57
  exports.AppEventAttributesTerritorySchedulesInnerToJSON = AppEventAttributesTerritorySchedulesInnerToJSON;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "appstore-connect-sdk",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "description": "An App Store Connect API client for Node.js",
5
5
  "keywords": [
6
6
  "appstore connect",