Skip to content

Commit

Permalink
[SC2] Initial release of @blizzard-api/sc2
Browse files Browse the repository at this point in the history
  • Loading branch information
Pewtro committed Sep 27, 2024
1 parent 7932e99 commit b04aa16
Show file tree
Hide file tree
Showing 22 changed files with 1,015 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-nails-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@blizzard-api/sc2': minor
---

Initial release of @blizzard-api/sc2
1 change: 1 addition & 0 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@blizzard-api/classic-wow": "workspace:*",
"@blizzard-api/core": "workspace:*",
"@blizzard-api/d3": "workspace:*",
"@blizzard-api/sc2": "workspace:*",
"@blizzard-api/wow": "workspace:*"
},
"scripts": {
Expand Down
72 changes: 72 additions & 0 deletions packages/client/src/tests/sc2.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { sc2 } from '@blizzard-api/sc2';
import { describe, expect, it } from 'vitest';
import { environment } from '../../../../environment';
import { createBlizzardApiClient } from '../client/create-client';

describe.concurrent('smoketest some sc2 api responses', async () => {
const client = await createBlizzardApiClient({
key: environment.blizzardClientId,
origin: 'eu',
secret: environment.blizzardClientSecret,
});
it('should get player data', async () => {
const result = await client.sendRequest(sc2.player('235782'));
expect(result.data).toBeDefined();
});
it('should get grandmaster leaderboard', async () => {
const result = await client.sendRequest(sc2.grandmasterLeaderboard('eu'));
expect(result.data).toBeDefined();
});
it('should get season data', async () => {
const result = await client.sendRequest(sc2.season('eu'));
expect(result.data).toBeDefined();
});
it('should get league data', async () => {
const result = await client.sendRequest(sc2.getLeagueData('42', 'lotv-1v1', 'arranged', 'grandmaster'));
expect(result.data).toBeDefined();
});
it('should get legacy achievements', async () => {
const result = await client.sendRequest(sc2.legacyAchievements('eu'));
expect(result.data).toBeDefined();
});
it('should get legacy ladder', async () => {
const result = await client.sendRequest(sc2.legacyLadder('eu', 235_782));
expect(result.data).toBeDefined();
});
it('should get legacy ladders', async () => {
const result = await client.sendRequest(sc2.legacyLadders('eu', 1, 235_782));
expect(result.data).toBeDefined();
});
it('should get legacy match history', async () => {
const result = await client.sendRequest(sc2.legacyMatchHistory('eu', 1, 235_782));
expect(result.data).toBeDefined();
});
it('should get legacy profile', async () => {
const result = await client.sendRequest(sc2.legacyProfile('eu', 1, 235_782));
expect(result.data).toBeDefined();
});
it('should get legacy rewards', async () => {
const result = await client.sendRequest(sc2.legacyRewards('eu'));
expect(result.data).toBeDefined();
});
it('should get ladder data', async () => {
const result = await client.sendRequest(sc2.ladder('eu', 1, 235_782, 131_418_961));
expect(result.data).toBeDefined();
});
it('should get ladder summary', async () => {
const result = await client.sendRequest(sc2.ladderSummary('eu', 1, 235_782));
expect(result.data).toBeDefined();
});
it('should get metadata', async () => {
const result = await client.sendRequest(sc2.metadata('eu', 1, 235_782));
expect(result.data).toBeDefined();
});
it('should get profile', async () => {
const result = await client.sendRequest(sc2.profile('eu', 1, 235_782));
expect(result.data).toBeDefined();
});
it('should get static profile', async () => {
const result = await client.sendRequest(sc2.staticProfile('eu'));
expect(result.data).toBeDefined();
});
});
1 change: 1 addition & 0 deletions packages/sc2/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @blizzard-api/sc2
51 changes: 51 additions & 0 deletions packages/sc2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "@blizzard-api/sc2",
"version": "0.0.0",
"license": "MIT",
"author": "Putro",
"description": "A series of helpers to interact with the Starcraft II Blizzard API",
"repository": "https://github.com/Pewtro/blizzard-api/tree/main/packages/sc2",
"type": "module",
"engines": {
"node": "^18.18 || ^20.9 || ^21.1 || ^22"
},
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
}
},
"files": [
"dist"
],
"keywords": [
"blizzard",
"battlenet",
"battle.net",
"bnet",
"api",
"starcraft",
"sc2"
],
"dependencies": {},
"peerDependencies": {
"@blizzard-api/core": "1.2.0"
},
"devDependencies": {
"@blizzard-api/core": "workspace:*"
},
"scripts": {
"build": "tsup",
"dev": "tsup --watch",
"test": "vitest run",
"test:coverage": "pnpm test -- --coverage",
"test:watch": "vitest watch"
}
}
12 changes: 12 additions & 0 deletions packages/sc2/src/account/account.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { describe, expect, it } from 'vitest';
import { player } from './account';

