|
| 1 | +import axios from 'axios' |
| 2 | + |
| 3 | +class Auth { |
| 4 | + constructor(api) { |
| 5 | + this.api = api |
| 6 | + } |
| 7 | + |
| 8 | + login = async (email, password) => { |
| 9 | + const formData = new FormData() |
| 10 | + formData.append('username', email) |
| 11 | + formData.append('password', password) |
| 12 | + return await this.api.post('/auth/login', formData) |
| 13 | + } |
| 14 | + |
| 15 | + logout = async () => await this.api.post('/auth/logout', {}) |
| 16 | +} |
| 17 | + |
| 18 | +class Me { |
| 19 | + constructor(api) { |
| 20 | + this.api = api |
| 21 | + } |
| 22 | + |
| 23 | + get = async () => await this.api.get('/users/me') |
| 24 | + patch = async (data) => await this.api.patch('/users/me', data) |
| 25 | +} |
| 26 | + |
| 27 | +class User { |
| 28 | + constructor(api) { |
| 29 | + this.api = api |
| 30 | + } |
| 31 | + |
| 32 | + getList = async (Page = 0, PerPage = 10) => |
| 33 | + await this.api.get('/users', { |
| 34 | + headers: { Page, PerPage }, |
| 35 | + }) |
| 36 | + |
| 37 | + get = async (id) => await this.api.get(`/users/${id}`) |
| 38 | + patch = async (id, data) => await this.api.patch(`/users/${id}`, data) |
| 39 | + delete = async (id) => await this.api.delete(`/users/${id}`) |
| 40 | +} |
| 41 | + |
| 42 | +export class API { |
| 43 | + constructor(prefix = '/api/v0', config = {}) { |
| 44 | + this.api = axios.create({ |
| 45 | + baseURL: prefix, |
| 46 | + withCredentials: true, |
| 47 | + ...config, |
| 48 | + }) |
| 49 | + this.auth = new Auth(this) |
| 50 | + this.me = new Me(this) |
| 51 | + this.user = new User(this) |
| 52 | + } |
| 53 | + |
| 54 | + get = async (...args) => { |
| 55 | + try { |
| 56 | + const response = await this.api.get(...args) |
| 57 | + return { ...response.data, ok: true } |
| 58 | + } catch (error) { |
| 59 | + return { ...error, ok: false } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + patch = async (...args) => { |
| 64 | + try { |
| 65 | + const response = await this.api.patch(...args) |
| 66 | + return { ...response.data, ok: true } |
| 67 | + } catch (error) { |
| 68 | + return { ...error, ok: false } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + post = async (...args) => { |
| 73 | + try { |
| 74 | + const response = await this.api.post(...args) |
| 75 | + return { ...response.data, ok: true } |
| 76 | + } catch (error) { |
| 77 | + return { ...error, ok: false } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + delete = async (...args) => { |
| 82 | + try { |
| 83 | + const response = await this.api.delete(...args) |
| 84 | + return { ...response.data, ok: true } |
| 85 | + } catch (error) { |
| 86 | + return { ...error, ok: false } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + errors = (response) => { |
| 91 | + const res = response.response |
| 92 | + const data = res && res.data && typeof res.data !== 'string' ? res.data : {} |
| 93 | + if (!data.message) { |
| 94 | + if (data.msg) { |
| 95 | + data.message = data.msg |
| 96 | + } else if (data.statusText) { |
| 97 | + data.message = data.statusText |
| 98 | + } else if (data.status) { |
| 99 | + data.message = data.status |
| 100 | + } else if (res.statusText) { |
| 101 | + data.message = res.statusText |
| 102 | + } else if (res.status) { |
| 103 | + data.message = res.status |
| 104 | + } |
| 105 | + } |
| 106 | + if (data.errors) { |
| 107 | + Object.getOwnPropertyNames(data.errors).forEach((property) => { |
| 108 | + if (property !== 'message') { |
| 109 | + data.errors[property] = data.errors[property].join(' ') |
| 110 | + } |
| 111 | + }) |
| 112 | + } |
| 113 | + return data |
| 114 | + } |
| 115 | +} |
0 commit comments