Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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));
```
Comment thread
nemuvski marked this conversation as resolved.

### Update content

The `update` method is used to update a single content specified by its ID.
Expand Down
39 changes: 39 additions & 0 deletions README_jp.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`メソッドは特定のコンテンツを編集するために使用します。
Expand Down
16 changes: 15 additions & 1 deletion src/createClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,27 @@ export const createClient = ({
contentId,
content,
isDraft = false,
isClosed = false,
customRequestInit,
}: CreateRequest<T>): Promise<WriteApiRequestResult> => {
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',
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export interface CreateRequest<T> {
contentId?: string;
content: T;
isDraft?: boolean;
isClosed?: boolean;
customRequestInit?: CustomRequestInit;
}

Expand Down
73 changes: 73 additions & 0 deletions tests/createClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});