Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions src/createClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,12 +358,14 @@ export const createClient = ({
endpoint,
contentId,
content,
isDraft = false,
customRequestInit,
}: UpdateRequest<T>): Promise<WriteApiRequestResult> => {
if (!endpoint) {
return Promise.reject(new Error('endpoint is required'));
}

const queries: MakeRequest['queries'] = isDraft ? { status: 'draft' } : {};
const requestInit: MakeRequest['requestInit'] = {
...customRequestInit,
method: 'PATCH',
Expand All @@ -376,6 +378,7 @@ export const createClient = ({
return makeRequest({
endpoint,
contentId,
queries,
requestInit,
});
};
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ export interface UpdateRequest<T> {
endpoint: string;
contentId?: string;
content: Partial<T>;
isDraft?: boolean;
customRequestInit?: CustomRequestInit;
}

Expand Down
48 changes: 44 additions & 4 deletions tests/write.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}),
);
Expand All @@ -159,10 +163,29 @@ describe('update', () => {
// Confirm PUT api was called
expect(patchListApiMockFn).toHaveBeenCalledTimes(1);
// Confirm that body is specified.
expect(patchListApiMockFn).toHaveBeenCalledWith({
expect(patchListApiMockFn).toHaveBeenCalledWith(null, {
title: 'title',
});
});

test('Draft list format content can be updated', async () => {
const data = await client.update<ContentType>({
endpoint: 'list-type',
contentId: 'foo',
content: {
title: 'title',
},
isDraft: true,
});
expect(data).toEqual({ id: 'foo' });
// Confirm PUT api was called
expect(patchListApiMockFn).toHaveBeenCalledTimes(1);
// Confirm that body is specified.
expect(patchListApiMockFn).toHaveBeenCalledWith('draft', {
title: 'title',
});
});

test('Object type content can be updated', async () => {
const data = await client.update<ContentType>({
endpoint: 'object-type',
Expand All @@ -174,7 +197,24 @@ describe('update', () => {
// Confirm PUT api was called
expect(patchObjectApiMockFn).toHaveBeenCalledTimes(1);
// Confirm that body is specified.
expect(patchObjectApiMockFn).toHaveBeenCalledWith({
expect(patchObjectApiMockFn).toHaveBeenCalledWith(null, {
title: 'title',
});
});

test('Draft object type content can be updated', async () => {
const data = await client.update<ContentType>({
endpoint: 'object-type',
content: {
title: 'title',
},
isDraft: true,
});
expect(data).toEqual({ id: 'foo' });
// Confirm PUT api was called
expect(patchObjectApiMockFn).toHaveBeenCalledTimes(1);
// Confirm that body is specified.
expect(patchObjectApiMockFn).toHaveBeenCalledWith('draft', {
title: 'title',
});
});
Expand Down