@blizzard-api/client 2.0.0 → 2.0.2

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/index.d.ts CHANGED
@@ -43,6 +43,44 @@ interface AccessTokenRequestArguments {
43
43
  origin?: Origins;
44
44
  secret?: string;
45
45
  }
46
+ /**
47
+ * A client configuration object.
48
+ * @interface ClientOptions
49
+ * @property key The client ID.
50
+ * @property secret The client secret.
51
+ * @property origin The region of the Blizzard API.
52
+ * @property locale The locale of the Blizzard API.
53
+ * @property token The access token.
54
+ * @example
55
+ * const options: ClientOptions = {
56
+ * key: 'client',
57
+ * secret: 'secret',
58
+ * origin: 'eu',
59
+ * locale: 'en_GB',
60
+ * token: 'access'
61
+ * };
62
+ */
63
+ interface ClientOptions {
64
+ key: string;
65
+ locale?: Locales;
66
+ origin: Origins;
67
+ secret: string;
68
+ token?: string;
69
+ }
70
+ /**
71
+ * A Blizzard API client.
72
+ * @interface IBlizzardApiClient
73
+ * @property getAccessToken Get an access token.
74
+ * @property setAccessToken Set an access token.
75
+ * @property refreshAccessToken Refresh an access token.
76
+ * @property validateAccessToken Validate an access token.
77
+ */
78
+ interface IBlizzardApiClient {
79
+ getAccessToken: (options: AccessTokenRequestArguments) => Promise<AxiosResponse<AccessToken>>;
80
+ refreshAccessToken: (options: AccessTokenRequestArguments) => Promise<AxiosResponse<AccessToken>>;
81
+ setAccessToken: (token: string) => void;
82
+ validateAccessToken: (options: ValidateAccessTokenArguments) => Promise<AxiosResponse<ValidateAccessTokenResponse>>;
83
+ }
46
84
  /**
47
85
  * Validate an access token.
48
86
  * @see https://develop.battle.net/documentation/guides/using-oauth/client-credentials-flow
@@ -87,44 +125,6 @@ interface ValidateAccessTokenResponse {
87
125
  exp: number;
88
126
  scope: Array<string>;
89
127
  }
90
- /**
91
- * A client configuration object.
92
- * @interface ClientOptions
93
- * @property key The client ID.
94
- * @property secret The client secret.
95
- * @property origin The region of the Blizzard API.
96
- * @property locale The locale of the Blizzard API.
97
- * @property token The access token.
98
- * @example
99
- * const options: ClientOptions = {
100
- * key: 'client',
101
- * secret: 'secret',
102
- * origin: 'eu',
103
- * locale: 'en_GB',
104
- * token: 'access'
105
- * };
106
- */
107
- interface ClientOptions {
108
- key: string;
109
- locale?: Locales;
110
- origin: Origins;
111
- secret: string;
112
- token?: string;
113
- }
114
- /**
115
- * A Blizzard API client.
116
- * @interface IBlizzardApiClient
117
- * @property getAccessToken Get an access token.
118
- * @property setAccessToken Set an access token.
119
- * @property refreshAccessToken Refresh an access token.
120
- * @property validateAccessToken Validate an access token.
121
- */
122
- interface IBlizzardApiClient {
123
- getAccessToken: (options: AccessTokenRequestArguments) => Promise<AxiosResponse<AccessToken>>;
124
- refreshAccessToken: (options: AccessTokenRequestArguments) => Promise<AxiosResponse<AccessToken>>;
125
- setAccessToken: (token: string) => void;
126
- validateAccessToken: (options: ValidateAccessTokenArguments) => Promise<AxiosResponse<ValidateAccessTokenResponse>>;
127
- }
128
128
 
129
129
  /**
130
130
  * A Blizzard API client.
@@ -141,7 +141,6 @@ interface IBlizzardApiClient {
141
141
  * });
142
142
  */
