Skip to content

Commit

Permalink
fix: biome linting
Browse files Browse the repository at this point in the history
  • Loading branch information
barbmarcio committed Jul 8, 2024
1 parent 44ffa5b commit 99ac130
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 75 deletions.
28 changes: 14 additions & 14 deletions src/constants/handlers.constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ import { apis, baseURL } from "./apis.constant";
const errorBaseURL = "https://error.api.topsort.com/";

export const handlers = {
events: http.post(`${baseURL}/${apis.events}`, () => {
return HttpResponse.json({}, { status: 200 });
}),
eventsError: http.post(`${errorBaseURL}/${apis.events}`, () => {
return HttpResponse.error();
}),
events: http.post(`${baseURL}/${apis.events}`, () => {
return HttpResponse.json({}, { status: 200 });
}),
eventsError: http.post(`${errorBaseURL}/${apis.events}`, () => {
return HttpResponse.error();
}),
};

export const returnStatus = (
status: number,
server: SetupServerApi,
url: string,
status: number,
server: SetupServerApi,
url: string,
) => {
return server.use(
http.post(url, () => {
return HttpResponse.json({}, { status: status });
}),
);
return server.use(
http.post(url, () => {
return HttpResponse.json({}, { status: status });
}),
);
};
4 changes: 2 additions & 2 deletions src/functions/report-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ export async function reportEvent(
config: Config,
): Promise<{ ok: boolean; retry: boolean }> {
try {
const url = `${config.url || baseURL}/${apis.events}`;
const url = `${config.host || baseURL}/${apis.events}`;
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
// Can't use User-Agent header because of
// https://bugs.chromium.org/p/chromium/issues/detail?id=571722
"X-UA": `ts.js/${version}`,
Authorization: "Bearer " + config.token,
Authorization: `Bearer ${config.apiKey}`,
},
body: JSON.stringify(event),
// This parameter ensures in most browsers that the request is performed even in case the browser navigates to another page.
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/events.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ export interface TopsortEvent {
}

export interface Config {
token: string;
url?: string;
apiKey: string;
host?: string;
}
87 changes: 45 additions & 42 deletions src/lib/api-client.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,51 @@
import AppError from "./app-error";

class ApiClient {
private baseUrl: string;

constructor(baseUrl: string) {
this.baseUrl = baseUrl;
}

private async handleResponse(response: Response): Promise<any> {
const contentType = response.headers.get("Content-Type") || "";
let data;
if (contentType.includes("application/json")) {
data = await response.json();
} else {
data = await response.text();
}

if (!response.ok) {
throw new AppError(response.status, response.statusText, data);
}

return data;
}

private async request(endpoint: string, options: RequestInit): Promise<any> {
const response = await fetch(`${this.baseUrl}${endpoint}`, options);
return await this.handleResponse(response);
}

public async get(endpoint: string): Promise<any> {
return await this.request(endpoint, {
method: "GET",
});
}

public async post(endpoint: string, body: any): Promise<any> {
return await this.request(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
}
private baseUrl: string;

constructor(baseUrl: string) {
this.baseUrl = baseUrl;
}

private async handleResponse(response: Response): Promise<unknown> {
const contentType = response.headers.get("Content-Type") || "";
let data: unknown;
if (contentType.includes("application/json")) {
data = await response.json();
} else {
data = await response.text();
}

if (!response.ok) {
throw new AppError(response.status, response.statusText, data);
}

return data;
}

private async request(
endpoint: string,
options: RequestInit,
): Promise<unknown> {
const response = await fetch(`${this.baseUrl}${endpoint}`, options);
return await this.handleResponse(response);
}

public async get(endpoint: string): Promise<unknown> {
return await this.request(endpoint, {
method: "GET",
});
}

public async post(endpoint: string, body: unknown): Promise<unknown> {
return await this.request(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
}
}

export { ApiClient };
4 changes: 2 additions & 2 deletions src/lib/app-error.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class AppError {
public readonly status: number; //change later to http status code
public readonly statusText: string;
public readonly body: any;
public readonly body: unknown;

constructor(status: number, statusText: string, body: any) {
constructor(status: number, statusText: string, body: unknown) {
this.status = status;
this.statusText = statusText;
this.body = body;
Expand Down
5 changes: 3 additions & 2 deletions src/lib/extract-comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ export async function extractJSDocComments(
const content = await Bun.file(filePath).text();
const comments = [];
const regex = /\/\*\*([\s\S]*?)\*\//g;
let match;
while ((match = regex.exec(content)) !== null) {
let match = regex.exec(content);
while (match !== null) {
comments.push(match[1].trim());
match = regex.exec(content);
}
return comments;
}
6 changes: 3 additions & 3 deletions src/lib/generate-test-cases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { extractJSDocComments } from "./extract-comments";

interface TestCase {
code: string;
expected: any;
expected: unknown;
}

export async function generateTestCases(filePath: string): Promise<TestCase[]> {
const comments = await extractJSDocComments(filePath);
const testCases: TestCase[] = [];

comments.forEach((comment) => {
for (const comment of comments) {
const exampleMatch = comment.match(
/@example\s+\*?\s*```js([\s\S]*?)\s*\*?\s*```/,
);
Expand All @@ -34,7 +34,7 @@ export async function generateTestCases(filePath: string): Promise<TestCase[]> {
}
}
}
});
}

return testCases;
}
16 changes: 8 additions & 8 deletions src/tests/report-event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ describe("reportEvent", () => {
it("should handle network error and retry", async () => {
await expect(
reportEvent({} as TopsortEvent, {
token: "token",
url: "https://error.api.topsort.com",
apiKey: "token",
host: "https://error.api.topsort.com",
}),
).rejects.toEqual({
status: 500,
Expand All @@ -28,7 +28,7 @@ describe("reportEvent", () => {
it("should handle permanent error", async () => {
returnStatus(400, server, `${baseURL}/${apis.events}`);
await expect(
reportEvent({} as TopsortEvent, { token: "token" }),
reportEvent({} as TopsortEvent, { apiKey: "apiKey" }),
).resolves.toEqual({
ok: false,
retry: false,
Expand All @@ -38,7 +38,7 @@ describe("reportEvent", () => {
it("should handle authentication error", async () => {
returnStatus(401, server, `${baseURL}/${apis.events}`);
await expect(
reportEvent({} as TopsortEvent, { token: "token" }),
reportEvent({} as TopsortEvent, { apiKey: "apiKey" }),
).resolves.toEqual({
ok: false,
retry: false,
Expand All @@ -48,7 +48,7 @@ describe("reportEvent", () => {
it("should handle retryable error", async () => {
returnStatus(429, server, `${baseURL}/${apis.events}`);
await expect(
reportEvent({} as TopsortEvent, { token: "token" }),
reportEvent({} as TopsortEvent, { apiKey: "apiKey" }),
).resolves.toEqual({
ok: false,
retry: true,
Expand All @@ -58,7 +58,7 @@ describe("reportEvent", () => {
it("should handle server error", async () => {
returnStatus(500, server, `${baseURL}/${apis.events}`);
await expect(
reportEvent({} as TopsortEvent, { token: "token" }),
reportEvent({} as TopsortEvent, { apiKey: "apiKey" }),
).resolves.toEqual({
ok: false,
retry: true,
Expand All @@ -69,8 +69,8 @@ describe("reportEvent", () => {
returnStatus(200, server, `https://demo.api.topsort.com/${apis.events}`);
await expect(
reportEvent({} as TopsortEvent, {
token: "token",
url: "https://demo.api.topsort.com",
apiKey: "apiKey",
host: "https://demo.api.topsort.com",
}),
).resolves.toEqual({ ok: true, retry: false });
});
Expand Down

0 comments on commit 99ac130

Please sign in to comment.