This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
api.ts
232 lines (216 loc) · 7.41 KB
/
api.ts
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* TESLER-UI
* Copyright (C) 2018-2020 Tesler Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { applyParams, applyRawParams, axiosDelete, axiosGet, axiosPost, axiosPut, ApiCallContext } from '../utils/api'
import { buildUrl } from '../utils/history'
import { BcDataResponse, DataItem, DataItemResponse, PendingDataItem } from '../interfaces/data'
import { RowMetaResponse } from '../interfaces/rowMeta'
import { AssociatedItem } from '../interfaces/operation'
import { Observable } from 'rxjs/Observable'
import axios, { CancelToken } from 'axios'
type GetParamsMap = Record<string, string | number>
/**
* TODO
*
* @param path
* @param params
* @category Tesler API Endpoints
*/
export function routerRequest(path: string, params: Record<string, unknown>) {
return axiosGet(applyRawParams(path, params))
}
/**
* Send request for BC data
*
* @param screenName
* @param bcUrl
* @param params
* @param cancelToken
* @category Tesler API Endpoints
*/
export function fetchBcData(screenName: string, bcUrl: string, params: GetParamsMap = {}, cancelToken?: CancelToken) {
const noLimit = params._limit === 0
const queryStringObject = {
...params,
_page: !noLimit && ('_page' in params ? params._page : 1),
_limit: !noLimit && ('_limit' in params ? params._limit : 30)
}
const url = applyParams(buildUrl`data/${screenName}/` + bcUrl, queryStringObject)
return axiosGet<BcDataResponse>(url, { cancelToken })
}
/**
* TODO
*
* @param screenName
* @param bcUrl
* @param params
* @category Tesler API Endpoints
*/
export function fetchBcDataAll(screenName: string, bcUrl: string, params: GetParamsMap = {}) {
let currentPage = 1
return fetchBcData(screenName, bcUrl, { ...params, _page: currentPage })
.expand(response => {
return response.hasNext ? fetchBcData(screenName, bcUrl, { ...params, _page: ++currentPage }) : Observable.empty()
})
.reduce((items, nextResponse) => {
return [...items, ...nextResponse.data]
}, [] as DataItem[])
}
/**
* Send request for `row-meta`
*
* @param screenName
* @param bcUrl
* @param params
* @param cancelToken
* @category Tesler API Endpoints
*/
export function fetchRowMeta(screenName: string, bcUrl: string, params?: GetParamsMap, cancelToken?: CancelToken) {
const url = applyParams(buildUrl`row-meta/${screenName}/` + bcUrl, params)
return axiosGet<RowMetaResponse>(url, { cancelToken }).map(response => response.data.row)
}
/**
* Request to create a new data record
*
* @param screenName Screen name
* @param bcUrl Business component cursors hierarchy
* @param params
* @category Tesler API Endpoints
*/
export function newBcData(screenName: string, bcUrl: string, context: ApiCallContext, params?: GetParamsMap) {
const url = applyParams(buildUrl`row-meta-new/${screenName}/` + bcUrl, params)
return axiosGet<RowMetaResponse>(url, null, context).map(response => response.data)
}
/**
* Request to save a data record
*
* @param screenName Screen name
* @param bcUrl Business component cursors hierarchy
* @param data Changed fields
* @param context Call context
* @param params
* @category Tesler API Endpoints
*/
export function saveBcData(
screenName: string,
bcUrl: string,
data: PendingDataItem & { vstamp: number },
context: ApiCallContext,
params?: GetParamsMap
) {
const url = applyParams(buildUrl`data/${screenName}/` + bcUrl, params)
return axiosPut<DataItemResponse>(url, { data }, context).map(response => response.data)
}
/**
* Request to delete a data record
*
* @param screenName Screen name
* @param bcUrl Business component cursors hierarchy
* @param context Call context
* @param params
* @category Tesler API Endpoints
*/
export function deleteBcData(screenName: string, bcUrl: string, context: ApiCallContext, params?: GetParamsMap) {
const url = applyParams(buildUrl`data/${screenName}/` + bcUrl, params)
return axiosDelete<DataItemResponse>(url, context).map(response => response.data)
}
/**
* Request for a custom operation
*
* @param screenName Screen name
* @param bcUrl Business component cursors hierarchy
* @param data Changed fields
* @param context Call context
* @param params
* @category Tesler API Endpoints
*/
export function customAction(screenName: string, bcUrl: string, data: Record<string, any>, context: ApiCallContext, params?: GetParamsMap) {
const url = applyParams(buildUrl`custom-action/${screenName}/` + bcUrl, params)
return axiosPost<DataItemResponse>(url, { data: data || {} }, null, context).map(response => response.data)
}
/**
* Request to save an association (many-to-many relation)
* - When request doest specify `bcUrl`, request body should have an array of records with a flag
* to set or drop an association.
* - When request does not specify `bcUrl`, request body should have a dictionary where key is `bcUrl`
* for business component and value is an array of records with associate flag
*
* @param screenName Screen name
* @param bcUrl Business component cursors hierarchy
* @param data An array of records or a dictionary
* @param params
* @category Tesler API Endpoints
*/
export function associate(
screenName: string,
bcUrl: string,
data: AssociatedItem[] | Record<string, AssociatedItem[]>,
params?: GetParamsMap
) {
// TODO: Why Tesler API sends underscored `_associate` but expects `associated` in return?
const processedData = Array.isArray(data)
? data.map(item => ({
id: item.id,
vstamp: item.vstamp,
associated: item._associate
}))
: data
const url = applyParams(buildUrl`associate/${screenName}/` + bcUrl, params)
return axiosPost<any>(url, processedData).map(response => response.data)
}
/**
* Request row meta with preview of force-active changes
*
* @param screenName Screen name
* @param bcUrl Business component cursors hierarchy
* @param data Changed fields
* @param params
* @category Tesler API Endpoints
*/
export function getRmByForceActive(screenName: string, bcUrl: string, data: PendingDataItem & { vstamp: number }, params?: GetParamsMap) {
const url = applyParams(buildUrl`row-meta/${screenName}/` + bcUrl, params)
return axiosPost<RowMetaResponse>(url, { data }).map(response => response.data.row)
}
/**
* Request for refresh screens, views and widgets meta
*
* @category Tesler API Endpoints
*/
export function refreshMeta() {
return axiosGet(buildUrl`bc-registry/refresh-meta`)
}
/**
* Request for role switching
*
* @param role Code of new role
* @category Tesler API Endpoints
*/
export function loginByRoleRequest(role: string) {
return axiosGet(buildUrl`login?role=${role}`)
}
/**
* Returns new cancel token and cancel callback
*/
export function createCanceler() {
let cancel: () => void
const cancelToken = new axios.CancelToken(c => {
cancel = c
})
return {
cancel,
cancelToken
}
}