-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicrocms.ts
68 lines (63 loc) · 1.8 KB
/
microcms.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { createClient, MicroCMSQueries } from "microcms-js-sdk";
const client = createClient({
serviceDomain: import.meta.env.MICROCMS_SERVICE_DOMAIN,
apiKey: import.meta.env.MICROCMS_API_KEY,
// serviceDomain: "import.meta.env.MICROCMS_SERVICE_DOMAIN",
// apiKey: "import.meta.env.MICROCMS_API_KEY",
// serviceDomain: process.env.MICROCMS_SERVICE_DOMAIN,
// apiKey: process.env.MICROCMS_API_KEY,
// serviceDomain: "process.env.MICROCMS_SERVICE_DOMAIN",
// apiKey: "process.env.MICROCMS_API_KEY",
});
export const clientFactoryFunction = () => {
return {
serviceDomain: import.meta.env.MICROCMS_SERVICE_DOMAIN,
apiKey: import.meta.env.MICROCMS_API_KEY,
}
}
export type Blog = {
id: string;
createdAt: string;
updatedAt: string;
publishedAt: string;
revisedAt: string;
title: string;
content: string;
eyecatch: {
url: string;
height: number;
width: number;
};
};
export type BlogResponse = {
totalCount: number;
offset: number;
limit: number;
contents: Blog[];
};
export const getBlogs = async (queries?: MicroCMSQueries) => {
const client = createClient(clientFactoryFunction());
const data = await client.get<BlogResponse>({ endpoint: "blogs", queries });
if (data.offset + data.limit < data.totalCount) {
queries ? (queries.offset = data.offset + data.limit) : "";
const result: BlogResponse = await getBlogs(queries);
return {
offset: result.offset,
limit: result.limit,
contents: [...data.contents, ...result.contents],
totalCount: result.totalCount,
};
}
return data;
};
export const getBlogDetail = async (
contentId: string,
queries?: MicroCMSQueries
) => {
const client = createClient(clientFactoryFunction());
return await client.getListDetail<Blog>({
endpoint: "blogs",
contentId,
queries,
});
};