diff --git a/README.md b/README.md index 406077b..5a0e84c 100644 --- a/README.md +++ b/README.md @@ -319,8 +319,6 @@ client title: 'title', body: 'body', }, - // Available with microCMS paid plans - // https://microcms.io/pricing isDraft: true, }) .then((res) => console.log(res.id)) @@ -340,8 +338,6 @@ client title: 'title', body: 'body', }, - // Available with microCMS paid plans - // https://microcms.io/pricing isDraft: true, }) .then((res) => console.log(res.id)) @@ -365,6 +361,24 @@ client .catch((err) => console.error(err)); ``` +#### Update content as draft + +By specifying the `isDraft` property, it is possible to update the content as a draft. + +```javascript +client + .update({ + endpoint: 'endpoint', + contentId: 'contentId', + content: { + title: 'title', + }, + isDraft: true, + }) + .then((res) => console.log(res.id)) + .catch((err) => console.error(err)); +``` + #### Update object format content When updating object content, use the `update` method without specifying a `contentId` property. diff --git a/README_jp.md b/README_jp.md index 608437e..fe1cc47 100644 --- a/README_jp.md +++ b/README_jp.md @@ -319,8 +319,6 @@ client title: 'タイトル', body: '本文', }, - // 有料プランから利用可能 - // https://microcms.io/pricing isDraft: true, }) .then((res) => console.log(res.id)) @@ -340,8 +338,6 @@ client title: 'タイトル', body: '本文', }, - // 有料プランから利用可能 - // https://microcms.io/pricing isDraft: true, }) .then((res) => console.log(res.id)) @@ -365,6 +361,24 @@ client .catch((err) => console.error(err)); ``` +#### コンテンツの下書き更新 + +`isDraft` プロパティを指定することで、コンテンツを下書き状態で更新することができます。 + +```javascript +client + .update({ + endpoint: 'endpoint', + contentId: 'contentId', + content: { + title: 'タイトル', + }, + isDraft: true, + }) + .then((res) => console.log(res.id)) + .catch((err) => console.error(err)); +``` + #### オブジェクト形式のコンテンツの編集 APIの型がオブジェクト形式のコンテンツを編集する場合は、`contentId`プロパティを使用せずに、エンドポイントのみを指定します。 diff --git a/package-lock.json b/package-lock.json index 8fe2ce1..b087e38 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "microcms-js-sdk", - "version": "3.2.0", + "version": "3.3.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "microcms-js-sdk", - "version": "3.2.0", + "version": "3.3.0", "license": "Apache-2.0", "dependencies": { "async-retry": "^1.3.3" @@ -152,7 +152,6 @@ "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.24.0.tgz", "integrity": "sha512-fQfkg0Gjkza3nf0c7/w6Xf34BW4YvzNfACRLmmb7XRLa6XHdR+K9AlJlxneFfWYf6uhOzuzZVTjF/8KfndZANw==", "dev": true, - "peer": true, "dependencies": { "@ampproject/remapping": "^2.2.0", "@babel/code-frame": "^7.23.5", @@ -2113,7 +2112,6 @@ "integrity": "sha512-uY2RSJcFPgNOEg12RQZL197LZX+MunGiKxsbxmh22VfVxrOYGRvh4mPANFlrD1yb38CgmW1wI6YgIi8LkIwmWg==", "dev": true, "hasInstallScript": true, - "peer": true, "dependencies": { "@swc/counter": "^0.1.2", "@swc/types": "^0.1.5" @@ -2497,7 +2495,6 @@ "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-7.2.0.tgz", "integrity": "sha512-mdekAHOqS9UjlmyF/LSs6AIEvfceV749GFxoBAjwAv0nkevfKHWQFDMcBZWUiIC5ft6ePWivXoS36aKQ0Cy3sw==", "dev": true, - "peer": true, "dependencies": { "@eslint-community/regexpp": "^4.5.1", "@typescript-eslint/scope-manager": "7.2.0", @@ -2533,7 +2530,6 @@ "resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-7.2.0.tgz", "integrity": "sha512-5FKsVcHTk6TafQKQbuIVkXq58Fnbkd2wDL4LB7AURN7RUOu1utVP+G8+6u3ZhEroW3DF6hyo3ZEXxgKgp4KeCg==", "dev": true, - "peer": true, "dependencies": { "@typescript-eslint/scope-manager": "7.2.0", "@typescript-eslint/types": "7.2.0", @@ -2695,7 +2691,6 @@ "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.11.3.tgz", "integrity": "sha512-Y9rRfJG5jcKOE0CLisYbojUjIrIEE7AGMzA/Sm4BslANhbS+cDMpgBdcPT91oJ7OuJ9hYJBx59RjbhxVnrF8Xg==", "dev": true, - "peer": true, "bin": { "acorn": "bin/acorn" }, @@ -2992,7 +2987,6 @@ "url": "https://github.com/sponsors/ai" } ], - "peer": true, "dependencies": { "caniuse-lite": "^1.0.30001587", "electron-to-chromium": "^1.4.668", @@ -3461,7 +3455,6 @@ "integrity": "sha512-aARqgq8roFBj054KvQr5f1sFu0D65G+miZRCuJyJ0G13Zwx7vRar5Zhn2tkQNzIXcBrNVsv/8stehpj+GAjgbg==", "dev": true, "hasInstallScript": true, - "peer": true, "bin": { "esbuild": "bin/esbuild" }, @@ -3520,7 +3513,6 @@ "resolved": "https://registry.npmjs.org/eslint/-/eslint-8.57.0.tgz", "integrity": "sha512-dZ6+mexnaTIbSBZWgou51U6OmzIhYM2VcNdtiTtI7qPNZm35Akpr0f6vtw3w1Kmn5PYo+tZVfh13WrhpS6oLqQ==", "dev": true, - "peer": true, "dependencies": { "@eslint-community/eslint-utils": "^4.2.0", "@eslint-community/regexpp": "^4.6.1", @@ -4565,7 +4557,6 @@ "resolved": "https://registry.npmjs.org/jest/-/jest-29.7.0.tgz", "integrity": "sha512-NIy3oAFp9shda19hy4HK0HRTWKtPJmGdnvywu01nOqNC2vZg+Z+fvJDxpMQA88eb2I9EcafcdjYgsDthnYTvGw==", "dev": true, - "peer": true, "dependencies": { "@jest/core": "^29.7.0", "@jest/types": "^29.6.3", @@ -6723,7 +6714,6 @@ "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.4.2.tgz", "integrity": "sha512-+2/g0Fds1ERlP6JsakQQDXjZdZMM+rqpamFZJEKh4kwTIn3iDkgKtby0CeNd5ATNZ4Ry1ax15TMx0W2V+miizQ==", "dev": true, - "peer": true, "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" diff --git a/package.json b/package.json index ede0c67..5efb005 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "microcms-js-sdk", - "version": "3.2.0", + "version": "3.3.0", "description": "JavaScript SDK Client for microCMS.", "engines": { "node": ">=18.0.0" diff --git a/src/createClient.ts b/src/createClient.ts index c7bed10..36432ec 100644 --- a/src/createClient.ts +++ b/src/createClient.ts @@ -358,12 +358,14 @@ export const createClient = ({ endpoint, contentId, content, + isDraft = false, customRequestInit, }: UpdateRequest): Promise => { if (!endpoint) { return Promise.reject(new Error('endpoint is required')); } + const queries: MakeRequest['queries'] = isDraft ? { status: 'draft' } : {}; const requestInit: MakeRequest['requestInit'] = { ...customRequestInit, method: 'PATCH', @@ -376,6 +378,7 @@ export const createClient = ({ return makeRequest({ endpoint, contentId, + queries, requestInit, }); }; diff --git a/src/types.ts b/src/types.ts index ea97bfa..bbf3ae3 100644 --- a/src/types.ts +++ b/src/types.ts @@ -159,6 +159,7 @@ export interface UpdateRequest { endpoint: string; contentId?: string; content: Partial; + isDraft?: boolean; customRequestInit?: CustomRequestInit; } diff --git a/tests/write.test.ts b/tests/write.test.ts index 82e9501..b0239b6 100644 --- a/tests/write.test.ts +++ b/tests/write.test.ts @@ -131,13 +131,17 @@ describe('update', () => { beforeEach(() => { server.use( http.patch(`${testBaseUrl}/list-type/foo`, async ({ request }) => { + const url = new URL(request.url); + const statusParams = url.searchParams.get('status'); const body = await request.json(); - patchListApiMockFn(body); + patchListApiMockFn(statusParams, body); return HttpResponse.json({ id: 'foo' }, { status: 200 }); }), http.patch(`${testBaseUrl}/object-type`, async ({ request }) => { + const url = new URL(request.url); + const statusParams = url.searchParams.get('status'); const body = await request.json(); - patchObjectApiMockFn(body); + patchObjectApiMockFn(statusParams, body); return HttpResponse.json({ id: 'foo' }, { status: 200 }); }), ); @@ -156,13 +160,32 @@ describe('update', () => { }, }); expect(data).toEqual({ id: 'foo' }); - // Confirm PUT api was called + // Confirm PATCH api was called + expect(patchListApiMockFn).toHaveBeenCalledTimes(1); + // Confirm that body is specified. + expect(patchListApiMockFn).toHaveBeenCalledWith(null, { + title: 'title', + }); + }); + + test('Draft list format content can be updated', async () => { + const data = await client.update({ + endpoint: 'list-type', + contentId: 'foo', + content: { + title: 'title', + }, + isDraft: true, + }); + expect(data).toEqual({ id: 'foo' }); + // Confirm PATCH api was called expect(patchListApiMockFn).toHaveBeenCalledTimes(1); // Confirm that body is specified. - expect(patchListApiMockFn).toHaveBeenCalledWith({ + expect(patchListApiMockFn).toHaveBeenCalledWith('draft', { title: 'title', }); }); + test('Object type content can be updated', async () => { const data = await client.update({ endpoint: 'object-type', @@ -171,10 +194,27 @@ describe('update', () => { }, }); expect(data).toEqual({ id: 'foo' }); - // Confirm PUT api was called + // Confirm PATCH api was called + expect(patchObjectApiMockFn).toHaveBeenCalledTimes(1); + // Confirm that body is specified. + expect(patchObjectApiMockFn).toHaveBeenCalledWith(null, { + title: 'title', + }); + }); + + test('Draft object type content can be updated', async () => { + const data = await client.update({ + endpoint: 'object-type', + content: { + title: 'title', + }, + isDraft: true, + }); + expect(data).toEqual({ id: 'foo' }); + // Confirm PATCH api was called expect(patchObjectApiMockFn).toHaveBeenCalledTimes(1); // Confirm that body is specified. - expect(patchObjectApiMockFn).toHaveBeenCalledWith({ + expect(patchObjectApiMockFn).toHaveBeenCalledWith('draft', { title: 'title', }); });