Skip to content

Commit

Permalink
updates to shared mods
Browse files Browse the repository at this point in the history
  • Loading branch information
Pinta365 committed Sep 12, 2023
1 parent 4688069 commit d9a8ca2
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 18 deletions.
22 changes: 10 additions & 12 deletions src/shared/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getSetup } from "./config.ts";
import { getSetup, tokenUrl } from "./config.ts";
import { AuthenticationError, MissingClientIdError, MissingClientSecretError } from "./errors.ts";

interface AuthConfig {
accessToken: string;
Expand All @@ -15,8 +16,12 @@ export function getauthConfig(): AuthConfig {
}

export async function authenticate(force = false) {
if (!getSetup().clientId || !getSetup().ClientSecret) {
throw new Error("clientId and ClientSecret must be set using setup() before calling authenticate.");
if (!getSetup().clientId) {
throw new MissingClientIdError();
}

if (!getSetup().ClientSecret) {
throw new MissingClientSecretError();
}

if (
Expand All @@ -26,11 +31,7 @@ export async function authenticate(force = false) {
return authConfig.accessToken;
}

const url = getSetup().region === "cn"
? "https://www.battlenet.com.cn/oauth/token"
: `https://${getSetup().region}.battle.net/oauth/token`;

const response = await fetch(url, {
const response = await fetch(tokenUrl(getSetup().region), {
method: "POST",
headers: {
"Authorization": "Basic " + btoa(`${getSetup().clientId}:${getSetup().ClientSecret}`),
Expand All @@ -41,13 +42,10 @@ export async function authenticate(force = false) {

if (response.ok) {
const data = await response.json();

console.log("Auth successful");
authConfig.accessToken = data.access_token;
authConfig.tokenExpiration = Date.now() + data.expires_in * 1000;
return true;
} else {
console.error("Auth failed:", response.status, response.statusText);
return false;
throw new AuthenticationError("Problem with Authentication", response.status, response.statusText);
}
}
8 changes: 8 additions & 0 deletions src/shared/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ let config: Config = {
ClientSecret: "",
};

export function tokenUrl(region: Regions) {
return region === "cn" ? "https://www.battlenet.com.cn/oauth/token" : `https://${region}.battle.net/oauth/token`;
}

export function apiBaseUrl(region: Regions) {
return region === "cn" ? "https://gateway.battlenet.com.cn" : `https://${region}.api.blizzard.com`;
}

export function setup(userConfig: Partial<Config>) {
config = { ...config, ...userConfig };
}
Expand Down
57 changes: 57 additions & 0 deletions src/shared/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Custom error class representing an API error.
* @class
* @extends {Error}
*/
export class APIError extends Error {
statusCode: number;
responseBody: string;

constructor(message: string, statusCode: number, responseBody: string) {
super(message);
this.name = "APIError";
this.statusCode = statusCode;
this.responseBody = responseBody;
}
}

/**
* Custom error class representing an Authentication error.
* @class
* @extends {Error}
*/
export class AuthenticationError extends Error {
statusCode: number;
responseBody: string;

constructor(message: string, statusCode: number, responseBody: string) {
super(message);
this.name = "AuthenticationError";
this.statusCode = statusCode;
this.responseBody = responseBody;
}
}

/**
* Custom error class representing a missing client id error.
* @class
* @extends {Error}
*/
export class MissingClientIdError extends Error {
constructor() {
super("Client Id must be setup before calling authenticate. Check documentation");
this.name = "MissingClientIdError";
}
}

/**
* Custom error class representing a missing client secret error.
* @class
* @extends {Error}
*/
export class MissingClientSecretError extends Error {
constructor() {
super("Client Secret must be setup before calling authenticate. Check documentation");
this.name = "MissingClientSecretError";
}
}
30 changes: 24 additions & 6 deletions src/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ export interface requestOptions {
qs?: Record<string, string>;
}

export interface LinkSelfHref {
self: {
href: string;
};
}

export type Regions = "us" | "eu" | "kr" | "tw" | "cn";

export type Locales =
Expand All @@ -30,5 +24,29 @@ export type Locales =

export type Namespaces = "static" | "dynamic" | "profile";

/** Self-referential link */
export interface LinkSelfHref {
self: {
href: string;
};
}

/** Represents a single string if localization was supplied in request or an object of localized key-value strings if not */
export interface LocalizedString {
name: string | Record<Locales, string>;
}

export interface KeyNameId {
key: {
href: string;
};
name: LocalizedString;
id: number;
}

export interface KeyId {
key: {
href: string;
};
id: number;
}

0 comments on commit d9a8ca2

Please sign in to comment.