Skip to content

Commit ac5908f

Browse files
authored
Merge pull request #65 from microcmsio/fix/get-all-content-ids
getAllContentIdsについてフィールド名とdepthの扱いを修正
2 parents 24a0e65 + fc1dfc0 commit ac5908f

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ client
110110

111111
This function can be used to retrieve all content IDs only.
112112
Since `filters` and `draftKey` can also be specified, it is possible to retrieve only the content IDs for a specific category, or to include content from a specific draft. \
113-
The `target` property can also be used to address cases where the value of a field other than content ID is used in a URL, etc.
113+
The `alternateField` property can also be used to address cases where the value of a field other than content ID is used in a URL, etc.
114114

115115
```javascript
116116
client
@@ -138,11 +138,11 @@ client
138138
.then((res) => console.log(res))
139139
.catch((err) => console.error(err));
140140

141-
// Get all content ids with target
141+
// Get all content ids with alternateField
142142
client
143143
.getAllContentIds({
144144
endpoint: 'endpoint',
145-
target: 'url',
145+
alternateField: 'url',
146146
})
147147
.then((res) => console.log(res))
148148
.catch((err) => console.error(err));

src/createClient.ts

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

223223
const getAllContentIds = async ({
224224
endpoint,
225-
target,
225+
alternateField,
226226
draftKey,
227227
filters,
228228
orders,
@@ -234,7 +234,8 @@ export const createClient = ({
234234
filters,
235235
orders,
236236
limit,
237-
fields: target ?? 'id',
237+
fields: alternateField ?? 'id',
238+
depth: 0,
238239
};
239240

240241
const { totalCount } = await makeRequest({
@@ -258,11 +259,11 @@ export const createClient = ({
258259
requestInit: customRequestInit,
259260
})) as MicroCMSListResponse<Record<string, unknown>>;
260261

261-
const ids = contents.map((content) => content[target ?? 'id']);
262+
const ids = contents.map((content) => content[alternateField ?? 'id']);
262263

263264
if (!isStringArray(ids)) {
264265
throw new Error(
265-
'The value of the field specified by `target` is not a string.',
266+
'The value of the field specified by `alternateField` is not a string.',
266267
);
267268
}
268269

src/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface MicroCMSClient {
1010
retry?: boolean;
1111
}
1212

13-
type depthNumber = 1 | 2 | 3;
13+
type depthNumber = 0 | 1 | 2 | 3;
1414

1515
/**
1616
* microCMS queries
@@ -118,11 +118,11 @@ export interface GetObjectRequest {
118118
export interface GetAllContentIdsRequest {
119119
endpoint: string;
120120
/**
121-
* @type {string} target
121+
* @type {string} alternateField
122122
* @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.
123+
* If you are using a URL other than the content ID, for example, you can specify that value in the `alternateField` field.
124124
*/
125-
target?: string;
125+
alternateField?: string;
126126
draftKey?: string;
127127
filters?: string;
128128
orders?: string;

tests/getAllContentIds.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ describe('getAllContentIds', () => {
9595
expect(result).toContain('id249');
9696
});
9797

98-
test('should fetch all content ids with target field', async () => {
98+
test('should fetch all content ids with alternateField field', async () => {
9999
server.use(
100100
rest.get(`${testBaseUrl}/getAllContentIds-list-type`, (_, res, ctx) => {
101101
return res.once(
@@ -119,15 +119,15 @@ describe('getAllContentIds', () => {
119119

120120
const result = await client.getAllContentIds({
121121
endpoint: 'getAllContentIds-list-type',
122-
target: 'url',
122+
alternateField: 'url',
123123
});
124124

125125
expect(result).toHaveLength(100);
126126
expect(result).toContain('id0');
127127
expect(result).toContain('id99');
128128
});
129129

130-
test('should throw error when target field is not string', async () => {
130+
test('should throw error when alternateField field is not string', async () => {
131131
server.use(
132132
rest.get(`${testBaseUrl}/getAllContentIds-list-type`, (_, res, ctx) => {
133133
return res.once(
@@ -152,10 +152,10 @@ describe('getAllContentIds', () => {
152152
await expect(
153153
client.getAllContentIds({
154154
endpoint: 'getAllContentIds-list-type',
155-
target: 'image',
155+
alternateField: 'image',
156156
}),
157157
).rejects.toThrowError(
158-
'The value of the field specified by `target` is not a string.',
158+
'The value of the field specified by `alternateField` is not a string.',
159159
);
160160
});
161161
});

0 commit comments

Comments
 (0)