@blizzard-api/client
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -23,5 +23,5 @@ import { Origins, Locales, Resource, ResourceResponse } from '@blizzard-api/core'; | ||
access_token: string; | ||
token_type: 'bearer'; | ||
expires_in: number; | ||
sub?: string; | ||
token_type: 'bearer'; | ||
} | ||
@@ -43,4 +43,4 @@ /** | ||
interface AccessTokenRequestArguments { | ||
key?: string; | ||
origin?: Origins; | ||
key?: string; | ||
secret?: string; | ||
@@ -85,8 +85,8 @@ } | ||
interface ValidateAccessTokenResponse { | ||
scope: Array<string>; | ||
account_authorities: Array<unknown>; | ||
exp: number; | ||
authorities: Array<string>; | ||
client_authorities: Array<unknown>; | ||
authorities: Array<string>; | ||
client_id: string; | ||
exp: number; | ||
scope: Array<string>; | ||
} | ||
@@ -112,5 +112,5 @@ /** | ||
key: string; | ||
locale?: Locales; | ||
origin: Origins; | ||
secret: string; | ||
origin: Origins; | ||
locale?: Locales; | ||
token?: string; | ||
@@ -128,4 +128,4 @@ } | ||
getAccessToken: (options: AccessTokenRequestArguments) => Promise<AxiosResponse<AccessToken>>; | ||
refreshAccessToken: (options: AccessTokenRequestArguments) => Promise<AxiosResponse<AccessToken>>; | ||
setAccessToken: (token: string) => void; | ||
refreshAccessToken: (options: AccessTokenRequestArguments) => Promise<AxiosResponse<AccessToken>>; | ||
validateAccessToken: (options: ValidateAccessTokenArguments) => Promise<AxiosResponse<ValidateAccessTokenResponse>>; | ||
@@ -149,44 +149,11 @@ } | ||
declare class BlizzardApiClient implements IBlizzardApiClient { | ||
private axios; | ||
defaults: { | ||
key: string; | ||
locale: Locales; | ||
origin: Origins; | ||
secret: string; | ||
origin: Origins; | ||
locale: Locales; | ||
token?: string; | ||
}; | ||
constructor(options: ClientOptions); | ||
private axios; | ||
/** | ||
* Get the request URL. | ||
* @param resource The resource to fetch. See {@link Resource}. | ||
* @param options Client options. See {@link ClientOptions}. | ||
* @returns The request URL. | ||
*/ | ||
getRequestUrl<T, Protected extends boolean = false>(resource: Resource<T, object, Protected>, options?: Partial<ClientOptions>): string; | ||
/** | ||
* Get the request configuration. | ||
* @param resource The resource to fetch. See {@link Resource}. | ||
* @param options Client options. See {@link ClientOptions}. | ||
* @param headers Additional headers to include in the request. | ||
* @returns The request configuration. | ||
*/ | ||
getRequestConfig<T, Protected extends boolean = false>(resource: Resource<T, object, Protected>, options?: Partial<ClientOptions>, headers?: Record<string, string>): { | ||
headers: { | ||
'Content-Type': string; | ||
Authorization: string; | ||
'Battlenet-Namespace'?: string | undefined; | ||
}; | ||
params: { | ||
locale: "en_US" | "es_MX" | "pt_BR" | "en_GB" | "es_ES" | "fr_FR" | "ru_RU" | "de_DE" | "pt_PT" | "it_IT" | "ko_KR" | "zh_TW" | "zh_CN" | "multi"; | ||
}; | ||
}; | ||
/** | ||
* Send a request to the Blizzard API. | ||
* @param resource The resource to fetch. See {@link Resource}. | ||
* @param options Client options. See {@link ClientOptions}. | ||
* @param headers Additional headers to include in the request. | ||
* @returns The response from the Blizzard API. See {@link ResourceResponse}. | ||
*/ | ||
sendRequest<T, Protected extends boolean = false>(resource: Resource<T, object, Protected>, options?: Partial<ClientOptions>, headers?: Record<string, string>): ResourceResponse<AxiosResponse<T>>; | ||
/** | ||
* Get an access token. | ||
@@ -206,7 +173,2 @@ * @param options The access token request arguments. See {@link AccessTokenRequestArguments}. | ||
/** | ||
* Set the access token. | ||
* @param token The access token. | ||
*/ | ||
setAccessToken: (token: string) => void; | ||
/** | ||
* Refresh the access token. | ||
@@ -226,2 +188,7 @@ * @param options The access token request arguments. See {@link AccessTokenRequestArguments}. | ||
/** | ||
* Set the access token. | ||
* @param token The access token. | ||
*/ | ||
setAccessToken: (token: string) => void; | ||
/** | ||
* Validate an access token. | ||
@@ -236,2 +203,35 @@ * @param options The validate access token arguments. See {@link ValidateAccessTokenArguments}. | ||
validateAccessToken: (options?: ValidateAccessTokenArguments) => Promise<AxiosResponse<ValidateAccessTokenResponse>>; | ||
constructor(options: ClientOptions); | ||
/** | ||
* Get the request configuration. | ||
* @param resource The resource to fetch. See {@link Resource}. | ||
* @param options Client options. See {@link ClientOptions}. | ||
* @param headers Additional headers to include in the request. | ||
* @returns The request configuration. | ||
*/ | ||
getRequestConfig<T, Protected extends boolean = false>(resource: Resource<T, object, Protected>, options?: Partial<ClientOptions>, headers?: Record<string, string>): { | ||
headers: { | ||
Authorization: string; | ||
'Content-Type': string; | ||
'Battlenet-Namespace'?: string | undefined; | ||
}; | ||
params: { | ||
locale: "de_DE" | "en_GB" | "en_US" | "es_ES" | "es_MX" | "fr_FR" | "it_IT" | "ko_KR" | "multi" | "pt_BR" | "pt_PT" | "ru_RU" | "zh_CN" | "zh_TW"; | ||
}; | ||
}; | ||
/** | ||
* Get the request URL. | ||
* @param resource The resource to fetch. See {@link Resource}. | ||
* @param options Client options. See {@link ClientOptions}. | ||
* @returns The request URL. | ||
*/ | ||
getRequestUrl<T, Protected extends boolean = false>(resource: Resource<T, object, Protected>, options?: Partial<ClientOptions>): string; | ||
/** | ||
* Send a request to the Blizzard API. | ||
* @param resource The resource to fetch. See {@link Resource}. | ||
* @param options Client options. See {@link ClientOptions}. | ||
* @param headers Additional headers to include in the request. | ||
* @returns The response from the Blizzard API. See {@link ResourceResponse}. | ||
*/ | ||
sendRequest<T, Protected extends boolean = false>(resource: Resource<T, object, Protected>, options?: Partial<ClientOptions>, headers?: Record<string, string>): ResourceResponse<AxiosResponse<T>>; | ||
} | ||
@@ -245,4 +245,4 @@ | ||
*/ | ||
declare const createBlizzardApiClient: (options: ClientOptions, onTokenRefresh?: boolean | ((token: AccessToken) => void)) => Promise<BlizzardApiClient>; | ||
declare const createBlizzardApiClient: (options: ClientOptions, onTokenRefresh?: ((token: AccessToken) => void) | boolean) => Promise<BlizzardApiClient>; | ||
export { type AccessToken, type AccessTokenRequestArguments, type ClientOptions, type IBlizzardApiClient, type ValidateAccessTokenArguments, type ValidateAccessTokenResponse, createBlizzardApiClient }; |
@@ -0,74 +1,10 @@ | ||
import { stringify } from 'querystring'; | ||
import { getBlizzardApi } from '@blizzard-api/core'; | ||
import axios, { isAxiosError, AxiosError } from 'axios'; | ||
// src/client/client.ts | ||
import { stringify } from "querystring"; | ||
import { getBlizzardApi } from "@blizzard-api/core"; | ||
import axios, { AxiosError, isAxiosError } from "axios"; | ||
var BlizzardApiClient = class { | ||
axios = axios.create(); | ||
defaults; | ||
constructor(options) { | ||
const { origin, locale } = getBlizzardApi(options.origin, options.locale); | ||
this.defaults = { | ||
key: options.key, | ||
secret: options.secret, | ||
token: options.token, | ||
origin, | ||
locale | ||
}; | ||
} | ||
axios = axios.create(); | ||
/** | ||
* Get the request URL. | ||
* @param resource The resource to fetch. See {@link Resource}. | ||
* @param options Client options. See {@link ClientOptions}. | ||
* @returns The request URL. | ||
*/ | ||
getRequestUrl(resource, options) { | ||
const config = { ...this.defaults, ...options }; | ||
const endpoint = getBlizzardApi(config.origin, config.locale); | ||
const backslashSeparator = resource.path.startsWith("/") ? "" : "/"; | ||
return `${endpoint.hostname}${backslashSeparator}${resource.path}`; | ||
} | ||
/** | ||
* Get the request configuration. | ||
* @param resource The resource to fetch. See {@link Resource}. | ||
* @param options Client options. See {@link ClientOptions}. | ||
* @param headers Additional headers to include in the request. | ||
* @returns The request configuration. | ||
*/ | ||
getRequestConfig(resource, options, headers) { | ||
const config = { ...this.defaults, ...options }; | ||
const endpoint = getBlizzardApi(config.origin, config.locale); | ||
const namespace = resource.namespace ? { "Battlenet-Namespace": `${resource.namespace}-${endpoint.origin}` } : void 0; | ||
return { | ||
headers: { | ||
...headers, | ||
...namespace, | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${config.token}` | ||
}, | ||
params: { | ||
locale: endpoint.locale, | ||
...resource.parameters | ||
} | ||
}; | ||
} | ||
/** | ||
* Send a request to the Blizzard API. | ||
* @param resource The resource to fetch. See {@link Resource}. | ||
* @param options Client options. See {@link ClientOptions}. | ||
* @param headers Additional headers to include in the request. | ||
* @returns The response from the Blizzard API. See {@link ResourceResponse}. | ||
*/ | ||
async sendRequest(resource, options, headers) { | ||
const url = this.getRequestUrl(resource, options); | ||
const config = this.getRequestConfig(resource, options, headers); | ||
try { | ||
return await this.axios.get(url, config); | ||
} catch (error) { | ||
if (isAxiosError(error)) { | ||
throw new AxiosError(error.message, error.code); | ||
} | ||
throw error; | ||
} | ||
} | ||
/** | ||
* Get an access token. | ||
@@ -87,13 +23,13 @@ * @param options The access token request arguments. See {@link AccessTokenRequestArguments}. | ||
getAccessToken = async (options) => { | ||
const { key, secret, origin } = { ...this.defaults, ...options }; | ||
const { key, origin, secret } = { ...this.defaults, ...options }; | ||
return this.axios.post(`https://${origin}.battle.net/oauth/token`, void 0, { | ||
params: { | ||
grant_type: "client_credentials" | ||
}, | ||
auth: { | ||
username: key, | ||
password: secret | ||
password: secret, | ||
username: key | ||
}, | ||
headers: { | ||
"Content-Type": "application/json" | ||
}, | ||
params: { | ||
grant_type: "client_credentials" | ||
} | ||
@@ -103,9 +39,2 @@ }); | ||
/** | ||
* Set the access token. | ||
* @param token The access token. | ||
*/ | ||
setAccessToken = (token) => { | ||
this.defaults.token = token; | ||
}; | ||
/** | ||
* Refresh the access token. | ||
@@ -129,2 +58,9 @@ * @param options The access token request arguments. See {@link AccessTokenRequestArguments}. | ||
/** | ||
* Set the access token. | ||
* @param token The access token. | ||
*/ | ||
setAccessToken = (token) => { | ||
this.defaults.token = token; | ||
}; | ||
/** | ||
* Validate an access token. | ||
@@ -153,2 +89,67 @@ * @param options The validate access token arguments. See {@link ValidateAccessTokenArguments}. | ||
}; | ||
constructor(options) { | ||
const { locale, origin } = getBlizzardApi(options.origin, options.locale); | ||
this.defaults = { | ||
key: options.key, | ||
locale, | ||
origin, | ||
secret: options.secret, | ||
token: options.token | ||
}; | ||
} | ||
/** | ||
* Get the request configuration. | ||
* @param resource The resource to fetch. See {@link Resource}. | ||
* @param options Client options. See {@link ClientOptions}. | ||
* @param headers Additional headers to include in the request. | ||
* @returns The request configuration. | ||
*/ | ||
getRequestConfig(resource, options, headers) { | ||
const config = { ...this.defaults, ...options }; | ||
const endpoint = getBlizzardApi(config.origin, config.locale); | ||
const namespace = resource.namespace ? { "Battlenet-Namespace": `${resource.namespace}-${endpoint.origin}` } : void 0; | ||
return { | ||
headers: { | ||
...headers, | ||
...namespace, | ||
Authorization: `Bearer ${config.token}`, | ||
"Content-Type": "application/json" | ||
}, | ||
params: { | ||
locale: endpoint.locale, | ||
...resource.parameters | ||
} | ||
}; | ||
} | ||
/** | ||
* Get the request URL. | ||
* @param resource The resource to fetch. See {@link Resource}. | ||
* @param options Client options. See {@link ClientOptions}. | ||
* @returns The request URL. | ||
*/ | ||
getRequestUrl(resource, options) { | ||
const config = { ...this.defaults, ...options }; | ||
const endpoint = getBlizzardApi(config.origin, config.locale); | ||
const backslashSeparator = resource.path.startsWith("/") ? "" : "/"; | ||
return `${endpoint.hostname}${backslashSeparator}${resource.path}`; | ||
} | ||
/** | ||
* Send a request to the Blizzard API. | ||
* @param resource The resource to fetch. See {@link Resource}. | ||
* @param options Client options. See {@link ClientOptions}. | ||
* @param headers Additional headers to include in the request. | ||
* @returns The response from the Blizzard API. See {@link ResourceResponse}. | ||
*/ | ||
async sendRequest(resource, options, headers) { | ||
const url = this.getRequestUrl(resource, options); | ||
const config = this.getRequestConfig(resource, options, headers); | ||
try { | ||
return await this.axios.get(url, config); | ||
} catch (error) { | ||
if (isAxiosError(error)) { | ||
throw new AxiosError(error.message, error.code); | ||
} | ||
throw error; | ||
} | ||
} | ||
}; | ||
@@ -197,5 +198,5 @@ | ||
}; | ||
export { | ||
createBlizzardApiClient | ||
}; | ||
export { createBlizzardApiClient }; | ||
//# sourceMappingURL=out.js.map | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@blizzard-api/client", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"license": "MIT", | ||
@@ -12,3 +12,5 @@ "author": "Putro", | ||
}, | ||
"main": "./dist/index.js", | ||
"main": "./dist/index.cjs", | ||
"module": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
@@ -49,9 +51,9 @@ "import": { | ||
"peerDependencies": { | ||
"@blizzard-api/core": "1.1.0" | ||
"@blizzard-api/core": "1.1.1" | ||
}, | ||
"devDependencies": { | ||
"@blizzard-api/classic-wow": "1.1.1", | ||
"@blizzard-api/core": "1.1.0", | ||
"@blizzard-api/d3": "0.1.0", | ||
"@blizzard-api/wow": "1.1.1" | ||
"@blizzard-api/classic-wow": "1.1.2", | ||
"@blizzard-api/core": "1.1.1", | ||
"@blizzard-api/d3": "0.1.1", | ||
"@blizzard-api/wow": "1.1.2" | ||
}, | ||
@@ -58,0 +60,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60426
623