@blizzard-api/client
Advanced tools
Comparing version 0.0.2 to 0.0.3
import * as axios from 'axios'; | ||
import { AxiosResponse } from 'axios'; | ||
import { Origins, Locales } from '@blizzard-api/core'; | ||
import { Origins, Locales, ClientOptions, Resource, ResourceResponse } from '@blizzard-api/core'; | ||
interface ClientOptions { | ||
key: string; | ||
secret: string; | ||
origin: Origins; | ||
locale?: Locales; | ||
token?: string; | ||
} | ||
interface AccessToken { | ||
@@ -41,2 +34,3 @@ access_token: string; | ||
} | ||
declare class BlizzardApiClient implements IBlizzardApiClient { | ||
@@ -52,2 +46,14 @@ defaults: { | ||
axios: axios.AxiosInstance; | ||
getRequestUrl<T = unknown>(resource: Resource<T>, options?: Partial<ClientOptions>): string; | ||
getRequestConfig<T = unknown>(resource: Resource<T>, 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" | "multi"; | ||
}; | ||
}; | ||
sendRequest<T = unknown>(resource: Resource<T>, options?: Partial<ClientOptions> & T, headers?: Record<string, string>): ResourceResponse<AxiosResponse<T>>; | ||
getAccessToken: (options?: AccessTokenRequestArguments) => Promise<AxiosResponse<AccessToken>>; | ||
@@ -59,2 +65,2 @@ setAccessToken: (token: string) => void; | ||
export { BlizzardApiClient, type ClientOptions }; | ||
export { BlizzardApiClient }; |
@@ -1,2 +0,2 @@ | ||
// src/client.ts | ||
// src/client/index.ts | ||
import { stringify } from "querystring"; | ||
@@ -18,2 +18,30 @@ import { getEndpoint } from "@blizzard-api/core"; | ||
axios = axios.create(); | ||
getRequestUrl(resource, options) { | ||
const config = { ...this.defaults, ...options }; | ||
const endpoint = getEndpoint(config.origin, config.locale); | ||
const backslashSeparator = resource.path.startsWith("/") ? "" : "/"; | ||
return `${endpoint.hostname}${backslashSeparator}${resource.path}`; | ||
} | ||
getRequestConfig(resource, options, headers) { | ||
const config = { ...this.defaults, ...options }; | ||
const endpoint = getEndpoint(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: { | ||
...resource.params, | ||
locale: endpoint.locale | ||
} | ||
}; | ||
} | ||
sendRequest(resource, options, headers) { | ||
const url = this.getRequestUrl(resource, options); | ||
const config = this.getRequestConfig(resource, options, headers); | ||
return this.axios.get(url, config); | ||
} | ||
getAccessToken = async (options) => { | ||
@@ -20,0 +48,0 @@ const { key, secret, origin } = { ...this.defaults, ...options }; |
{ | ||
"name": "@blizzard-api/client", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"license": "MIT", | ||
@@ -50,6 +50,7 @@ "author": "Putro", | ||
"peerDependencies": { | ||
"@blizzard-api/core": "0.0.2" | ||
"@blizzard-api/core": "0.0.3" | ||
}, | ||
"devDependencies": { | ||
"@blizzard-api/core": "0.0.2" | ||
"@blizzard-api/core": "0.0.3", | ||
"@blizzard-api/wow": "0.0.1" | ||
}, | ||
@@ -56,0 +57,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
25975
269
2
24
3