forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.ts
More file actions
160 lines (134 loc) · 4.33 KB
/
http.ts
File metadata and controls
160 lines (134 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import axios, { AxiosInstance } from 'axios';
import { store } from '@/state';
const http: AxiosInstance = axios.create({
withCredentials: true,
timeout: 20000,
headers: {
'X-Requested-With': 'XMLHttpRequest',
Accept: 'application/json',
'Content-Type': 'application/json',
},
});
http.interceptors.request.use((req) => {
if (!req.url?.endsWith('/resources')) {
store.getActions().progress.startContinuous();
}
return req;
});
http.interceptors.response.use(
(resp) => {
if (!resp.request?.url?.endsWith('/resources')) {
store.getActions().progress.setComplete();
}
return resp;
},
(error) => {
store.getActions().progress.setComplete();
throw error;
}
);
export default http;
/**
* Converts an error into a human readable response. Mostly just a generic helper to
* make sure we display the message from the server back to the user if we can.
*/
export function httpErrorToHuman(error: any): string {
if (error.response && error.response.data) {
let { data } = error.response;
// Some non-JSON requests can still return the error as a JSON block. In those cases, attempt
// to parse it into JSON so we can display an actual error.
if (typeof data === 'string') {
try {
data = JSON.parse(data);
} catch (e) {
// do nothing, bad json
}
}
if (data.errors && data.errors[0] && data.errors[0].detail) {
return data.errors[0].detail;
}
// Errors from wings directory, mostly just for file uploads.
if (data.error && typeof data.error === 'string') {
return data.error;
}
}
return error.message;
}
export interface FractalResponseData {
object: string;
attributes: {
[k: string]: any;
relationships?: Record<string, FractalResponseData | FractalResponseList | null | undefined>;
};
}
export interface FractalResponseList {
object: 'list';
data: FractalResponseData[];
}
export interface FractalPaginatedResponse extends FractalResponseList {
meta: {
pagination: {
total: number;
count: number;
/* eslint-disable camelcase */
per_page: number;
current_page: number;
total_pages: number;
/* eslint-enable camelcase */
};
};
}
export interface PaginatedResult<T> {
items: T[];
pagination: PaginationDataSet;
}
export interface PaginationDataSet {
total: number;
count: number;
perPage: number;
currentPage: number;
totalPages: number;
}
export function getPaginationSet(data: any): PaginationDataSet {
return {
total: data.total,
count: data.count,
perPage: data.per_page,
currentPage: data.current_page,
totalPages: data.total_pages,
};
}
type QueryBuilderFilterValue = string | number | boolean | null;
export interface QueryBuilderParams<FilterKeys extends string = string, SortKeys extends string = string> {
page?: number;
filters?: {
[K in FilterKeys]?: QueryBuilderFilterValue | Readonly<QueryBuilderFilterValue[]>;
};
sorts?: {
[K in SortKeys]?: -1 | 0 | 1 | 'asc' | 'desc' | null;
};
}
/**
* Helper function that parses a data object provided and builds query parameters
* for the Laravel Query Builder package automatically. This will apply sorts and
* filters deterministically based on the provided values.
*/
export const withQueryBuilderParams = (data?: QueryBuilderParams): Record<string, unknown> => {
if (!data) return {};
const filters = Object.keys(data.filters || {}).reduce((obj, key) => {
const value = data.filters?.[key];
return !value || value === '' ? obj : { ...obj, [`filter[${key}]`]: value };
}, {} as NonNullable<QueryBuilderParams['filters']>);
const sorts = Object.keys(data.sorts || {}).reduce((arr, key) => {
const value = data.sorts?.[key];
if (!value || !['asc', 'desc', 1, -1].includes(value)) {
return arr;
}
return [...arr, (value === -1 || value === 'desc' ? '-' : '') + key];
}, [] as string[]);
return {
...filters,
sort: !sorts.length ? undefined : sorts.join(','),
page: data.page,
};
};