|
1 | | -import axios, { AxiosInstance, AxiosRequestConfig } from 'axios' |
2 | | -import qs from 'qs' |
| 1 | +import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'; |
| 2 | +import * as qs from 'qs'; |
3 | 3 |
|
4 | | -export class Strapi { |
5 | | - private axios: AxiosInstance |
6 | | - private authentication: object = {} |
| 4 | +export default class Strapi { |
| 5 | + private axios: AxiosInstance; |
7 | 6 |
|
8 | | - /** |
9 | | - * Default constructor. |
10 | | - * @param baseURL Your Strapi host. |
11 | | - * @param axiosConfig Extend Axios configuration. |
12 | | - */ |
13 | | - constructor (baseURL: string, axiosConfig?: AxiosRequestConfig) { |
14 | | - this.axios = axios.create({ |
15 | | - baseURL, |
16 | | - paramsSerializer: qs.stringify, |
17 | | - ...axiosConfig |
18 | | - }) |
19 | | - } |
| 7 | + /** |
| 8 | + * Default constructor. |
| 9 | + * @param baseURL Your Strapi host. |
| 10 | + * @param axiosConfig Extend Axios configuration. |
| 11 | + */ |
| 12 | + constructor(baseURL: string, axiosConfig?: AxiosRequestConfig) { |
| 13 | + this.axios = axios.create({ |
| 14 | + baseURL, |
| 15 | + paramsSerializer: qs.stringify, |
| 16 | + ...axiosConfig |
| 17 | + }); |
| 18 | + } |
20 | 19 |
|
21 | | - /** |
22 | | - * Register a new user. |
23 | | - * @param username |
24 | | - * @param email |
25 | | - * @param password |
26 | | - */ |
27 | | - public async register(username: string, email: string, password: string): Promise<object> { |
28 | | - this.authentication = await this.axios.post('/auth/local/register', { |
29 | | - email, |
30 | | - password, |
31 | | - username |
32 | | - }) |
33 | | - return this.authentication |
34 | | - } |
| 20 | + /** |
| 21 | + * Register a new user. |
| 22 | + * @param username |
| 23 | + * @param email |
| 24 | + * @param password |
| 25 | + * @returns Authentication object with user and token |
| 26 | + */ |
| 27 | + public async register( |
| 28 | + username: string, |
| 29 | + email: string, |
| 30 | + password: string |
| 31 | + ): Promise<object> { |
| 32 | + this.clearToken(); |
| 33 | + const authentication = await this.request('post', '/auth/local/register', { |
| 34 | + data: { |
| 35 | + email, |
| 36 | + password, |
| 37 | + username |
| 38 | + } |
| 39 | + }); |
| 40 | + this.setToken(authentication.jwt); |
| 41 | + return authentication; |
| 42 | + } |
35 | 43 |
|
36 | | - /** |
37 | | - * Login by getting an authentication token. |
38 | | - * @param identifier Can either be an email or a username. |
39 | | - * @param password |
40 | | - */ |
41 | | - public async login(identifier: string, password: string): Promise<object> { |
42 | | - this.authentication = await this.axios.post('/auth/local', { |
43 | | - identifier, |
44 | | - password |
45 | | - }) |
46 | | - return this.authentication |
47 | | - } |
| 44 | + /** |
| 45 | + * Login by getting an authentication token. |
| 46 | + * @param identifier Can either be an email or a username. |
| 47 | + * @param password |
| 48 | + * @returns Authentication object with user and token |
| 49 | + */ |
| 50 | + public async login(identifier: string, password: string): Promise<object> { |
| 51 | + this.clearToken(); |
| 52 | + const authentication = await this.request('post', '/auth/local', { |
| 53 | + data: { |
| 54 | + identifier, |
| 55 | + password |
| 56 | + } |
| 57 | + }); |
| 58 | + this.setToken(authentication.jwt); |
| 59 | + return authentication; |
| 60 | + } |
48 | 61 |
|
49 | | - /** |
50 | | - * Sends an email to a user with the link of your reset password page. |
51 | | - * This link contains an URL param code which is required to reset user password. |
52 | | - * Received link url format http://my-domain.com/rest-password?code=privateCode. |
53 | | - * @param email |
54 | | - * @param url Link that user will receive. |
55 | | - */ |
56 | | - public async forgotPassword(email: string, url: string): Promise<any> { |
57 | | - await this.axios.post('/auth/forgot-password', { |
58 | | - email, |
59 | | - url |
60 | | - }) |
61 | | - } |
| 62 | + /** |
| 63 | + * Sends an email to a user with the link of your reset password page. |
| 64 | + * This link contains an URL param code which is required to reset user password. |
| 65 | + * Received link url format https://my-domain.com/rest-password?code=privateCode. |
| 66 | + * @param email |
| 67 | + * @param url Link that user will receive. |
| 68 | + */ |
| 69 | + public async forgotPassword(email: string, url: string): Promise<void> { |
| 70 | + this.clearToken(); |
| 71 | + await this.request('post', '/auth/forgot-password', { |
| 72 | + data: { |
| 73 | + email, |
| 74 | + url |
| 75 | + } |
| 76 | + }); |
| 77 | + } |
62 | 78 |
|
63 | | - /** |
64 | | - * Reset the user password. |
65 | | - * @param code Is the url params received from the email link (see forgot password). |
66 | | - * @param password |
67 | | - * @param passwordConfirmation |
68 | | - */ |
69 | | - public async resetPassword(code: string, password: string, passwordConfirmation: string): Promise<any> { |
70 | | - await this.axios.post('/auth/reset-password', { |
71 | | - code, |
72 | | - password, |
73 | | - passwordConfirmation |
74 | | - }) |
| 79 | + /** |
| 80 | + * Reset the user password. |
| 81 | + * @param code Is the url params received from the email link (see forgot password). |
| 82 | + * @param password |
| 83 | + * @param passwordConfirmation |
| 84 | + */ |
| 85 | + public async resetPassword( |
| 86 | + code: string, |
| 87 | + password: string, |
| 88 | + passwordConfirmation: string |
| 89 | + ): Promise<void> { |
| 90 | + this.clearToken(); |
| 91 | + await this.request('post', '/auth/reset-password', { |
| 92 | + data: { |
| 93 | + code, |
| 94 | + password, |
| 95 | + passwordConfirmation |
| 96 | + } |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Axios request |
| 102 | + * @param method Request method |
| 103 | + * @param url Server URL |
| 104 | + * @param requestConfig Custom Axios config |
| 105 | + * @returns Data |
| 106 | + */ |
| 107 | + public async request( |
| 108 | + method: string = 'get', |
| 109 | + url: string, |
| 110 | + requestConfig?: AxiosRequestConfig |
| 111 | + ): Promise<any> { |
| 112 | + try { |
| 113 | + const response: AxiosResponse = await this.axios.request({ |
| 114 | + method, |
| 115 | + url, |
| 116 | + ...requestConfig |
| 117 | + }); |
| 118 | + return response.data; |
| 119 | + } catch (error) { |
| 120 | + throw error.response.message; |
75 | 121 | } |
| 122 | + } |
| 123 | + |
| 124 | + private setToken(token: string): void { |
| 125 | + this.axios.defaults.headers.common.Authorization = 'Bearer ' + token; |
| 126 | + } |
| 127 | + |
| 128 | + private clearToken(): void { |
| 129 | + delete this.axios.defaults.headers.common.Authorization; |
| 130 | + } |
76 | 131 | } |
0 commit comments