fluidd

Форк
0
/
httpClientActions.ts 
208 строк · 5.2 Кб
1
import Vue from 'vue'
2
import type { AxiosRequestConfig, AxiosResponse } from 'axios'
3

4
export const httpClientActions = {
5
  get<T = unknown, R = AxiosResponse<T>, D = unknown> (url: string, options?: AxiosRequestConfig) {
6
    return Vue.$httpClient.get<T, R, D>(url, options)
7
  },
8

9
  post<T = unknown, R = AxiosResponse<T>, D = unknown> (url: string, data: D, options?: AxiosRequestConfig) {
10
    return Vue.$httpClient.post<T, R, D>(url, data, options)
11
  },
12

13
  postForm<T = unknown, R = AxiosResponse<T>, D = unknown> (url: string, data: D, options?: AxiosRequestConfig) {
14
    return Vue.$httpClient.postForm<T, R, D>(url, data, options)
15
  },
16

17
  delete<T = unknown, R = AxiosResponse<T>, D = unknown> (url: string, options?: AxiosRequestConfig) {
18
    return Vue.$httpClient.delete<T, R, D>(url, options)
19
  },
20

21
  get defaults () {
22
    return Vue.$httpClient.defaults
23
  },
24

25
  accessInfoGet (options?: AxiosRequestConfig) {
26
    return this.get<{
27
      result: {
28
        default_source: string,
29
        available_sources: string[]
30
      }
31
    }>('/access/info', options)
32
  },
33

34
  accessRefreshJwtPost (refresh_token: string, options?: AxiosRequestConfig) {
35
    return this.post<{
36
      result: {
37
        username: string,
38
        token: string,
39
        action: string,
40
        source: string
41
      }
42
    }>('/access/refresh_jwt', { refresh_token }, options)
43
  },
44

45
  accessLoginPost (username: string, password: string, source = 'moonraker', options?: AxiosRequestConfig) {
46
    return this.post<{
47
      result: {
48
        username: string,
49
        token: string,
50
        refresh_token: string,
51
        action: string,
52
        source: string
53
      }
54
    }>('/access/login', {
55
      username,
56
      password,
57
      source
58
    }, options)
59
  },
60

61
  accessLogoutPost (options?: AxiosRequestConfig) {
62
    return this.post<{
63
      result: {
64
        username: string,
65
        action: string
66
      }
67
    }>('access/logout', undefined, options)
68
  },
69

70
  accessOneshotTokenGet (options?: AxiosRequestConfig) {
71
    return this.get<{
72
      result: string
73
    }>('/access/oneshot_token', options)
74
  },
75

76
  accessCurrentUserGet (options?: AxiosRequestConfig) {
77
    return this.get<{
78
      result: {
79
        username: string,
80
        source: string,
81
        created_on: number
82
      }
83
    }>('/access/user', options)
84
  },
85

86
  accessUsersListGet (options?: AxiosRequestConfig) {
87
    return this.get<{
88
      result: {
89
        users: Array<{
90
          username: string,
91
          source: string,
92
          created_on: number
93
        }>
94
      }
95
    }>('/access/users/list', options)
96
  },
97

98
  accessUserPost (username: string, password: string, options?: AxiosRequestConfig) {
99
    return this.post<{
100
      result: {
101
        username: string,
102
        token: string,
103
        refresh_token: string,
104
        action: string,
105
        source: string
106
      }
107
    }>('/access/user', {
108
      username,
109
      password
110
    }, options)
111
  },
112

113
  accessUserDelete (username: string, options?: AxiosRequestConfig) {
114
    return this.delete<{
115
      result: {
116
        username: string,
117
        action: string
118
      }
119
    }>('/access/user', {
120
      ...options,
121
      params: { username }
122
    })
123
  },
124

125
  accessUserPasswordPost (password: string, new_password: string, options?: AxiosRequestConfig) {
126
    return this.post<{
127
      result: {
128
        username: string,
129
        action: string
130
      }
131
    }>('/access/user/password', {
132
      password,
133
      new_password
134
    }, options)
135
  },
136

137
  accessApiKeyGet (options?: AxiosRequestConfig) {
138
    return this.get<{
139
      result: string
140
    }>('/access/api_key', options)
141
  },
142

143
  accessApiKeyPost (options?: AxiosRequestConfig) {
144
    return this.post<{
145
      result: string
146
    }>('/access/api_key', undefined, options)
147
  },
148

149
  serverDatabaseItemGet<T = unknown> (namespace: string, options?: AxiosRequestConfig) {
150
    return this.get<{
151
      result: {
152
        namespace: string,
153
        key: string,
154
        value: T
155
      }
156
    }>(`/server/database/item?namespace=${namespace}`, options)
157
  },
158

159
  serverDatabaseItemPost<T = unknown> (namespace: string, key: string, value: T, options?: AxiosRequestConfig) {
160
    return this.post<{
161
      result: {
162
        namespace: string,
163
        key: string,
164
        value: T
165
      }
166
    }>('/server/database/item', {
167
      namespace,
168
      key,
169
      value
170
    }, options)
171
  },
172

173
  serverDatabaseItemDelete<T = unknown> (namespace: string, key: string, options?: AxiosRequestConfig) {
174
    return this.delete<{
175
      result: {
176
        namespace: string,
177
        key: string,
178
        value: T
179
      }
180
    }>(`/server/database/item?namespace=${namespace}&key=${key}`, options)
181
  },
182

183
  serverFilesUploadPost (file: File, path: string, root: string, print?: boolean, options?: AxiosRequestConfig) {
184
    const formData = new FormData()
185

186
    formData.append('file', file, file.name)
187
    formData.append('path', path)
188
    formData.append('root', root)
189
    if (print) {
190
      formData.append('print', 'true')
191
    }
192

193
    return this.postForm<{
194
      result: {
195
        item: {
196
          path: string,
197
          root: string
198
        }
199
        print_started?: boolean,
200
        action: string
201
      }
202
    }>('/server/files/upload', formData, options)
203
  },
204

205
  serverFilesGet<T = unknown> (filepath: string, options?: AxiosRequestConfig) {
206
    return this.get<T>(`/server/files/${encodeURI(filepath)}?date=${Date.now()}`, options)
207
  }
208
}
209

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.