Skip to content
This repository was archived by the owner on Sep 1, 2022. It is now read-only.

Commit 9afce94

Browse files
committed
docs(README): improve docs + upload examples
Browser and Node.js upload for #2
1 parent f12b4f7 commit 9afce94

File tree

1 file changed

+44
-15
lines changed

1 file changed

+44
-15
lines changed

README.md

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,57 @@ npm install strapi-sdk-javascript
2020

2121
## Start now
2222

23+
### New instance
2324
```js
2425
import Strapi from 'strapi-sdk-javascript';
2526

2627
const strapi = new Strapi('http://localhost:1337');
28+
```
29+
30+
### Authentications
31+
32+
#### Local
33+
```js
34+
await strapi.login('username_or_email', 's3cr3t');
35+
```
36+
37+
#### [Providers](https://strapi.io/documentation/guides/authentication.html#providers)
38+
```js
39+
// Redirect your user to the provider's authentication page.
40+
window.location = strapi.getProviderAuthenticationUrl('facebook');
41+
```
42+
Once authorized, the provider will redirects the user to your app with an access token in the URL.
43+
```js
44+
// Complete the authentication: (The SDK will store the access token for you)
45+
await strapi.authenticateProvider('facebook');
46+
```
47+
You can now fetch private APIs
48+
```js
49+
const posts = await strapi.getEntries('post');
50+
```
2751

28-
(async () => {
29-
// Local authentication
30-
await strapi.login('username_or_email', 's3cr3t');
31-
32-
// Or within provider
33-
window.location = strapi.getProviderAuthenticationUrl('facebook')
34-
// ...
35-
// Once authorized, Facebook redirects the user to your app with an access token in the URL.
36-
// Complete the authentication: (The SDK will handle the access token for you)
37-
await strapi.authenticateProvider('facebook')
38-
39-
// You can now fetch private APIs
40-
const posts = await strapi.getEntries('post');
41-
console.log(`Posts count: ${posts.length}`);
42-
})();
52+
### Files management
53+
54+
#### Browser
55+
```js
56+
const form = new FormData();
57+
form.append('files', fileInputElement.files[0], 'file-name.ext');
58+
form.append('files', fileInputElement.files[1], 'file-2-name.ext');
59+
const files = await strapi.upload(form);
4360
```
4461

62+
#### Node.js
63+
```js
64+
const FormData = require('form-data');
65+
const fs = require('fs');
66+
const form = new FormData();
67+
form.append('files', fs.createReadStream('./file-name.ext'), 'file-name.ext');
68+
const files = await strapi.upload(form, {
69+
headers: form.getHeaders()
70+
});
71+
```
72+
73+
4574
## API
4675

4776
### `Strapi(baseURL, storeConfig, requestConfig)`

0 commit comments

Comments
 (0)