Skip to content

Commit 0394f67

Browse files
authored
Merge pull request #76 from microcmsio/feat/upload-media
feat: マネジメントAPIクライアント(画像アップロードについてのメソッドを追加)
2 parents c7b4003 + 012d9a7 commit 0394f67

File tree

11 files changed

+3217
-7271
lines changed

11 files changed

+3217
-7271
lines changed

.prettierignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

README.md

Lines changed: 130 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,29 @@ or
3636
First, create a client.
3737

3838
```javascript
39-
const { createClient } = require("microcms-js-sdk"); // CommonJS
39+
const { createClient } = require('microcms-js-sdk'); // CommonJS
4040
import { createClient } from 'microcms-js-sdk'; //ES6
4141

4242
// Initialize Client SDK.
4343
const client = createClient({
44-
serviceDomain: "YOUR_DOMAIN", // YOUR_DOMAIN is the XXXX part of XXXX.microcms.io
45-
apiKey: "YOUR_API_KEY",
46-
// retry: true // Retry attempts up to a maximum of two times.
44+
serviceDomain: 'YOUR_DOMAIN', // YOUR_DOMAIN is the XXXX part of XXXX.microcms.io
45+
apiKey: 'YOUR_API_KEY',
46+
// retry: true // Retry attempts up to a maximum of two times.
4747
});
4848
```
4949

5050
When using with a browser.
5151

5252
```html
5353
<script>
54-
const { createClient } = microcms;
55-
56-
// Initialize Client SDK.
57-
const client = createClient({
58-
serviceDomain: "YOUR_DOMAIN", // YOUR_DOMAIN is the XXXX part of XXXX.microcms.io
59-
apiKey: "YOUR_API_KEY",
60-
// retry: true // Retry attempts up to a maximum of two times.
61-
});
54+
const { createClient } = microcms;
55+
56+
// Initialize Client SDK.
57+
const client = createClient({
58+
serviceDomain: 'YOUR_DOMAIN', // YOUR_DOMAIN is the XXXX part of XXXX.microcms.io
59+
apiKey: 'YOUR_API_KEY',
60+
// retry: true // Retry attempts up to a maximum of two times.
61+
});
6262
</script>
6363
```
6464

@@ -115,7 +115,7 @@ client
115115
#### Get all content ids
116116

117117
This function can be used to retrieve all content IDs only.
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. \
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. \
119119
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.
120120

121121
```javascript
@@ -276,7 +276,7 @@ client
276276
.catch((err) => console.error(err));
277277
```
278278

279-
### TypeScript
279+
### TypeScript
280280

281281
If you are using TypeScript, use `getList`, `getListDetail`, `getObject`. This internally contains a common type of content.
282282

