Skip to content

Commit 8f1cc1b

Browse files
authored
Merge pull request #24 from microcmsio/fix/update-x-microcms-api-key
X-MICROCMS-API-KEY対応
2 parents bb00094 + 60dd445 commit 8f1cc1b

File tree

7 files changed

+114
-113
lines changed

7 files changed

+114
-113
lines changed

README.md

Lines changed: 80 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ module.exports = {
2929
options: {
3030
apiKey: 'MICROCMS_API_KEY',
3131
serviceId: 'myblog',
32-
apis: [{
33-
endpoint: 'posts',
34-
}],
32+
apis: [
33+
{
34+
endpoint: 'posts',
35+
},
36+
],
3537
},
3638
},
3739
],
@@ -92,14 +94,6 @@ module.exports = {
9294
**/
9395
apiKey: '11111111-2222-3333-4444-555555555555',
9496

95-
/**
96-
* Global draft key.
97-
* If you set this key, you can get all draft contents.
98-
*
99-
* Type: string.
100-
**/
101-
globalDraftKey: '11111111-2222-3333-4444-555555555555',
102-
10397
/**
10498
* Service information. (Required)
10599
* xxxx.microcms.io
@@ -114,55 +108,57 @@ module.exports = {
114108
*
115109
* Type: array.
116110
**/
117-
apis: [{
118-
/**
119-
* API endpoint name. (Required)
120-
* https://xxxx.microcms.io/api/v1/posts
121-
*
122-
* Type: string.
123-
**/
124-
endpoint: 'posts',
125-
126-
/**
127-
* Graphql type. (Optional)
128-
* This is used in GraphQL queries.
129-
* If type = 'post', the GraphQL types are named 'microcmsPost' and 'allMicrocmsPost'.
130-
*
131-
* Type: string.
132-
* Default: endpoint value.
133-
**/
134-
type: 'post',
135-
136-
/**
137-
* microCMS's content type('list' or 'object'). (Optional)
138-
* if format is 'list', read all contents by fetching multiple times.
139-
*
140-
* Type: string.
141-
* Default: 'list'.
142-
**/
143-
format: 'object',
144-
145-
/**
146-
* API request query options. (Optional)
147-
*
148-
* Type:
149-
* draftKey: string.
150-
* limit: number.
151-
* offset: number.
152-
* fields: string.
153-
* filters: string.
154-
* depth: number.
155-
* Default: {}.
156-
**/
157-
query: {
158-
draftKey: 'DRAFT_KEY',
159-
limit: 100,
160-
offset: 40,
161-
fields: ['id', 'title', 'body'].join(','),
162-
filters: 'tag[exists]',
163-
depth: 2
164-
}
165-
}],
111+
apis: [
112+
{
113+
/**
114+
* API endpoint name. (Required)
115+
* https://xxxx.microcms.io/api/v1/posts
116+
*
117+
* Type: string.
118+
**/
119+
endpoint: 'posts',
120+
121+
/**
122+
* Graphql type. (Optional)
123+
* This is used in GraphQL queries.
124+
* If type = 'post', the GraphQL types are named 'microcmsPost' and 'allMicrocmsPost'.
125+
*
126+
* Type: string.
127+
* Default: endpoint value.
128+
**/
129+
type: 'post',
130+
131+
/**
132+
* microCMS's content type('list' or 'object'). (Optional)
133+
* if format is 'list', read all contents by fetching multiple times.
134+
*
135+
* Type: string.
136+
* Default: 'list'.
137+
**/
138+
format: 'object',
139+
140+
/**
141+
* API request query options. (Optional)
142+
*
143+
* Type:
144+
* draftKey: string.
145+
* limit: number.
146+
* offset: number.
147+
* fields: string.
148+
* filters: string.
149+
* depth: number.
150+
* Default: {}.
151+
**/
152+
query: {
153+
draftKey: 'DRAFT_KEY',
154+
limit: 100,
155+
offset: 40,
156+
fields: ['id', 'title', 'body'].join(','),
157+
filters: 'tag[exists]',
158+
depth: 2,
159+
},
160+
},
161+
],
166162
},
167163
},
168164
],
@@ -175,81 +171,86 @@ This plugin provides [filters query](https://microcms.io/blog/filters-parameter/
175171

176172
```js
177173
// gatsby-config.js
178-
const { and, contains, exists } = require('gatsby-source-microcms/src/query-builder');
174+
const {
175+
and,
176+
contains,
177+
exists,
178+
} = require('gatsby-source-microcms/src/query-builder');
179179

180180
module.exports = {
181181
plugins: [
182182
{
183183
resolve: 'gatsby-source-microcms',
184184
options: {
185-
apis: [{
186-
query: {
187-
filters: and(contains('title', 'sale'), exists('tag')),
188-
//=> `title[contains]sale[and]tag[exists]`
189-
}
190-
}]
185+
apis: [
186+
{
187+
query: {
188+
filters: and(contains('title', 'sale'), exists('tag')),
189+
//=> `title[contains]sale[and]tag[exists]`
190+
},
191+
},
192+
],
191193
},
192194
},
193195
],
194196
};
195-
196197
```
197198

198199
Helper list:
199200

200201
- `equals` (alias: `eq`)
201202

202203
```js
203-
equals('gender', 'women')
204+
equals('gender', 'women');
204205
//=> gender[equals]women
205206
```
206207

207208
- `notEquals` (alias: `neq`)
208209

209210
```js
210-
notEquals('gender', 'women')
211+
notEquals('gender', 'women');
211212
//=> gender[not_equals]women
212213
```
213214

214215
- `lessThan` (alias: `lg`)
215216

216217
```js
217-
lessThan('createdAt', '2019-11')
218+
lessThan('createdAt', '2019-11');
218219
//=> createdAt[less_than]2019-11
219220
```
220221

221222
- `greaterThan` (alias: `gt`)
222223

223224
```js
224-
greaterThan('createdAt', '2019-11')
225+
greaterThan('createdAt', '2019-11');
225226
//=> createdAt[greater_than]2019-11
226227
```
227228

228229
- `contains`
229230

230231
```js
231-
contains('title', 'sale')
232+
contains('title', 'sale');
232233
//=> title[contains]sale
233234
```
234235

235236
- `exists`
236237

237238
```js
238-
exists('nextLink')
239+
exists('nextLink');
239240
//=> nextLink[exists]
240241
```
241242

242243
- `notExists`
243244

244245
```js
245-
notExists('nextLink')
246+
notExists('nextLink');
246247
//=> nextLink[not_exists]
247248
```
248249

249250
- `beginsWith`
250251

251252
```js
252-
beginsWith('publishedAt', '2019-11')
253+
beginsWith('publishedAt', '2019-11');
253254
//=> publishedAt[begins_with]2019-11
254255
```
255256

@@ -268,5 +269,6 @@ or('filter1', 'filter2', ..., 'filter10')
268269
```
269270

270271
## Contributing
271-
日本語歓迎🇯🇵
272-
Pull Request, Issueお気軽にどうぞ。
272+
273+
日本語歓迎 🇯🇵
274+
Pull Request, Issue お気軽にどうぞ。

example/gatsby-config.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ require('dotenv').config();
22

33
// We prepared one service to make it easy for anyone to develop.
44
const MICROCMS_API_KEY = 'dc59f358-4622-471f-8d1e-6c7a6f969558';
5-
const MICROCMS_GLOBAL_DRAFT_KEY = '0523713e-efeb-4aec-87fd-12c58588c4e3';
65
const MICROCMS_SERVICE_ID = 'example';
76

87
const { and, equals, exists, notExists } = require('../src/query-builder');
@@ -18,7 +17,6 @@ module.exports = {
1817
resolve: require.resolve('../gatsby-node'),
1918
options: {
2019
apiKey: process.env.MICROCMS_API_KEY || MICROCMS_API_KEY,
21-
globalDraftKey: process.env.MICROCMS_GLOBAL_DRAFT_KEY || MICROCMS_GLOBAL_DRAFT_KEY,
2220
serviceId: process.env.MICROCMS_SERVICE_ID || MICROCMS_SERVICE_ID,
2321
apis: [
2422
{
@@ -35,14 +33,14 @@ module.exports = {
3533
'updatedAt',
3634
].join(','),
3735
filters: exists('title'),
38-
}
36+
},
3937
},
4038
{
4139
endpoint: 'gatsbyobject',
42-
format: 'object'
43-
}
44-
]
40+
format: 'object',
41+
},
42+
],
4543
},
46-
}
44+
},
4745
],
4846
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gatsby-source-microcms",
3-
"version": "1.2.1",
3+
"version": "2.0.0",
44
"description": "Source plugin for Gatsby from microCMS.",
55
"main": "gatsby-node.js",
66
"license": "MIT",

src/__tests__/fetch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ const apiKey = 'key';
55
const baseUrl = 'https://example.com';
66
const nockBase = nock(baseUrl, {
77
reqheaders: {
8-
'x-api-key': apiKey,
8+
'x-microcms-api-key': apiKey,
99
},
10-
}).matchHeader('x-api-key', apiKey);
10+
}).matchHeader('x-microcms-api-key', apiKey);
1111

1212
describe('fetch', () => {
1313
test('default', async () => {

src/fetch.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const fetch = require('node-fetch');
77
* @param {string} param.apiKey
88
* @param {object} param.query
99
*/
10-
module.exports = function fetchData(url, { apiKey, globalDraftKey, query }) {
10+
module.exports = function fetchData(url, { apiKey, query }) {
1111
// remove empty string or undefined or null query
1212
for (let q in query) {
1313
if (!query[q]) {
@@ -20,8 +20,7 @@ module.exports = function fetchData(url, { apiKey, globalDraftKey, query }) {
2020

2121
return fetch(reqUrl, {
2222
headers: {
23-
'x-api-key': apiKey,
24-
'x-global-draft-key': globalDraftKey,
23+
'x-microcms-api-key': apiKey,
2524
},
2625
}).then(async res => {
2726
const body = await res.json();

src/pluginOptions.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,29 @@ const optionsSchema = Joi.object().keys({
1717
apiKey: Joi.string()
1818
.required()
1919
.empty(),
20-
globalDraftKey: Joi.string(),
2120
serviceId: Joi.string()
2221
.required()
2322
.empty(),
24-
apis: Joi.array().items(Joi.object({
25-
endpoint: Joi.string().required().empty(),
26-
type: Joi.string(),
27-
format: Joi.string().pattern(/^(list|object)$/),
28-
query: Joi.object({
29-
draftKey: Joi.string(),
30-
fields: Joi.string(),
31-
limit: Joi.number().integer(),
32-
offset: Joi.number().integer(),
33-
filters: Joi.string(),
34-
depth: Joi.number()
35-
.integer()
36-
.max(3),
37-
}),
38-
}))
23+
apis: Joi.array()
24+
.items(
25+
Joi.object({
26+
endpoint: Joi.string()
27+
.required()
28+
.empty(),
29+
type: Joi.string(),
30+
format: Joi.string().pattern(/^(list|object)$/),
31+
query: Joi.object({
32+
draftKey: Joi.string(),
33+
fields: Joi.string(),
34+
limit: Joi.number().integer(),
35+
offset: Joi.number().integer(),
36+
filters: Joi.string(),
37+
depth: Joi.number()
38+
.integer()
39+
.max(3),
40+
}),
41+
})
42+
)
3943
.required()
4044
.empty(),
4145
version: Joi.string(),

0 commit comments

Comments
 (0)