Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
194 changes: 32 additions & 162 deletions src/api/offstylesApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ export interface ModerationLogResponse {
actions: ModerationLogAction[];
}

async function throwApiError(response: Response): Promise<never> {
const errorText = await response.text();
let parsed: JsonError | null = null;
try {
parsed = JSON.parse(errorText);
} catch {
// response body was not JSON — fall through
}
if (parsed && typeof parsed.code === 'number' && typeof parsed.reason === 'string') {
throw new Error(`${parsed.code}: ${parsed.reason}`);
}
throw new Error(`${response.status}: ${response.statusText || errorText.slice(0, 200)}`);
}

class OffstylesApi extends Api {
static offstylesApiUrl = "/api";

Expand All @@ -101,15 +115,7 @@ class OffstylesApi extends Api {
params.append("limit", filter.limit.toString());

const response = await fetch(`${this.offstylesApiUrl}/times?${params.toString()}`);
if (!response.ok) {
const errorText = await response.text();
try {
const error: JsonError = JSON.parse(errorText);
throw new Error(`${error.code}: ${error.reason}`);
} catch {
throw new Error(`${response.status}: ${response.statusText}`);
}
}
if (!response.ok) await throwApiError(response);
return await response.json();
}

Expand Down Expand Up @@ -165,15 +171,7 @@ class OffstylesApi extends Api {
credentials: "include", // Include cookies for session authentication
});

if (!response.ok) {
const errorText = await response.text();
try {
const error: JsonError = JSON.parse(errorText);
throw new Error(`${error.code}: ${error.reason}`);
} catch {
throw new Error(`${response.status}: ${response.statusText}`);
}
}
if (!response.ok) await throwApiError(response);

return response;
}
Expand All @@ -198,15 +196,7 @@ class OffstylesApi extends Api {
credentials: "include", // Include cookies for session authentication
});

if (!response.ok) {
const errorText = await response.text();
try {
const error: JsonError = JSON.parse(errorText);
throw new Error(`${error.code}: ${error.reason}`);
} catch {
throw new Error(`${response.status}: ${response.statusText}`);
}
}
if (!response.ok) await throwApiError(response);
}

static async moderateRecord(
Expand Down Expand Up @@ -237,15 +227,7 @@ class OffstylesApi extends Api {
credentials: "include", // Include cookies for session authentication
});

if (!response.ok) {
const errorText = await response.text();
try {
const error: JsonError = JSON.parse(errorText);
throw new Error(`${error.code}: ${error.reason}`);
} catch {
throw new Error(`${response.status}: ${response.statusText}`);
}
}
if (!response.ok) await throwApiError(response);
}

// Get user profile data
Expand All @@ -256,15 +238,7 @@ class OffstylesApi extends Api {

const response = await fetch(`${this.offstylesApiUrl}/profile?${params.toString()}`);

if (!response.ok) {
const errorText = await response.text();
try {
const error: JsonError = JSON.parse(errorText);
throw new Error(`${error.code}: ${error.reason}`);
} catch {
throw new Error(`${response.status}: ${response.statusText}`);
}
}
if (!response.ok) await throwApiError(response);

return await response.json();
}
Expand Down Expand Up @@ -305,15 +279,7 @@ class OffstylesApi extends Api {
credentials: "include", // Include cookies for session authentication
});

if (!response.ok) {
const errorText = await response.text();
try {
const error: JsonError = JSON.parse(errorText);
throw new Error(`${error.code}: ${error.reason}`);
} catch {
throw new Error(`${response.status}: ${response.statusText}`);
}
}
if (!response.ok) await throwApiError(response);

return await response.json();
}
Expand Down Expand Up @@ -353,15 +319,7 @@ class OffstylesApi extends Api {
credentials: "include",
});

if (!response.ok) {
const errorText = await response.text();
try {
const error: JsonError = JSON.parse(errorText);
throw new Error(`${error.code}: ${error.reason}`);
} catch {
throw new Error(`${response.status}: ${response.statusText}`);
}
}
if (!response.ok) await throwApiError(response);

// The API returns the created key info as KeyReturnJson
return await response.json();
Expand All @@ -382,15 +340,7 @@ class OffstylesApi extends Api {
credentials: "include", // Include cookies for session authentication
});

if (!response.ok) {
const errorText = await response.text();
try {
const error: JsonError = JSON.parse(errorText);
throw new Error(`${error.code}: ${error.reason}`);
} catch {
throw new Error(`${response.status}: ${response.statusText}`);
}
}
if (!response.ok) await throwApiError(response);