@@ -293,7 +293,7 @@ type Content = {
293293
* totalCount: number;
294294
* limit: number;
295295
* offset: number;
296-
* }
296+
* }
297297
*/
298298
client.getList<Content>({ //other })
299299

@@ -306,7 +306,7 @@ client.getList<Content>({ //other })
306306
* publishedAt?: string;
307307
* revisedAt?: string;
308308
* text: string; // This is Content type.
309-
* }
309+
* }
310310
*/
311311
client.getListDetail<Content>({ //other })
312312

@@ -318,7 +318,7 @@ client.getListDetail<Content>({ //other })
318318
* publishedAt?: string;
319319
* revisedAt?: string;
320320
* text: string; // This is Content type.
321-
* }
321+
* }
322322
*/
323323
client.getObject<Content>({ //other })
324324
```
@@ -337,9 +337,9 @@ Write functions can also be performed type-safely.
337337
338338
```typescript
339339
type Content = {
340-
title: string
341-
body?: string
342-
}
340+
title: string;
341+
body?: string;
342+
};
343343

344344
client.create<Content>({
345345
endpoint: 'endpoint',
@@ -348,15 +348,15 @@ client.create<Content>({
348348
title: 'title',
349349
body: 'body',
350350
},
351-
})
351+
});
352352

353353
client.update<Content>({
354354
endpoint: 'endpoint',
355355
// The `content` will be of type `Partial<Content>`, so you can enter only the items needed for the update.
356356
content: {
357357
body: 'body',
358358
},
359-
})
359+
});
360360
```
361361
362362
## CustomRequestInit
@@ -375,7 +375,7 @@ const response = await client.getList({
375375
revalidate: 60,
376376
},
377377
},
378-
endpoint: "endpoint",
378+
endpoint: 'endpoint',
379379
});
380380
```
381381
@@ -384,17 +384,117 @@ const response = await client.getList({
384384
You can abort fetch requests.
385385
386386
```ts
387-
const controller = new AbortController()
387+
const controller = new AbortController();
388388
const response = await client.getObject({
389389
customRequestInit: {
390-
signal: controller.signal
390+
signal: controller.signal,
391391
},
392-
endpoint: "config",
392+
endpoint: 'config',
393393
});
394394

395395
setTimeout(() => {
396-
controller.abort()
397-
}, 1000)
396+
controller.abort();
397+
}, 1000);
398+
```
399+
400+
## Management API
401+
402+
Clients can be created for the Management API.
403+
404+
### How to use
405+
406+
First, create a client.
407+
408+
```javascript
409+
import { createManagementClient } from 'microcms-js-sdk'; //ES6
410+
411+
// Initialize Client SDK.
412+
const client = createManagementClient({
413+
serviceDomain: 'YOUR_DOMAIN', // YOUR_DOMAIN is the XXXX part of XXXX.microcms.io
414+
apiKey: 'YOUR_API_KEY',
415+
});
416+
```
417+
418+
### UploadMedia API
419+
420+
Media files can be uploaded using the 'POST /api/v1/media' endpoint of the Management API.
421+
422+
#### Node.js
423+
424+
```javascript
425+
// Blob
426+
import { readFileSync } from 'fs';
427+
428+
const file = readFileSync('path/to/file');
429+
client
430+
.uploadMedia({
431+
data: new Blob([file], { type: 'image/png' }),
432+
name: 'image.png',
433+
})
434+
.then((res) => console.log(res))
435+
.catch((err) => console.error(err));
436+
437+
// or ReadableStream
438+
import { createReadStream } from 'fs';
439+
import { Stream } from 'stream';
440+
441+
const file = createReadStream('path/to/file');
442+
client
443+
.uploadMedia({
444+
data: Stream.Readable.toWeb(file),
445+
name: 'image.png',
446+
type: 'image/png',
447+
})
448+
.then((res) => console.log(res))
449+
.catch((err) => console.error(err));
450+
451+
// or URL
452+
client
453+
.uploadMedia({
454+
data: 'https://example.com/image.png',
455+
// name: 'image.png', ← Optional
456+
})
457+
.then((res) => console.log(res))
458+
.catch((err) => console.error(err));
459+
```
460+
461+
#### Browser
462+
463+
```javascript
464+
// File
465+
const file = document.querySelector('input[type="file"]').files[0];
466+
client
467+
.uploadMedia({
468+
data: file,
469+
})
470+
.then((res) => console.log(res))
471+
.catch((err) => console.error(err));
472+
473+
// or URL
474+
client
475+
.uploadMedia({
476+
data: 'https://example.com/image.png',
477+
// name: 'image.png', ← Optional
478+
})
479+
.then((res) => console.log(res))
480+
.catch((err) => console.error(err));
481+
```
482+
483+
### Type Definition
484+
485+
#### UploadMedia
486+
487+
```typescript
488+
type UploadMediaRequest =
489+
| { data: File }
490+
| { data: Blob; name: string }
491+
| { data: ReadableStream; name: string; type: `image/${string}` }
492+
| {
493+
data: URL | string;
494+
name?: string | null | undefined;
495+
customRequestHeaders?: HeadersInit;
496+
};
497+
function uploadMedia(params: UploadMediaRequest): Promise<{ url: string }>;
398498
```
399499
400500
## Tips
@@ -405,14 +505,13 @@ setTimeout(() => {
405505
const readClient = createClient({
406506
serviceDomain: 'serviceDomain',
407507
apiKey: 'readApiKey',
408-
})
508+
});
409509
const writeClient = createClient({
410510
serviceDomain: 'serviceDomain',
411511
apiKey: 'writeApiKey',
412-
})
512+
});
413513
```
414514
415-
416515
# LICENSE
417516
418517
Apache-2.0

0 commit comments

Comments
 (0)