diff --git a/README.md b/README.md index 5a0e84c..b2579c2 100644 --- a/README.md +++ b/README.md @@ -344,6 +344,45 @@ client .catch((err) => console.error(err)); ``` +#### Create closed content + +By specifying the `isClosed` property, the content can be registered as archived. + +> **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). + +```javascript +client + .create({ + endpoint: 'endpoint', + content: { + title: 'title', + body: 'body', + }, + isClosed: true, + }) + .then((res) => console.log(res.id)) + .catch((err) => console.error(err)); +``` + +#### Create closed content with specified ID + +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`. + +```javascript +client + .create({ + endpoint: 'endpoint', + contentId: 'contentId', + content: { + title: 'title', + body: 'body', + }, + isClosed: true, + }) + .then((res) => console.log(res.id)) + .catch((err) => console.error(err)); +``` + ### Update content The `update` method is used to update a single content specified by its ID. diff --git a/README_jp.md b/README_jp.md index fe1cc47..f0953c3 100644 --- a/README_jp.md +++ b/README_jp.md @@ -344,6 +344,45 @@ client .catch((err) => console.error(err)); ``` +#### 公開終了のステータスでコンテンツを登録 + +`isClosed`プロパティを使用することで、公開終了のステータスでコンテンツを登録できます。 + +> **注:** `isDraft` と `isClosed` は同時に `true` にできません。両方を `true` で渡すと、SDK はランタイムでエラーとして拒否します。`isClosed: true` を使う場合は、`isDraft` を省略、または `false` を設定してください。 + +```javascript +client + .create({ + endpoint: 'endpoint', + content: { + title: 'タイトル', + body: '本文', + }, + isClosed: true, + }) + .then((res) => console.log(res.id)) + .catch((err) => console.error(err)); +``` + +#### 指定されたIDかつ公開終了のステータスでコンテンツを登録 + +`contentId`プロパティと`isClosed`プロパティを使用することで、指定されたIDかつ公開終了のステータスでコンテンツを登録できます。上記と同様、`isDraft` と `isClosed` を同時に `true` にすることはできません。 + +```javascript +client + .create({ + endpoint: 'endpoint', + contentId: 'contentId', + content: { + title: 'タイトル', + body: '本文', + }, + isClosed: true, + }) + .then((res) => console.log(res.id)) + .catch((err) => console.error(err)); +``` + ### コンテンツの編集 `update`メソッドは特定のコンテンツを編集するために使用します。 diff --git a/src/createClient.ts b/src/createClient.ts index 36432ec..ddbbdb4 100644 --- a/src/createClient.ts +++ b/src/createClient.ts @@ -327,13 +327,27 @@ export const createClient = ({ contentId, content, isDraft = false, + isClosed = false, customRequestInit, }: CreateRequest): Promise => { if (!endpoint) { return Promise.reject(new Error('endpoint is required')); } - const queries: MakeRequest['queries'] = isDraft ? { status: 'draft' } : {}; + // if `isClosed` and `isDraft` are true, return an error + if (isClosed && isDraft) { + return Promise.reject( + new Error('isClosed and isDraft cannot be true at the same time'), + ); + } + + const queries: MakeRequest['queries'] = {}; + if (isDraft) { + queries.status = 'draft'; + } else if (isClosed) { + queries.status = 'closed'; + } + const requestInit: MakeRequest['requestInit'] = { ...customRequestInit, method: contentId ? 'PUT' : 'POST', diff --git a/src/types.ts b/src/types.ts index bbf3ae3..f6ff71f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -152,6 +152,7 @@ export interface CreateRequest { contentId?: string; content: T; isDraft?: boolean; + isClosed?: boolean; customRequestInit?: CustomRequestInit; } diff --git a/tests/createClient.test.ts b/tests/createClient.test.ts index 39db356..e3b0a87 100644 --- a/tests/createClient.test.ts +++ b/tests/createClient.test.ts @@ -239,4 +239,77 @@ describe('createClient', () => { expect(apiCallCount).toBe(1); }, 30000); }); + + describe('create', () => { + const client = createClient({ + serviceDomain: 'serviceDomain', + apiKey: 'apiKey', + }); + + test('Rejects when `isDraft` and `isClosed` are both true', () => { + return expect( + client.create({ + endpoint: 'list-type', + content: { title: 'test' }, + isDraft: true, + isClosed: true, + }), + ).rejects.toThrow( + new Error('isClosed and isDraft cannot be true at the same time'), + ); + }); + + test('Sends `status=draft` when only `isDraft` is true', async () => { + let requestUrl = ''; + server.use( + http.post(`${testBaseUrl}/list-type`, ({ request }) => { + requestUrl = request.url; + return HttpResponse.json({ id: 'foo' }); + }), + ); + + await client.create({ + endpoint: 'list-type', + content: { title: 'test' }, + isDraft: true, + }); + + expect(new URL(requestUrl).searchParams.get('status')).toBe('draft'); + }); + + test('Sends `status=closed` when only `isClosed` is true', async () => { + let requestUrl = ''; + server.use( + http.post(`${testBaseUrl}/list-type`, ({ request }) => { + requestUrl = request.url; + return HttpResponse.json({ id: 'foo' }); + }), + ); + + await client.create({ + endpoint: 'list-type', + content: { title: 'test' }, + isClosed: true, + }); + + expect(new URL(requestUrl).searchParams.get('status')).toBe('closed'); + }); + + test('Does not send `status` when both `isDraft` and `isClosed` are false', async () => { + let requestUrl = ''; + server.use( + http.post(`${testBaseUrl}/list-type`, ({ request }) => { + requestUrl = request.url; + return HttpResponse.json({ id: 'foo' }); + }), + ); + + await client.create({ + endpoint: 'list-type', + content: { title: 'test' }, + }); + + expect(new URL(requestUrl).searchParams.get('status')).toBeNull(); + }); + }); });