strapi-sdk-js 2.1.5 → 2.2.0
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/CHANGELOG.md +2 -0
- package/README.md +11 -4
- package/dist/index.cjs +23 -24
- package/dist/index.d.ts +6 -15
- package/dist/index.mjs +23 -24
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# [2.2.0](https://github.com/Stun3R/strapi-sdk-js/compare/v2.1.5...v2.2.0) (2022-02-17)
|
|
2
|
+
|
|
1
3
|
## [2.1.5](https://github.com/Stun3R/strapi-sdk-js/compare/v2.1.4...v2.1.5) (2022-01-17)
|
|
2
4
|
|
|
3
5
|
## [2.1.4](https://github.com/Stun3R/strapi-sdk-js/compare/v2.1.3...v2.1.4) (2022-01-14)
|
package/README.md
CHANGED
|
@@ -15,29 +15,36 @@
|
|
|
15
15
|
|
|
16
16
|
## 🚀 Features
|
|
17
17
|
|
|
18
|
+
- Strapi v4 supports
|
|
18
19
|
- RESTful methods
|
|
19
20
|
- Simplified request responses
|
|
20
21
|
- Formatted error handling
|
|
21
22
|
|
|
22
23
|
[📚 Documentation](https://strapi-sdk-js.netlify.app)
|
|
23
24
|
|
|
25
|
+
> If you are using Strapi **v3**, check out [here](https://v1-strapi-sdk-js.netlify.app) the documentation of the SDK `v1`
|
|
26
|
+
|
|
27
|
+
## ⚙️ Contributing
|
|
28
|
+
|
|
29
|
+
1. Clone the repository
|
|
30
|
+
2. Move on `develop` branch
|
|
31
|
+
3. You will need to install dependencies thanks `npm install` or `yarn install`
|
|
32
|
+
4. To start development you can use `yarn start` in order to enable build watch or `yarn build`
|
|
33
|
+
5. After you finished your change be sure to open a PR against `develop` branch
|
|
24
34
|
|
|
25
35
|
## License
|
|
26
36
|
|
|
27
37
|
[MIT License](./LICENSE)
|
|
28
38
|
|
|
29
39
|
<!-- Badges -->
|
|
40
|
+
|
|
30
41
|
[actions-src]: https://github.com/Stun3R/strapi-sdk-js/actions/workflows/main.yml/badge.svg
|
|
31
42
|
[actions-href]: https://github.com/Stun3R/strapi-sdk-js/actions/workflows/main.yml
|
|
32
|
-
|
|
33
43
|
[codecov-src]: https://img.shields.io/codecov/c/github/Stun3R/strapi-sdk-js.svg?style=flat-square
|
|
34
44
|
[codecov-href]: https://codecov.io/gh/Stun3R/strapi-sdk-js
|
|
35
|
-
|
|
36
45
|
[npm-version-src]: https://img.shields.io/npm/v/strapi-sdk-js/latest.svg?style=flat-square
|
|
37
46
|
[npm-version-href]: https://npmjs.com/package/strapi-sdk-js
|
|
38
|
-
|
|
39
47
|
[npm-downloads-src]: https://img.shields.io/npm/dt/strapi-sdk-js.svg?style=flat-square
|
|
40
48
|
[npm-downloads-href]: https://npmjs.com/package/strapi-sdk-js
|
|
41
|
-
|
|
42
49
|
[license-src]: https://img.shields.io/github/license/stun3r/strapi-sdk-js.svg?style=flat-square
|
|
43
50
|
[license-href]: ./LICENSE
|
package/dist/index.cjs
CHANGED
|
@@ -28,20 +28,23 @@ const defaults = {
|
|
|
28
28
|
};
|
|
29
29
|
class Strapi {
|
|
30
30
|
constructor(options) {
|
|
31
|
-
this.
|
|
31
|
+
this.user = null;
|
|
32
32
|
this.options = defu__default["default"](options || {}, defaults);
|
|
33
33
|
this.axios = axios__default["default"].create({
|
|
34
34
|
baseURL: new URL(this.options.prefix, this.options.url).href,
|
|
35
35
|
paramsSerializer: qs__default["default"].stringify,
|
|
36
36
|
...this.options.axiosOptions
|
|
37
37
|
});
|
|
38
|
-
this.
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
38
|
+
this.axios.interceptors.request.use((config) => {
|
|
39
|
+
const token = this.getToken();
|
|
40
|
+
if (token) {
|
|
41
|
+
config.headers = {
|
|
42
|
+
...config.headers,
|
|
43
|
+
Authorization: `Bearer ${token}`
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
return config;
|
|
47
|
+
});
|
|
45
48
|
}
|
|
46
49
|
async request(method, url, axiosConfig) {
|
|
47
50
|
try {
|
|
@@ -74,7 +77,7 @@ class Strapi {
|
|
|
74
77
|
data
|
|
75
78
|
});
|
|
76
79
|
this.setToken(jwt);
|
|
77
|
-
this.
|
|
80
|
+
this.user = user;
|
|
78
81
|
return { user, jwt };
|
|
79
82
|
}
|
|
80
83
|
async register(data) {
|
|
@@ -83,7 +86,7 @@ class Strapi {
|
|
|
83
86
|
data
|
|
84
87
|
});
|
|
85
88
|
this.setToken(jwt);
|
|
86
|
-
this.
|
|
89
|
+
this.user = user;
|
|
87
90
|
return { user, jwt };
|
|
88
91
|
}
|
|
89
92
|
async forgotPassword(data) {
|
|
@@ -96,7 +99,7 @@ class Strapi {
|
|
|
96
99
|
data
|
|
97
100
|
});
|
|
98
101
|
this.setToken(jwt);
|
|
99
|
-
this.
|
|
102
|
+
this.user = user;
|
|
100
103
|
return { user, jwt };
|
|
101
104
|
}
|
|
102
105
|
async sendEmailConfirmation(data) {
|
|
@@ -120,11 +123,11 @@ class Strapi {
|
|
|
120
123
|
params: { access_token }
|
|
121
124
|
});
|
|
122
125
|
this.setToken(jwt);
|
|
123
|
-
this.
|
|
126
|
+
this.user = user;
|
|
124
127
|
return { user, jwt };
|
|
125
128
|
}
|
|
126
129
|
logout() {
|
|
127
|
-
this.
|
|
130
|
+
this.user = null;
|
|
128
131
|
this.removeToken();
|
|
129
132
|
}
|
|
130
133
|
find(contentType, params) {
|
|
@@ -154,37 +157,33 @@ class Strapi {
|
|
|
154
157
|
params
|
|
155
158
|
});
|
|
156
159
|
}
|
|
157
|
-
setUser(user) {
|
|
158
|
-
this._user = user;
|
|
159
|
-
}
|
|
160
160
|
async fetchUser() {
|
|
161
161
|
try {
|
|
162
162
|
const user = await this.request("get", "/users/me");
|
|
163
|
-
this.
|
|
163
|
+
this.user = user;
|
|
164
164
|
} catch (e) {
|
|
165
165
|
this.logout();
|
|
166
166
|
}
|
|
167
|
-
return this.
|
|
167
|
+
return this.user;
|
|
168
168
|
}
|
|
169
|
-
|
|
169
|
+
getToken() {
|
|
170
170
|
const { useLocalStorage, key } = this.options.store;
|
|
171
171
|
if (isBrowser()) {
|
|
172
172
|
const token = useLocalStorage ? window.localStorage.getItem(key) : Cookies__default["default"].get(key);
|
|
173
|
-
if (token)
|
|
174
|
-
|
|
175
|
-
|
|
173
|
+
if (typeof token === "undefined")
|
|
174
|
+
return null;
|
|
175
|
+
return token;
|
|
176
176
|
}
|
|
177
|
+
return null;
|
|
177
178
|
}
|
|
178
179
|
setToken(token) {
|
|
179
180
|
const { useLocalStorage, key, cookieOptions } = this.options.store;
|
|
180
|
-
this.axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
|
181
181
|
if (isBrowser()) {
|
|
182
182
|
useLocalStorage ? window.localStorage.setItem(key, token) : Cookies__default["default"].set(key, token, cookieOptions);
|
|
183
183
|
}
|
|
184
184
|
}
|
|
185
185
|
removeToken() {
|
|
186
186
|
const { useLocalStorage, key } = this.options.store;
|
|
187
|
-
delete this.axios.defaults.headers.common["Authorization"];
|
|
188
187
|
if (isBrowser()) {
|
|
189
188
|
useLocalStorage ? window.localStorage.removeItem(key) : Cookies__default["default"].remove(key);
|
|
190
189
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -83,7 +83,7 @@ interface StrapiEmailConfirmationData {
|
|
|
83
83
|
declare class Strapi {
|
|
84
84
|
axios: AxiosInstance;
|
|
85
85
|
options: StrapiDefaultOptions;
|
|
86
|
-
|
|
86
|
+
user: StrapiUser;
|
|
87
87
|
/**
|
|
88
88
|
* Strapi SDK Constructor
|
|
89
89
|
*
|
|
@@ -94,8 +94,6 @@ declare class Strapi {
|
|
|
94
94
|
* @param {AxiosRequestConfig} options.axiosOptions? - The list of your Content type on your Strapi API
|
|
95
95
|
*/
|
|
96
96
|
constructor(options?: StrapiOptions);
|
|
97
|
-
get user(): StrapiUser;
|
|
98
|
-
set user(user: StrapiUser);
|
|
99
97
|
/**
|
|
100
98
|
* Basic axios request
|
|
101
99
|
*
|
|
@@ -216,13 +214,6 @@ declare class Strapi {
|
|
|
216
214
|
* @returns Promise<StrapiResponse<T>>
|
|
217
215
|
*/
|
|
218
216
|
delete<T>(contentType: string, id: string | number, params?: StrapiBaseRequestParams): Promise<StrapiResponse<T>>;
|
|
219
|
-
/**
|
|
220
|
-
* Define local data of the logged-in user
|
|
221
|
-
*
|
|
222
|
-
* @param {StrapiUser} user - New user data
|
|
223
|
-
* @returns void
|
|
224
|
-
*/
|
|
225
|
-
setUser(user: StrapiUser): void;
|
|
226
217
|
/**
|
|
227
218
|
* Refresh local data of the logged-in user
|
|
228
219
|
*
|
|
@@ -230,20 +221,20 @@ declare class Strapi {
|
|
|
230
221
|
*/
|
|
231
222
|
fetchUser(): Promise<StrapiUser>;
|
|
232
223
|
/**
|
|
233
|
-
*
|
|
224
|
+
* Retrieve token from chosen storage
|
|
234
225
|
*
|
|
235
|
-
* @returns
|
|
226
|
+
* @returns string | null
|
|
236
227
|
*/
|
|
237
|
-
|
|
228
|
+
getToken(): string | null;
|
|
238
229
|
/**
|
|
239
|
-
* Set token in
|
|
230
|
+
* Set token in chosen storage
|
|
240
231
|
*
|
|
241
232
|
* @param {string} token - Token retrieve from login or register method
|
|
242
233
|
* @returns void
|
|
243
234
|
*/
|
|
244
235
|
setToken(token: string): void;
|
|
245
236
|
/**
|
|
246
|
-
* Remove token
|
|
237
|
+
* Remove token from chosen storage (Cookies or Local)
|
|
247
238
|
*
|
|
248
239
|
* @returns void
|
|
249
240
|
*/
|
package/dist/index.mjs
CHANGED
|
@@ -19,20 +19,23 @@ const defaults = {
|
|
|
19
19
|
};
|
|
20
20
|
class Strapi {
|
|
21
21
|
constructor(options) {
|
|
22
|
-
this.
|
|
22
|
+
this.user = null;
|
|
23
23
|
this.options = defu(options || {}, defaults);
|
|
24
24
|
this.axios = axios.create({
|
|
25
25
|
baseURL: new URL(this.options.prefix, this.options.url).href,
|
|
26
26
|
paramsSerializer: qs.stringify,
|
|
27
27
|
...this.options.axiosOptions
|
|
28
28
|
});
|
|
29
|
-
this.
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
29
|
+
this.axios.interceptors.request.use((config) => {
|
|
30
|
+
const token = this.getToken();
|
|
31
|
+
if (token) {
|
|
32
|
+
config.headers = {
|
|
33
|
+
...config.headers,
|
|
34
|
+
Authorization: `Bearer ${token}`
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
return config;
|
|
38
|
+
});
|
|
36
39
|
}
|
|
37
40
|
async request(method, url, axiosConfig) {
|
|
38
41
|
try {
|
|
@@ -65,7 +68,7 @@ class Strapi {
|
|
|
65
68
|
data
|
|
66
69
|
});
|
|
67
70
|
this.setToken(jwt);
|
|
68
|
-
this.
|
|
71
|
+
this.user = user;
|
|
69
72
|
return { user, jwt };
|
|
70
73
|
}
|
|
71
74
|
async register(data) {
|
|
@@ -74,7 +77,7 @@ class Strapi {
|
|
|
74
77
|
data
|
|
75
78
|
});
|
|
76
79
|
this.setToken(jwt);
|
|
77
|
-
this.
|
|
80
|
+
this.user = user;
|
|
78
81
|
return { user, jwt };
|
|
79
82
|
}
|
|
80
83
|
async forgotPassword(data) {
|
|
@@ -87,7 +90,7 @@ class Strapi {
|
|
|
87
90
|
data
|
|
88
91
|
});
|
|
89
92
|
this.setToken(jwt);
|
|
90
|
-
this.
|
|
93
|
+
this.user = user;
|
|
91
94
|
return { user, jwt };
|
|
92
95
|
}
|
|
93
96
|
async sendEmailConfirmation(data) {
|
|
@@ -111,11 +114,11 @@ class Strapi {
|
|
|
111
114
|
params: { access_token }
|
|
112
115
|
});
|
|
113
116
|
this.setToken(jwt);
|
|
114
|
-
this.
|
|
117
|
+
this.user = user;
|
|
115
118
|
return { user, jwt };
|
|
116
119
|
}
|
|
117
120
|
logout() {
|
|
118
|
-
this.
|
|
121
|
+
this.user = null;
|
|
119
122
|
this.removeToken();
|
|
120
123
|
}
|
|
121
124
|
find(contentType, params) {
|
|
@@ -145,37 +148,33 @@ class Strapi {
|
|
|
145
148
|
params
|
|
146
149
|
});
|
|
147
150
|
}
|
|
148
|
-
setUser(user) {
|
|
149
|
-
this._user = user;
|
|
150
|
-
}
|
|
151
151
|
async fetchUser() {
|
|
152
152
|
try {
|
|
153
153
|
const user = await this.request("get", "/users/me");
|
|
154
|
-
this.
|
|
154
|
+
this.user = user;
|
|
155
155
|
} catch (e) {
|
|
156
156
|
this.logout();
|
|
157
157
|
}
|
|
158
|
-
return this.
|
|
158
|
+
return this.user;
|
|
159
159
|
}
|
|
160
|
-
|
|
160
|
+
getToken() {
|
|
161
161
|
const { useLocalStorage, key } = this.options.store;
|
|
162
162
|
if (isBrowser()) {
|
|
163
163
|
const token = useLocalStorage ? window.localStorage.getItem(key) : Cookies.get(key);
|
|
164
|
-
if (token)
|
|
165
|
-
|
|
166
|
-
|
|
164
|
+
if (typeof token === "undefined")
|
|
165
|
+
return null;
|
|
166
|
+
return token;
|
|
167
167
|
}
|
|
168
|
+
return null;
|
|
168
169
|
}
|
|
169
170
|
setToken(token) {
|
|
170
171
|
const { useLocalStorage, key, cookieOptions } = this.options.store;
|
|
171
|
-
this.axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
|
172
172
|
if (isBrowser()) {
|
|
173
173
|
useLocalStorage ? window.localStorage.setItem(key, token) : Cookies.set(key, token, cookieOptions);
|
|
174
174
|
}
|
|
175
175
|
}
|
|
176
176
|
removeToken() {
|
|
177
177
|
const { useLocalStorage, key } = this.options.store;
|
|
178
|
-
delete this.axios.defaults.headers.common["Authorization"];
|
|
179
178
|
if (isBrowser()) {
|
|
180
179
|
useLocalStorage ? window.localStorage.removeItem(key) : Cookies.remove(key);
|
|
181
180
|
}
|