Skip to content

Commit a839519

Browse files
committed
feat: target field
1 parent 36da82c commit a839519

File tree

3 files changed

+91
-11
lines changed

3 files changed

+91
-11
lines changed

src/createClient.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ export const createClient = ({
222222

223223
const getAllContentIds = async ({
224224
endpoint,
225+
target,
225226
draftKey,
226227
filters,
227228
orders,
@@ -233,7 +234,7 @@ export const createClient = ({
233234
filters,
234235
orders,
235236
limit,
236-
fields: 'id',
237+
fields: target ?? 'id',
237238
};
238239

239240
const { totalCount } = await makeRequest({
@@ -247,15 +248,24 @@ export const createClient = ({
247248

248249
const sleep = (ms: number) =>
249250
new Promise((resolve) => setTimeout(resolve, ms));
251+
const isStringArray = (arr: unknown[]): arr is string[] =>
252+
arr.every((item) => typeof item === 'string');
250253

251254
while (contentIds.length < totalCount) {
252255
const { contents } = (await makeRequest({
253256
endpoint,
254257
queries: { ...defaultQueries, offset },
255258
requestInit: customRequestInit,
256-
})) as MicroCMSListResponse<Record<string, never>>;
259+
})) as MicroCMSListResponse<Record<string, unknown>>;
260+
261+
const ids = contents.map((content) => content[target ?? 'id']);
262+
263+
if (!isStringArray(ids)) {
264+
throw new Error(
265+
'The value of the field specified by `target` is not a string.',
266+
);
267+
}
257268

258-
const ids = contents.map((content) => content.id);
259269
contentIds = [...contentIds, ...ids];
260270

261271
offset += limit;

src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ export interface GetObjectRequest {
117117

118118
export interface GetAllContentIdsRequest {
119119
endpoint: string;
120+
/**
121+
* @type {string} target
122+
* @example 'url'
123+
* If you are using a URL other than the content ID, for example, you can specify that value in the `target` field.
124+
*/
125+
target?: string;
120126
draftKey?: string;
121127
filters?: string;
122128
orders?: string;

tests/getAllContentIds.test.ts

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('getAllContentIds', () => {
2020
ctx.status(200),
2121
ctx.json({
2222
totalCount: 100,
23-
})
23+
}),
2424
);
2525
}),
2626
rest.get(`${testBaseUrl}/getAllContentIds-list-type`, (_, res, ctx) => {
@@ -30,9 +30,9 @@ describe('getAllContentIds', () => {
3030
contents: Array(100)
3131
.fill(null)
3232
.map((_, index) => ({ id: `id${index}` })),
33-
})
33+
}),
3434
);
35-
})
35+
}),
3636
);
3737

3838
const result = await client.getAllContentIds({
@@ -51,7 +51,7 @@ describe('getAllContentIds', () => {
5151
ctx.status(200),
5252
ctx.json({
5353
totalCount: 250,
54-
})
54+
}),
5555
);
5656
}),
5757
rest.get(`${testBaseUrl}/getAllContentIds-list-type`, (_, res, ctx) => {
@@ -61,7 +61,7 @@ describe('getAllContentIds', () => {
6161
contents: Array(100)
6262
.fill(null)
6363
.map((_, index) => ({ id: `id${index}` })),
64-
})
64+
}),
6565
);
6666
}),
6767
rest.get(`${testBaseUrl}/getAllContentIds-list-type`, (_, res, ctx) => {
@@ -71,7 +71,7 @@ describe('getAllContentIds', () => {
7171
contents: Array(100)
7272
.fill(null)
7373
.map((_, index) => ({ id: `id${index + 100}` })),
74-
})
74+
}),
7575
);
7676
}),
7777
rest.get(`${testBaseUrl}/getAllContentIds-list-type`, (_, res, ctx) => {
@@ -81,9 +81,9 @@ describe('getAllContentIds', () => {
8181
contents: Array(50)
8282
.fill(null)
8383
.map((_, index) => ({ id: `id${index + 200}` })),
84-
})
84+
}),
8585
);
86-
})
86+
}),
8787
);
8888

8989
const result = await client.getAllContentIds({
@@ -94,4 +94,68 @@ describe('getAllContentIds', () => {
9494
expect(result).toContain('id0');
9595
expect(result).toContain('id249');
9696
});
97+
98+
test('should fetch all content ids with target field', async () => {
99+
server.use(
100+
rest.get(`${testBaseUrl}/getAllContentIds-list-type`, (_, res, ctx) => {
101+
return res.once(
102+
ctx.status(200),
103+
ctx.json({
104+
totalCount: 100,
105+
}),
106+
);
107+
}),
108+
rest.get(`${testBaseUrl}/getAllContentIds-list-type`, (_, res, ctx) => {
109+
return res.once(
110+
ctx.status(200),
111+
ctx.json({
112+
contents: Array(100)
113+
.fill(null)
114+
.map((_, index) => ({ url: `id${index}` })),
115+
}),
116+
);
117+
}),
118+
);
119+
120+
const result = await client.getAllContentIds({
121+
endpoint: 'getAllContentIds-list-type',
122+
target: 'url',
123+
});
124+
125+
expect(result).toHaveLength(100);
126+
expect(result).toContain('id0');
127+
expect(result).toContain('id99');
128+
});
129+
130+
test('should throw error when target field is not string', async () => {
131+
server.use(
132+
rest.get(`${testBaseUrl}/getAllContentIds-list-type`, (_, res, ctx) => {
133+
return res.once(
134+
ctx.status(200),
135+
ctx.json({
136+
totalCount: 100,
137+
}),
138+
);
139+
}),
140+
rest.get(`${testBaseUrl}/getAllContentIds-list-type`, (_, res, ctx) => {
141+
return res.once(
142+
ctx.status(200),
143+
ctx.json({
144+
contents: Array(100)
145+
.fill(null)
146+
.map(() => ({ image: { url: 'url', width: 100, height: 100 } })),
147+
}),
148+
);
149+
}),
150+
);
151+
152+
expect(() =>
153+
client.getAllContentIds({
154+
endpoint: 'getAllContentIds-list-type',
155+
target: 'image',
156+
}),
157+
).toThrowError(
158+
'The value of the field specified by `target` is not a string.',
159+
);
160+
});
97161
});

0 commit comments

Comments
 (0)