Skip to content

Commit

Permalink
Merge pull request #69 from microcmsio/feat/get-all-contents
Browse files Browse the repository at this point in the history
feat: getAllContents
  • Loading branch information
dc7290 authored Oct 19, 2023
2 parents d2873f1 + 29a3a05 commit 5612ec3
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "microcms-js-sdk",
"version": "2.6.1",
"version": "2.7.0",
"description": "JavaScript SDK Client for microCMS.",
"main": "./dist/microcms-js-sdk.js",
"module": "./dist/esm/microcms-js-sdk.js",
Expand Down
42 changes: 42 additions & 0 deletions src/createClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
DeleteRequest,
GetAllContentIdsRequest,
MicroCMSQueries,
GetAllContentRequest,
} from './types';
import {
API_VERSION,
Expand Down Expand Up @@ -278,6 +279,46 @@ export const createClient = ({
return contentIds;
};

/**
* Get all content API data for microCMS
*/
const getAllContents = async <T = any>({
endpoint,
queries = {},
customRequestInit,
}: GetAllContentRequest): Promise<(T & MicroCMSListContent)[]> => {
const limit = 100;

const { totalCount } = await makeRequest({
endpoint,
queries: { ...queries, limit: 0 },
requestInit: customRequestInit,
});

let contents: (T & MicroCMSListContent)[] = [];
let offset = 0;

const sleep = (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms));

while (contents.length < totalCount) {
const { contents: _contents } = (await makeRequest({
endpoint,
queries: { ...queries, limit, offset },
requestInit: customRequestInit,
})) as MicroCMSListResponse<T>;

contents = contents.concat(_contents);

offset += limit;
if (contents.length < totalCount) {
await sleep(1000); // sleep for 1 second before the next request
}
}

return contents;
};

/**
* Create new content in the microCMS list API data
*/
Expand Down Expand Up @@ -371,6 +412,7 @@ export const createClient = ({
getListDetail,
getObject,
getAllContentIds,
getAllContents,
create,
update,
delete: _delete,
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ export interface GetAllContentIdsRequest {
customRequestInit?: CustomRequestInit;
}

export interface GetAllContentRequest {
endpoint: string;
queries?: Omit<MicroCMSQueries, 'limit' | 'offset' | 'ids'>;
customRequestInit?: CustomRequestInit;
}

export interface WriteApiRequestResult {
id: string;
}
Expand Down
97 changes: 97 additions & 0 deletions tests/getAllContents.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { rest } from 'msw';
import { createClient } from '../src/createClient';
import { testBaseUrl } from './mocks/handlers';
import { server } from './mocks/server';

const client = createClient({
serviceDomain: 'serviceDomain',
apiKey: 'apiKey',
});

describe('getAllContents', () => {
afterEach(() => {
jest.resetAllMocks();
});

test('should fetch all contents', async () => {
server.use(
rest.get(`${testBaseUrl}/getAllContents-list-type`, (_, res, ctx) => {
return res.once(
ctx.status(200),
ctx.json({
totalCount: 100,
}),
);
}),
rest.get(`${testBaseUrl}/getAllContents-list-type`, (_, res, ctx) => {
return res.once(
ctx.status(200),
ctx.json({
contents: Array(100)
.fill(null)
.map((_, index) => ({ id: `id${index}` })),
}),
);
}),
);

const result = await client.getAllContents({
endpoint: 'getAllContents-list-type',
});

expect(result).toHaveLength(100);
expect(result).toContainEqual({ id: 'id0' });
expect(result).toContainEqual({ id: 'id99' });
});

test('should handle pagination and fetch more than limit', async () => {
server.use(
rest.get(`${testBaseUrl}/getAllContents-list-type`, (_, res, ctx) => {
return res.once(
ctx.status(200),
ctx.json({
totalCount: 250,
}),
);
}),
rest.get(`${testBaseUrl}/getAllContents-list-type`, (_, res, ctx) => {
return res.once(
ctx.status(200),
ctx.json({
contents: Array(100)
.fill(null)
.map((_, index) => ({ id: `id${index}` })),
}),
);
}),
rest.get(`${testBaseUrl}/getAllContents-list-type`, (_, res, ctx) => {
return res.once(
ctx.status(200),
ctx.json({
contents: Array(100)
.fill(null)
.map((_, index) => ({ id: `id${index + 100}` })),
}),
);
}),
rest.get(`${testBaseUrl}/getAllContents-list-type`, (_, res, ctx) => {
return res.once(
ctx.status(200),
ctx.json({
contents: Array(50)
.fill(null)
.map((_, index) => ({ id: `id${index + 200}` })),
}),
);
}),
);

const result = await client.getAllContents({
endpoint: 'getAllContents-list-type',
});

expect(result).toHaveLength(250);
expect(result).toContainEqual({ id: 'id0' });
expect(result).toContainEqual({ id: 'id249' });
});
});

0 comments on commit 5612ec3

Please sign in to comment.