forked from mbsantiago/whombat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.ts
38 lines (29 loc) · 1021 Bytes
/
common.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Purpose: Common types and constants for the API.
import axios from "axios";
import { z } from "zod";
import { GetManySchema } from "@/lib/schemas";
export const HOST = `${process.env.NEXT_PUBLIC_BACKEND_HOST}`;
export const BASE_ROUTE = `/api/v1`;
export const instance = axios.create({
withCredentials: true,
baseURL: HOST,
});
export const Page = <T extends z.ZodTypeAny>(schema: T) =>
z.object({
items: z.array(schema),
total: z.number().int(),
limit: z.number().int(),
offset: z.number().int(),
});
export const GetMany = <T extends z.ZodTypeAny>(schema: T) =>
z.intersection(GetManySchema, schema);
export function downloadContent(data: any, filename: string, filetype: string) {
const href = URL.createObjectURL(new Blob([data], { type: filetype }));
const link = document.createElement("a");
link.href = href;
link.setAttribute("download", filename);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(href);
}