Skip to content

Commit e8a5b2b

Browse files
authored
Merge pull request #97 from microcmsio/add-draft-to-patch
updateのリクエストにisDraft追加
2 parents 39ef400 + 7e952f7 commit e8a5b2b

File tree

7 files changed

+89
-27
lines changed

7 files changed

+89
-27
lines changed

README.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,6 @@ client
319319
title: 'title',
320320
body: 'body',
321321
},
322-
// Available with microCMS paid plans
323-
// https://microcms.io/pricing
324322
isDraft: true,
325323
})
326324
.then((res) => console.log(res.id))
@@ -340,8 +338,6 @@ client
340338
title: 'title',
341339
body: 'body',
342340
},
343-
// Available with microCMS paid plans
344-
// https://microcms.io/pricing
345341
isDraft: true,
346342
})
347343
.then((res) => console.log(res.id))
@@ -365,6 +361,24 @@ client
365361
.catch((err) => console.error(err));
366362
```
367363

364+
#### Update content as draft
365+
366+
By specifying the `isDraft` property, it is possible to update the content as a draft.
367+
368+
```javascript
369+
client
370+
.update({
371+
endpoint: 'endpoint',
372+
contentId: 'contentId',
373+
content: {
374+
title: 'title',
375+
},
376+
isDraft: true,
377+
})
378+
.then((res) => console.log(res.id))
379+
.catch((err) => console.error(err));
380+
```
381+
368382
#### Update object format content
369383

370384
When updating object content, use the `update` method without specifying a `contentId` property.

README_jp.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,6 @@ client
319319
title: 'タイトル',
320320
body: '本文',
321321
},
322-
// 有料プランから利用可能
323-
// https://microcms.io/pricing
324322
isDraft: true,
325323
})
326324
.then((res) => console.log(res.id))
@@ -340,8 +338,6 @@ client
340338
title: 'タイトル',
341339
body: '本文',
342340
},
343-
// 有料プランから利用可能
344-
// https://microcms.io/pricing
345341
isDraft: true,
346342
})
347343
.then((res) => console.log(res.id))
@@ -365,6 +361,24 @@ client
365361
.catch((err) => console.error(err));
366362
```
367363

364+
#### コンテンツの下書き更新
365+
366+
`isDraft` プロパティを指定することで、コンテンツを下書き状態で更新することができます。
367+
368+
```javascript
369+
client
370+
.update({
371+
endpoint: 'endpoint',
372+
contentId: 'contentId',
373+
content: {
374+
title: 'タイトル',
375+
},
376+
isDraft: true,
377+
})
378+
.then((res) => console.log(res.id))
379+
.catch((err) => console.error(err));
380+
```
381+
368382
#### オブジェクト形式のコンテンツの編集
369383

370384
APIの型がオブジェクト形式のコンテンツを編集する場合は、`contentId`プロパティを使用せずに、エンドポイントのみを指定します。

package-lock.json

Lines changed: 2 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "microcms-js-sdk",
3-
"version": "3.2.0",
3+
"version": "3.3.0",
44
"description": "JavaScript SDK Client for microCMS.",
55
"engines": {
66
"node": ">=18.0.0"

src/createClient.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,12 +358,14 @@ export const createClient = ({
358358
endpoint,
359359
contentId,
360360
content,
361+
isDraft = false,
361362
customRequestInit,
362363
}: UpdateRequest<T>): Promise<WriteApiRequestResult> => {
363364
if (!endpoint) {
364365
return Promise.reject(new Error('endpoint is required'));
365366
}
366367

368+
const queries: MakeRequest['queries'] = isDraft ? { status: 'draft' } : {};
367369
const requestInit: MakeRequest['requestInit'] = {
368370
...customRequestInit,
369371
method: 'PATCH',
@@ -376,6 +378,7 @@ export const createClient = ({
376378
return makeRequest({
377379
endpoint,
378380
contentId,
381+
queries,
379382
requestInit,
380383
});
381384
};

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ export interface UpdateRequest<T> {
159159
endpoint: string;
160160
contentId?: string;
161161
content: Partial<T>;
162+
isDraft?: boolean;
162163
customRequestInit?: CustomRequestInit;
163164
}
164165

tests/write.test.ts

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,17 @@ describe('update', () => {
131131
beforeEach(() => {
132132
server.use(
133133
http.patch(`${testBaseUrl}/list-type/foo`, async ({ request }) => {
134+
const url = new URL(request.url);
135+
const statusParams = url.searchParams.get('status');
134136
const body = await request.json();
135-
patchListApiMockFn(body);
137+
patchListApiMockFn(statusParams, body);
136138
return HttpResponse.json({ id: 'foo' }, { status: 200 });
137139
}),
138140
http.patch(`${testBaseUrl}/object-type`, async ({ request }) => {
141+
const url = new URL(request.url);
142+
const statusParams = url.searchParams.get('status');
139143
const body = await request.json();
140-
patchObjectApiMockFn(body);
144+
patchObjectApiMockFn(statusParams, body);
141145
return HttpResponse.json({ id: 'foo' }, { status: 200 });
142146
}),
143147
);
@@ -156,13 +160,32 @@ describe('update', () => {
156160
},
157161
});
158162
expect(data).toEqual({ id: 'foo' });
159-
// Confirm PUT api was called
163+
// Confirm PATCH api was called
164+
expect(patchListApiMockFn).toHaveBeenCalledTimes(1);
165+
// Confirm that body is specified.
166+
expect(patchListApiMockFn).toHaveBeenCalledWith(null, {
167+
title: 'title',
168+
});
169+
});
170+
171+
test('Draft list format content can be updated', async () => {
172+
const data = await client.update<ContentType>({
173+
endpoint: 'list-type',
174+
contentId: 'foo',
175+
content: {
176+
title: 'title',
177+
},
178+
isDraft: true,
179+
});
180+
expect(data).toEqual({ id: 'foo' });
181+
// Confirm PATCH api was called
160182
expect(patchListApiMockFn).toHaveBeenCalledTimes(1);
161183
// Confirm that body is specified.
162-
expect(patchListApiMockFn).toHaveBeenCalledWith({
184+
expect(patchListApiMockFn).toHaveBeenCalledWith('draft', {
163185
title: 'title',
164186
});
165187
});
188+
166189
test('Object type content can be updated', async () => {
167190
const data = await client.update<ContentType>({
168191
endpoint: 'object-type',
@@ -171,10 +194,27 @@ describe('update', () => {
171194
},
172195
});
173196
expect(data).toEqual({ id: 'foo' });
174-
// Confirm PUT api was called
197+
// Confirm PATCH api was called
198+
expect(patchObjectApiMockFn).toHaveBeenCalledTimes(1);
199+
// Confirm that body is specified.
200+
expect(patchObjectApiMockFn).toHaveBeenCalledWith(null, {
201+
title: 'title',
202+
});
203+
});
204+
205+
test('Draft object type content can be updated', async () => {
206+
const data = await client.update<ContentType>({
207+
endpoint: 'object-type',
208+
content: {
209+
title: 'title',
210+
},
211+
isDraft: true,
212+
});
213+
expect(data).toEqual({ id: 'foo' });
214+
// Confirm PATCH api was called
175215
expect(patchObjectApiMockFn).toHaveBeenCalledTimes(1);
176216
// Confirm that body is specified.
177-
expect(patchObjectApiMockFn).toHaveBeenCalledWith({
217+
expect(patchObjectApiMockFn).toHaveBeenCalledWith('draft', {
178218
title: 'title',
179219
});
180220
});

0 commit comments

Comments
 (0)