return await response.json();
}
Expand All @@ -406,15 +356,7 @@ class OffstylesApi extends Api {
credentials: "include", // Include cookies for session authentication
});

if (!response.ok) {
const errorText = await response.text();
try {
const error: JsonError = JSON.parse(errorText);
throw new Error(`${error.code}: ${error.reason}`);
} catch {
throw new Error(`${response.status}: ${response.statusText}`);
}
}
if (!response.ok) await throwApiError(response);

return await response.json();
}
Expand All @@ -428,15 +370,7 @@ class OffstylesApi extends Api {
credentials: "include", // Include cookies for session authentication
});

if (!response.ok) {
const errorText = await response.text();
try {
const error: JsonError = JSON.parse(errorText);
throw new Error(`${error.code}: ${error.reason}`);
} catch {
throw new Error(`${response.status}: ${response.statusText}`);
}
}
if (!response.ok) await throwApiError(response);

return await response.json();
}
Expand All @@ -451,15 +385,7 @@ class OffstylesApi extends Api {
credentials: "include", // Include cookies for session authentication
});

if (!response.ok) {
const errorText = await response.text();
try {
const error: JsonError = JSON.parse(errorText);
throw new Error(`${error.code}: ${error.reason}`);
} catch {
throw new Error(`${response.status}: ${response.statusText}`);
}
}
if (!response.ok) await throwApiError(response);

// Handle plain text response "true"
const responseText = await response.text();
Expand All @@ -479,15 +405,7 @@ class OffstylesApi extends Api {
credentials: "include", // Include cookies for session authentication
});

if (!response.ok) {
const errorText = await response.text();
try {
const error: JsonError = JSON.parse(errorText);
throw new Error(`${error.code}: ${error.reason}`);
} catch {
throw new Error(`${response.status}: ${response.statusText}`);
}
}
if (!response.ok) await throwApiError(response);

return await response.json();
}
Expand All @@ -513,15 +431,7 @@ class OffstylesApi extends Api {
credentials: "include", // Include cookies for session authentication
});

if (!response.ok) {
const errorText = await response.text();
try {
const error: JsonError = JSON.parse(errorText);
throw new Error(`${error.code}: ${error.reason}`);
} catch {
throw new Error(`${response.status}: ${response.statusText}`);
}
}
if (!response.ok) await throwApiError(response);

return await response.text();
}
Expand All @@ -544,15 +454,7 @@ class OffstylesApi extends Api {
},
);

if (!response.ok) {
const errorText = await response.text();
try {
const error: JsonError = JSON.parse(errorText);
throw new Error(`${error.code}: ${error.reason}`);
} catch {
throw new Error(`${response.status}: ${response.statusText}`);
}
}
if (!response.ok) await throwApiError(response);

return await response.json();
}
Expand All @@ -577,15 +479,7 @@ class OffstylesApi extends Api {
},
);

if (!response.ok) {
const errorText = await response.text();
try {
const error: JsonError = JSON.parse(errorText);
throw new Error(`${error.code}: ${error.reason}`);
} catch {
throw new Error(`${response.status}: ${response.statusText}`);
}
}
if (!response.ok) await throwApiError(response);

return await response.json();
}
Expand All @@ -610,15 +504,7 @@ class OffstylesApi extends Api {
},
);

if (!response.ok) {
const errorText = await response.text();
try {
const error: JsonError = JSON.parse(errorText);
throw new Error(`${error.code}: ${error.reason}`);
} catch {
throw new Error(`${response.status}: ${response.statusText}`);
}
}
if (!response.ok) await throwApiError(response);

return await response.json();
}
Expand All @@ -634,15 +520,7 @@ class OffstylesApi extends Api {
credentials: "include",
});

if (!response.ok) {
const errorText = await response.text();
try {
const error: JsonError = JSON.parse(errorText);
throw new Error(`${error.code}: ${error.reason}`);
} catch {
throw new Error(`${response.status}: ${response.statusText}`);
}
}
if (!response.ok) await throwApiError(response);

return await response.json();
}
Expand All @@ -661,15 +539,7 @@ class OffstylesApi extends Api {
credentials: "include",
});

if (!response.ok) {
const errorText = await response.text();
try {
const error: JsonError = JSON.parse(errorText);
throw new Error(`${error.code}: ${error.reason}`);
} catch {
throw new Error(`${response.status}: ${response.statusText}`);
}
}
if (!response.ok) await throwApiError(response);

return await response.json();
}
Expand Down