microcms-js-sdk 3.1.0 → 3.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +233 -80
- package/dist/esm/microcms-js-sdk.js +1 -1
- package/dist/esm/microcms-js-sdk.js.map +1 -1
- package/dist/iife/microcms-js-sdk.js +1 -1
- package/dist/iife/microcms-js-sdk.js.map +1 -1
- package/dist/microcms-js-sdk.js +1 -1
- package/dist/microcms-js-sdk.js.map +1 -1
- package/dist/umd/microcms-js-sdk.js +1 -1
- package/dist/umd/microcms-js-sdk.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,14 +4,15 @@ It helps you to use microCMS from JavaScript and Node.js applications.
|
|
|
4
4
|
|
|
5
5
|
<a href="https://discord.com/invite/K3DPqw4EJ2" target="_blank"><img src="https://img.shields.io/badge/Discord-%235865F2.svg?style=for-the-badge&logo=discord&logoColor=white" alt="Discord"></a>
|
|
6
6
|
|
|
7
|
-
##
|
|
7
|
+
## Tutorial
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
See the [official tutorial](https://document.microcms.io/tutorial/javascript/javascript-top).
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
## Getting started
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
### Installation
|
|
14
|
+
|
|
15
|
+
#### Node.js
|
|
15
16
|
|
|
16
17
|
```bash
|
|
17
18
|
$ npm install microcms-js-sdk
|
|
@@ -21,24 +22,61 @@ or
|
|
|
21
22
|
$ yarn add microcms-js-sdk
|
|
22
23
|
```
|
|
23
24
|
|
|
24
|
-
|
|
25
|
+
> [!IMPORTANT]
|
|
26
|
+
> v3.0.0 or later requires Node.js **v18 or higher**.
|
|
27
|
+
|
|
28
|
+
#### Browser(Self-hosting)
|
|
29
|
+
|
|
30
|
+
Download and unzip `microcms-js-sdk-x.y.z.tgz` from the [releases page](https://github.com/microcmsio/microcms-js-sdk/releases). Then, host it on any server of your choice and use it. The target file is `./dist/umd/microcms-js-sdk.js`.
|
|
31
|
+
|
|
32
|
+
```html
|
|
33
|
+
<script src="./microcms-js-sdk.js"></script>
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
#### Browser(CDN)
|
|
37
|
+
|
|
38
|
+
Please load and use the URL provided by an external provider.
|
|
25
39
|
|
|
26
40
|
```html
|
|
27
|
-
<script src="https://
|
|
41
|
+
<script src="https://cdn.jsdelivr.net/npm/microcms-js-sdk@3.1.1/dist/umd/microcms-js-sdk.min.js"></script>
|
|
42
|
+
```
|
|
28
43
|
|
|
29
44
|
or
|
|
30
45
|
|
|
31
|
-
|
|
46
|
+
```html
|
|
47
|
+
<script src="https://cdn.jsdelivr.net/npm/microcms-js-sdk/dist/umd/microcms-js-sdk.min.js"></script>
|
|
32
48
|
```
|
|
33
49
|
|
|
34
|
-
|
|
50
|
+
> [!WARNING]
|
|
51
|
+
> The hosting service (cdn.jsdelivr.net) is not related to microCMS. For production use, we recommend self-hosting on your own server.
|
|
52
|
+
|
|
53
|
+
## Contents API
|
|
35
54
|
|
|
36
|
-
|
|
55
|
+
### Import
|
|
56
|
+
|
|
57
|
+
#### Node.js
|
|
37
58
|
|
|
38
59
|
```javascript
|
|
39
60
|
const { createClient } = require('microcms-js-sdk'); // CommonJS
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
or
|
|
64
|
+
|
|
65
|
+
```javascript
|
|
40
66
|
import { createClient } from 'microcms-js-sdk'; //ES6
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
#### Usage with a browser
|
|
41
70
|
|
|
71
|
+
```html
|
|
72
|
+
<script>
|
|
73
|
+
const { createClient } = microcms;
|
|
74
|
+
</script>
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Create client object
|
|
78
|
+
|
|
79
|
+
```javascript
|
|
42
80
|
// Initialize Client SDK.
|
|
43
81
|
const client = createClient({
|
|
44
82
|
serviceDomain: 'YOUR_DOMAIN', // YOUR_DOMAIN is the XXXX part of XXXX.microcms.io
|
|
@@ -47,63 +85,101 @@ const client = createClient({
|
|
|
47
85
|
});
|
|
48
86
|
```
|
|
49
87
|
|
|
50
|
-
|
|
88
|
+
### API methods
|
|
51
89
|
|
|
52
|
-
|
|
53
|
-
<script>
|
|
54
|
-
const { createClient } = microcms;
|
|
90
|
+
The table below shows each API method of microCMS JavaScript SDK and indicates which API format (List Format or Object Format) they can be used with using ✔️.
|
|
55
91
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
92
|
+
| Method | List Format | Object Format |
|
|
93
|
+
|-------------------|-------------|---------------|
|
|
94
|
+
| getList | ✔️ | |
|
|
95
|
+
| getListDetail | ✔️ | |
|
|
96
|
+
| getObject | | ✔️ |
|
|
97
|
+
| getAllContentIds | ✔️ | |
|
|
98
|
+
| getAllContents | ✔️ | |
|
|
99
|
+
| create | ✔️ | |
|
|
100
|
+
| update | ✔️ | ✔️ |
|
|
101
|
+
| delete | ✔️ | |
|
|
102
|
+
|
|
103
|
+
> [!NOTE]
|
|
104
|
+
> - ✔️ in "List Format" indicates the method can be used when the API type is set to List Format.
|
|
105
|
+
> - ✔️ in "Object Format" indicates the method can be used when the API type is set to Object Format.
|
|
106
|
+
|
|
107
|
+
### Get content list
|
|
64
108
|
|
|
65
|
-
|
|
109
|
+
The `getList` method is used to retrieve a list of content from a specified endpoint.
|
|
66
110
|
|
|
67
111
|
```javascript
|
|
68
112
|
client
|
|
69
|
-
.
|
|
113
|
+
.getList({
|
|
70
114
|
endpoint: 'endpoint',
|
|
71
|
-
queries: { limit: 20, filters: 'createdAt[greater_than]2021' },
|
|
72
115
|
})
|
|
73
116
|
.then((res) => console.log(res))
|
|
74
117
|
.catch((err) => console.error(err));
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
#### Get content list with parameters
|
|
121
|
+
|
|
122
|
+
The `queries` property can be used to specify parameters for retrieving content that matches specific criteria. For more details on each available property, refer to the [microCMS Documentation](https://document.microcms.io/content-api/get-list-contents#h929d25d495).
|
|
75
123
|
|
|
124
|
+
```javascript
|
|
76
125
|
client
|
|
77
|
-
.
|
|
126
|
+
.getList({
|
|
78
127
|
endpoint: 'endpoint',
|
|
79
|
-
|
|
80
|
-
|
|
128
|
+
queries: {
|
|
129
|
+
draftKey: 'abcd',
|
|
130
|
+
limit: 100,
|
|
131
|
+
offset: 1,
|
|
132
|
+
orders: 'createdAt',
|
|
133
|
+
q: 'Hello',
|
|
134
|
+
fields: 'id,title',
|
|
135
|
+
ids: 'foo',
|
|
136
|
+
filters: 'publishedAt[greater_than]2021-01-01T03:00:00.000Z',
|
|
137
|
+
depth: 1,
|
|
138
|
+
}
|
|
81
139
|
})
|
|
82
140
|
.then((res) => console.log(res))
|
|
83
141
|
.catch((err) => console.error(err));
|
|
84
142
|
```
|
|
85
143
|
|
|
86
|
-
|
|
144
|
+
### Get single content
|
|
145
|
+
|
|
146
|
+
The `getListDetail` method is used to retrieve a single content specified by its ID.
|
|
87
147
|
|
|
88
148
|
```javascript
|
|
89
|
-
// Get list API data
|
|
90
149
|
client
|
|
91
|
-
.
|
|
150
|
+
.getListDetail({
|
|
92
151
|
endpoint: 'endpoint',
|
|
152
|
+
contentId: 'contentId',
|
|
93
153
|
})
|
|
94
154
|
.then((res) => console.log(res))
|
|
95
155
|
.catch((err) => console.error(err));
|
|
156
|
+
```
|
|
96
157
|
|
|
97
|
-
|
|
158
|
+
#### Get single content with parameters
|
|
159
|
+
|
|
160
|
+
The `queries` property can be used to specify parameters for retrieving a single content that matches specific criteria. For more details on each available property, refer to the [microCMS Documentation](https://document.microcms.io/content-api/get-content#h929d25d495).
|
|
161
|
+
|
|
162
|
+
```javascript
|
|
98
163
|
client
|
|
99
164
|
.getListDetail({
|
|
100
165
|
endpoint: 'endpoint',
|
|
101
166
|
contentId: 'contentId',
|
|
167
|
+
queries: {
|
|
168
|
+
draftKey: 'abcd',
|
|
169
|
+
fields: 'id,title',
|
|
170
|
+
depth: 1,
|
|
171
|
+
}
|
|
102
172
|
})
|
|
103
173
|
.then((res) => console.log(res))
|
|
104
174
|
.catch((err) => console.error(err));
|
|
105
175
|
|
|
106
|
-
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### Get object format content
|
|
179
|
+
|
|
180
|
+
The `getObject` method is used to retrieve a single object format content
|
|
181
|
+
|
|
182
|
+
```javascript
|
|
107
183
|
client
|
|
108
184
|
.getObject({
|
|
109
185
|
endpoint: 'endpoint',
|
|
@@ -112,11 +188,9 @@ client
|
|
|
112
188
|
.catch((err) => console.error(err));
|
|
113
189
|
```
|
|
114
190
|
|
|
115
|
-
|
|
191
|
+
### Get all contentIds
|
|
116
192
|
|
|
117
|
-
|
|
118
|
-
Since `filters` and `draftKey` can also be specified, it is possible to retrieve only the content IDs for a specific category, or to include content from a specific draft. \
|
|
119
|
-
The `alternateField` property can also be used to address cases where the value of a field other than content ID is used in a URL, etc.
|
|
193
|
+
The `getAllContentIds` method is used to retrieve all content IDs only.
|
|
120
194
|
|
|
121
195
|
```javascript
|
|
122
196
|
client
|
|
@@ -125,8 +199,13 @@ client
|
|
|
125
199
|
})
|
|
126
200
|
.then((res) => console.log(res))
|
|
127
201
|
.catch((err) => console.error(err));
|
|
202
|
+
```
|
|
128
203
|
|
|
129
|
-
|
|
204
|
+
#### Get all contentIds with filters
|
|
205
|
+
|
|
206
|
+
It is possible to retrieve only the content IDs for a specific category by specifying the `filters`.
|
|
207
|
+
|
|
208
|
+
```javascript
|
|
130
209
|
client
|
|
131
210
|
.getAllContentIds({
|
|
132
211
|
endpoint: 'endpoint',
|
|
@@ -134,8 +213,13 @@ client
|
|
|
134
213
|
})
|
|
135
214
|
.then((res) => console.log(res))
|
|
136
215
|
.catch((err) => console.error(err));
|
|
216
|
+
```
|
|
137
217
|
|
|
138
|
-
|
|
218
|
+
#### Get all contentIds with draftKey
|
|
219
|
+
|
|
220
|
+
It is possible to include content from a specific draft by specifying the `draftKey`.
|
|
221
|
+
|
|
222
|
+
```javascript
|
|
139
223
|
client
|
|
140
224
|
.getAllContentIds({
|
|
141
225
|
endpoint: 'endpoint',
|
|
@@ -143,8 +227,13 @@ client
|
|
|
143
227
|
})
|
|
144
228
|
.then((res) => console.log(res))
|
|
145
229
|
.catch((err) => console.error(err));
|
|
230
|
+
```
|
|
146
231
|
|
|
147
|
-
|
|
232
|
+
#### Get all contentIds with alternateField
|
|
233
|
+
|
|
234
|
+
The `alternateField` property can be used to address cases where the value of a field other than content ID is used in a URL, etc.
|
|
235
|
+
|
|
236
|
+
```javascript
|
|
148
237
|
client
|
|
149
238
|
.getAllContentIds({
|
|
150
239
|
endpoint: 'endpoint',
|
|
@@ -154,9 +243,9 @@ client
|
|
|
154
243
|
.catch((err) => console.error(err));
|
|
155
244
|
```
|
|
156
245
|
|
|
157
|
-
|
|
246
|
+
### Get all contents
|
|
158
247
|
|
|
159
|
-
|
|
248
|
+
The `getAllContents` method is used to retrieve all content data.
|
|
160
249
|
|
|
161
250
|
```javascript
|
|
162
251
|
client
|
|
@@ -165,23 +254,27 @@ client
|
|
|
165
254
|
})
|
|
166
255
|
.then((res) => console.log(res))
|
|
167
256
|
.catch((err) => console.error(err));
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
#### Get all contents with parameters
|
|
168
260
|
|
|
169
|
-
|
|
261
|
+
The `queries` property can be used to specify parameters for retrieving all content that matches specific criteria. For more details on each available property, refer to the [microCMS Documentation](https://document.microcms.io/content-api/get-list-contents#h929d25d495).
|
|
262
|
+
|
|
263
|
+
```javascript
|
|
170
264
|
client
|
|
171
265
|
.getAllContents({
|
|
172
266
|
endpoint: 'endpoint',
|
|
173
|
-
queries: { filters: 'createdAt[greater_than]2021', orders: '-createdAt' },
|
|
267
|
+
queries: { filters: 'createdAt[greater_than]2021-01-01T03:00:00.000Z', orders: '-createdAt' },
|
|
174
268
|
})
|
|
175
269
|
.then((res) => console.log(res))
|
|
176
270
|
.catch((err) => console.error(err));
|
|
177
271
|
```
|
|
178
272
|
|
|
179
|
-
|
|
273
|
+
### Create content
|
|
180
274
|
|
|
181
|
-
The
|
|
275
|
+
The `create` method is used to register content.
|
|
182
276
|
|
|
183
277
|
```javascript
|
|
184
|
-
// Create content
|
|
185
278
|
client
|
|
186
279
|
.create({
|
|
187
280
|
endpoint: 'endpoint',
|
|
@@ -192,8 +285,13 @@ client
|
|
|
192
285
|
})
|
|
193
286
|
.then((res) => console.log(res.id))
|
|
194
287
|
.catch((err) => console.error(err));
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
#### Create content with specified ID
|
|
195
291
|
|
|
196
|
-
|
|
292
|
+
By specifying the `contentId` property, it is possible to register content with a specified ID.
|
|
293
|
+
|
|
294
|
+
```javascript
|
|
197
295
|
client
|
|
198
296
|
.create({
|
|
199
297
|
endpoint: 'endpoint',
|
|
@@ -205,7 +303,13 @@ client
|
|
|
205
303
|
})
|
|
206
304
|
.then((res) => console.log(res.id))
|
|
207
305
|
.catch((err) => console.error(err));
|
|
208
|
-
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
#### Create draft content
|
|
309
|
+
|
|
310
|
+
By specifying the `isDraft` property, it is possible to register the content as a draft.
|
|
311
|
+
|
|
312
|
+
```javascript
|
|
209
313
|
client
|
|
210
314
|
.create({
|
|
211
315
|
endpoint: 'endpoint',
|
|
@@ -219,8 +323,13 @@ client
|
|
|
219
323
|
})
|
|
220
324
|
.then((res) => console.log(res.id))
|
|
221
325
|
.catch((err) => console.error(err));
|
|
326
|
+
```
|
|
222
327
|
|
|
223
|
-
|
|
328
|
+
#### Create draft content with specified ID
|
|
329
|
+
|
|
330
|
+
By specifying the `contentId` and `isDraft` properties, it is possible to register the content as a draft with a specified ID.
|
|
331
|
+
|
|
332
|
+
```javascript
|
|
224
333
|
client
|
|
225
334
|
.create({
|
|
226
335
|
endpoint: 'endpoint',
|
|
@@ -237,10 +346,11 @@ client
|
|
|
237
346
|
.catch((err) => console.error(err));
|
|
238
347
|
```
|
|
239
348
|
|
|
240
|
-
###
|
|
349
|
+
### Update content
|
|
350
|
+
|
|
351
|
+
The `update` method is used to update a single content specified by its ID.
|
|
241
352
|
|
|
242
353
|
```javascript
|
|
243
|
-
// Update content
|
|
244
354
|
client
|
|
245
355
|
.update({
|
|
246
356
|
endpoint: 'endpoint',
|
|
@@ -251,8 +361,13 @@ client
|
|
|
251
361
|
})
|
|
252
362
|
.then((res) => console.log(res.id))
|
|
253
363
|
.catch((err) => console.error(err));
|
|
364
|
+
```
|
|
254
365
|
|
|
255
|
-
|
|
366
|
+
#### Update object format content
|
|
367
|
+
|
|
368
|
+
When updating object content, use the `update` method without specifying a `contentId` property.
|
|
369
|
+
|
|
370
|
+
```javascript
|
|
256
371
|
client
|
|
257
372
|
.update({
|
|
258
373
|
endpoint: 'endpoint',
|
|
@@ -264,10 +379,11 @@ client
|
|
|
264
379
|
.catch((err) => console.error(err));
|
|
265
380
|
```
|
|
266
381
|
|
|
267
|
-
###
|
|
382
|
+
### Delete content
|
|
383
|
+
|
|
384
|
+
The `delete` method is used to delete a single content specified by its ID.
|
|
268
385
|
|
|
269
386
|
```javascript
|
|
270
|
-
// Delete content
|
|
271
387
|
client
|
|
272
388
|
.delete({
|
|
273
389
|
endpoint: 'endpoint',
|
|
@@ -280,14 +396,13 @@ client
|
|
|
280
396
|
|
|
281
397
|
If you are using TypeScript, use `getList`, `getListDetail`, `getObject`. This internally contains a common type of content.
|
|
282
398
|
|
|
399
|
+
#### Response type for getList method
|
|
400
|
+
|
|
283
401
|
```typescript
|
|
284
|
-
// Type definition
|
|
285
402
|
type Content = {
|
|
286
403
|
text: string,
|
|
287
|
-
}
|
|
288
|
-
|
|
404
|
+
};
|
|
289
405
|
/**
|
|
290
|
-
* // getList response type
|
|
291
406
|
* {
|
|
292
407
|
* contents: Content[]; // This is array type of Content
|
|
293
408
|
* totalCount: number;
|
|
@@ -295,10 +410,16 @@ type Content = {
|
|
|
295
410
|
* offset: number;
|
|
296
411
|
* }
|
|
297
412
|
*/
|
|
298
|
-
client.getList<Content>({
|
|
413
|
+
client.getList<Content>({ /* other */ })
|
|
414
|
+
```
|
|
299
415
|
|
|
416
|
+
#### Response type for getListDetail method
|
|
417
|
+
|
|
418
|
+
```typescript
|
|
419
|
+
type Content = {
|
|
420
|
+
text: string,
|
|
421
|
+
};
|
|
300
422
|
/**
|
|
301
|
-
* // getListDetail response type
|
|
302
423
|
* {
|
|
303
424
|
* id: string;
|
|
304
425
|
* createdAt: string;
|
|
@@ -308,10 +429,16 @@ client.getList<Content>({ //other })
|
|
|
308
429
|
* text: string; // This is Content type.
|
|
309
430
|
* }
|
|
310
431
|
*/
|
|
311
|
-
client.getListDetail<Content>({
|
|
432
|
+
client.getListDetail<Content>({ /* other */ })
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
#### Response type for getObject method
|
|
312
436
|
|
|
437
|
+
```typescript
|
|
438
|
+
type Content = {
|
|
439
|
+
text: string,
|
|
440
|
+
};
|
|
313
441
|
/**
|
|
314
|
-
* // getObject response type
|
|
315
442
|
* {
|
|
316
443
|
* createdAt: string;
|
|
317
444
|
* updatedAt: string;
|
|
@@ -320,20 +447,22 @@ client.getListDetail<Content>({ //other })
|
|
|
320
447
|
* text: string; // This is Content type.
|
|
321
448
|
* }
|
|
322
449
|
*/
|
|
323
|
-
|
|
450
|
+
|
|
451
|
+
client.getObject<Content>({ /* other */ })
|
|
324
452
|
```
|
|
325
453
|
|
|
326
|
-
|
|
454
|
+
#### Response type for getAllContentIds method
|
|
327
455
|
|
|
328
456
|
```typescript
|
|
329
457
|
/**
|
|
330
|
-
* // getAllContentIds response type
|
|
331
458
|
* string[] // This is array type of string
|
|
332
459
|
*/
|
|
333
|
-
client.getAllContentIds({
|
|
460
|
+
client.getAllContentIds({ /* other */ })
|
|
334
461
|
```
|
|
335
462
|
|
|
336
|
-
|
|
463
|
+
#### Create method with type safety
|
|
464
|
+
|
|
465
|
+
Since `content` will be of type `Content`, no required fields will be missed.
|
|
337
466
|
|
|
338
467
|
```typescript
|
|
339
468
|
type Content = {
|
|
@@ -343,25 +472,34 @@ type Content = {
|
|
|
343
472
|
|
|
344
473
|
client.create<Content>({
|
|
345
474
|
endpoint: 'endpoint',
|
|
346
|
-
// Since `content` will be of type `Content`, no required fields will be missed.
|
|
347
475
|
content: {
|
|
348
476
|
title: 'title',
|
|
349
477
|
body: 'body',
|
|
350
478
|
},
|
|
351
479
|
});
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
#### Update method with type safety
|
|
483
|
+
|
|
484
|
+
The `content` will be of type `Partial<Content>`, so you can enter only the items needed for the update.
|
|
485
|
+
|
|
486
|
+
```typescript
|
|
487
|
+
type Content = {
|
|
488
|
+
title: string;
|
|
489
|
+
body?: string;
|
|
490
|
+
};
|
|
352
491
|
|
|
353
492
|
client.update<Content>({
|
|
354
493
|
endpoint: 'endpoint',
|
|
355
|
-
// The `content` will be of type `Partial<Content>`, so you can enter only the items needed for the update.
|
|
356
494
|
content: {
|
|
357
495
|
body: 'body',
|
|
358
496
|
},
|
|
359
497
|
});
|
|
360
498
|
```
|
|
361
499
|
|
|
362
|
-
|
|
500
|
+
### CustomRequestInit
|
|
363
501
|
|
|
364
|
-
|
|
502
|
+
#### Next.js App Router
|
|
365
503
|
|
|
366
504
|
You can now use the fetch option of the Next.js App Router as CustomRequestInit.
|
|
367
505
|
Please refer to the official Next.js documentation as the available options depend on the Next.js Type file.
|
|
@@ -379,7 +517,7 @@ const response = await client.getList({
|
|
|
379
517
|
});
|
|
380
518
|
```
|
|
381
519
|
|
|
382
|
-
|
|
520
|
+
#### AbortController: abort() method
|
|
383
521
|
|
|
384
522
|
You can abort fetch requests.
|
|
385
523
|
|
|
@@ -399,23 +537,38 @@ setTimeout(() => {
|
|
|
399
537
|
|
|
400
538
|
## Management API
|
|
401
539
|
|
|
402
|
-
|
|
540
|
+
### Import
|
|
403
541
|
|
|
404
|
-
|
|
542
|
+
#### Node.js
|
|
405
543
|
|
|
406
|
-
|
|
544
|
+
```javascript
|
|
545
|
+
const { createManagementClient } = require('microcms-js-sdk'); // CommonJS
|
|
546
|
+
```
|
|
547
|
+
|
|
548
|
+
or
|
|
407
549
|
|
|
408
550
|
```javascript
|
|
409
551
|
import { createManagementClient } from 'microcms-js-sdk'; //ES6
|
|
552
|
+
```
|
|
410
553
|
|
|
411
|
-
|
|
554
|
+
#### Usage with a browser
|
|
555
|
+
|
|
556
|
+
```html
|
|
557
|
+
<script>
|
|
558
|
+
const { createManagementClient } = microcms;
|
|
559
|
+
</script>
|
|
560
|
+
```
|
|
561
|
+
|
|
562
|
+
### Create client object
|
|
563
|
+
|
|
564
|
+
```javascript
|
|
412
565
|
const client = createManagementClient({
|
|
413
566
|
serviceDomain: 'YOUR_DOMAIN', // YOUR_DOMAIN is the XXXX part of XXXX.microcms.io
|
|
414
567
|
apiKey: 'YOUR_API_KEY',
|
|
415
568
|
});
|
|
416
569
|
```
|
|
417
570
|
|
|
418
|
-
###
|
|
571
|
+
### Upload media
|
|
419
572
|
|
|
420
573
|
Media files can be uploaded using the 'POST /api/v1/media' endpoint of the Management API.
|
|
421
574
|
|
|
@@ -480,9 +633,9 @@ client
|
|
|
480
633
|
.catch((err) => console.error(err));
|
|
481
634
|
```
|
|
482
635
|
|
|
483
|
-
###
|
|
636
|
+
### TypeScript
|
|
484
637
|
|
|
485
|
-
####
|
|
638
|
+
#### Parameter type for uploadMedia method
|
|
486
639
|
|
|
487
640
|
```typescript
|
|
488
641
|
type UploadMediaRequest =
|
|
@@ -1 +1 @@
|
|
|
1
|
-
function e(e,t){if(t==null||t>e.length)t=e.length;for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function t(e){if(Array.isArray(e))return e}function r(t){if(Array.isArray(t))return e(t)}function n(e,t,r,n,i,o,u){try{var a=e[o](u);var s=a.value}catch(e){r(e);return}if(a.done){t(s)}else{Promise.resolve(s).then(n,i)}}function i(e){return function(){var t=this,r=arguments;return new Promise(function(i,o){var u=e.apply(t,r);function a(e){n(u,i,o,a,s,"next",e)}function s(e){n(u,i,o,a,s,"throw",e)}a(undefined)})}}function o(e,t,r){if(t in e){Object.defineProperty(e,t,{value:r,enumerable:true,configurable:true,writable:true})}else{e[t]=r}return e}function u(e,t){if(t!=null&&typeof Symbol!=="undefined"&&t[Symbol.hasInstance]){return!!t[Symbol.hasInstance](e)}else{return e instanceof t}}function a(e){if(typeof Symbol!=="undefined"&&e[Symbol.iterator]!=null||e["@@iterator"]!=null)return Array.from(e)}function s(e,t){var r=e==null?null:typeof Symbol!=="undefined"&&e[Symbol.iterator]||e["@@iterator"];if(r==null)return;var n=[];var i=true;var o=false;var u,a;try{for(r=r.call(e);!(i=(u=r.next()).done);i=true){n.push(u.value);if(t&&n.length===t)break}}catch(e){o=true;a=e}finally{try{if(!i&&r["return"]!=null)r["return"]()}finally{if(o)throw a}}return n}function c(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function l(){throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function f(e){for(var t=1;t<arguments.length;t++){var r=arguments[t]!=null?arguments[t]:{};var n=Object.keys(r);if(typeof Object.getOwnPropertySymbols==="function"){n=n.concat(Object.getOwnPropertySymbols(r).filter(function(e){return Object.getOwnPropertyDescriptor(r,e).enumerable}))}n.forEach(function(t){o(e,t,r[t])})}return e}function d(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);if(t){n=n.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})}r.push.apply(r,n)}return r}function p(e,t){t=t!=null?t:{};if(Object.getOwnPropertyDescriptors){Object.defineProperties(e,Object.getOwnPropertyDescriptors(t))}else{d(Object(t)).forEach(function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))})}return e}function h(e,r){return t(e)||s(e,r)||y(e,r)||c()}function v(e){return r(e)||a(e)||y(e)||l()}function y(t,r){if(!t)return;if(typeof t==="string")return e(t,r);var n=Object.prototype.toString.call(t).slice(8,-1);if(n==="Object"&&t.constructor)n=t.constructor.name;if(n==="Map"||n==="Set")return Array.from(n);if(n==="Arguments"||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return e(t,r)}function m(e,t){var r,n,i,o,u={label:0,sent:function(){if(i[0]&1)throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),"throw":a(1),"return":a(2)},typeof Symbol==="function"&&(o[Symbol.iterator]=function(){return this}),o;function a(e){return function(t){return s([e,t])}}function s(o){if(r)throw new TypeError("Generator is already executing.");while(u)try{if(r=1,n&&(i=o[0]&2?n["return"]:o[0]?n["throw"]||((i=n["return"])&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;if(n=0,i)o=[o[0]&2,i.value];switch(o[0]){case 0:case 1:i=o;break;case 4:u.label++;return{value:o[1],done:false};case 5:u.label++;n=o[1];o=[0];continue;case 7:o=u.ops.pop();u.trys.pop();continue;default:if(!(i=u.trys,i=i.length>0&&i[i.length-1])&&(o[0]===6||o[0]===2)){u=0;continue}if(o[0]===3&&(!i||o[1]>i[0]&&o[1]<i[3])){u.label=o[1];break}if(o[0]===6&&u.label<i[1]){u.label=i[1];i=o;break}if(i&&u.label<i[2]){u.label=i[2];u.ops.push(o);break}if(i[2])u.ops.pop();u.trys.pop();continue}o=t.call(e,u)}catch(e){o=[6,e];n=0}finally{r=i=0}if(o[0]&5)throw o[1];return{value:o[0]?o[1]:void 0,done:true}}}import b from"async-retry";var w=function(e){return function(){var t=i(function(t,r){var n;return m(this,function(i){n=new Headers(r===null||r===void 0?void 0:r.headers);return[2,(n.has("X-MICROCMS-API-KEY")||n.set("X-MICROCMS-API-KEY",e),fetch(t,p(f({},r),{headers:n})))]})});return function(e,r){return t.apply(this,arguments)}}()};var g="microcms.io",q="microcms-management.io",I="v1";var j=function(e){return e!==null&&typeof e=="object"},E=function(e){return typeof e=="string"};var P=function(e){if(!j(e))throw new Error("queries is not object");return new URLSearchParams(Object.entries(e).reduce(function(e,t){var r=h(t,2),n=r[0],i=r[1];return e[n]=String(i),e},{})).toString()};var O=function(e){var t=e.serviceDomain,r=e.apiKey,n=e.retry;if(!t||!r)throw new Error("parameter is required (check serviceDomain and apiKey)");if(!E(t)||!E(r))throw new Error("parameter is not string");var o="https://".concat(t,".").concat(g,"/api/").concat(I),u=function(){var e=i(function(e){var t,u,a,s,c,l,d,h,v;return m(this,function(y){switch(y.label){case 0:t=e.endpoint,u=e.contentId,a=e.queries,s=a===void 0?{}:a,c=e.requestInit;l=w(r),d=P(s),h="".concat(o,"/").concat(t).concat(u?"/".concat(u):"").concat(d?"?".concat(d):""),v=function(){var e=i(function(e){var t,r,n;return m(this,function(i){switch(i.label){case 0:i.trys.push([0,2,,3]);return[4,e.json()];case 1:t=i.sent(),r=t.message;return[2,r!==null&&r!==void 0?r:null];case 2:n=i.sent();return[2,null];case 3:return[2]}})});return function t(t){return e.apply(this,arguments)}}();return[4,b(function(){var e=i(function(e){var t,r,n,i,o,u,a;return m(this,function(s){switch(s.label){case 0:s.trys.push([0,6,,7]);return[4,l(h,p(f({},c),{method:(r=c===null||c===void 0?void 0:c.method)!==null&&r!==void 0?r:"GET"}))];case 1:if(!(t=s.sent(),t.status!==429&&t.status>=400&&t.status<500))return[3,3];return[4,v(t)];case 2:n=s.sent();return[2,e(new Error("fetch API response status: ".concat(t.status).concat(n?"\n message is `".concat(n,"`"):"")))];case 3:if(!!t.ok)return[3,5];return[4,v(t)];case 4:i=s.sent();return[2,Promise.reject(new Error("fetch API response status: ".concat(t.status).concat(i?"\n message is `".concat(i,"`"):"")))];case 5:return[2,(c===null||c===void 0?void 0:c.method)==="DELETE"?void 0:t.json()];case 6:o=s.sent();if(o.data)throw o.data;if((u=o.response)===null||u===void 0?void 0:u.data)throw o.response.data;return[2,Promise.reject(new Error("Network Error.\n Details: ".concat((a=o.message)!==null&&a!==void 0?a:"")))];case 7:return[2]}})});return function(t){return e.apply(this,arguments)}}(),{retries:n?2:0,onRetry:function(e,t){console.log(e),console.log("Waiting for retry (".concat(t,"/",2,")"))},minTimeout:5e3})];case 1:return[2,y.sent()]}})});return function t(t){return e.apply(this,arguments)}}();return{get:function(){var e=i(function(e){var t,r,n,i,o,a;return m(this,function(s){switch(s.label){case 0:t=e.endpoint,r=e.contentId,n=e.queries,i=n===void 0?{}:n,o=e.customRequestInit;if(!t)return[3,2];return[4,u({endpoint:t,contentId:r,queries:i,requestInit:o})];case 1:a=s.sent();return[3,3];case 2:a=Promise.reject(new Error("endpoint is required"));s.label=3;case 3:return[2,a]}})});return function(t){return e.apply(this,arguments)}}(),getList:function(){var e=i(function(e){var t,r,n,i,o;return m(this,function(a){switch(a.label){case 0:t=e.endpoint,r=e.queries,n=r===void 0?{}:r,i=e.customRequestInit;if(!t)return[3,2];return[4,u({endpoint:t,queries:n,requestInit:i})];case 1:o=a.sent();return[3,3];case 2:o=Promise.reject(new Error("endpoint is required"));a.label=3;case 3:return[2,o]}})});return function(t){return e.apply(this,arguments)}}(),getListDetail:function(){var e=i(function(e){var t,r,n,i,o,a;return m(this,function(s){switch(s.label){case 0:t=e.endpoint,r=e.contentId,n=e.queries,i=n===void 0?{}:n,o=e.customRequestInit;if(!t)return[3,2];return[4,u({endpoint:t,contentId:r,queries:i,requestInit:o})];case 1:a=s.sent();return[3,3];case 2:a=Promise.reject(new Error("endpoint is required"));s.label=3;case 3:return[2,a]}})});return function(t){return e.apply(this,arguments)}}(),getObject:function(){var e=i(function(e){var t,r,n,i,o;return m(this,function(a){switch(a.label){case 0:t=e.endpoint,r=e.queries,n=r===void 0?{}:r,i=e.customRequestInit;if(!t)return[3,2];return[4,u({endpoint:t,queries:n,requestInit:i})];case 1:o=a.sent();return[3,3];case 2:o=Promise.reject(new Error("endpoint is required"));a.label=3;case 3:return[2,o]}})});return function(t){return e.apply(this,arguments)}}(),getAllContentIds:function(){var e=i(function(e){var t,r,n,i,o,a,s,c,l,d,h,y,b,w,g,q,I;return m(this,function(m){switch(m.label){case 0:t=e.endpoint,r=e.alternateField,n=e.draftKey,i=e.filters,o=e.orders,a=e.customRequestInit;s={draftKey:n,filters:i,orders:o,limit:100,fields:r!==null&&r!==void 0?r:"id",depth:0};return[4,u({endpoint:t,queries:p(f({},s),{limit:0}),requestInit:a})];case 1:c=m.sent(),l=c.totalCount,d=[],h=0,y=function(e){return new Promise(function(t){return setTimeout(t,e)})},b=function(e){return e.every(function(e){return typeof e=="string"})};m.label=2;case 2:if(!(d.length<l))return[3,7];return[4,u({endpoint:t,queries:p(f({},s),{offset:h}),requestInit:a})];case 3:w=m.sent(),g=w.contents,q=g.map(function(e){return e[r!==null&&r!==void 0?r:"id"]});if(!b(q))throw new Error("The value of the field specified by `alternateField` is not a string.");d=v(d).concat(v(q)),h+=100;I=d.length<l;if(!I)return[3,5];return[4,y(1e3)];case 4:I=m.sent();m.label=5;case 5:I;m.label=6;case 6:return[3,2];case 7:return[2,d]}})});return function(t){return e.apply(this,arguments)}}(),getAllContents:function(){var e=i(function(e){var t,r,n,i,o,a,s,c,l,d,h,v;return m(this,function(y){switch(y.label){case 0:t=e.endpoint,r=e.queries,n=r===void 0?{}:r,i=e.customRequestInit;return[4,u({endpoint:t,queries:p(f({},n),{limit:0}),requestInit:i})];case 1:o=y.sent(),a=o.totalCount,s=[],c=0,l=function(e){return new Promise(function(t){return setTimeout(t,e)})};y.label=2;case 2:if(!(s.length<a))return[3,7];return[4,u({endpoint:t,queries:p(f({},n),{limit:100,offset:c}),requestInit:i})];case 3:d=y.sent(),h=d.contents;s=s.concat(h),c+=100;v=s.length<a;if(!v)return[3,5];return[4,l(1e3)];case 4:v=y.sent();y.label=5;case 5:v;y.label=6;case 6:return[3,2];case 7:return[2,s]}})});return function(t){return e.apply(this,arguments)}}(),create:function(){var e=i(function(e){var t,r,n,i,o,a,s,c;return m(this,function(l){t=e.endpoint,r=e.contentId,n=e.content,i=e.isDraft,o=i===void 0?!1:i,a=e.customRequestInit;if(!t)return[2,Promise.reject(new Error("endpoint is required"))];s=o?{status:"draft"}:{},c=p(f({},a),{method:r?"PUT":"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(n)});return[2,u({endpoint:t,contentId:r,queries:s,requestInit:c})]})});return function(t){return e.apply(this,arguments)}}(),update:function(){var e=i(function(e){var t,r,n,i,o;return m(this,function(a){t=e.endpoint,r=e.contentId,n=e.content,i=e.customRequestInit;if(!t)return[2,Promise.reject(new Error("endpoint is required"))];o=p(f({},i),{method:"PATCH",headers:{"Content-Type":"application/json"},body:JSON.stringify(n)});return[2,u({endpoint:t,contentId:r,requestInit:o})]})});return function(t){return e.apply(this,arguments)}}(),delete:function(){var e=i(function(e){var t,r,n,i;return m(this,function(o){switch(o.label){case 0:t=e.endpoint,r=e.contentId,n=e.customRequestInit;if(!t)return[2,Promise.reject(new Error("endpoint is required"))];if(!r)return[2,Promise.reject(new Error("contentId is required"))];i=p(f({},n),{method:"DELETE",headers:{},body:void 0});return[4,u({endpoint:t,contentId:r,requestInit:i})];case 1:o.sent();return[2]}})});return function(t){return e.apply(this,arguments)}}()}};var S=function(e){var t=e.serviceDomain,r=e.apiKey;if(!t||!r)throw new Error("parameter is required (check serviceDomain and apiKey)");if(!E(t)||!E(r))throw new Error("parameter is not string");var n=function(){var e=i(function(e){var n,o,u,a,s,c,l,d,h,v,y,b,g,I,j,E;return m(this,function(O){switch(O.label){case 0:n=e.path,o=e.apiVersion,u=e.queries,a=u===void 0?{}:u,s=e.requestInit;c="https://".concat(t,".").concat(q,"/api/").concat(o),l=w(r),d=P(a),h="".concat(c,"/").concat(n).concat(d?"?".concat(d):""),v=function(){var e=i(function(e){var t,r,n;return m(this,function(i){switch(i.label){case 0:i.trys.push([0,2,,3]);return[4,e.json()];case 1:t=i.sent(),r=t.message;return[2,r!==null&&r!==void 0?r:null];case 2:n=i.sent();return[2,null];case 3:return[2]}})});return function t(t){return e.apply(this,arguments)}}();O.label=1;case 1:O.trys.push([1,3,,4]);return[4,l(h,p(f({},s),{method:(b=s===null||s===void 0?void 0:s.method)!==null&&b!==void 0?b:"GET"}))];case 2:y=O.sent();return[3,4];case 3:g=O.sent();if(g.data)throw g.data;if((I=g.response)===null||I===void 0?void 0:I.data)throw g.response.data;return[2,Promise.reject(new Error("Network Error.\n Details: ".concat((j=g.message)!==null&&j!==void 0?j:"")))];case 4:if(!!y.ok)return[3,6];return[4,v(y)];case 5:E=O.sent();return[2,Promise.reject(new Error("fetch API response status: ".concat(y.status).concat(E?"\n message is `".concat(E,"`"):"")))];case 6:return[2,y.json()]}})});return function t(t){return e.apply(this,arguments)}}();return{uploadMedia:function(){var e=i(function(e){var t,r,i,o,a,s,c,l,f,d,p,h;return m(this,function(v){switch(v.label){case 0:t=e.data,r=e.name,i=e.type,o=e.customRequestHeaders;a=new FormData;if(!u(t,Blob))return[3,1];if(t.name)a.set("file",t,t.name);else{if(!r)throw new Error("name is required when data is a Blob");a.set("file",t,r)}return[3,9];case 1:if(!u(t,ReadableStream))return[3,6];if(!r)throw new Error("name is required when data is a ReadableStream");if(!i)throw new Error("type is required when data is a ReadableStream");s=[],c=t.getReader();v.label=2;case 2:return[4,c.read()];case 3:if(!!(l=v.sent()).done)return[3,5];s.push(l.value);v.label=4;case 4:return[3,2];case 5:a.set("file",new Blob(s,{type:i}),r);return[3,9];case 6:if(!(typeof t=="string"||u(t,URL)))return[3,9];f=u(t,URL)?t:new URL(t);return[4,fetch(f.toString(),o?{headers:o}:void 0)];case 7:d=v.sent();return[4,d.blob()];case 8:p=v.sent(),h=new URL(d.url).pathname.split("/").pop();a.set("file",p,r!==null&&r!==void 0?r:h);v.label=9;case 9:return[2,n({path:"media",apiVersion:I,requestInit:{method:"POST",body:a}})]}})});return function(t){return e.apply(this,arguments)}}()}};export{O as createClient,S as createManagementClient};//# sourceMappingURL=microcms-js-sdk.js.map
|
|
1
|
+
function e(e,t){if(t==null||t>e.length)t=e.length;for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function t(e){if(Array.isArray(e))return e}function r(t){if(Array.isArray(t))return e(t)}function n(e,t,r,n,i,o,u){try{var a=e[o](u);var s=a.value}catch(e){r(e);return}if(a.done){t(s)}else{Promise.resolve(s).then(n,i)}}function i(e){return function(){var t=this,r=arguments;return new Promise(function(i,o){var u=e.apply(t,r);function a(e){n(u,i,o,a,s,"next",e)}function s(e){n(u,i,o,a,s,"throw",e)}a(undefined)})}}function o(e,t,r){if(t in e){Object.defineProperty(e,t,{value:r,enumerable:true,configurable:true,writable:true})}else{e[t]=r}return e}function u(e,t){if(t!=null&&typeof Symbol!=="undefined"&&t[Symbol.hasInstance]){return!!t[Symbol.hasInstance](e)}else{return e instanceof t}}function a(e){if(typeof Symbol!=="undefined"&&e[Symbol.iterator]!=null||e["@@iterator"]!=null)return Array.from(e)}function s(e,t){var r=e==null?null:typeof Symbol!=="undefined"&&e[Symbol.iterator]||e["@@iterator"];if(r==null)return;var n=[];var i=true;var o=false;var u,a;try{for(r=r.call(e);!(i=(u=r.next()).done);i=true){n.push(u.value);if(t&&n.length===t)break}}catch(e){o=true;a=e}finally{try{if(!i&&r["return"]!=null)r["return"]()}finally{if(o)throw a}}return n}function c(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function l(){throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function f(e){for(var t=1;t<arguments.length;t++){var r=arguments[t]!=null?arguments[t]:{};var n=Object.keys(r);if(typeof Object.getOwnPropertySymbols==="function"){n=n.concat(Object.getOwnPropertySymbols(r).filter(function(e){return Object.getOwnPropertyDescriptor(r,e).enumerable}))}n.forEach(function(t){o(e,t,r[t])})}return e}function d(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);if(t){n=n.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})}r.push.apply(r,n)}return r}function p(e,t){t=t!=null?t:{};if(Object.getOwnPropertyDescriptors){Object.defineProperties(e,Object.getOwnPropertyDescriptors(t))}else{d(Object(t)).forEach(function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))})}return e}function h(e,r){return t(e)||s(e,r)||y(e,r)||c()}function v(e){return r(e)||a(e)||y(e)||l()}function y(t,r){if(!t)return;if(typeof t==="string")return e(t,r);var n=Object.prototype.toString.call(t).slice(8,-1);if(n==="Object"&&t.constructor)n=t.constructor.name;if(n==="Map"||n==="Set")return Array.from(n);if(n==="Arguments"||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return e(t,r)}function m(e,t){var r,n,i,o,u={label:0,sent:function(){if(i[0]&1)throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),"throw":a(1),"return":a(2)},typeof Symbol==="function"&&(o[Symbol.iterator]=function(){return this}),o;function a(e){return function(t){return s([e,t])}}function s(o){if(r)throw new TypeError("Generator is already executing.");while(u)try{if(r=1,n&&(i=o[0]&2?n["return"]:o[0]?n["throw"]||((i=n["return"])&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;if(n=0,i)o=[o[0]&2,i.value];switch(o[0]){case 0:case 1:i=o;break;case 4:u.label++;return{value:o[1],done:false};case 5:u.label++;n=o[1];o=[0];continue;case 7:o=u.ops.pop();u.trys.pop();continue;default:if(!(i=u.trys,i=i.length>0&&i[i.length-1])&&(o[0]===6||o[0]===2)){u=0;continue}if(o[0]===3&&(!i||o[1]>i[0]&&o[1]<i[3])){u.label=o[1];break}if(o[0]===6&&u.label<i[1]){u.label=i[1];i=o;break}if(i&&u.label<i[2]){u.label=i[2];u.ops.push(o);break}if(i[2])u.ops.pop();u.trys.pop();continue}o=t.call(e,u)}catch(e){o=[6,e];n=0}finally{r=i=0}if(o[0]&5)throw o[1];return{value:o[0]?o[1]:void 0,done:true}}}import b from"async-retry";var w=function(e){return function(){var t=i(function(t,r){var n;return m(this,function(i){n=new Headers(r===null||r===void 0?void 0:r.headers);return[2,(n.has("X-MICROCMS-API-KEY")||n.set("X-MICROCMS-API-KEY",e),fetch(t,p(f({},r),{headers:n})))]})});return function(e,r){return t.apply(this,arguments)}}()};var g="microcms.io",q="microcms-management.io",I="v1";var j=function(e){return e!==null&&typeof e=="object"},E=function(e){return typeof e=="string"};var P=function(e){if(!j(e))throw new Error("queries is not object");return new URLSearchParams(Object.entries(e).reduce(function(e,t){var r=h(t,2),n=r[0],i=r[1];return i!==void 0&&(e[n]=String(i)),e},{})).toString()};var O=function(e){var t=e.serviceDomain,r=e.apiKey,n=e.retry;if(!t||!r)throw new Error("parameter is required (check serviceDomain and apiKey)");if(!E(t)||!E(r))throw new Error("parameter is not string");var o="https://".concat(t,".").concat(g,"/api/").concat(I),u=function(){var e=i(function(e){var t,u,a,s,c,l,d,h,v;return m(this,function(y){switch(y.label){case 0:t=e.endpoint,u=e.contentId,a=e.queries,s=a===void 0?{}:a,c=e.requestInit;l=w(r),d=P(s),h="".concat(o,"/").concat(t).concat(u?"/".concat(u):"").concat(d?"?".concat(d):""),v=function(){var e=i(function(e){var t,r,n;return m(this,function(i){switch(i.label){case 0:i.trys.push([0,2,,3]);return[4,e.json()];case 1:t=i.sent(),r=t.message;return[2,r!==null&&r!==void 0?r:null];case 2:n=i.sent();return[2,null];case 3:return[2]}})});return function t(t){return e.apply(this,arguments)}}();return[4,b(function(){var e=i(function(e){var t,r,n,i,o,u,a;return m(this,function(s){switch(s.label){case 0:s.trys.push([0,6,,7]);return[4,l(h,p(f({},c),{method:(r=c===null||c===void 0?void 0:c.method)!==null&&r!==void 0?r:"GET"}))];case 1:if(!(t=s.sent(),t.status!==429&&t.status>=400&&t.status<500))return[3,3];return[4,v(t)];case 2:n=s.sent();return[2,e(new Error("fetch API response status: ".concat(t.status).concat(n?"\n message is `".concat(n,"`"):"")))];case 3:if(!!t.ok)return[3,5];return[4,v(t)];case 4:i=s.sent();return[2,Promise.reject(new Error("fetch API response status: ".concat(t.status).concat(i?"\n message is `".concat(i,"`"):"")))];case 5:return[2,(c===null||c===void 0?void 0:c.method)==="DELETE"?void 0:t.json()];case 6:o=s.sent();if(o.data)throw o.data;if((u=o.response)===null||u===void 0?void 0:u.data)throw o.response.data;return[2,Promise.reject(new Error("Network Error.\n Details: ".concat((a=o.message)!==null&&a!==void 0?a:"")))];case 7:return[2]}})});return function(t){return e.apply(this,arguments)}}(),{retries:n?2:0,onRetry:function(e,t){console.log(e),console.log("Waiting for retry (".concat(t,"/",2,")"))},minTimeout:5e3})];case 1:return[2,y.sent()]}})});return function t(t){return e.apply(this,arguments)}}();return{get:function(){var e=i(function(e){var t,r,n,i,o,a;return m(this,function(s){switch(s.label){case 0:t=e.endpoint,r=e.contentId,n=e.queries,i=n===void 0?{}:n,o=e.customRequestInit;if(!t)return[3,2];return[4,u({endpoint:t,contentId:r,queries:i,requestInit:o})];case 1:a=s.sent();return[3,3];case 2:a=Promise.reject(new Error("endpoint is required"));s.label=3;case 3:return[2,a]}})});return function(t){return e.apply(this,arguments)}}(),getList:function(){var e=i(function(e){var t,r,n,i,o;return m(this,function(a){switch(a.label){case 0:t=e.endpoint,r=e.queries,n=r===void 0?{}:r,i=e.customRequestInit;if(!t)return[3,2];return[4,u({endpoint:t,queries:n,requestInit:i})];case 1:o=a.sent();return[3,3];case 2:o=Promise.reject(new Error("endpoint is required"));a.label=3;case 3:return[2,o]}})});return function(t){return e.apply(this,arguments)}}(),getListDetail:function(){var e=i(function(e){var t,r,n,i,o,a;return m(this,function(s){switch(s.label){case 0:t=e.endpoint,r=e.contentId,n=e.queries,i=n===void 0?{}:n,o=e.customRequestInit;if(!t)return[3,2];return[4,u({endpoint:t,contentId:r,queries:i,requestInit:o})];case 1:a=s.sent();return[3,3];case 2:a=Promise.reject(new Error("endpoint is required"));s.label=3;case 3:return[2,a]}})});return function(t){return e.apply(this,arguments)}}(),getObject:function(){var e=i(function(e){var t,r,n,i,o;return m(this,function(a){switch(a.label){case 0:t=e.endpoint,r=e.queries,n=r===void 0?{}:r,i=e.customRequestInit;if(!t)return[3,2];return[4,u({endpoint:t,queries:n,requestInit:i})];case 1:o=a.sent();return[3,3];case 2:o=Promise.reject(new Error("endpoint is required"));a.label=3;case 3:return[2,o]}})});return function(t){return e.apply(this,arguments)}}(),getAllContentIds:function(){var e=i(function(e){var t,r,n,i,o,a,s,c,l,d,h,y,b,w,g,q,I;return m(this,function(m){switch(m.label){case 0:t=e.endpoint,r=e.alternateField,n=e.draftKey,i=e.filters,o=e.orders,a=e.customRequestInit;s={draftKey:n,filters:i,orders:o,limit:100,fields:r!==null&&r!==void 0?r:"id",depth:0};return[4,u({endpoint:t,queries:p(f({},s),{limit:0}),requestInit:a})];case 1:c=m.sent(),l=c.totalCount,d=[],h=0,y=function(e){return new Promise(function(t){return setTimeout(t,e)})},b=function(e){return e.every(function(e){return typeof e=="string"})};m.label=2;case 2:if(!(d.length<l))return[3,7];return[4,u({endpoint:t,queries:p(f({},s),{offset:h}),requestInit:a})];case 3:w=m.sent(),g=w.contents,q=g.map(function(e){return e[r!==null&&r!==void 0?r:"id"]});if(!b(q))throw new Error("The value of the field specified by `alternateField` is not a string.");d=v(d).concat(v(q)),h+=100;I=d.length<l;if(!I)return[3,5];return[4,y(1e3)];case 4:I=m.sent();m.label=5;case 5:I;m.label=6;case 6:return[3,2];case 7:return[2,d]}})});return function(t){return e.apply(this,arguments)}}(),getAllContents:function(){var e=i(function(e){var t,r,n,i,o,a,s,c,l,d,h,v;return m(this,function(y){switch(y.label){case 0:t=e.endpoint,r=e.queries,n=r===void 0?{}:r,i=e.customRequestInit;return[4,u({endpoint:t,queries:p(f({},n),{limit:0}),requestInit:i})];case 1:o=y.sent(),a=o.totalCount,s=[],c=0,l=function(e){return new Promise(function(t){return setTimeout(t,e)})};y.label=2;case 2:if(!(s.length<a))return[3,7];return[4,u({endpoint:t,queries:p(f({},n),{limit:100,offset:c}),requestInit:i})];case 3:d=y.sent(),h=d.contents;s=s.concat(h),c+=100;v=s.length<a;if(!v)return[3,5];return[4,l(1e3)];case 4:v=y.sent();y.label=5;case 5:v;y.label=6;case 6:return[3,2];case 7:return[2,s]}})});return function(t){return e.apply(this,arguments)}}(),create:function(){var e=i(function(e){var t,r,n,i,o,a,s,c;return m(this,function(l){t=e.endpoint,r=e.contentId,n=e.content,i=e.isDraft,o=i===void 0?!1:i,a=e.customRequestInit;if(!t)return[2,Promise.reject(new Error("endpoint is required"))];s=o?{status:"draft"}:{},c=p(f({},a),{method:r?"PUT":"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(n)});return[2,u({endpoint:t,contentId:r,queries:s,requestInit:c})]})});return function(t){return e.apply(this,arguments)}}(),update:function(){var e=i(function(e){var t,r,n,i,o;return m(this,function(a){t=e.endpoint,r=e.contentId,n=e.content,i=e.customRequestInit;if(!t)return[2,Promise.reject(new Error("endpoint is required"))];o=p(f({},i),{method:"PATCH",headers:{"Content-Type":"application/json"},body:JSON.stringify(n)});return[2,u({endpoint:t,contentId:r,requestInit:o})]})});return function(t){return e.apply(this,arguments)}}(),delete:function(){var e=i(function(e){var t,r,n,i;return m(this,function(o){switch(o.label){case 0:t=e.endpoint,r=e.contentId,n=e.customRequestInit;if(!t)return[2,Promise.reject(new Error("endpoint is required"))];if(!r)return[2,Promise.reject(new Error("contentId is required"))];i=p(f({},n),{method:"DELETE",headers:{},body:void 0});return[4,u({endpoint:t,contentId:r,requestInit:i})];case 1:o.sent();return[2]}})});return function(t){return e.apply(this,arguments)}}()}};var S=function(e){var t=e.serviceDomain,r=e.apiKey;if(!t||!r)throw new Error("parameter is required (check serviceDomain and apiKey)");if(!E(t)||!E(r))throw new Error("parameter is not string");var n=function(){var e=i(function(e){var n,o,u,a,s,c,l,d,h,v,y,b,g,I,j,E;return m(this,function(O){switch(O.label){case 0:n=e.path,o=e.apiVersion,u=e.queries,a=u===void 0?{}:u,s=e.requestInit;c="https://".concat(t,".").concat(q,"/api/").concat(o),l=w(r),d=P(a),h="".concat(c,"/").concat(n).concat(d?"?".concat(d):""),v=function(){var e=i(function(e){var t,r,n;return m(this,function(i){switch(i.label){case 0:i.trys.push([0,2,,3]);return[4,e.json()];case 1:t=i.sent(),r=t.message;return[2,r!==null&&r!==void 0?r:null];case 2:n=i.sent();return[2,null];case 3:return[2]}})});return function t(t){return e.apply(this,arguments)}}();O.label=1;case 1:O.trys.push([1,3,,4]);return[4,l(h,p(f({},s),{method:(b=s===null||s===void 0?void 0:s.method)!==null&&b!==void 0?b:"GET"}))];case 2:y=O.sent();return[3,4];case 3:g=O.sent();if(g.data)throw g.data;if((I=g.response)===null||I===void 0?void 0:I.data)throw g.response.data;return[2,Promise.reject(new Error("Network Error.\n Details: ".concat((j=g.message)!==null&&j!==void 0?j:"")))];case 4:if(!!y.ok)return[3,6];return[4,v(y)];case 5:E=O.sent();return[2,Promise.reject(new Error("fetch API response status: ".concat(y.status).concat(E?"\n message is `".concat(E,"`"):"")))];case 6:return[2,y.json()]}})});return function t(t){return e.apply(this,arguments)}}();return{uploadMedia:function(){var e=i(function(e){var t,r,i,o,a,s,c,l,f,d,p,h;return m(this,function(v){switch(v.label){case 0:t=e.data,r=e.name,i=e.type,o=e.customRequestHeaders;a=new FormData;if(!u(t,Blob))return[3,1];if(t.name)a.set("file",t,t.name);else{if(!r)throw new Error("name is required when data is a Blob");a.set("file",t,r)}return[3,9];case 1:if(!u(t,ReadableStream))return[3,6];if(!r)throw new Error("name is required when data is a ReadableStream");if(!i)throw new Error("type is required when data is a ReadableStream");s=[],c=t.getReader();v.label=2;case 2:return[4,c.read()];case 3:if(!!(l=v.sent()).done)return[3,5];s.push(l.value);v.label=4;case 4:return[3,2];case 5:a.set("file",new Blob(s,{type:i}),r);return[3,9];case 6:if(!(typeof t=="string"||u(t,URL)))return[3,9];f=u(t,URL)?t:new URL(t);return[4,fetch(f.toString(),o?{headers:o}:void 0)];case 7:d=v.sent();return[4,d.blob()];case 8:p=v.sent(),h=new URL(d.url).pathname.split("/").pop();a.set("file",p,r!==null&&r!==void 0?r:h);v.label=9;case 9:return[2,n({path:"media",apiVersion:I,requestInit:{method:"POST",body:a}})]}})});return function(t){return e.apply(this,arguments)}}()}};export{O as createClient,S as createManagementClient};//# sourceMappingURL=microcms-js-sdk.js.map
|