strapi-sdk-js 2.1.5 → 2.3.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/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
@@ -1,16 +1,10 @@
1
1
  'use strict';
2
2
 
3
- const axios = require('axios');
4
- const defu = require('defu');
5
- const qs = require('qs');
6
- const Cookies = require('js-cookie');
7
-
8
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
-
10
- const axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
11
- const defu__default = /*#__PURE__*/_interopDefaultLegacy(defu);
12
- const qs__default = /*#__PURE__*/_interopDefaultLegacy(qs);
13
- const Cookies__default = /*#__PURE__*/_interopDefaultLegacy(Cookies);
3
+ var axios = require('axios');
4
+ var defu = require('defu');
5
+ var qs = require('qs');
6
+ var Cookies = require('js-cookie');
7
+ var ufo = require('ufo');
14
8
 
15
9
  function isBrowser() {
16
10
  return typeof window !== "undefined";
@@ -27,22 +21,49 @@ const defaults = {
27
21
  axiosOptions: {}
28
22
  };
29
23
  class Strapi {
24
+ axios;
25
+ options;
26
+ user = null;
27
+ /**
28
+ * Strapi SDK Constructor
29
+ *
30
+ * @constructor
31
+ * @param {StrapiOptions} options? - Options in order to configure API URL, list your Content Types & extend the axios configuration
32
+ * @param {string} options.url? - Your Strapi API URL, Default: http://localhost::1337
33
+ * @param {StoreConfig} options.store? - Config the way you want to store JWT (Cookie or LocalStorage)
34
+ * @param {AxiosRequestConfig} options.axiosOptions? - The list of your Content type on your Strapi API
35
+ */
30
36
  constructor(options) {
31
- this._user = null;
32
- this.options = defu__default["default"](options || {}, defaults);
33
- this.axios = axios__default["default"].create({
34
- baseURL: new URL(this.options.prefix, this.options.url).href,
35
- paramsSerializer: qs__default["default"].stringify,
37
+ const _options = defu(options || {}, defaults);
38
+ this.options = {
39
+ ..._options,
40
+ url: ufo.cleanDoubleSlashes(_options?.url),
41
+ prefix: ufo.cleanDoubleSlashes(_options?.prefix)
42
+ };
43
+ this.axios = axios.create({
44
+ baseURL: ufo.joinURL(this.options.url, this.options.prefix),
45
+ paramsSerializer: qs.stringify,
36
46
  ...this.options.axiosOptions
37
47
  });
38
- this.syncToken();
39
- }
40
- get user() {
41
- return this._user;
42
- }
43
- set user(user) {
44
- this._user = user;
48
+ this.axios.interceptors.request.use((config) => {
49
+ const token = this.getToken();
50
+ if (token) {
51
+ config.headers = {
52
+ ...config.headers,
53
+ Authorization: `Bearer ${token}`
54
+ };
55
+ }
56
+ return config;
57
+ });
45
58
  }
59
+ /**
60
+ * Basic axios request
61
+ *
62
+ * @param {Method} method - HTTP method
63
+ * @param {string} url - Custom or Strapi API URL
64
+ * @param {AxiosRequestConfig} axiosConfig? - Custom Axios config
65
+ * @returns Promise<T>
66
+ */
46
67
  async request(method, url, axiosConfig) {
47
68
  try {
48
69
  const response = await this.axios.request({
@@ -68,125 +89,274 @@ class Strapi {
68
89
  }
69
90
  }
70
91
  }
92
+ /**
93
+ * Authenticate user & retrieve his JWT
94
+ *
95
+ * @param {StrapiAuthenticationData} data - User authentication form data: `identifier`, `password`
96
+ * @param {string} data.identifier - The email or username of the user
97
+ * @param {string} data.password - The password of the user
98
+ * @returns Promise<StrapiAuthenticationResponse>
99
+ */
71
100
  async login(data) {
72
101
  this.removeToken();
73
102
  const { user, jwt } = await this.request("post", "/auth/local", {
74
103
  data
75
104
  });
76
105
  this.setToken(jwt);
77
- this.setUser(user);
106
+ this.user = user;
78
107
  return { user, jwt };
79
108
  }
109
+ /**
110
+ * Register a new user & retrieve JWT
111
+ *
112
+ * @param {StrapiRegistrationData} data - New user registration data: `username`, `email`, `password`
113
+ * @param {string} data.username - Username of the new user
114
+ * @param {string} data.email - Email of the new user
115
+ * @param {string} data.password - Password of the new user
116
+ * @returns Promise<StrapiAuthenticationResponse>
117
+ */
80
118
  async register(data) {
81
119
  this.removeToken();
82
- const { user, jwt } = await this.request("post", "/auth/local/register", {
83
- data
84
- });
120
+ const { user, jwt } = await this.request(
121
+ "post",
122
+ "/auth/local/register",
123
+ {
124
+ data
125
+ }
126
+ );
85
127
  this.setToken(jwt);
86
- this.setUser(user);
128
+ this.user = user;
87
129
  return { user, jwt };
88
130
  }
131
+ /**
132
+ * Send an email to a user in order to reset his password
133
+ *
134
+ * @param {StrapiForgotPasswordData} data - Forgot password data: `email`
135
+ * @param {string} data.email - Email of the user who forgot his password
136
+ * @returns Promise<void>
137
+ */
89
138
  async forgotPassword(data) {
90
139
  this.removeToken();
91
140
  return this.request("post", "/auth/forgot-password", { data });
92
141
  }
142
+ /**
143
+ * Change the password of the logged in user.
144
+ * @author AnnikenYT
145
+ *
146
+ * @param {StrapiChangePasswordData} data - Change password data: `currentPassword`, `password`, `passwordConfirmation`
147
+ * @param {string} data.currentPassword - The current password of the user
148
+ * @param {string} data.password - The new password of the user
149
+ * @param {string} data.passwordConfirmation - Confirmation of the new password of the user
150
+ * @returns Promise<StrapiAuthenticationResponse>
151
+ */
152
+ async changePassword(data) {
153
+ const { user, jwt } = await this.request(
154
+ "post",
155
+ "/auth/change-password",
156
+ {
157
+ data
158
+ }
159
+ );
160
+ this.setToken(jwt);
161
+ this.user = user;
162
+ return { jwt, user };
163
+ }
164
+ /**
165
+ * Reset the user password
166
+ *
167
+ * @param {StrapiResetPasswordData} data - Reset password data object: `code`, `password`, `passwordConfirmation`
168
+ * @param {string} data.code - Code received by email after calling the `forgotPassword` method
169
+ * @param {string} data.password - New password of the user
170
+ * @param {string} data.passwordConfirmation - Confirmation of the new password of the user
171
+ * @returns Promise<StrapiAuthenticationResponse>
172
+ */
93
173
  async resetPassword(data) {
94
174
  this.removeToken();
95
- const { user, jwt } = await this.request("post", "/auth/reset-password", {
96
- data
97
- });
175
+ const { user, jwt } = await this.request(
176
+ "post",
177
+ "/auth/reset-password",
178
+ {
179
+ data
180
+ }
181
+ );
98
182
  this.setToken(jwt);
99
- this.setUser(user);
183
+ this.user = user;
100
184
  return { user, jwt };
101
185
  }
186
+ /**
187
+ * Send programmatically an email to a user in order to confirm his account
188
+ *
189
+ * @param {StrapiEmailConfirmationData} data - Email confirmation data: `email`
190
+ * @param {string} data.email - Email of the user who want to be confirmed
191
+ * @returns Promise<void>
192
+ */
102
193
  async sendEmailConfirmation(data) {
103
194
  return this.request("post", "/auth/send-email-confirmation", {
104
195
  data
105
196
  });
106
197
  }
198
+ /**
199
+ * Get the correct URL to authenticate with provider
200
+ *
201
+ * @param {StrapiAuthProvider} provider - Provider name
202
+ * @returns string
203
+ */
107
204
  getProviderAuthenticationUrl(provider) {
108
- return new URL(`/connect/${provider}`, this.options.url).href;
205
+ return ufo.joinURL(this.options.url, this.options.prefix, "connect", provider);
109
206
  }
207
+ /**
208
+ * Authenticate user with the token present on the URL or in `params`
209
+ *
210
+ * @param {StrapiAuthProvider} provider - Provider name
211
+ * @param {string} access_token? - Access Token return from Strapi
212
+ * @returns Promise<StrapiAuthenticationResponse>
213
+ */
110
214
  async authenticateProvider(provider, access_token) {
111
215
  this.removeToken();
112
216
  if (isBrowser()) {
113
- const params = qs__default["default"].parse(window.location.search, {
217
+ const params = qs.parse(window.location.search, {
114
218
  ignoreQueryPrefix: true
115
219
  });
116
220
  if (params.access_token)
117
221
  access_token = params.access_token;
118
222
  }
119
- const { user, jwt } = await this.request("get", `/auth/${provider}/callback`, {
120
- params: { access_token }
121
- });
223
+ const { user, jwt } = await this.request(
224
+ "get",
225
+ `/auth/${provider}/callback`,
226
+ {
227
+ params: { access_token }
228
+ }
229
+ );
122
230
  this.setToken(jwt);
123
- this.setUser(user);
231
+ this.user = user;
124
232
  return { user, jwt };
125
233
  }
234
+ /**
235
+ * Logout by removing authentication token
236
+ *
237
+ * @returns void
238
+ */
126
239
  logout() {
127
- this.setUser(null);
240
+ this.user = null;
128
241
  this.removeToken();
129
242
  }
243
+ /**
244
+ * Get a list of {content-type} entries
245
+ *
246
+ * @param {string} contentType - Content type's name pluralized
247
+ * @param {StrapiRequestParams} params? - Query parameters
248
+ * @returns Promise<StrapiResponse<T>>
249
+ */
130
250
  find(contentType, params) {
131
251
  return this.request("get", `/${contentType}`, {
132
252
  params
133
253
  });
134
254
  }
255
+ /**
256
+ * Get a specific {content-type} entry
257
+ *
258
+ * @param {string} contentType - Content type's name pluralized
259
+ * @param {string|number} id - ID of entry
260
+ * @param {StrapiBaseRequestParams} params? - Fields selection & Relations population
261
+ * @returns Promise<StrapiResponse<T>>
262
+ */
135
263
  findOne(contentType, id, params) {
136
264
  return this.request("get", `/${contentType}/${id}`, {
137
265
  params
138
266
  });
139
267
  }
268
+ /**
269
+ * Create a {content-type} entry
270
+ *
271
+ * @param {string} contentType - Content type's name pluralized
272
+ * @param {AxiosRequestConfig["data"]} data - New entry
273
+ * @param {StrapiBaseRequestParams} params? - Fields selection & Relations population
274
+ * @returns Promise<StrapiResponse<T>>
275
+ */
140
276
  create(contentType, data, params) {
141
277
  return this.request("post", `/${contentType}`, {
142
278
  data: { data },
143
279
  params
144
280
  });
145
281
  }
282
+ /**
283
+ * Update a specific entry
284
+ *
285
+ * @param {string} contentType - Content type's name pluralized
286
+ * @param {string|number} id - ID of entry to be updated
287
+ * @param {AxiosRequestConfig["data"]} data - New entry data
288
+ * @param {StrapiBaseRequestParams} params? - Fields selection & Relations population
289
+ * @returns Promise<StrapiResponse<T>>
290
+ */
146
291
  update(contentType, id, data, params) {
147
292
  return this.request("put", `/${contentType}/${id}`, {
148
293
  data: { data },
149
294
  params
150
295
  });
151
296
  }
297
+ /**
298
+ * Delete en entry
299
+ *
300
+ * @param {string} contentType - Content type's name pluralized
301
+ * @param {string|number} id - ID of entry to be deleted
302
+ * @param {StrapiBaseRequestParams} params? - Fields selection & Relations population
303
+ * @returns Promise<StrapiResponse<T>>
304
+ */
152
305
  delete(contentType, id, params) {
153
306
  return this.request("delete", `/${contentType}/${id}`, {
154
307
  params
155
308
  });
156
309
  }
157
- setUser(user) {
158
- this._user = user;
159
- }
310
+ /**
311
+ * Refresh local data of the logged-in user
312
+ *
313
+ * @returns Promise<StrapiUser>
314
+ */
160
315
  async fetchUser() {
161
316
  try {
162
317
  const user = await this.request("get", "/users/me");
163
- this.setUser(user);
318
+ this.user = user;
164
319
  } catch (e) {
165
320
  this.logout();
166
321
  }
167
- return this._user;
322
+ return this.user;
168
323
  }
169
- syncToken() {
324
+ /**
325
+ * Retrieve token from chosen storage
326
+ *
327
+ * @returns string | null
328
+ */
329
+ getToken() {
170
330
  const { useLocalStorage, key } = this.options.store;
171
331
  if (isBrowser()) {
172
- const token = useLocalStorage ? window.localStorage.getItem(key) : Cookies__default["default"].get(key);
173
- if (token) {
174
- this.axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
175
- }
332
+ const token = useLocalStorage ? window.localStorage.getItem(key) : Cookies.get(key);
333
+ if (typeof token === "undefined")
334
+ return null;
335
+ return token;
176
336
  }
337
+ return null;
177
338
  }
339
+ /**
340
+ * Set token in chosen storage
341
+ *
342
+ * @param {string} token - Token retrieve from login or register method
343
+ * @returns void
344
+ */
178
345
  setToken(token) {
179
346
  const { useLocalStorage, key, cookieOptions } = this.options.store;
180
- this.axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
181
347
  if (isBrowser()) {
182
- useLocalStorage ? window.localStorage.setItem(key, token) : Cookies__default["default"].set(key, token, cookieOptions);
348
+ useLocalStorage ? window.localStorage.setItem(key, token) : Cookies.set(key, token, cookieOptions);
183
349
  }
184
350
  }
351
+ /**
352
+ * Remove token from chosen storage (Cookies or Local)
353
+ *
354
+ * @returns void
355
+ */
185
356
  removeToken() {
186
357
  const { useLocalStorage, key } = this.options.store;
187
- delete this.axios.defaults.headers.common["Authorization"];
188
358
  if (isBrowser()) {
189
- useLocalStorage ? window.localStorage.removeItem(key) : Cookies__default["default"].remove(key);
359
+ useLocalStorage ? window.localStorage.removeItem(key) : Cookies.remove(key);
190
360
  }
191
361
  }
192
362
  }
package/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { AxiosRequestConfig, AxiosInstance, Method } from 'axios';
2
2
  import { CookieAttributes } from 'js-cookie';
3
3
 
4
- declare type StrapiAuthProvider = "github" | "facebook" | "google" | "cognito" | "twitter" | "discord" | "twitch" | "instagram" | "vk" | "linkedin" | "reddit" | "auth0";
5
- declare type StrapiLocale = "af" | "af-NA" | "af-ZA" | "agq" | "agq-CM" | "ak" | "ak-GH" | "sq" | "sq-AL" | "am" | "am-ET" | "ar" | "ar-DZ" | "ar-BH" | "ar-EG" | "ar-IQ" | "ar-JO" | "ar-KW" | "ar-LB" | "ar-LY" | "ar-MA" | "ar-OM" | "ar-QA" | "ar-SA" | "ar-SD" | "ar-SY" | "ar-TN" | "ar-AE" | "ar-001" | "ar-YE" | "hy" | "hy-AM" | "as" | "as-IN" | "asa" | "asa-TZ" | "az" | "az-Cyrl" | "az-Cyrl-AZ" | "az-Latn" | "az-Latn-AZ" | "ksf" | "ksf-CM" | "bm" | "bm-ML" | "bas" | "bas-CM" | "eu" | "eu-ES" | "be" | "be-BY" | "bem" | "bem-ZM" | "bez" | "bez-TZ" | "bn" | "bn-BD" | "bn-IN" | "brx" | "brx-IN" | "bs" | "bs-BA" | "br" | "br-FR" | "bg" | "bg-BG" | "my" | "my-MM" | "ca" | "ca-ES" | "ckb" | "tzm" | "tzm-Latn" | "tzm-Latn-MA" | "chr" | "chr-US" | "cgg" | "cgg-UG" | "zh" | "zh-Hans" | "zh-CN" | "zh-Hans-CN" | "zh-Hans-HK" | "zh-Hans-MO" | "zh-Hans-SG" | "zh-Hant" | "zh-Hant-HK" | "zh-Hant-MO" | "zh-Hant-TW" | "swc" | "swc-CD" | "kw" | "kw-GB" | "hr" | "hr-HR" | "cs" | "cs-CZ" | "da" | "da-DK" | "dua" | "dua-CM" | "nl" | "nl-AW" | "nl-BE" | "nl-CW" | "nl-NL" | "nl-SX" | "ebu" | "ebu-KE" | "en" | "en-AS" | "en-AU" | "en-BB" | "en-BE" | "en-BZ" | "en-BM" | "en-BW" | "en-CA" | "en-EG" | "en-EU" | "en-GU" | "en-GY" | "en-HK" | "en-IN" | "en-IE" | "en-JM" | "en-MT" | "en-MH" | "en-MU" | "en-NA" | "en-NZ" | "en-MP" | "en-PK" | "en-PH" | "en-SA" | "en-SG" | "en-ZA" | "en-TT" | "en-AE" | "en-UM" | "en-VI" | "en-US-POSIX" | "en-GB" | "en-US" | "en-ZW" | "eo" | "et" | "et-EE" | "ee" | "ee-GH" | "ee-TG" | "ewo" | "ewo-CM" | "fo" | "fo-FO" | "fil" | "fil-PH" | "fi" | "fi-FI" | "fr" | "fr-BE" | "fr-BJ" | "fr-BF" | "fr-BI" | "fr-CM" | "fr-CA" | "fr-CF" | "fr-TD" | "fr-KM" | "fr-CG" | "fr-CD" | "fr-CI" | "fr-DJ" | "fr-GQ" | "fr-FR" | "fr-GF" | "fr-GA" | "fr-GP" | "fr-GN" | "fr-LU" | "fr-MG" | "fr-ML" | "fr-MQ" | "fr-YT" | "fr-MC" | "fr-NE" | "fr-RW" | "fr-RE" | "fr-BL" | "fr-MF" | "fr-SN" | "fr-CH" | "fr-TG" | "ff" | "ff-SN" | "gl" | "gl-ES" | "lg" | "lg-UG" | "ka" | "ka-GE" | "de" | "de-AT" | "de-BE" | "de-DE" | "de-LI" | "de-LU" | "de-CH" | "el" | "el-CY" | "el-GR" | "gu" | "gu-IN" | "guz" | "guz-KE" | "ha" | "ha-Latn" | "ha-Latn-GH" | "ha-Latn-NE" | "ha-Latn-NG" | "haw" | "haw-US" | "he" | "he-IL" | "hi" | "hi-IN" | "hu" | "hu-HU" | "is" | "is-IS" | "ig" | "ig-NG" | "id" | "id-ID" | "ga" | "ga-IE" | "it" | "it-IT" | "it-CH" | "ja" | "ja-JP" | "dyo" | "dyo-SN" | "kea" | "kea-CV" | "kab" | "kab-DZ" | "kl" | "kl-GL" | "kln" | "kln-KE" | "kam" | "kam-KE" | "kn" | "kn-IN" | "kk" | "kk-Cyrl" | "kk-Cyrl-KZ" | "km" | "km-KH" | "ki" | "ki-KE" | "rw" | "rw-RW" | "kok" | "kok-IN" | "ko" | "ko-KR" | "khq" | "khq-ML" | "ses" | "ses-ML" | "nmg" | "nmg-CM" | "lag" | "lag-TZ" | "lv" | "lv-LV" | "ln" | "ln-CG" | "ln-CD" | "lt" | "lt-LT" | "lu" | "lu-CD" | "luo" | "luo-KE" | "luy" | "luy-KE" | "mk" | "mk-MK" | "jmc" | "jmc-TZ" | "mgh" | "mgh-MZ" | "kde" | "kde-TZ" | "mg" | "mg-MG" | "ms" | "ms-BN" | "ms-MY" | "ml" | "ml-IN" | "mt" | "mt-MT" | "gv" | "gv-GB" | "mr" | "mr-IN" | "mas" | "mas-KE" | "mas-TZ" | "mer" | "mer-KE" | "mfe" | "mfe-MU" | "mua" | "mua-CM" | "naq" | "naq-NA" | "ne" | "ne-IN" | "ne-NP" | "nd" | "nd-ZW" | "nb" | "nb-NO" | "nn" | "nn-NO" | "nus" | "nus-SD" | "nyn" | "nyn-UG" | "or" | "or-IN" | "om" | "om-ET" | "om-KE" | "ps" | "ps-AF" | "fa" | "fa-AF" | "fa-IR" | "pl" | "pl-PL" | "pt" | "pt-AO" | "pt-BR" | "pt-GW" | "pt-MZ" | "pt-PT" | "pt-ST" | "pa" | "pa-Arab" | "pa-Arab-PK" | "pa-Guru" | "pa-Guru-IN" | "ro" | "ro-MD" | "ro-RO" | "rm" | "rm-CH" | "rof" | "rof-TZ" | "rn" | "rn-BI" | "ru" | "ru-MD" | "ru-RU" | "ru-UA" | "rwk" | "rwk-TZ" | "saq" | "saq-KE" | "sg" | "sg-CF" | "sbp" | "sbp-TZ" | "seh" | "seh-MZ" | "sr" | "sr-Cyrl" | "sr-Cyrl-BA" | "sr-Cyrl-ME" | "sr-Cyrl-RS" | "sr-Latn" | "sr-Latn-BA" | "sr-Latn-ME" | "sr-Latn-RS" | "ksb" | "ksb-TZ" | "sn" | "sn-ZW" | "ii" | "ii-CN" | "si" | "si-LK" | "sk" | "sk-SK" | "sl" | "sl-SI" | "xog" | "xog-UG" | "so" | "so-DJ" | "so-ET" | "so-KE" | "so-SO" | "es" | "es-AR" | "es-BO" | "es-CL" | "es-CO" | "es-CR" | "es-DO" | "es-EC" | "es-SV" | "es-GQ" | "es-GT" | "es-HN" | "es-419" | "es-MX" | "es-NI" | "es-PA" | "es-PY" | "es-PE" | "es-PR" | "es-ES" | "es-US" | "es-UY" | "es-VE" | "sw" | "sw-KE" | "sw-TZ" | "sv" | "sv-FI" | "sv-SE" | "gsw" | "gsw-CH" | "shi" | "shi-Latn" | "shi-Latn-MA" | "shi-Tfng" | "shi-Tfng-MA" | "dav" | "dav-KE" | "ta" | "ta-IN" | "ta-LK" | "twq" | "twq-NE" | "te" | "te-IN" | "teo" | "teo-KE" | "teo-UG" | "th" | "th-TH" | "bo" | "bo-CN" | "bo-IN" | "ti" | "ti-ER" | "ti-ET" | "to" | "to-TO" | "tr" | "tr-TR" | "uk" | "uk-UA" | "ur" | "ur-IN" | "ur-PK" | "uz" | "uz-Arab" | "uz-Arab-AF" | "uz-Cyrl" | "uz-Cyrl-UZ" | "uz-Latn" | "uz-Latn-UZ" | "vai" | "vai-Latn" | "vai-Latn-LR" | "vai-Vaii" | "vai-Vaii-LR" | "vi" | "vi-VN" | "vun" | "vun-TZ" | "cy" | "cy-GB" | "yav" | "yav-CM" | "yo" | "yo-NG" | "dje" | "dje-NE" | "zu" | "zu-ZA";
4
+ type StrapiAuthProvider = "github" | "facebook" | "google" | "cognito" | "twitter" | "discord" | "twitch" | "instagram" | "vk" | "linkedin" | "reddit" | "auth0";
5
+ type StrapiLocale = "af" | "af-NA" | "af-ZA" | "agq" | "agq-CM" | "ak" | "ak-GH" | "sq" | "sq-AL" | "am" | "am-ET" | "ar" | "ar-DZ" | "ar-BH" | "ar-EG" | "ar-IQ" | "ar-JO" | "ar-KW" | "ar-LB" | "ar-LY" | "ar-MA" | "ar-OM" | "ar-QA" | "ar-SA" | "ar-SD" | "ar-SY" | "ar-TN" | "ar-AE" | "ar-001" | "ar-YE" | "hy" | "hy-AM" | "as" | "as-IN" | "asa" | "asa-TZ" | "az" | "az-Cyrl" | "az-Cyrl-AZ" | "az-Latn" | "az-Latn-AZ" | "ksf" | "ksf-CM" | "bm" | "bm-ML" | "bas" | "bas-CM" | "eu" | "eu-ES" | "be" | "be-BY" | "bem" | "bem-ZM" | "bez" | "bez-TZ" | "bn" | "bn-BD" | "bn-IN" | "brx" | "brx-IN" | "bs" | "bs-BA" | "br" | "br-FR" | "bg" | "bg-BG" | "my" | "my-MM" | "ca" | "ca-ES" | "ckb" | "tzm" | "tzm-Latn" | "tzm-Latn-MA" | "chr" | "chr-US" | "cgg" | "cgg-UG" | "zh" | "zh-Hans" | "zh-CN" | "zh-Hans-CN" | "zh-Hans-HK" | "zh-Hans-MO" | "zh-Hans-SG" | "zh-Hant" | "zh-Hant-HK" | "zh-Hant-MO" | "zh-Hant-TW" | "swc" | "swc-CD" | "kw" | "kw-GB" | "hr" | "hr-HR" | "cs" | "cs-CZ" | "da" | "da-DK" | "dua" | "dua-CM" | "nl" | "nl-AW" | "nl-BE" | "nl-CW" | "nl-NL" | "nl-SX" | "ebu" | "ebu-KE" | "en" | "en-AS" | "en-AU" | "en-BB" | "en-BE" | "en-BZ" | "en-BM" | "en-BW" | "en-CA" | "en-EG" | "en-EU" | "en-GU" | "en-GY" | "en-HK" | "en-IN" | "en-IE" | "en-JM" | "en-MT" | "en-MH" | "en-MU" | "en-NA" | "en-NZ" | "en-MP" | "en-PK" | "en-PH" | "en-SA" | "en-SG" | "en-ZA" | "en-TT" | "en-AE" | "en-UM" | "en-VI" | "en-US-POSIX" | "en-GB" | "en-US" | "en-ZW" | "eo" | "et" | "et-EE" | "ee" | "ee-GH" | "ee-TG" | "ewo" | "ewo-CM" | "fo" | "fo-FO" | "fil" | "fil-PH" | "fi" | "fi-FI" | "fr" | "fr-BE" | "fr-BJ" | "fr-BF" | "fr-BI" | "fr-CM" | "fr-CA" | "fr-CF" | "fr-TD" | "fr-KM" | "fr-CG" | "fr-CD" | "fr-CI" | "fr-DJ" | "fr-GQ" | "fr-FR" | "fr-GF" | "fr-GA" | "fr-GP" | "fr-GN" | "fr-LU" | "fr-MG" | "fr-ML" | "fr-MQ" | "fr-YT" | "fr-MC" | "fr-NE" | "fr-RW" | "fr-RE" | "fr-BL" | "fr-MF" | "fr-SN" | "fr-CH" | "fr-TG" | "ff" | "ff-SN" | "gl" | "gl-ES" | "lg" | "lg-UG" | "ka" | "ka-GE" | "de" | "de-AT" | "de-BE" | "de-DE" | "de-LI" | "de-LU" | "de-CH" | "el" | "el-CY" | "el-GR" | "gu" | "gu-IN" | "guz" | "guz-KE" | "ha" | "ha-Latn" | "ha-Latn-GH" | "ha-Latn-NE" | "ha-Latn-NG" | "haw" | "haw-US" | "he" | "he-IL" | "hi" | "hi-IN" | "hu" | "hu-HU" | "is" | "is-IS" | "ig" | "ig-NG" | "id" | "id-ID" | "ga" | "ga-IE" | "it" | "it-IT" | "it-CH" | "ja" | "ja-JP" | "dyo" | "dyo-SN" | "kea" | "kea-CV" | "kab" | "kab-DZ" | "kl" | "kl-GL" | "kln" | "kln-KE" | "kam" | "kam-KE" | "kn" | "kn-IN" | "kk" | "kk-Cyrl" | "kk-Cyrl-KZ" | "km" | "km-KH" | "ki" | "ki-KE" | "rw" | "rw-RW" | "kok" | "kok-IN" | "ko" | "ko-KR" | "khq" | "khq-ML" | "ses" | "ses-ML" | "nmg" | "nmg-CM" | "lag" | "lag-TZ" | "lv" | "lv-LV" | "ln" | "ln-CG" | "ln-CD" | "lt" | "lt-LT" | "lu" | "lu-CD" | "luo" | "luo-KE" | "luy" | "luy-KE" | "mk" | "mk-MK" | "jmc" | "jmc-TZ" | "mgh" | "mgh-MZ" | "kde" | "kde-TZ" | "mg" | "mg-MG" | "ms" | "ms-BN" | "ms-MY" | "ml" | "ml-IN" | "mt" | "mt-MT" | "gv" | "gv-GB" | "mr" | "mr-IN" | "mas" | "mas-KE" | "mas-TZ" | "mer" | "mer-KE" | "mfe" | "mfe-MU" | "mua" | "mua-CM" | "naq" | "naq-NA" | "ne" | "ne-IN" | "ne-NP" | "nd" | "nd-ZW" | "nb" | "nb-NO" | "nn" | "nn-NO" | "nus" | "nus-SD" | "nyn" | "nyn-UG" | "or" | "or-IN" | "om" | "om-ET" | "om-KE" | "ps" | "ps-AF" | "fa" | "fa-AF" | "fa-IR" | "pl" | "pl-PL" | "pt" | "pt-AO" | "pt-BR" | "pt-GW" | "pt-MZ" | "pt-PT" | "pt-ST" | "pa" | "pa-Arab" | "pa-Arab-PK" | "pa-Guru" | "pa-Guru-IN" | "ro" | "ro-MD" | "ro-RO" | "rm" | "rm-CH" | "rof" | "rof-TZ" | "rn" | "rn-BI" | "ru" | "ru-MD" | "ru-RU" | "ru-UA" | "rwk" | "rwk-TZ" | "saq" | "saq-KE" | "sg" | "sg-CF" | "sbp" | "sbp-TZ" | "seh" | "seh-MZ" | "sr" | "sr-Cyrl" | "sr-Cyrl-BA" | "sr-Cyrl-ME" | "sr-Cyrl-RS" | "sr-Latn" | "sr-Latn-BA" | "sr-Latn-ME" | "sr-Latn-RS" | "ksb" | "ksb-TZ" | "sn" | "sn-ZW" | "ii" | "ii-CN" | "si" | "si-LK" | "sk" | "sk-SK" | "sl" | "sl-SI" | "xog" | "xog-UG" | "so" | "so-DJ" | "so-ET" | "so-KE" | "so-SO" | "es" | "es-AR" | "es-BO" | "es-CL" | "es-CO" | "es-CR" | "es-DO" | "es-EC" | "es-SV" | "es-GQ" | "es-GT" | "es-HN" | "es-419" | "es-MX" | "es-NI" | "es-PA" | "es-PY" | "es-PE" | "es-PR" | "es-ES" | "es-US" | "es-UY" | "es-VE" | "sw" | "sw-KE" | "sw-TZ" | "sv" | "sv-FI" | "sv-SE" | "gsw" | "gsw-CH" | "shi" | "shi-Latn" | "shi-Latn-MA" | "shi-Tfng" | "shi-Tfng-MA" | "dav" | "dav-KE" | "ta" | "ta-IN" | "ta-LK" | "twq" | "twq-NE" | "te" | "te-IN" | "teo" | "teo-KE" | "teo-UG" | "th" | "th-TH" | "bo" | "bo-CN" | "bo-IN" | "ti" | "ti-ER" | "ti-ET" | "to" | "to-TO" | "tr" | "tr-TR" | "uk" | "uk-UA" | "ur" | "ur-IN" | "ur-PK" | "uz" | "uz-Arab" | "uz-Arab-AF" | "uz-Cyrl" | "uz-Cyrl-UZ" | "uz-Latn" | "uz-Latn-UZ" | "vai" | "vai-Latn" | "vai-Latn-LR" | "vai-Vaii" | "vai-Vaii-LR" | "vi" | "vi-VN" | "vun" | "vun-TZ" | "cy" | "cy-GB" | "yav" | "yav-CM" | "yo" | "yo-NG" | "dje" | "dje-NE" | "zu" | "zu-ZA";
6
6
  interface StoreConfig {
7
7
  key: string;
8
8
  useLocalStorage?: boolean;
@@ -20,7 +20,7 @@ interface StrapiDefaultOptions {
20
20
  store: StoreConfig;
21
21
  axiosOptions: AxiosRequestConfig;
22
22
  }
23
- declare type StrapiUser = Record<string, unknown> | null;
23
+ type StrapiUser = Record<string, unknown> | null;
24
24
  interface PaginationByPage {
25
25
  page: number;
26
26
  pageSize: number;
@@ -71,6 +71,11 @@ interface StrapiRegistrationData {
71
71
  interface StrapiForgotPasswordData {
72
72
  email: string;
73
73
  }
74
+ interface StrapiChangePasswordData {
75
+ currentPassword: string;
76
+ password: string;
77
+ passwordConfirmation: string;
78
+ }
74
79
  interface StrapiResetPasswordData {
75
80
  code: string;
76
81
  password: string;
@@ -83,7 +88,7 @@ interface StrapiEmailConfirmationData {
83
88
  declare class Strapi {
84
89
  axios: AxiosInstance;
85
90
  options: StrapiDefaultOptions;
86
- private _user;
91
+ user: StrapiUser;
87
92
  /**
88
93
  * Strapi SDK Constructor
89
94
  *
@@ -94,8 +99,6 @@ declare class Strapi {
94
99
  * @param {AxiosRequestConfig} options.axiosOptions? - The list of your Content type on your Strapi API
95
100
  */
96
101
  constructor(options?: StrapiOptions);
97
- get user(): StrapiUser;
98
- set user(user: StrapiUser);
99
102
  /**
100
103
  * Basic axios request
101
104
  *
@@ -132,6 +135,17 @@ declare class Strapi {
132
135
  * @returns Promise<void>
133
136
  */
134
137
  forgotPassword(data: StrapiForgotPasswordData): Promise<void>;
138
+ /**
139
+ * Change the password of the logged in user.
140
+ * @author AnnikenYT
141
+ *
142
+ * @param {StrapiChangePasswordData} data - Change password data: `currentPassword`, `password`, `passwordConfirmation`
143
+ * @param {string} data.currentPassword - The current password of the user
144
+ * @param {string} data.password - The new password of the user
145
+ * @param {string} data.passwordConfirmation - Confirmation of the new password of the user
146
+ * @returns Promise<StrapiAuthenticationResponse>
147
+ */
148
+ changePassword(data: StrapiChangePasswordData): Promise<StrapiAuthenticationResponse>;
135
149
  /**
136
150
  * Reset the user password
137
151
  *
@@ -216,13 +230,6 @@ declare class Strapi {
216
230
  * @returns Promise<StrapiResponse<T>>
217
231
  */
218
232
  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
233
  /**
227
234
  * Refresh local data of the logged-in user
228
235
  *
@@ -230,20 +237,20 @@ declare class Strapi {
230
237
  */
231
238
  fetchUser(): Promise<StrapiUser>;
232
239
  /**
233
- * Sync token between storage & header when SDK is instantiate
240
+ * Retrieve token from chosen storage
234
241
  *
235
- * @returns void
242
+ * @returns string | null
236
243
  */
237
- private syncToken;
244
+ getToken(): string | null;
238
245
  /**
239
- * Set token in Axios headers & in choosen storage
246
+ * Set token in chosen storage
240
247
  *
241
248
  * @param {string} token - Token retrieve from login or register method
242
249
  * @returns void
243
250
  */
244
251
  setToken(token: string): void;
245
252
  /**
246
- * Remove token in Axios headers & in choosen storage (Cookies or Local)
253
+ * Remove token from chosen storage (Cookies or Local)
247
254
  *
248
255
  * @returns void
249
256
  */
package/dist/index.mjs CHANGED
@@ -2,6 +2,7 @@ import axios from 'axios';
2
2
  import defu from 'defu';
3
3
  import qs from 'qs';
4
4
  import Cookies from 'js-cookie';
5
+ import { cleanDoubleSlashes, joinURL } from 'ufo';
5
6
 
6
7
  function isBrowser() {
7
8
  return typeof window !== "undefined";
@@ -18,22 +19,49 @@ const defaults = {
18
19
  axiosOptions: {}
19
20
  };
20
21
  class Strapi {
22
+ axios;
23
+ options;
24
+ user = null;
25
+ /**
26
+ * Strapi SDK Constructor
27
+ *
28
+ * @constructor
29
+ * @param {StrapiOptions} options? - Options in order to configure API URL, list your Content Types & extend the axios configuration
30
+ * @param {string} options.url? - Your Strapi API URL, Default: http://localhost::1337
31
+ * @param {StoreConfig} options.store? - Config the way you want to store JWT (Cookie or LocalStorage)
32
+ * @param {AxiosRequestConfig} options.axiosOptions? - The list of your Content type on your Strapi API
33
+ */
21
34
  constructor(options) {
22
- this._user = null;
23
- this.options = defu(options || {}, defaults);
35
+ const _options = defu(options || {}, defaults);
36
+ this.options = {
37
+ ..._options,
38
+ url: cleanDoubleSlashes(_options?.url),
39
+ prefix: cleanDoubleSlashes(_options?.prefix)
40
+ };
24
41
  this.axios = axios.create({
25
- baseURL: new URL(this.options.prefix, this.options.url).href,
42
+ baseURL: joinURL(this.options.url, this.options.prefix),
26
43
  paramsSerializer: qs.stringify,
27
44
  ...this.options.axiosOptions
28
45
  });
29
- this.syncToken();
30
- }
31
- get user() {
32
- return this._user;
33
- }
34
- set user(user) {
35
- this._user = user;
46
+ this.axios.interceptors.request.use((config) => {
47
+ const token = this.getToken();
48
+ if (token) {
49
+ config.headers = {
50
+ ...config.headers,
51
+ Authorization: `Bearer ${token}`
52
+ };
53
+ }
54
+ return config;
55
+ });
36
56
  }
57
+ /**
58
+ * Basic axios request
59
+ *
60
+ * @param {Method} method - HTTP method
61
+ * @param {string} url - Custom or Strapi API URL
62
+ * @param {AxiosRequestConfig} axiosConfig? - Custom Axios config
63
+ * @returns Promise<T>
64
+ */
37
65
  async request(method, url, axiosConfig) {
38
66
  try {
39
67
  const response = await this.axios.request({
@@ -59,45 +87,128 @@ class Strapi {
59
87
  }
60
88
  }
61
89
  }
90
+ /**
91
+ * Authenticate user & retrieve his JWT
92
+ *
93
+ * @param {StrapiAuthenticationData} data - User authentication form data: `identifier`, `password`
94
+ * @param {string} data.identifier - The email or username of the user
95
+ * @param {string} data.password - The password of the user
96
+ * @returns Promise<StrapiAuthenticationResponse>
97
+ */
62
98
  async login(data) {
63
99
  this.removeToken();
64
100
  const { user, jwt } = await this.request("post", "/auth/local", {
65
101
  data
66
102
  });
67
103
  this.setToken(jwt);
68
- this.setUser(user);
104
+ this.user = user;
69
105
  return { user, jwt };
70
106
  }
107
+ /**
108
+ * Register a new user & retrieve JWT
109
+ *
110
+ * @param {StrapiRegistrationData} data - New user registration data: `username`, `email`, `password`
111
+ * @param {string} data.username - Username of the new user
112
+ * @param {string} data.email - Email of the new user
113
+ * @param {string} data.password - Password of the new user
114
+ * @returns Promise<StrapiAuthenticationResponse>
115
+ */
71
116
  async register(data) {
72
117
  this.removeToken();
73
- const { user, jwt } = await this.request("post", "/auth/local/register", {
74
- data
75
- });
118
+ const { user, jwt } = await this.request(
119
+ "post",
120
+ "/auth/local/register",
121
+ {
122
+ data
123
+ }
124
+ );
76
125
  this.setToken(jwt);
77
- this.setUser(user);
126
+ this.user = user;
78
127
  return { user, jwt };
79
128
  }
129
+ /**
130
+ * Send an email to a user in order to reset his password
131
+ *
132
+ * @param {StrapiForgotPasswordData} data - Forgot password data: `email`
133
+ * @param {string} data.email - Email of the user who forgot his password
134
+ * @returns Promise<void>
135
+ */
80
136
  async forgotPassword(data) {
81
137
  this.removeToken();
82
138
  return this.request("post", "/auth/forgot-password", { data });
83
139
  }
140
+ /**
141
+ * Change the password of the logged in user.
142
+ * @author AnnikenYT
143
+ *
144
+ * @param {StrapiChangePasswordData} data - Change password data: `currentPassword`, `password`, `passwordConfirmation`
145
+ * @param {string} data.currentPassword - The current password of the user
146
+ * @param {string} data.password - The new password of the user
147
+ * @param {string} data.passwordConfirmation - Confirmation of the new password of the user
148
+ * @returns Promise<StrapiAuthenticationResponse>
149
+ */
150
+ async changePassword(data) {
151
+ const { user, jwt } = await this.request(
152
+ "post",
153
+ "/auth/change-password",
154
+ {
155
+ data
156
+ }
157
+ );
158
+ this.setToken(jwt);
159
+ this.user = user;
160
+ return { jwt, user };
161
+ }
162
+ /**
163
+ * Reset the user password
164
+ *
165
+ * @param {StrapiResetPasswordData} data - Reset password data object: `code`, `password`, `passwordConfirmation`
166
+ * @param {string} data.code - Code received by email after calling the `forgotPassword` method
167
+ * @param {string} data.password - New password of the user
168
+ * @param {string} data.passwordConfirmation - Confirmation of the new password of the user
169
+ * @returns Promise<StrapiAuthenticationResponse>
170
+ */
84
171
  async resetPassword(data) {
85
172
  this.removeToken();
86
- const { user, jwt } = await this.request("post", "/auth/reset-password", {
87
- data
88
- });
173
+ const { user, jwt } = await this.request(
174
+ "post",
175
+ "/auth/reset-password",
176
+ {
177
+ data
178
+ }
179
+ );
89
180
  this.setToken(jwt);
90
- this.setUser(user);
181
+ this.user = user;
91
182
  return { user, jwt };
92
183
  }
184
+ /**
185
+ * Send programmatically an email to a user in order to confirm his account
186
+ *
187
+ * @param {StrapiEmailConfirmationData} data - Email confirmation data: `email`
188
+ * @param {string} data.email - Email of the user who want to be confirmed
189
+ * @returns Promise<void>
190
+ */
93
191
  async sendEmailConfirmation(data) {
94
192
  return this.request("post", "/auth/send-email-confirmation", {
95
193
  data
96
194
  });
97
195
  }
196
+ /**
197
+ * Get the correct URL to authenticate with provider
198
+ *
199
+ * @param {StrapiAuthProvider} provider - Provider name
200
+ * @returns string
201
+ */
98
202
  getProviderAuthenticationUrl(provider) {
99
- return new URL(`/connect/${provider}`, this.options.url).href;
203
+ return joinURL(this.options.url, this.options.prefix, "connect", provider);
100
204
  }
205
+ /**
206
+ * Authenticate user with the token present on the URL or in `params`
207
+ *
208
+ * @param {StrapiAuthProvider} provider - Provider name
209
+ * @param {string} access_token? - Access Token return from Strapi
210
+ * @returns Promise<StrapiAuthenticationResponse>
211
+ */
101
212
  async authenticateProvider(provider, access_token) {
102
213
  this.removeToken();
103
214
  if (isBrowser()) {
@@ -107,75 +218,141 @@ class Strapi {
107
218
  if (params.access_token)
108
219
  access_token = params.access_token;
109
220
  }
110
- const { user, jwt } = await this.request("get", `/auth/${provider}/callback`, {
111
- params: { access_token }
112
- });
221
+ const { user, jwt } = await this.request(
222
+ "get",
223
+ `/auth/${provider}/callback`,
224
+ {
225
+ params: { access_token }
226
+ }
227
+ );
113
228
  this.setToken(jwt);
114
- this.setUser(user);
229
+ this.user = user;
115
230
  return { user, jwt };
116
231
  }
232
+ /**
233
+ * Logout by removing authentication token
234
+ *
235
+ * @returns void
236
+ */
117
237
  logout() {
118
- this.setUser(null);
238
+ this.user = null;
119
239
  this.removeToken();
120
240
  }
241
+ /**
242
+ * Get a list of {content-type} entries
243
+ *
244
+ * @param {string} contentType - Content type's name pluralized
245
+ * @param {StrapiRequestParams} params? - Query parameters
246
+ * @returns Promise<StrapiResponse<T>>
247
+ */
121
248
  find(contentType, params) {
122
249
  return this.request("get", `/${contentType}`, {
123
250
  params
124
251
  });
125
252
  }
253
+ /**
254
+ * Get a specific {content-type} entry
255
+ *
256
+ * @param {string} contentType - Content type's name pluralized
257
+ * @param {string|number} id - ID of entry
258
+ * @param {StrapiBaseRequestParams} params? - Fields selection & Relations population
259
+ * @returns Promise<StrapiResponse<T>>
260
+ */
126
261
  findOne(contentType, id, params) {
127
262
  return this.request("get", `/${contentType}/${id}`, {
128
263
  params
129
264
  });
130
265
  }
266
+ /**
267
+ * Create a {content-type} entry
268
+ *
269
+ * @param {string} contentType - Content type's name pluralized
270
+ * @param {AxiosRequestConfig["data"]} data - New entry
271
+ * @param {StrapiBaseRequestParams} params? - Fields selection & Relations population
272
+ * @returns Promise<StrapiResponse<T>>
273
+ */
131
274
  create(contentType, data, params) {
132
275
  return this.request("post", `/${contentType}`, {
133
276
  data: { data },
134
277
  params
135
278
  });
136
279
  }
280
+ /**
281
+ * Update a specific entry
282
+ *
283
+ * @param {string} contentType - Content type's name pluralized
284
+ * @param {string|number} id - ID of entry to be updated
285
+ * @param {AxiosRequestConfig["data"]} data - New entry data
286
+ * @param {StrapiBaseRequestParams} params? - Fields selection & Relations population
287
+ * @returns Promise<StrapiResponse<T>>
288
+ */
137
289
  update(contentType, id, data, params) {
138
290
  return this.request("put", `/${contentType}/${id}`, {
139
291
  data: { data },
140
292
  params
141
293
  });
142
294
  }
295
+ /**
296
+ * Delete en entry
297
+ *
298
+ * @param {string} contentType - Content type's name pluralized
299
+ * @param {string|number} id - ID of entry to be deleted
300
+ * @param {StrapiBaseRequestParams} params? - Fields selection & Relations population
301
+ * @returns Promise<StrapiResponse<T>>
302
+ */
143
303
  delete(contentType, id, params) {
144
304
  return this.request("delete", `/${contentType}/${id}`, {
145
305
  params
146
306
  });
147
307
  }
148
- setUser(user) {
149
- this._user = user;
150
- }
308
+ /**
309
+ * Refresh local data of the logged-in user
310
+ *
311
+ * @returns Promise<StrapiUser>
312
+ */
151
313
  async fetchUser() {
152
314
  try {
153
315
  const user = await this.request("get", "/users/me");
154
- this.setUser(user);
316
+ this.user = user;
155
317
  } catch (e) {
156
318
  this.logout();
157
319
  }
158
- return this._user;
320
+ return this.user;
159
321
  }
160
- syncToken() {
322
+ /**
323
+ * Retrieve token from chosen storage
324
+ *
325
+ * @returns string | null
326
+ */
327
+ getToken() {
161
328
  const { useLocalStorage, key } = this.options.store;
162
329
  if (isBrowser()) {
163
330
  const token = useLocalStorage ? window.localStorage.getItem(key) : Cookies.get(key);
164
- if (token) {
165
- this.axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
166
- }
331
+ if (typeof token === "undefined")
332
+ return null;
333
+ return token;
167
334
  }
335
+ return null;
168
336
  }
337
+ /**
338
+ * Set token in chosen storage
339
+ *
340
+ * @param {string} token - Token retrieve from login or register method
341
+ * @returns void
342
+ */
169
343
  setToken(token) {
170
344
  const { useLocalStorage, key, cookieOptions } = this.options.store;
171
- this.axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
172
345
  if (isBrowser()) {
173
346
  useLocalStorage ? window.localStorage.setItem(key, token) : Cookies.set(key, token, cookieOptions);
174
347
  }
175
348
  }
349
+ /**
350
+ * Remove token from chosen storage (Cookies or Local)
351
+ *
352
+ * @returns void
353
+ */
176
354
  removeToken() {
177
355
  const { useLocalStorage, key } = this.options.store;
178
- delete this.axios.defaults.headers.common["Authorization"];
179
356
  if (isBrowser()) {
180
357
  useLocalStorage ? window.localStorage.removeItem(key) : Cookies.remove(key);
181
358
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "strapi-sdk-js",
3
- "version": "2.1.5",
3
+ "version": "2.3.0",
4
4
  "description": "📦 Javascript SDK for your Strapi API",
5
5
  "keywords": [
6
6
  "strapi",
@@ -38,7 +38,8 @@
38
38
  "exports": {
39
39
  ".": {
40
40
  "require": "./dist/index.cjs",
41
- "import": "./dist/index.mjs"
41
+ "import": "./dist/index.mjs",
42
+ "types": "./dist/index.d.ts"
42
43
  },
43
44
  "./package.json": "./package.json"
44
45
  },
@@ -49,8 +50,8 @@
49
50
  "dist/"
50
51
  ],
51
52
  "scripts": {
52
- "start": "siroc build --watch",
53
- "build": "siroc build",
53
+ "start": "rollup -c --watch",
54
+ "build": "rollup -c",
54
55
  "clean": "rm -rf bin dist coverage",
55
56
  "lint": "eslint . --ext .ts,.js",
56
57
  "lint:fix": "eslint . --ext .ts,.js --fix",
@@ -58,7 +59,7 @@
58
59
  "test:unit": "jest",
59
60
  "commit": "git-cz",
60
61
  "release": "dotenv release-it --",
61
- "prepare": "siroc build"
62
+ "prepare": "rollup -c"
62
63
  },
63
64
  "lint-staged": {
64
65
  "*.{js,jsx,ts,tsx}": "eslint --cache --fix"
@@ -71,31 +72,38 @@
71
72
  "dependencies": {
72
73
  "@types/js-cookie": "^3.0.0",
73
74
  "@types/qs": "^6.9.6",
74
- "axios": "^0.24.0",
75
+ "axios": "^0.27.0",
75
76
  "defu": "^5.0.0",
76
77
  "js-cookie": "^3.0.0",
77
- "qs": "^6.10.1"
78
+ "qs": "^6.10.1",
79
+ "ufo": "^1.2.0"
78
80
  },
79
81
  "devDependencies": {
80
- "@commitlint/cli": "16.0.1",
81
- "@commitlint/config-conventional": "16.0.0",
82
- "@release-it/conventional-changelog": "4.1.0",
83
- "@types/jest": "27.4.0",
84
- "@types/sinon": "10.0.6",
85
- "@typescript-eslint/eslint-plugin": "5.9.0",
86
- "@typescript-eslint/parser": "5.9.0",
87
- "commitizen": "4.2.4",
82
+ "@commitlint/cli": "17.6.7",
83
+ "@commitlint/config-conventional": "17.6.7",
84
+ "@release-it/conventional-changelog": "4.3.0",
85
+ "@rollup/plugin-commonjs": "^25.0.3",
86
+ "@rollup/plugin-json": "^6.0.0",
87
+ "@rollup/plugin-node-resolve": "^15.1.0",
88
+ "@types/jest": "27.5.2",
89
+ "@types/sinon": "10.0.16",
90
+ "@typescript-eslint/eslint-plugin": "5.62.0",
91
+ "@typescript-eslint/parser": "5.62.0",
92
+ "commitizen": "4.3.0",
88
93
  "cz-conventional-changelog": "3.3.0",
89
- "dotenv-cli": "4.1.1",
90
- "eslint": "8.6.0",
91
- "husky": "7.0.4",
92
- "jest": "27.4.7",
93
- "lint-staged": "12.1.7",
94
+ "dotenv-cli": "7.2.1",
95
+ "esbuild": "^0.18.19",
96
+ "eslint": "8.46.0",
97
+ "husky": "8.0.3",
98
+ "jest": "27.5.1",
99
+ "lint-staged": "13.2.3",
94
100
  "npm-run-all": "4.1.5",
95
- "release-it": "14.12.3",
96
- "sinon": "12.0.1",
97
- "siroc": "0.16.0",
98
- "ts-jest": "27.1.3",
99
- "typescript": "4.5.4"
101
+ "release-it": "14.14.3",
102
+ "rollup": "^3.27.2",
103
+ "rollup-plugin-dts": "^5.3.1",
104
+ "rollup-plugin-esbuild": "^5.0.0",
105
+ "sinon": "15.2.0",
106
+ "ts-jest": "27.1.5",
107
+ "typescript": "5.1.6"
100
108
  }
101
109
  }
package/CHANGELOG.md DELETED
@@ -1,98 +0,0 @@
1
- ## [2.1.5](https://github.com/Stun3R/strapi-sdk-js/compare/v2.1.4...v2.1.5) (2022-01-17)
2
-
3
- ## [2.1.4](https://github.com/Stun3R/strapi-sdk-js/compare/v2.1.3...v2.1.4) (2022-01-14)
4
-
5
-
6
- ### Bug Fixes
7
-
8
- * **options:** remove process usage in default to be compatible with more framework ([aaa162d](https://github.com/Stun3R/strapi-sdk-js/commit/aaa162d53bc656a149da8d2aacc9560a42a11d55))
9
-
10
- ## [2.1.3](https://github.com/Stun3R/strapi-sdk-js/compare/v2.1.2...v2.1.3) (2022-01-05)
11
-
12
-
13
- ### Bug Fixes
14
-
15
- * **deps:** pin dependencies ([2e6e5a9](https://github.com/Stun3R/strapi-sdk-js/commit/2e6e5a984e3c3bce226bdb0a55218d6707cc0488))
16
- * **deps:** update dependency @nuxt/content-theme-docs to v0.11.1 ([1541e39](https://github.com/Stun3R/strapi-sdk-js/commit/1541e392bb9e70ee7cdc65fa1362358518c0d084))
17
- * **deps:** update dependency axios to ^0.24.0 ([4e2a41d](https://github.com/Stun3R/strapi-sdk-js/commit/4e2a41d5a3ffca07cf502d488ce9caea809f4bbc))
18
-
19
- ## [2.1.2](https://github.com/Stun3R/strapi-sdk-js/compare/v2.1.1...v2.1.2) (2021-12-30)
20
-
21
-
22
- ### Bug Fixes
23
-
24
- * **params:** change type to accept object for populate ([bfa835c](https://github.com/Stun3R/strapi-sdk-js/commit/bfa835c6d01425049f7d675f96316516d59bca5d)), closes [#108](https://github.com/Stun3R/strapi-sdk-js/issues/108)
25
-
26
-
27
- ### Features
28
-
29
- * add integrations ([439b292](https://github.com/Stun3R/strapi-sdk-js/commit/439b2926e474c06a0e581f9af7c1ad08c5eb7282))
30
-
31
- ## [2.1.1](https://github.com/Stun3R/strapi-sdk-js/compare/v2.1.0...v2.1.1) (2021-11-29)
32
-
33
- # [2.0.0](https://github.com/Stun3R/strapi-sdk-js/compare/v1.1.0...v2.0.0) (2021-11-08)
34
-
35
-
36
- ### Features
37
-
38
- * **Strapi v4 support**
39
- * add params to create, update, delete methods in order to selecting field in response ([a693dac](https://github.com/Stun3R/strapi-sdk-js/commit/a693dac21101260125db8672229644387b2ee3e4))
40
- * add StrapiLocale type for better autocompletion ([583c5b8](https://github.com/Stun3R/strapi-sdk-js/commit/583c5b8d561426c581f31ed6d335fc322411be71))
41
- * replace axios request config params by custom type based on strapi rest api parameters ([2047433](https://github.com/Stun3R/strapi-sdk-js/commit/2047433f93aaae9b4eaeb0c7f4b49cccfb46f770))
42
- * v4 error handling ([4d0eb3f](https://github.com/Stun3R/strapi-sdk-js/commit/4d0eb3f50ed9796e7dd3837cd45a2d926e9679d5))
43
-
44
- # [1.1.0](https://github.com/Stun3R/strapi-sdk-js/compare/v1.0.2...v1.1.0) (2021-10-19)
45
-
46
-
47
- ### Bug Fixes
48
-
49
- * add axioserror type on error ([1140bbb](https://github.com/Stun3R/strapi-sdk-js/commit/1140bbb990c37e492aa8d457a1e65ff5a0ac24ed))
50
- * **deps:** update dependency @types/js-cookie to v3 ([88483aa](https://github.com/Stun3R/strapi-sdk-js/commit/88483aa0ff841c440716bbe1014ff8a6ef58c27f))
51
- * **deps:** update dependency axios to ^0.23.0 ([854e84e](https://github.com/Stun3R/strapi-sdk-js/commit/854e84e02cdb818a69dddfecb8c1530f791609c4))
52
- * missing comma after dependencies ([6efe03b](https://github.com/Stun3R/strapi-sdk-js/commit/6efe03b1713c5df97a077d3c5db43022d3fcc3f6))
53
- * **tests:** switch response type to follow latest change ([362b5d9](https://github.com/Stun3R/strapi-sdk-js/commit/362b5d911ffb9fbeb37de7678dd60b97c7dbf2f6))
54
-
55
-
56
- ### Code Refactoring
57
-
58
- * drop of graphql methods support ([eebd7a3](https://github.com/Stun3R/strapi-sdk-js/commit/eebd7a34ddbce216ba0e8b3f5a3f95c0a0a5add5))
59
-
60
-
61
- ### BREAKING CHANGES
62
-
63
- * SDK supports Strapi v4. Every CRUD methods response changed. Please refer to [documentation](https://strapi-sdk-js.netlify.app)
64
- * GraphQL is no longer supported in v1.1.0
65
-
66
- ## [1.0.2](https://github.com/Stun3R/strapi-sdk-js/compare/v1.0.1...v1.0.2) (2021-08-30)
67
-
68
-
69
- ### Bug Fixes
70
-
71
- * **deps:** update dependency js-cookie to v3 ([54be993](https://github.com/Stun3R/strapi-sdk-js/commit/54be993f1c02949fb320786bca409681eb6c9ed0))
72
- * **deps:** update dependency nuxt to v2.15.7 ([062c012](https://github.com/Stun3R/strapi-sdk-js/commit/062c0121f63746c728f895fcf4b608e3e820826d))
73
- * **deps:** update dependency nuxt to v2.15.8 ([598dacd](https://github.com/Stun3R/strapi-sdk-js/commit/598dacdeab43252d92405fc67882de2c00609b48))
74
-
75
- ## 1.0.1 (2021-06-17)
76
-
77
-
78
- ### Bug Fixes
79
-
80
- * **npm:** dist folder is missing when installing package ([8629d3d](https://github.com/Stun3R/strapi-sdk-js/commit/8629d3d85f399925f1697a5ab39881f47e1fe5fa)) @lhillebrandt
81
- * **dist:** change path in order to take dist inside npm package ([b494a35](https://github.com/Stun3R/strapi-sdk-js/commit/b494a352cb5559a8e9a2e2e931ee30a67836ef82))
82
- * **package.json:** remove trailing slash from dist folder in files ([7d3000c](https://github.com/Stun3R/strapi-sdk-js/commit/7d3000c15f63443d03971bc510cecf86ae530983))
83
-
84
- # 1.0.0 (2021-06-11)
85
-
86
-
87
- ### Bug Fixes
88
-
89
- * **deps:** pin dependency docus to v0.1.5 ([9870d86](https://github.com/Stun3R/strapi-sdk-js/commit/9870d8669457bd31bfb184dcafc2e90b6fd0ecba))
90
- * **deps:** update dependency nuxt to v2.15.6 ([f37aefd](https://github.com/Stun3R/strapi-sdk-js/commit/f37aefdebbcc9fb6bea8628c5b10c8c706dbc287))
91
-
92
-
93
- ### Features
94
-
95
- * **user:** add user object, getter, setter & fetch methods ([e18089a](https://github.com/Stun3R/strapi-sdk-js/commit/e18089ae31446eeaff49c6e8a01250bc5cbe14ac))
96
- * **auth:** add auth & token methods ([a554ee9](https://github.com/Stun3R/strapi-sdk-js/commit/a554ee9930f3dc87e792afaba571e84143707528))
97
- * **core:** add crud methods, typescript support & graphql method ([cbe3b47](https://github.com/Stun3R/strapi-sdk-js/commit/cbe3b475dcdf49e3d6a37d594de2c69db8e185f3))
98
-