@blizzard-api/client
Advanced tools
Comparing version 0.0.9 to 0.1.0
@@ -1,4 +0,3 @@ | ||
import * as axios from 'axios'; | ||
import { Origins, Locales, Resource, ResourceResponse } from '@blizzard-api/core'; | ||
import { AxiosResponse } from 'axios'; | ||
import { Origins, Locales, Resource, ResourceResponse } from '@blizzard-api/core'; | ||
@@ -51,3 +50,3 @@ interface AccessToken { | ||
constructor(options: ClientOptions); | ||
axios: axios.AxiosInstance; | ||
private axios; | ||
getRequestUrl<T, Protected extends boolean = false>(resource: Resource<T, Protected>, options?: Partial<ClientOptions>): string; | ||
@@ -71,2 +70,4 @@ getRequestConfig<T, Protected extends boolean = false>(resource: Resource<T, Protected>, options?: Partial<ClientOptions>, headers?: Record<string, string>): { | ||
export { type AccessToken, type AccessTokenRequestArguments, BlizzardApiClient, type ClientOptions, type IBlizzardApiClient, type ValidateAccessTokenArguments, type ValidateAccessTokenResponse }; | ||
declare const createBlizzardApiClient: (options: ClientOptions, onTokenRefresh?: boolean | ((token: AccessToken) => void)) => Promise<BlizzardApiClient>; | ||
export { type AccessToken, type AccessTokenRequestArguments, type ClientOptions, type IBlizzardApiClient, type ValidateAccessTokenArguments, type ValidateAccessTokenResponse, createBlizzardApiClient }; |
@@ -1,2 +0,2 @@ | ||
// src/client/index.ts | ||
// src/client/client.ts | ||
import { stringify } from "querystring"; | ||
@@ -85,5 +85,47 @@ import { getBlizzardApi } from "@blizzard-api/core"; | ||
}; | ||
// src/client/create-client.ts | ||
var getTokenExpiration = (expiresIn) => expiresIn * 1e3 - 6e4; | ||
var createBlizzardApiClient = async (options, onTokenRefresh = true) => { | ||
const { key, secret, token } = options; | ||
if (!key) { | ||
throw new Error(`Client missing 'key' parameter`); | ||
} | ||
if (!secret) { | ||
throw new Error(`Client missing 'secret' parameter`); | ||
} | ||
const client = new BlizzardApiClient(options); | ||
const refreshToken = async () => { | ||
const response = await client.getAccessToken(); | ||
client.setAccessToken(response.data.access_token); | ||
if (typeof onTokenRefresh === "function") { | ||
onTokenRefresh?.(response.data); | ||
} | ||
const timeout = setTimeout(() => void refreshToken(), getTokenExpiration(response.data.expires_in)); | ||
timeout.unref(); | ||
}; | ||
if (!onTokenRefresh) { | ||
return client; | ||
} | ||
if (token) { | ||
try { | ||
const validatedToken = await client.validateAccessToken({ token }); | ||
const expiry = getTokenExpiration(validatedToken.data.exp); | ||
if (expiry - Date.now() < 6e4) { | ||
await refreshToken(); | ||
} else { | ||
const timeout = setTimeout(() => void refreshToken(), expiry - Date.now()); | ||
timeout.unref(); | ||
} | ||
} catch { | ||
await refreshToken(); | ||
} | ||
} else { | ||
await refreshToken(); | ||
} | ||
return client; | ||
}; | ||
export { | ||
BlizzardApiClient | ||
createBlizzardApiClient | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@blizzard-api/client", | ||
"version": "0.0.9", | ||
"version": "0.1.0", | ||
"license": "MIT", | ||
@@ -54,3 +54,3 @@ "author": "Putro", | ||
"@blizzard-api/core": "0.0.9", | ||
"@blizzard-api/wow": "0.0.7" | ||
"@blizzard-api/wow": "0.1.1" | ||
}, | ||
@@ -57,0 +57,0 @@ "scripts": { |
@@ -26,6 +26,6 @@ # @blizzard-api/client | ||
```ts | ||
import { BlizzardApiClient } from '@blizzard-api/client'; | ||
import { createBlizzardApiClient } from '@blizzard-api/client'; | ||
import { wow } from '@blizzard-api/wow'; | ||
const client = new BlizzardApiClient({ | ||
const client = await createBlizzardApiClient({ | ||
key: 'environment.blizzardClientId', | ||
@@ -36,6 +36,2 @@ secret: 'environment.blizzardClientSecret', | ||
//For now, you will need to authenticate and set the token like so, but this will be changed in a future update to be handled implicitly in the client | ||
const access = await client.getAccessToken(); | ||
client.setAccessToken(access.data.access_token); | ||
//Response will automatically be typed with the appropriate values | ||
@@ -42,0 +38,0 @@ const response = await client.sendRequest(wow.commodities()); |
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
38136
358
45