mirror of
https://github.com/StepanovPlaton/torrent_frontend.git
synced 2026-04-04 12:50:48 +04:00
Add game cards and dev reverse proxy
This commit is contained in:
34
src/shared/http/httpService.ts
Normal file
34
src/shared/http/httpService.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export abstract class HTTPService {
|
||||
public static async get<Z>(
|
||||
url: string,
|
||||
schema: z.ZodTypeAny
|
||||
): Promise<Z | null> {
|
||||
return await fetch(process.env.NEXT_PUBLIC_BASE_URL + url, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
accept: "application/json",
|
||||
},
|
||||
cache: "no-cache",
|
||||
})
|
||||
.then((r) => {
|
||||
if (r && r.ok) return r;
|
||||
else throw Error("Response ok = false");
|
||||
})
|
||||
.then((r) => r.json())
|
||||
.then((d) => {
|
||||
const parseResult = schema.safeParse(d);
|
||||
if (parseResult.success) {
|
||||
return parseResult.data as Z;
|
||||
} else {
|
||||
console.error(parseResult.error);
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.catch((e) => {
|
||||
console.error(e);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user