describe('account', () => {
it('should return the correct resource path for a given accountId', () => {
const accountId = '12345';
const result = player(accountId);
expect(result).toEqual({
path: `/sc2/player/12345`,
});
});
});
7 changes: 7 additions & 0 deletions packages/sc2/src/account/account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Resource } from '@blizzard-api/core';

export function player(accountId: string): Resource<Array<unknown>> {
return {
path: `/sc2/player/${accountId}`,
};
}
21 changes: 21 additions & 0 deletions packages/sc2/src/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Base interface for Blizzard API responses.
*/
export interface ResponseBase {
_links: {
self: {
href: string;
};
};
}

//The region (1=US, 2=EU, 3=KO and TW, 5=CN).
export type StarcraftRegion = 'cn' | 'eu' | 'kr' | 'tw' | 'us';

export const starcraftRegion: Record<StarcraftRegion, number> = {
cn: 5,
eu: 2,
kr: 3,
tw: 3,
us: 1,
};
45 changes: 45 additions & 0 deletions packages/sc2/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { player } from './account/account';
import { grandmasterLeaderboard, season } from './ladder/ladder';
import { getLeagueData } from './league/league';
import {
legacyAchievements,
legacyLadder,
legacyLadders,
legacyMatchHistory,
legacyProfile,
legacyRewards,
} from './legacy/legacy';
import { ladder, ladderSummary, metadata, profile, staticProfile } from './profile/profile';

export const sc2 = {
//Account
player,
//Ladder
grandmasterLeaderboard,
season,
//League
getLeagueData,
//Legacy
legacyAchievements,
legacyLadder,
legacyLadders,
legacyMatchHistory,
legacyProfile,
legacyRewards,
//Profile
ladder,
ladderSummary,
metadata,
profile,
staticProfile,
};

//Account
//Ladder
export type * from './ladder/types';
//League
export type * from './league/types';
//Legacy
export type * from './legacy/types';
//Profile
export type * from './profile/types';
29 changes: 29 additions & 0 deletions packages/sc2/src/ladder/ladder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { Resource } from '@blizzard-api/core';
import { describe, expect, it } from 'vitest';
import type { StarcraftRegion } from '../base';
import { grandmasterLeaderboard, season } from './ladder';
import type { GrandmasterLeaderboardResponse, SeasonResponse } from './types';

describe('ladder', () => {
describe('grandmasterLeaderboard', () => {
it('should return the correct path for a given region', () => {
const regionId: StarcraftRegion = 'us';
const expected: Resource<GrandmasterLeaderboardResponse> = {
path: `/sc2/ladder/grandmaster/1`,
};
const result = grandmasterLeaderboard(regionId);
expect(result).toEqual(expected);
});
});

describe('season', () => {
it('should return the correct path for a given region', () => {
const regionId: StarcraftRegion = 'eu';
const expected: Resource<SeasonResponse> = {
path: `/sc2/ladder/season/2`,
};
const result = season(regionId);
expect(result).toEqual(expected);
});
});
});
15 changes: 15 additions & 0 deletions packages/sc2/src/ladder/ladder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Resource } from '@blizzard-api/core';
import { starcraftRegion, type StarcraftRegion } from '../base';
import type { GrandmasterLeaderboardResponse, SeasonResponse } from './types';

export function grandmasterLeaderboard(regionId: StarcraftRegion): Resource<GrandmasterLeaderboardResponse> {
return {
path: `/sc2/ladder/grandmaster/${starcraftRegion[regionId]}`,
};
}

export function season(regionId: StarcraftRegion): Resource<SeasonResponse> {
return {
path: `/sc2/ladder/season/${starcraftRegion[regionId]}`,
};
}
30 changes: 30 additions & 0 deletions packages/sc2/src/ladder/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export interface GrandmasterLeaderboardResponse {
ladderTeams: Array<LadderTeam>;
}

interface LadderTeam {
joinTimestamp: number;
losses: number;
mmr: number;
points: number;
previousRank: number;
teamMembers: Array<TeamMember>;
wins: number;
}

interface TeamMember {
clanTag?: string;
displayName: string;
favoriteRace: 'protoss' | 'random' | 'terran' | 'zerg';
id: string;
realm: number;
region: number;
}

export interface SeasonResponse {
endDate: string;
number: number;
seasonId: number;
startDate: string;
year: number;
}
Loading

0 comments on commit b04aa16

Please sign in to comment.