143
143
  declare class BlizzardApiClient implements IBlizzardApiClient {
144
- private axios;
145
144
  defaults: {
146
145
  key: string;
147
146
  locale: Locales;
@@ -149,6 +148,7 @@ declare class BlizzardApiClient implements IBlizzardApiClient {
149
148
  secret: string;
150
149
  token?: string;
151
150
  };
151
+ private axios;
152
152
  /**
153
153
  * Get an access token.
154
154
  * @param options The access token request arguments. See {@link AccessTokenRequestArguments}.
@@ -191,8 +191,8 @@ declare class BlizzardApiClient implements IBlizzardApiClient {
191
191
  * console.log(response.data.client_id);
192
192
  * // => 'client-id'
193
193
  */
194
- validateAccessToken: (options?: ValidateAccessTokenArguments) => Promise<AxiosResponse<ValidateAccessTokenResponse>>;
195
194
  constructor(options: ClientOptions);
195
+ validateAccessToken: (options?: ValidateAccessTokenArguments) => Promise<AxiosResponse<ValidateAccessTokenResponse>>;
196
196
  /**
197
197
  * Get the request configuration.
198
198
  * @param resource The resource to fetch. See {@link Resource}.
package/dist/index.js CHANGED
@@ -5,8 +5,8 @@ import axios, { isAxiosError, AxiosError } from 'axios';
5
5
 
6
6
  // src/client/create-client.ts
7
7
  var BlizzardApiClient = class {
8
- axios = axios.create();
9
8
  defaults;
9
+ axios = axios.create();
10
10
  /**
11
11
  * Get an access token.
12
12
  * @param options The access token request arguments. See {@link AccessTokenRequestArguments}.
@@ -69,6 +69,16 @@ var BlizzardApiClient = class {
69
69
  * console.log(response.data.client_id);
70
70
  * // => 'client-id'
71
71
  */
72
+ constructor(options) {
73
+ const { locale, origin } = getBlizzardApi(options.origin, options.locale);
74
+ this.defaults = {
75
+ key: options.key,
76
+ locale,
77
+ origin,
78
+ secret: options.secret,
79
+ token: options.token
80
+ };
81
+ }
72
82
  validateAccessToken = async (options) => {
73
83
  const { origin, token } = { ...this.defaults, ...options };
74
84
  if (!token) {
@@ -84,16 +94,6 @@ var BlizzardApiClient = class {
84
94
  }
85
95
  );
86
96
  };
87
- constructor(options) {
88
- const { locale, origin } = getBlizzardApi(options.origin, options.locale);
89
- this.defaults = {
90
- key: options.key,
91
- locale,
92
- origin,
93
- secret: options.secret,
94
- token: options.token
95
- };
96
- }
97
97
  /**
98
98
  * Get the request configuration.
99
99
  * @param resource The resource to fetch. See {@link Resource}.
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/client/client.ts","../src/client/create-client.ts"],"names":[],"mappings":";;;;;;AA4BO,IAAM,oBAAN,MAAsD;AAAA,EACnD,KAAA,GAAQ,MAAM,MAAO,EAAA,CAAA;AAAA,EAEtB,QAAA,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAqBA,cAAA,GAAiB,OAAO,OAA+E,KAAA;AAC5G,IAAM,MAAA,EAAE,GAAK,EAAA,MAAA,EAAQ,MAAO,EAAA,GAAI,EAA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,GAAA,kBAAA,CAAmB,cAAe,CAAA,IAAA,CAAK,GAAG,CAAA,CAAA;AAEzD,MAAA,IAAI,MAAS,GAAA,IAAA,CAAK,GAAI,EAAA,GAAI,GAAQ,EAAA;AAChC,QAAA,MAAM,YAAa,EAAA,CAAA;AAAA,OACd,MAAA;AAEL,QAAM,MAAA,OAAA,GAAU,WAAW,MAAM,KAAK,cAAgB,EAAA,MAAA,GAAS,IAAK,CAAA,GAAA,EAAK,CAAA,CAAA;AAEzE,QAAA,OAAA,CAAQ,KAAM,EAAA,CAAA;AAAA,OAChB;AAAA,KACM,CAAA,MAAA;AAEN,MAAA,MAAM,YAAa,EAAA,CAAA;AAAA,KACrB;AAAA,GACK,MAAA;AAEL,IAAA,MAAM,YAAa,EAAA,CAAA;AAAA,GACrB;AAEA,EAAO,OAAA,MAAA,CAAA;AACT","file":"index.js","sourcesContent":["import { stringify } from 'node:querystring';\r\nimport { getBlizzardApi } from '@blizzard-api/core';\r\nimport type { Locales, Origins, Resource, ResourceResponse } from '@blizzard-api/core';\r\nimport type { AxiosResponse } from 'axios';\r\nimport axios, { AxiosError, isAxiosError } from 'axios';\r\nimport type {\r\n AccessToken,\r\n AccessTokenRequestArguments,\r\n ClientOptions,\r\n IBlizzardApiClient,\r\n ValidateAccessTokenArguments,\r\n ValidateAccessTokenResponse,\r\n} from './types';\r\n\r\n/**\r\n * A Blizzard API client.\r\n * @implements IBlizzardApiClient\r\n * @class\r\n * @classdesc A client to interact with the Blizzard API.\r\n * @example\r\n * const client = new BlizzardApiClient({\r\n * key: 'client',\r\n * secret: 'secret',\r\n * origin: 'eu',\r\n * locale: 'en_GB',\r\n * token: 'access'\r\n * });\r\n */\r\nexport class BlizzardApiClient implements IBlizzardApiClient {\r\n private axios = axios.create();\r\n\r\n public defaults: {\r\n key: string;\r\n locale: Locales;\r\n origin: Origins;\r\n secret: string;\r\n token?: string;\r\n };\r\n\r\n /**\r\n * Get an access token.\r\n * @param options The access token request arguments. See {@link AccessTokenRequestArguments}.\r\n * @returns The access token. See {@link AccessToken}.\r\n * @example\r\n * const response = await client.getAccessToken();\r\n * const { access_token, token_type, expires_in, sub } = response.data;\r\n * console.log(access_token, token_type, expires_in, sub);\r\n * // => 'access'\r\n * // => 'bearer'\r\n * // => 86399\r\n * // => 'client-id'\r\n */\r\n public getAccessToken = async (options?: AccessTokenRequestArguments): Promise<AxiosResponse<AccessToken>> => {\r\n const { key, origin, secret } = { ...this.defaults, ...options };\r\n return this.axios.post<AccessToken>(`https://${origin}.battle.net/oauth/token`, undefined, {\r\n auth: {\r\n password: secret,\r\n username: key,\r\n },\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n },\r\n params: {\r\n grant_type: 'client_credentials',\r\n },\r\n });\r\n };\r\n\r\n /**\r\n * Refresh the access token.\r\n * @param options The access token request arguments. See {@link AccessTokenRequestArguments}.\r\n * @returns The access token. See {@link AccessToken}.\r\n * @example\r\n * const response = await client.refreshAccessToken();\r\n * const { access_token, token_type, expires_in, sub } = response.data;\r\n * console.log(access_token, token_type, expires_in, sub);\r\n * // => 'access'\r\n * // => 'bearer'\r\n * // => 86399\r\n * // => 'client-id'\r\n */\r\n public refreshAccessToken = async (options?: AccessTokenRequestArguments): Promise<AxiosResponse<AccessToken>> => {\r\n const response = await this.getAccessToken(options);\r\n this.setAccessToken(response.data.access_token);\r\n return response;\r\n };\r\n\r\n /**\r\n * Set the access token.\r\n * @param token The access token.\r\n */\r\n public setAccessToken = (token: string): void => {\r\n this.defaults.token = token;\r\n };\r\n\r\n /**\r\n * Validate an access token.\r\n * @param options The validate access token arguments. See {@link ValidateAccessTokenArguments}.\r\n * @returns The response from the Blizzard API. See {@link ValidateAccessTokenResponse}.\r\n * @example\r\n * const response = await client.validateAccessToken({ token: 'access' });\r\n * console.log(response.data.client_id);\r\n * // => 'client-id'\r\n */\r\n public validateAccessToken = async (\r\n options?: ValidateAccessTokenArguments,\r\n ): Promise<AxiosResponse<ValidateAccessTokenResponse>> => {\r\n const { origin, token } = { ...this.defaults, ...options };\r\n\r\n if (!token) {\r\n throw new Error('No token has been set previously or been passed to the validateAccessToken method.');\r\n }\r\n\r\n return await this.axios.post<ValidateAccessTokenResponse>(\r\n `https://${origin}.battle.net/oauth/check_token`,\r\n stringify({ token }),\r\n {\r\n headers: {\r\n 'Content-Type': 'application/x-www-form-urlencoded',\r\n },\r\n },\r\n );\r\n };\r\n\r\n constructor(options: ClientOptions) {\r\n const { locale, origin } = getBlizzardApi(options.origin, options.locale);\r\n this.defaults = {\r\n key: options.key,\r\n locale: locale,\r\n origin: origin,\r\n secret: options.secret,\r\n token: options.token,\r\n };\r\n }\r\n\r\n /**\r\n * Get the request configuration.\r\n * @param resource The resource to fetch. See {@link Resource}.\r\n * @param options Client options. See {@link ClientOptions}.\r\n * @param headers Additional headers to include in the request.\r\n * @returns The request configuration.\r\n */\r\n public getRequestConfig<T, Protected extends boolean = false>(\r\n resource: Resource<T, object, Protected>,\r\n options?: Partial<ClientOptions>,\r\n headers?: Record<string, string>,\r\n ) {\r\n const config = { ...this.defaults, ...options };\r\n const endpoint = getBlizzardApi(config.origin, config.locale);\r\n\r\n const namespace = resource.namespace\r\n ? { 'Battlenet-Namespace': `${resource.namespace}-${endpoint.origin}` }\r\n : undefined;\r\n\r\n return {\r\n headers: {\r\n ...headers,\r\n ...namespace,\r\n Authorization: `Bearer ${config.token}`,\r\n 'Content-Type': 'application/json',\r\n },\r\n params: {\r\n locale: endpoint.locale,\r\n ...resource.parameters,\r\n },\r\n };\r\n }\r\n\r\n /**\r\n * Get the request URL.\r\n * @param resource The resource to fetch. See {@link Resource}.\r\n * @param options Client options. See {@link ClientOptions}.\r\n * @returns The request URL.\r\n */\r\n public getRequestUrl<T, Protected extends boolean = false>(\r\n resource: Resource<T, object, Protected>,\r\n options?: Partial<ClientOptions>,\r\n ) {\r\n const config = { ...this.defaults, ...options };\r\n const endpoint = getBlizzardApi(config.origin, config.locale);\r\n\r\n const backslashSeparator = resource.path.startsWith('/') ? '' : '/';\r\n\r\n return `${endpoint.hostname}${backslashSeparator}${resource.path}`;\r\n }\r\n\r\n /**\r\n * Send a request to the Blizzard API.\r\n * @param resource The resource to fetch. See {@link Resource}.\r\n * @param options Client options. See {@link ClientOptions}.\r\n * @param headers Additional headers to include in the request.\r\n * @returns The response from the Blizzard API. See {@link ResourceResponse}.\r\n */\r\n public async sendRequest<T, Protected extends boolean = false>(\r\n resource: Resource<T, object, Protected>,\r\n options?: Partial<ClientOptions>,\r\n headers?: Record<string, string>,\r\n ): ResourceResponse<AxiosResponse<T>> {\r\n const url = this.getRequestUrl(resource, options);\r\n const config = this.getRequestConfig(resource, options, headers);\r\n\r\n try {\r\n return await this.axios.get<T>(url, config);\r\n } catch (error) {\r\n if (isAxiosError(error)) {\r\n throw new AxiosError(error.message, error.code);\r\n }\r\n throw error;\r\n }\r\n }\r\n}\r\n","//We have to explicitly import setTimeout because of https://github.com/electron/electron/issues/21162#issuecomment-554792447\r\nimport { setTimeout } from 'node:timers';\r\nimport { BlizzardApiClient } from './client';\r\nimport type { AccessToken, ClientOptions } from './types';\r\n\r\nconst getTokenExpiration = (expiresIn: number) => expiresIn * 1000 - 60_000;\r\n\r\n/**\r\n * Create a new Blizzard API client.\r\n * @param options Client options, see {@link ClientOptions} & https://develop.battle.net/documentation/guides/getting-started\r\n * @param onTokenRefresh Callback function to handle token refresh. If set to `true`, the client will automatically refresh the token. If set to `false`, the client will not refresh the token. If set to a function, the function will be called with the new token.\r\n * @returns A new Blizzard API client.\r\n */\r\nexport const createBlizzardApiClient = async (\r\n options: ClientOptions,\r\n onTokenRefresh: ((token: AccessToken) => void) | boolean = true,\r\n): Promise<BlizzardApiClient> => {\r\n const { key, secret, token } = options;\r\n if (!key) {\r\n throw new Error(`Client missing 'key' parameter`);\r\n }\r\n if (!secret) {\r\n throw new Error(`Client missing 'secret' parameter`);\r\n }\r\n\r\n const client = new BlizzardApiClient(options);\r\n\r\n const refreshToken = async () => {\r\n const response = await client.getAccessToken();\r\n\r\n client.setAccessToken(response.data.access_token);\r\n\r\n if (typeof onTokenRefresh === 'function') {\r\n onTokenRefresh?.(response.data);\r\n }\r\n\r\n //Schedule a refresh of the token\r\n const timeout = setTimeout(() => void refreshToken(), getTokenExpiration(response.data.expires_in));\r\n timeout.unref();\r\n };\r\n\r\n //If tokenRefresh is false, return the client without refreshing the token\r\n if (!onTokenRefresh) {\r\n return client;\r\n }\r\n\r\n if (token) {\r\n try {\r\n //If token is set, validate the token\r\n const validatedToken = await client.validateAccessToken({ token });\r\n const expiry = getTokenExpiration(validatedToken.data.exp);\r\n //If token is expiring in less than 60 seconds, refresh the token\r\n if (expiry - Date.now() < 60_000) {\r\n await refreshToken();\r\n } else {\r\n //If token is not expiring, schedule a refresh\r\n const timeout = setTimeout(() => void refreshToken(), expiry - Date.now());\r\n //Unref the timeout so the process can exit\r\n timeout.unref();\r\n }\r\n } catch {\r\n //If token is invalid, refresh the token\r\n await refreshToken();\r\n }\r\n } else {\r\n //If token is not set, refresh the token\r\n await refreshToken();\r\n }\r\n\r\n return client;\r\n};\r\n"]}
1
+ {"version":3,"sources":["../src/client/client.ts","../src/client/create-client.ts"],"names":[],"mappings":";;;;;;AA4BO,IAAM,oBAAN,MAAsD;AAAA,EACpD,QAAA,CAAA;AAAA,EAQC,KAAA,GAAQ,MAAM,MAAO,EAAA,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAetB,cAAA,GAAiB,OAAO,OAA+E,KAAA;AAC5G,IAAM,MAAA,EAAE,GAAK,EAAA,MAAA,EAAQ,MAAO,EAAA,GAAI,EAAE,GAAG,IAAA,CAAK,QAAU,EAAA,GAAG,OAAQ,EAAA,CAAA;AAC/D,IAAA,OAAO,KAAK,KAAM,CAAA,IAAA,CAAkB,CAAW,QAAA,EAAA,MAAM,2BAA2B,KAAW,CAAA,EAAA;AAAA,MACzF,IAAM,EAAA;AAAA,QACJ,QAAU,EAAA,MA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OAAA,GAAU,WAAW,MAAM,KAAK,cAAgB,EAAA,MAAA,GAAS,IAAK,CAAA,GAAA,EAAK,CAAA,CAAA;AAEzE,QAAA,OAAA,CAAQ,KAAM,EAAA,CAAA;AAAA,OAChB;AAAA,KACM,CAAA,MAAA;AAEN,MAAA,MAAM,YAAa,EAAA,CAAA;AAAA,KACrB;AAAA,GACK,MAAA;AAEL,IAAA,MAAM,YAAa,EAAA,CAAA;AAAA,GACrB;AAEA,EAAO,OAAA,MAAA,CAAA;AACT","file":"index.js","sourcesContent":["import { stringify } from 'node:querystring';\r\nimport { getBlizzardApi } from '@blizzard-api/core';\r\nimport type { Locales, Origins, Resource, ResourceResponse } from '@blizzard-api/core';\r\nimport type { AxiosResponse } from 'axios';\r\nimport axios, { AxiosError, isAxiosError } from 'axios';\r\nimport type {\r\n AccessToken,\r\n AccessTokenRequestArguments,\r\n ClientOptions,\r\n IBlizzardApiClient,\r\n ValidateAccessTokenArguments,\r\n ValidateAccessTokenResponse,\r\n} from './types';\r\n\r\n/**\r\n * A Blizzard API client.\r\n * @implements IBlizzardApiClient\r\n * @class\r\n * @classdesc A client to interact with the Blizzard API.\r\n * @example\r\n * const client = new BlizzardApiClient({\r\n * key: 'client',\r\n * secret: 'secret',\r\n * origin: 'eu',\r\n * locale: 'en_GB',\r\n * token: 'access'\r\n * });\r\n */\r\nexport class BlizzardApiClient implements IBlizzardApiClient {\r\n public defaults: {\r\n key: string;\r\n locale: Locales;\r\n origin: Origins;\r\n secret: string;\r\n token?: string;\r\n };\r\n\r\n private axios = axios.create();\r\n\r\n /**\r\n * Get an access token.\r\n * @param options The access token request arguments. See {@link AccessTokenRequestArguments}.\r\n * @returns The access token. See {@link AccessToken}.\r\n * @example\r\n * const response = await client.getAccessToken();\r\n * const { access_token, token_type, expires_in, sub } = response.data;\r\n * console.log(access_token, token_type, expires_in, sub);\r\n * // => 'access'\r\n * // => 'bearer'\r\n * // => 86399\r\n * // => 'client-id'\r\n */\r\n public getAccessToken = async (options?: AccessTokenRequestArguments): Promise<AxiosResponse<AccessToken>> => {\r\n const { key, origin, secret } = { ...this.defaults, ...options };\r\n return this.axios.post<AccessToken>(`https://${origin}.battle.net/oauth/token`, undefined, {\r\n auth: {\r\n password: secret,\r\n username: key,\r\n },\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n },\r\n params: {\r\n grant_type: 'client_credentials',\r\n },\r\n });\r\n };\r\n\r\n /**\r\n * Refresh the access token.\r\n * @param options The access token request arguments. See {@link AccessTokenRequestArguments}.\r\n * @returns The access token. See {@link AccessToken}.\r\n * @example\r\n * const response = await client.refreshAccessToken();\r\n * const { access_token, token_type, expires_in, sub } = response.data;\r\n * console.log(access_token, token_type, expires_in, sub);\r\n * // => 'access'\r\n * // => 'bearer'\r\n * // => 86399\r\n * // => 'client-id'\r\n */\r\n public refreshAccessToken = async (options?: AccessTokenRequestArguments): Promise<AxiosResponse<AccessToken>> => {\r\n const response = await this.getAccessToken(options);\r\n this.setAccessToken(response.data.access_token);\r\n return response;\r\n };\r\n\r\n /**\r\n * Set the access token.\r\n * @param token The access token.\r\n */\r\n public setAccessToken = (token: string): void => {\r\n this.defaults.token = token;\r\n };\r\n\r\n /**\r\n * Validate an access token.\r\n * @param options The validate access token arguments. See {@link ValidateAccessTokenArguments}.\r\n * @returns The response from the Blizzard API. See {@link ValidateAccessTokenResponse}.\r\n * @example\r\n * const response = await client.validateAccessToken({ token: 'access' });\r\n * console.log(response.data.client_id);\r\n * // => 'client-id'\r\n */\r\n constructor(options: ClientOptions) {\r\n const { locale, origin } = getBlizzardApi(options.origin, options.locale);\r\n this.defaults = {\r\n key: options.key,\r\n locale: locale,\r\n origin: origin,\r\n secret: options.secret,\r\n token: options.token,\r\n };\r\n }\r\n\r\n public validateAccessToken = async (\r\n options?: ValidateAccessTokenArguments,\r\n ): Promise<AxiosResponse<ValidateAccessTokenResponse>> => {\r\n const { origin, token } = { ...this.defaults, ...options };\r\n\r\n if (!token) {\r\n throw new Error('No token has been set previously or been passed to the validateAccessToken method.');\r\n }\r\n\r\n return await this.axios.post<ValidateAccessTokenResponse>(\r\n `https://${origin}.battle.net/oauth/check_token`,\r\n stringify({ token }),\r\n {\r\n headers: {\r\n 'Content-Type': 'application/x-www-form-urlencoded',\r\n },\r\n },\r\n );\r\n };\r\n\r\n /**\r\n * Get the request configuration.\r\n * @param resource The resource to fetch. See {@link Resource}.\r\n * @param options Client options. See {@link ClientOptions}.\r\n * @param headers Additional headers to include in the request.\r\n * @returns The request configuration.\r\n */\r\n public getRequestConfig<T, Protected extends boolean = false>(\r\n resource: Resource<T, object, Protected>,\r\n options?: Partial<ClientOptions>,\r\n headers?: Record<string, string>,\r\n ) {\r\n const config = { ...this.defaults, ...options };\r\n const endpoint = getBlizzardApi(config.origin, config.locale);\r\n\r\n const namespace = resource.namespace\r\n ? { 'Battlenet-Namespace': `${resource.namespace}-${endpoint.origin}` }\r\n : undefined;\r\n\r\n return {\r\n headers: {\r\n ...headers,\r\n ...namespace,\r\n Authorization: `Bearer ${config.token}`,\r\n 'Content-Type': 'application/json',\r\n },\r\n params: {\r\n locale: endpoint.locale,\r\n ...resource.parameters,\r\n },\r\n };\r\n }\r\n\r\n /**\r\n * Get the request URL.\r\n * @param resource The resource to fetch. See {@link Resource}.\r\n * @param options Client options. See {@link ClientOptions}.\r\n * @returns The request URL.\r\n */\r\n public getRequestUrl<T, Protected extends boolean = false>(\r\n resource: Resource<T, object, Protected>,\r\n options?: Partial<ClientOptions>,\r\n ) {\r\n const config = { ...this.defaults, ...options };\r\n const endpoint = getBlizzardApi(config.origin, config.locale);\r\n\r\n const backslashSeparator = resource.path.startsWith('/') ? '' : '/';\r\n\r\n return `${endpoint.hostname}${backslashSeparator}${resource.path}`;\r\n }\r\n\r\n /**\r\n * Send a request to the Blizzard API.\r\n * @param resource The resource to fetch. See {@link Resource}.\r\n * @param options Client options. See {@link ClientOptions}.\r\n * @param headers Additional headers to include in the request.\r\n * @returns The response from the Blizzard API. See {@link ResourceResponse}.\r\n */\r\n public async sendRequest<T, Protected extends boolean = false>(\r\n resource: Resource<T, object, Protected>,\r\n options?: Partial<ClientOptions>,\r\n headers?: Record<string, string>,\r\n ): ResourceResponse<AxiosResponse<T>> {\r\n const url = this.getRequestUrl(resource, options);\r\n const config = this.getRequestConfig(resource, options, headers);\r\n\r\n try {\r\n return await this.axios.get<T>(url, config);\r\n } catch (error) {\r\n if (isAxiosError(error)) {\r\n throw new AxiosError(error.message, error.code);\r\n }\r\n throw error;\r\n }\r\n }\r\n}\r\n","//We have to explicitly import setTimeout because of https://github.com/electron/electron/issues/21162#issuecomment-554792447\r\nimport { setTimeout } from 'node:timers';\r\nimport { BlizzardApiClient } from './client';\r\nimport type { AccessToken, ClientOptions } from './types';\r\n\r\nconst getTokenExpiration = (expiresIn: number) => expiresIn * 1000 - 60_000;\r\n\r\n/**\r\n * Create a new Blizzard API client.\r\n * @param options Client options, see {@link ClientOptions} & https://develop.battle.net/documentation/guides/getting-started\r\n * @param onTokenRefresh Callback function to handle token refresh. If set to `true`, the client will automatically refresh the token. If set to `false`, the client will not refresh the token. If set to a function, the function will be called with the new token.\r\n * @returns A new Blizzard API client.\r\n */\r\nexport const createBlizzardApiClient = async (\r\n options: ClientOptions,\r\n onTokenRefresh: ((token: AccessToken) => void) | boolean = true,\r\n): Promise<BlizzardApiClient> => {\r\n const { key, secret, token } = options;\r\n if (!key) {\r\n throw new Error(`Client missing 'key' parameter`);\r\n }\r\n if (!secret) {\r\n throw new Error(`Client missing 'secret' parameter`);\r\n }\r\n\r\n const client = new BlizzardApiClient(options);\r\n\r\n const refreshToken = async () => {\r\n const response = await client.getAccessToken();\r\n\r\n client.setAccessToken(response.data.access_token);\r\n\r\n if (typeof onTokenRefresh === 'function') {\r\n onTokenRefresh?.(response.data);\r\n }\r\n\r\n //Schedule a refresh of the token\r\n const timeout = setTimeout(() => void refreshToken(), getTokenExpiration(response.data.expires_in));\r\n timeout.unref();\r\n };\r\n\r\n //If tokenRefresh is false, return the client without refreshing the token\r\n if (!onTokenRefresh) {\r\n return client;\r\n }\r\n\r\n if (token) {\r\n try {\r\n //If token is set, validate the token\r\n const validatedToken = await client.validateAccessToken({ token });\r\n const expiry = getTokenExpiration(validatedToken.data.exp);\r\n //If token is expiring in less than 60 seconds, refresh the token\r\n if (expiry - Date.now() < 60_000) {\r\n await refreshToken();\r\n } else {\r\n //If token is not expiring, schedule a refresh\r\n const timeout = setTimeout(() => void refreshToken(), expiry - Date.now());\r\n //Unref the timeout so the process can exit\r\n timeout.unref();\r\n }\r\n } catch {\r\n //If token is invalid, refresh the token\r\n await refreshToken();\r\n }\r\n } else {\r\n //If token is not set, refresh the token\r\n await refreshToken();\r\n }\r\n\r\n return client;\r\n};\r\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blizzard-api/client",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "license": "MIT",
5
5
  "author": "Putro",
6
6
  "description": "A node.js axios client to integrate with the blizzard battle.net api.",
@@ -40,18 +40,18 @@
40
40
  "hearthstone"
41
41
  ],
42
42
  "dependencies": {
43
- "axios": "1.7.7"
43
+ "axios": "1.7.9"
44
44
  },
45
45
  "peerDependencies": {
46
- "@blizzard-api/core": "2.0.0"
46
+ "@blizzard-api/core": "2.0.1"
47
47
  },
48
48
  "devDependencies": {
49
- "@blizzard-api/classic-wow": "2.0.0",
50
- "@blizzard-api/core": "2.0.0",
51
- "@blizzard-api/d3": "1.0.0",
52
- "@blizzard-api/hs": "1.0.0",
53
- "@blizzard-api/sc2": "1.0.0",
54
- "@blizzard-api/wow": "2.0.0"
49
+ "@blizzard-api/classic-wow": "2.0.1",
50
+ "@blizzard-api/core": "2.0.1",
51
+ "@blizzard-api/hs": "1.0.1",
52
+ "@blizzard-api/d3": "1.0.1",
53
+ "@blizzard-api/sc2": "1.0.1",
54
+ "@blizzard-api/wow": "2.0.1"
55
55
  },
56
56
  "scripts": {
57
57
  "build": "tsup",