Skip to content

Commit ff1f798

Browse files
authored
Merge pull request #101 from microcmsio/add-status-pattern
feat: コンテンツAPI POSTとPUTで `isClosed` オプションをつけると公開終了ステータスで登録できる
2 parents a091b59 + e8cc3c6 commit ff1f798

5 files changed

Lines changed: 167 additions & 1 deletion

File tree

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,45 @@ client
344344
.catch((err) => console.error(err));
345345
```
346346

347+
#### Create closed content
348+
349+
By specifying the `isClosed` property, the content can be registered as archived.
350+
351+
> **Note:** `isDraft` and `isClosed` are mutually exclusive. Do not pass both as `true`; the SDK rejects that combination at runtime with an error. When using `isClosed: true`, omit `isDraft` or set it to `false` (the default).
352+
353+
```javascript
354+
client
355+
.create({
356+
endpoint: 'endpoint',
357+
content: {
358+
title: 'title',
359+
body: 'body',
360+
},
361+
isClosed: true,
362+
})
363+
.then((res) => console.log(res.id))
364+
.catch((err) => console.error(err));
365+
```
366+
367+
#### Create closed content with specified ID
368+
369+
By specifying the `contentId` and `isClosed` properties, the content can be registered as archived with a specified ID. The same rule applies as above: `isDraft` and `isClosed` cannot both be `true`.
370+
371+
```javascript
372+
client
373+
.create({
374+
endpoint: 'endpoint',
375+
contentId: 'contentId',
376+
content: {
377+
title: 'title',
378+
body: 'body',
379+
},
380+
isClosed: true,
381+
})
382+
.then((res) => console.log(res.id))
383+
.catch((err) => console.error(err));
384+
```
385+
347386
### Update content
348387

349388
The `update` method is used to update a single content specified by its ID.

README_jp.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,45 @@ client
344344
.catch((err) => console.error(err));
345345
```
346346

347+
#### 公開終了のステータスでコンテンツを登録
348+
349+
`isClosed`プロパティを使用することで、公開終了のステータスでコンテンツを登録できます。
350+
351+
> **注:** `isDraft``isClosed` は同時に `true` にできません。両方を `true` で渡すと、SDK はランタイムでエラーとして拒否します。`isClosed: true` を使う場合は、`isDraft` を省略、または `false` を設定してください。
352+
353+
```javascript
354+
client
355+
.create({
356+
endpoint: 'endpoint',
357+
content: {
358+
title: 'タイトル',
359+
body: '本文',
360+
},
361+
isClosed: true,
362+
})
363+
.then((res) => console.log(res.id))
364+
.catch((err) => console.error(err));
365+
```
366+
367+
#### 指定されたIDかつ公開終了のステータスでコンテンツを登録
368+
369+
`contentId`プロパティと`isClosed`プロパティを使用することで、指定されたIDかつ公開終了のステータスでコンテンツを登録できます。上記と同様、`isDraft``isClosed` を同時に `true` にすることはできません。
370+
371+
```javascript
372+
client
373+
.create({
374+
endpoint: 'endpoint',
375+
contentId: 'contentId',
376+
content: {
377+
title: 'タイトル',
378+
body: '本文',
379+
},
380+
isClosed: true,
381+
})
382+
.then((res) => console.log(res.id))
383+
.catch((err) => console.error(err));
384+
```
385+
347386
### コンテンツの編集
348387

349388
`update`メソッドは特定のコンテンツを編集するために使用します。

src/createClient.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,27 @@ export const createClient = ({
327327
contentId,
328328
content,
329329
isDraft = false,
330+
isClosed = false,
330331
customRequestInit,
331332
}: CreateRequest<T>): Promise<WriteApiRequestResult> => {
332333
if (!endpoint) {
333334
return Promise.reject(new Error('endpoint is required'));
334335
}
335336

336-
const queries: MakeRequest['queries'] = isDraft ? { status: 'draft' } : {};
337+
// if `isClosed` and `isDraft` are true, return an error
338+
if (isClosed && isDraft) {
339+
return Promise.reject(
340+
new Error('isClosed and isDraft cannot be true at the same time'),
341+
);
342+
}
343+
344+
const queries: MakeRequest['queries'] = {};
345+
if (isDraft) {
346+
queries.status = 'draft';
347+
} else if (isClosed) {
348+
queries.status = 'closed';
349+
}
350+
337351
const requestInit: MakeRequest['requestInit'] = {
338352
...customRequestInit,
339353
method: contentId ? 'PUT' : 'POST',

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ export interface CreateRequest<T> {
152152
contentId?: string;
153153
content: T;
154154
isDraft?: boolean;
155+
isClosed?: boolean;
155156
customRequestInit?: CustomRequestInit;
156157
}
157158

tests/createClient.test.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,77 @@ describe('createClient', () => {
239239
expect(apiCallCount).toBe(1);
240240
}, 30000);
241241
});
242+
243+
describe('create', () => {
244+
const client = createClient({
245+
serviceDomain: 'serviceDomain',
246+
apiKey: 'apiKey',
247+
});
248+
249+
test('Rejects when `isDraft` and `isClosed` are both true', () => {
250+
return expect(
251+
client.create({
252+
endpoint: 'list-type',
253+
content: { title: 'test' },
254+
isDraft: true,
255+
isClosed: true,
256+
}),
257+
).rejects.toThrow(
258+
new Error('isClosed and isDraft cannot be true at the same time'),
259+
);
260+
});
261+
262+
test('Sends `status=draft` when only `isDraft` is true', async () => {
263+
let requestUrl = '';
264+
server.use(
265+
http.post(`${testBaseUrl}/list-type`, ({ request }) => {
266+
requestUrl = request.url;
267+
return HttpResponse.json({ id: 'foo' });
268+
}),
269+
);
270+
271+
await client.create({
272+
endpoint: 'list-type',
273+
content: { title: 'test' },
274+
isDraft: true,
275+
});
276+
277+
expect(new URL(requestUrl).searchParams.get('status')).toBe('draft');
278+
});
279+
280+
test('Sends `status=closed` when only `isClosed` is true', async () => {
281+
let requestUrl = '';
282+
server.use(
283+
http.post(`${testBaseUrl}/list-type`, ({ request }) => {
284+
requestUrl = request.url;
285+
return HttpResponse.json({ id: 'foo' });
286+
}),
287+
);
288+
289+
await client.create({
290+
endpoint: 'list-type',
291+
content: { title: 'test' },
292+
isClosed: true,
293+
});
294+
295+
expect(new URL(requestUrl).searchParams.get('status')).toBe('closed');
296+
});
297+
298+
test('Does not send `status` when both `isDraft` and `isClosed` are false', async () => {
299+
let requestUrl = '';
300+
server.use(
301+
http.post(`${testBaseUrl}/list-type`, ({ request }) => {
302+
requestUrl = request.url;
303+
return HttpResponse.json({ id: 'foo' });
304+
}),
305+
);
306+
307+
await client.create({
308+
endpoint: 'list-type',
309+
content: { title: 'test' },
310+
});
311+
312+
expect(new URL(requestUrl).searchParams.get('status')).toBeNull();
313+
});
314+
});
242315
});

0 commit comments

Comments
 (0)