gatsby-source-microcms
Advanced tools
Comparing version 0.1.1 to 0.2.0
{ | ||
"name": "gatsby-source-microcms", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "Source plugin for Gatsby from microCMS.", | ||
@@ -24,3 +24,3 @@ "main": "gatsby-node.js", | ||
"eslint-plugin-react": "^7.16.0", | ||
"jest": "^24.9.0", | ||
"jest": "^26.0.1", | ||
"nock": "^11.5.0", | ||
@@ -27,0 +27,0 @@ "prettier": "^1.18.2" |
@@ -128,2 +128,10 @@ # gatsby-source-microcms | ||
/** | ||
* if format is 'list' and readAll is true then read all contents with fetchs which divided into multiple times. (Optional) | ||
* | ||
* Type: boolean. | ||
* Default: false. | ||
**/ | ||
readAll: true, | ||
/** | ||
* API request query options. (Optional) | ||
@@ -249,1 +257,5 @@ * | ||
``` | ||
## Contributing | ||
日本語歓迎🇯🇵 | ||
Pull Request, Issueお気軽にどうぞ。 |
@@ -108,2 +108,28 @@ const { createPluginConfig, validateOptions } = require('../pluginOptions'); | ||
test('valid readAll option should be success.', () => { | ||
const options = { | ||
apiKey: 'key', | ||
serviceId: 'id', | ||
endpoint: 'endpoint', | ||
format: 'list', | ||
readAll: true, | ||
}; | ||
validateOptions({ reporter }, options); | ||
expect(reporter.panic.mock.calls.length).toBe(0); | ||
}); | ||
test('invalid readAll option should be fail.', () => { | ||
const options = { | ||
apiKey: 'key', | ||
serviceId: 'id', | ||
endpoint: 'endpoint', | ||
format: 'list', | ||
readAll: 'true', | ||
}; | ||
validateOptions({ reporter }, options); | ||
expect(reporter.panic.mock.calls.length).toBe(0); | ||
}); | ||
test('draftKey option should be success.', () => { | ||
@@ -110,0 +136,0 @@ const options = { |
@@ -28,2 +28,3 @@ const { sourceNodes } = require('../sourceNodes'); | ||
format: 'list', | ||
readAll: false, | ||
version: 'v1', | ||
@@ -36,3 +37,3 @@ }; | ||
statusCode: 200, | ||
body: { contents: [{ id: '1' }, { id: '2' }] }, | ||
body: { contents: [{ id: '1' }, { id: '2' }], totalCount: 2 }, | ||
}; | ||
@@ -80,2 +81,149 @@ await sourceNodes({ actions, createNodeId, reporter }, pluginOptions); | ||
}); | ||
test('sourceNodes with list and readAll, success response', async () => { | ||
const options = { | ||
apiKey: 'key', | ||
serviceId: 'id', | ||
endpoint: 'point', | ||
format: 'list', | ||
readAll: true, | ||
version: 'v1', | ||
}; | ||
mockResponse = { | ||
statusCode: 200, | ||
body: { contents: [{ id: '1' }, { id: '2' }], totalCount: 2 }, | ||
}; | ||
await sourceNodes({ actions, createNodeId, reporter }, options); | ||
expect(actions.createNode.mock.calls.length).toBe(2); | ||
expect(createNodeId.mock.calls.length).toBe(2); | ||
expect(reporter.panic).not.toBeCalled(); | ||
}); | ||
test('sourceNodes with list and readAll, success response', async () => { | ||
const options = { | ||
apiKey: 'key', | ||
serviceId: 'id', | ||
endpoint: 'point', | ||
format: 'list', | ||
readAll: true, | ||
version: 'v1', | ||
query: { | ||
limit: 3, | ||
}, | ||
}; | ||
mockResponse = { | ||
statusCode: 200, | ||
body: { | ||
contents: [{ id: '1' }, { id: '2' }, { id: '3' }], | ||
totalCount: 12, | ||
}, | ||
}; | ||
await sourceNodes({ actions, createNodeId, reporter }, options); | ||
expect(actions.createNode.mock.calls.length).toBe(12); | ||
expect(createNodeId.mock.calls.length).toBe(12); | ||
expect(reporter.panic).not.toBeCalled(); | ||
}); | ||
test('sourceNodes with list, error response', async () => { | ||
const options = { | ||
apiKey: 'key', | ||
serviceId: 'id', | ||
endpoint: 'point', | ||
format: 'list', | ||
readAll: true, | ||
version: 'v1', | ||
}; | ||
mockResponse = { | ||
statusCode: 400, | ||
body: { message: 'error' }, | ||
}; | ||
await sourceNodes({ actions, createNodeId, reporter }, options); | ||
expect(actions.createNode).not.toBeCalled(); | ||
expect(createNodeId).not.toBeCalled(); | ||
expect(reporter.panic.mock.calls.length).toBe(1); | ||
}); | ||
test('sourceNodes with list, error response', async () => { | ||
const options = { | ||
apiKey: 'key', | ||
serviceId: 'id', | ||
endpoint: 'point', | ||
format: 'list', | ||
readAll: true, | ||
version: 'v1', | ||
}; | ||
mockResponse = { | ||
statusCode: 200, | ||
body: { content: { id: '1' }, totalCount: 1 }, | ||
}; | ||
await sourceNodes({ actions, createNodeId, reporter }, options); | ||
expect(actions.createNode).not.toBeCalled(); | ||
expect(createNodeId).not.toBeCalled(); | ||
expect(reporter.panic.mock.calls.length).toBe(1); | ||
}); | ||
test('sourceNodes with list and readAll, success response', async () => { | ||
const options = { | ||
apiKey: 'key', | ||
serviceId: 'id', | ||
endpoint: 'point', | ||
format: 'list', | ||
readAll: true, | ||
version: 'v1', | ||
query: { | ||
limit: 0, | ||
}, | ||
}; | ||
mockResponse = { | ||
statusCode: 200, | ||
body: { | ||
contents: [{ id: '1' }, { id: '2' }, { id: '3' }], | ||
totalCount: 3, | ||
}, | ||
}; | ||
await sourceNodes({ actions, createNodeId, reporter }, options); | ||
expect(actions.createNode.mock.calls.length).toBe(3); | ||
expect(createNodeId.mock.calls.length).toBe(3); | ||
expect(reporter.panic).not.toBeCalled(); | ||
}); | ||
test('sourceNodes with list and readAll, success response', async () => { | ||
const options = { | ||
apiKey: 'key', | ||
serviceId: 'id', | ||
endpoint: 'point', | ||
format: 'list', | ||
readAll: true, | ||
version: 'v1', | ||
query: { | ||
limit: 20, | ||
}, | ||
}; | ||
mockResponse = { | ||
statusCode: 200, | ||
body: { contents: [{ id: '1' }, { id: '2' }, { id: '3' }] }, | ||
}; | ||
await sourceNodes({ actions, createNodeId, reporter }, options); | ||
expect(actions.createNode.mock.calls.length).toBe(3); | ||
expect(createNodeId.mock.calls.length).toBe(3); | ||
expect(reporter.panic).not.toBeCalled(); | ||
}); | ||
test('sourceNodes with list and readAll, success response', async () => { | ||
const options = { | ||
apiKey: 'key', | ||
serviceId: 'id', | ||
endpoint: 'point', | ||
format: 'list', | ||
readAll: true, | ||
version: 'v1', | ||
query: { | ||
limit: 1, | ||
}, | ||
}; | ||
mockResponse = { | ||
statusCode: 200, | ||
body: { contents: [{ id: '1' }], totalCount: 3 }, | ||
}; | ||
await sourceNodes({ actions, createNodeId, reporter }, options); | ||
expect(actions.createNode.mock.calls.length).toBe(3); | ||
expect(createNodeId.mock.calls.length).toBe(3); | ||
expect(reporter.panic).not.toBeCalled(); | ||
}); | ||
}); |
@@ -6,2 +6,3 @@ const Joi = require('@hapi/joi'); | ||
version: 'v1', | ||
readAll: false, | ||
fields: [], | ||
@@ -32,2 +33,3 @@ query: {}, | ||
format: Joi.string().pattern(/^(list|object)$/), | ||
readAll: Joi.boolean(), | ||
query: Joi.object({ | ||
@@ -34,0 +36,0 @@ draftKey: Joi.string(), |
@@ -16,3 +16,2 @@ const { createPluginConfig } = require('./pluginOptions'); | ||
const pluginConfig = createPluginConfig(pluginOptions); | ||
const apiUrl = `https://${pluginConfig.get( | ||
@@ -23,3 +22,48 @@ 'serviceId' | ||
)}`; | ||
// type option. default is endpoint value. | ||
const type = pluginConfig.get('type') || pluginConfig.get('endpoint'); | ||
if (pluginConfig.get('readAll') && pluginConfig.get('format') === 'list') { | ||
let offset = 0; | ||
while (true) { | ||
const query = { ...pluginConfig.get('query'), offset }; | ||
const { statusCode, body } = await fetchData(apiUrl, { | ||
apiKey: pluginConfig.get('apiKey'), | ||
query, | ||
}); | ||
if (statusCode !== 200) { | ||
reporter.panic(`microCMS API ERROR: | ||
statusCode: ${statusCode} | ||
message: ${body.message}`); | ||
return; | ||
} | ||
if (!Array.isArray(body.contents)) { | ||
reporter.panic(`format set to 'list' but got ${typeof body.contents}`); | ||
return; | ||
} | ||
body.contents.forEach(content => { | ||
createContentNode({ | ||
createNode, | ||
createNodeId, | ||
content: content, | ||
type: type, | ||
}); | ||
}); | ||
const limit = query.limit || 10; | ||
// totalCount not found | ||
if (!body.totalCount) { | ||
break; | ||
} | ||
offset += limit; | ||
if (offset >= body.totalCount) { | ||
break; | ||
} | ||
} | ||
return; | ||
} | ||
const { statusCode, body } = await fetchData(apiUrl, { | ||
@@ -36,5 +80,2 @@ apiKey: pluginConfig.get('apiKey'), | ||
// type option. default is endpoint value. | ||
const type = pluginConfig.get('type') || pluginConfig.get('endpoint'); | ||
// list content | ||
@@ -41,0 +82,0 @@ if ( |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
35250
980
260
1