Skip to content

Commit 97864a9

Browse files
committed
plugin: version 4.3.0
1 parent 8788b1c commit 97864a9

20 files changed

Lines changed: 266 additions & 117 deletions

plugin/chrome_manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifest_version": 3,
33
"name": "Offi",
44
"description": "tf2 competitive QoL extension",
5-
"version": "4.2.0",
5+
"version": "4.3.0",
66
"content_scripts": [
77
{
88
"run_at": "document_end",

plugin/firefox_manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifest_version": 2,
33
"name": "Offi",
44
"description": "tf2 competitive QoL extension",
5-
"version": "4.2.0",
5+
"version": "4.3.0",
66
"content_scripts": [
77
{
88
"run_at": "document_end",

plugin/package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "offi",
3-
"version": "4.2.0",
3+
"version": "4.3.0",
44
"scripts": {
5-
"build_ff": "cp firefox_manifest.json manifest.json && parcel build manifest.json --dist-dir build/firefox --no-optimize && rm manifest.json",
6-
"build_c": "cp chrome_manifest.json manifest.json && parcel build manifest.json --dist-dir build/chrome --no-optimize && rm manifest.json",
7-
"lint": "eslint src/*.ts"
5+
"build_ff": "cp firefox_manifest.json manifest.json && parcel build manifest.json --dist-dir build/firefox --no-optimize --no-content-hash && rm manifest.json",
6+
"build_c": "cp chrome_manifest.json manifest.json && parcel build manifest.json --dist-dir build/chrome --no-optimize --no-scope-hoist --no-content-hash && rm manifest.json",
7+
"lint": "eslint src/*.ts src/api/*.ts"
88
},
99
"dependencies": {
1010
"@parcel/config-webextension": "2.13.2",
@@ -15,6 +15,8 @@
1515
},
1616
"devDependencies": {
1717
"@eslint/js": "^9.15.0",
18+
"@parcel/optimizer-data-url": "2.13.2",
19+
"@parcel/transformer-inline-string": "2.13.2",
1820
"@parcel/transformer-typescript-tsc": "2.13.2",
1921
"@types/node": "^22.9.3",
2022
"@types/webextension-polyfill": "^0.12.1",

plugin/src/api/api.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const VERSION =
2+
(process.env.OFFI_PLATFORM || "unknown") + "/" + (process.env.OFFI_VERSION || "dev");
3+
4+
export const requestHeaders = {
5+
"X-Offi-Version": VERSION,
6+
"Content-Type": "application/json",
7+
}
8+

plugin/src/api/error.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export class APIError extends Error {
2+
public status: number
3+
public traceparent: string | null
4+
5+
constructor(message: string, status: number, traceparent: string | null) {
6+
super(message);
7+
8+
this.status = status
9+
this.traceparent = traceparent
10+
}
11+
12+
toString(): string {
13+
return `[${this.status}] API error: ${this.message}\nTrace: ${this.traceparent})`
14+
}
15+
16+
static async fromResponse(r: Response): Promise<APIError> {
17+
return new APIError(await r.text(), r.status, r.headers.get("traceparent"));
18+
}
19+
}
20+
21+
export const NoRecruitmentInfo = new Error("this team doesn't have recruitment post");
22+
export const MatchNotFound = new Error("match not found or was not played yet");
23+
export const NoLogsError = new Error("api didn't found logs for this match");

plugin/src/api/get_logs.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
import { Log, LogResponse } from "./types";
2-
3-
export const NoLogsError = new Error("api didnt found logs for this match");
2+
import {APIError, NoLogsError} from "./error";
3+
import {requestHeaders} from "./api";
44

55
export async function getLogs(apiBaseUrl: string, matchId: number): Promise<Log[]> {
66
const getMatchURL = new URL(apiBaseUrl + `/match/${matchId}`);
77

8-
const res = await fetch(getMatchURL);
9-
if (!res.ok) {
10-
throw new Error("offi api returned error: " + res.statusText);
11-
}
12-
13-
const { logs } = (await res.json()) as LogResponse;
14-
if (logs === null) {
8+
const res = await fetch(getMatchURL, {
9+
headers: requestHeaders,
10+
});
11+
if (res.status === 404 || res.status === 425) {
1512
throw NoLogsError;
13+
} else if (res.status !== 200) {
14+
throw await APIError.fromResponse(res);
1615
}
1716

18-
const parsedLogs: Log[] = [];
19-
20-
for (const rawLog of logs) {
21-
parsedLogs.push(new Log(rawLog));
22-
}
17+
const { logs } = (await res.json()) as LogResponse;
2318

24-
return parsedLogs;
19+
return logs.map((log => new Log(log)));
2520
}

plugin/src/api/get_match.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
import {Match, MatchResponse} from "./types";
1+
import { MatchResponse } from "./types";
2+
import { MatchNotFound, APIError } from "./error";
3+
import { requestHeaders } from "./api";
24

3-
export async function getMatch(apiBaseUrl: string, logId: number): Promise<Match> {
5+
export async function getMatch(apiBaseUrl: string, logId: number): Promise<MatchResponse> {
46
const logURL = new URL(apiBaseUrl + `/log/${logId}`);
57

6-
const res = await fetch(logURL);
7-
8-
if (!res.ok) {
9-
throw new Error("api returned error: " + res.statusText);
8+
const res = await fetch(logURL, {
9+
headers: requestHeaders,
10+
});
11+
if (res.status === 404) {
12+
throw MatchNotFound
13+
}
14+
if (res.status !== 200) {
15+
throw await APIError.fromResponse(res);
1016
}
1117

12-
const apiResponse = await res.json() as MatchResponse;
13-
14-
return apiResponse.match;
18+
return await res.json() as MatchResponse;
1519
}

plugin/src/api/get_players.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Player, PlayersResponse } from "./types";
2+
import { APIError } from "./error";
3+
import {requestHeaders} from "./api";
24

3-
export async function getPlayers(apiBaseUrl: string, ids: string[], withRecruitmentStatus: boolean): Promise<Player[]> {
5+
export async function getPlayers(apiBaseUrl: string, ids: string[], withRecruitmentStatus: boolean = false): Promise<Player[]> {
46
const playersURL = new URL(apiBaseUrl + "/players");
57

68
const idsString = ids.join(",");
@@ -9,9 +11,11 @@ export async function getPlayers(apiBaseUrl: string, ids: string[], withRecruitm
911

1012
if (withRecruitmentStatus) playersURL.searchParams.append("with_recruitment_status", "true");
1113

12-
const res = await fetch(playersURL.toString());
13-
if (!res.ok) {
14-
throw new Error("offi api returned error: " + res.statusText);
14+
const res = await fetch(playersURL.toString(), {
15+
headers: requestHeaders,
16+
});
17+
if (res.status !== 200) {
18+
throw await APIError.fromResponse(res);
1519
}
1620

1721
const response = await res.json() as PlayersResponse;

plugin/src/api/get_team.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
import {Team, TeamResponse} from "./types";
2-
3-
export const NoRecruitmentInfo = new Error("this team doesn't have recruitment post");
1+
import { Team, TeamResponse } from "./types";
2+
import { NoRecruitmentInfo, APIError } from "./error";
3+
import {requestHeaders} from "./api";
44

55
export async function getTeam(apiBaseUrl: string, teamId: string): Promise<Team> {
66
const getTeamURL = new URL(apiBaseUrl + `/team/${teamId}`);
77

8-
const res = await fetch(getTeamURL);
9-
if (!res.ok) {
10-
throw new Error("offi api returned error: " + res.statusText);
8+
const res = await fetch(getTeamURL, {
9+
headers: requestHeaders,
10+
});
11+
if (res.status === 404) {
12+
throw NoRecruitmentInfo;
13+
} else if (res.status !== 200) {
14+
throw await APIError.fromResponse(res);
1115
}
1216

1317
const response = (await res.json()) as TeamResponse;
14-
if (response.team === null || !response.team.recruitment) {
15-
throw NoRecruitmentInfo;
16-
}
1718

1819
return response.team;
1920
}

plugin/src/api/types.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,21 @@ export type Team = {
2626
recruitment: Recruitment;
2727
}
2828

29-
30-
3129
export class Log {
3230
id: number;
3331
title: string;
3432
map: string;
3533
played_at: Date;
3634
is_secondary: boolean;
35+
demo_id?: number;
3736

3837
constructor(data: Log) {
3938
this.id = data.id;
4039
this.title = data.title;
4140
this.map = data.map;
4241
this.played_at = new Date(data.played_at);
4342
this.is_secondary = data.is_secondary;
43+
this.demo_id = data.demo_id;
4444
}
4545
}
4646

@@ -51,8 +51,13 @@ export type Match = {
5151
tier: string;
5252
}
5353

54+
export type LogMeta = {
55+
demo_id?: number
56+
}
57+
5458
export type MatchResponse = {
5559
match: Match;
60+
log: LogMeta;
5661
}
5762

5863
export type LogResponse = {

0 commit comments

Comments
 (0)