strapi-sdk-js 2.1.2 → 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 CHANGED
@@ -1,3 +1,23 @@
1
+ # [2.2.0](https://github.com/Stun3R/strapi-sdk-js/compare/v2.1.5...v2.2.0) (2022-02-17)
2
+
3
+ ## [2.1.5](https://github.com/Stun3R/strapi-sdk-js/compare/v2.1.4...v2.1.5) (2022-01-17)
4
+
5
+ ## [2.1.4](https://github.com/Stun3R/strapi-sdk-js/compare/v2.1.3...v2.1.4) (2022-01-14)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * **options:** remove process usage in default to be compatible with more framework ([aaa162d](https://github.com/Stun3R/strapi-sdk-js/commit/aaa162d53bc656a149da8d2aacc9560a42a11d55))
11
+
12
+ ## [2.1.3](https://github.com/Stun3R/strapi-sdk-js/compare/v2.1.2...v2.1.3) (2022-01-05)
13
+
14
+
15
+ ### Bug Fixes
16
+
17
+ * **deps:** pin dependencies ([2e6e5a9](https://github.com/Stun3R/strapi-sdk-js/commit/2e6e5a984e3c3bce226bdb0a55218d6707cc0488))
18
+ * **deps:** update dependency @nuxt/content-theme-docs to v0.11.1 ([1541e39](https://github.com/Stun3R/strapi-sdk-js/commit/1541e392bb9e70ee7cdc65fa1362358518c0d084))
19
+ * **deps:** update dependency axios to ^0.24.0 ([4e2a41d](https://github.com/Stun3R/strapi-sdk-js/commit/4e2a41d5a3ffca07cf502d488ce9caea809f4bbc))
20
+
1
21
  ## [2.1.2](https://github.com/Stun3R/strapi-sdk-js/compare/v2.1.1...v2.1.2) (2021-12-30)
2
22
 
3
23
 
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
@@ -17,7 +17,7 @@ function isBrowser() {
17
17
  }
18
18
 
19
19
  const defaults = {
20
- url: process.env.STRAPI_URL || "http://localhost:1337",
20
+ url: "http://localhost:1337",
21
21
  prefix: "/api",
22
22
  store: {
23
23
  key: "strapi_jwt",
@@ -28,20 +28,23 @@ const defaults = {
28
28
  };
29
29
  class Strapi {
30
30
  constructor(options) {
31
- this._user = null;
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.syncToken();
39
- }
40
- get user() {
41
- return this._user;
42
- }
43
- set user(user) {
44
- this._user = user;
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.setUser(user);
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.setUser(user);
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.setUser(user);
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.setUser(user);
126
+ this.user = user;
124
127
  return { user, jwt };
125
128
  }
126
129
  logout() {
127
- this.setUser(null);
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.setUser(user);
163
+ this.user = user;
164
164
  } catch (e) {
165
165
  this.logout();
166
166
  }
167
- return this._user;
167
+ return this.user;
168
168
  }
169
- syncToken() {
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
- this.axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
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,19 +83,17 @@ interface StrapiEmailConfirmationData {
83
83
  declare class Strapi {
84
84
  axios: AxiosInstance;
85
85
  options: StrapiDefaultOptions;
86
- private _user;
86
+ user: StrapiUser;
87
87
  /**
88
88
  * Strapi SDK Constructor
89
89
  *
90
90
  * @constructor
91
91
  * @param {StrapiOptions} options? - Options in order to configure API URL, list your Content Types & extend the axios configuration
92
- * @param {string} options.url? - Your Strapi API URL, Default: process.env.STRAPI_URL || http://localhost::1337
92
+ * @param {string} options.url? - Your Strapi API URL, Default: http://localhost::1337
93
93
  * @param {StoreConfig} options.store? - Config the way you want to store JWT (Cookie or LocalStorage)
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
- * Sync token between storage & header when SDK is instantiate
224
+ * Retrieve token from chosen storage
234
225
  *
235
- * @returns void
226
+ * @returns string | null
236
227
  */
237
- private syncToken;
228
+ getToken(): string | null;
238
229
  /**
239
- * Set token in Axios headers & in choosen storage
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 in Axios headers & in choosen storage (Cookies or Local)
237
+ * Remove token from chosen storage (Cookies or Local)
247
238
  *
248
239
  * @returns void
249
240
  */
@@ -8,7 +8,7 @@ function isBrowser() {
8
8
  }
9
9
 
10
10
  const defaults = {
11
- url: process.env.STRAPI_URL || "http://localhost:1337",
11
+ url: "http://localhost:1337",
12
12
  prefix: "/api",
13
13
  store: {
14
14
  key: "strapi_jwt",
@@ -19,20 +19,23 @@ const defaults = {
19
19
  };
20
20
  class Strapi {
21
21
  constructor(options) {
22
- this._user = null;
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.syncToken();
30
- }
31
- get user() {
32
- return this._user;
33
- }
34
- set user(user) {
35
- this._user = user;
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.setUser(user);
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.setUser(user);
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.setUser(user);
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.setUser(user);
117
+ this.user = user;
115
118
  return { user, jwt };
116
119
  }
117
120
  logout() {
118
- this.setUser(null);
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.setUser(user);
154
+ this.user = user;
155
155
  } catch (e) {
156
156
  this.logout();
157
157
  }
158
- return this._user;
158
+ return this.user;
159
159
  }
160
- syncToken() {
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
- this.axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "strapi-sdk-js",
3
- "version": "2.1.2",
3
+ "version": "2.2.0",
4
4
  "description": "📦 Javascript SDK for your Strapi API",
5
5
  "keywords": [
6
6
  "strapi",
@@ -37,13 +37,13 @@
37
37
  ],
38
38
  "exports": {
39
39
  ".": {
40
- "require": "./dist/index.js",
41
- "import": "./dist/index.es.js"
40
+ "require": "./dist/index.cjs",
41
+ "import": "./dist/index.mjs"
42
42
  },
43
43
  "./package.json": "./package.json"
44
44
  },
45
- "main": "dist/index.js",
46
- "module": "dist/index.es.js",
45
+ "main": "dist/index.cjs",
46
+ "module": "dist/index.mjs",
47
47
  "types": "dist/index.d.ts",
48
48
  "files": [
49
49
  "dist/"
@@ -71,31 +71,31 @@
71
71
  "dependencies": {
72
72
  "@types/js-cookie": "^3.0.0",
73
73
  "@types/qs": "^6.9.6",
74
- "axios": "^0.23.0",
74
+ "axios": "^0.24.0",
75
75
  "defu": "^5.0.0",
76
76
  "js-cookie": "^3.0.0",
77
77
  "qs": "^6.10.1"
78
78
  },
79
79
  "devDependencies": {
80
- "@commitlint/cli": "13.2.1",
81
- "@commitlint/config-conventional": "13.2.0",
82
- "@release-it/conventional-changelog": "3.3.0",
83
- "@types/jest": "27.0.2",
84
- "@types/sinon": "10.0.4",
85
- "@typescript-eslint/eslint-plugin": "5.1.0",
86
- "@typescript-eslint/parser": "5.1.0",
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
87
  "commitizen": "4.2.4",
88
88
  "cz-conventional-changelog": "3.3.0",
89
- "dotenv-cli": "4.0.0",
90
- "eslint": "8.0.1",
91
- "husky": "7.0.2",
92
- "jest": "27.3.1",
93
- "lint-staged": "11.2.3",
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
94
  "npm-run-all": "4.1.5",
95
- "release-it": "14.11.6",
96
- "sinon": "11.1.2",
95
+ "release-it": "14.12.3",
96
+ "sinon": "12.0.1",
97
97
  "siroc": "0.16.0",
98
- "ts-jest": "27.0.7",
99
- "typescript": "4.4.4"
98
+ "ts-jest": "27.1.3",
99
+ "typescript": "4.5.4"
100
100
  }
101
101
  }