-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathqueries.ts
More file actions
384 lines (320 loc) · 13.4 KB
/
queries.ts
File metadata and controls
384 lines (320 loc) · 13.4 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import axios from 'axios';
import type { QueryClient } from '@tanstack/react-query';
import type { AxiosRequestConfig, AxiosResponse } from 'axios';
import type { JSONSchema7 } from 'json-schema';
import { LAYOUT_SCHEMA_NAME } from 'src/features/devtools/utils/layoutSchemaValidation';
import { removeProcessFromInstance } from 'src/features/instance/instanceUtils';
import { signingQueries } from 'src/layout/SigneeList/api';
import { getFileContentType } from 'src/utils/attachmentsUtils';
import { httpDelete, httpGetRaw, httpPatch, httpPost, putWithoutConfig } from 'src/utils/network/networking';
import { httpGet, httpPut } from 'src/utils/network/sharedNetworking';
import {
applicationLanguagesUrl,
applicationMetadataApiUrl,
applicationSettingsApiUrl,
appPath,
getActionsUrl,
getActiveInstancesUrl,
getCreateInstancesUrl,
getCustomValidationConfigUrl,
getDataElementIdUrl,
getDataElementUrl,
getDataModelTypeUrl,
getDataValidationUrl,
getFetchFormDynamicsUrl,
getFileTagUrl,
getFileUploadUrl,
getFileUploadUrlOld,
getFooterLayoutUrl,
getInstanceLayoutsUrl,
getInstantiateUrl,
getJsonSchemaUrl,
getLayoutSetsUrl,
getLayoutSettingsUrl,
getLayoutsUrl,
getOrderDetailsUrl,
getPaymentInformationUrl,
getPdfFormatUrl,
getProcessNextUrl,
getProcessStateUrl,
getRedirectUrl,
getRulehandlerUrl,
getSetSelectedPartyUrl,
getUpdateFileTagsUrl,
getValidationUrl,
instancesControllerUrl,
profileApiUrl,
refreshJwtTokenUrl,
selectedPartyUrl,
textResourcesUrl,
validPartiesUrl,
} from 'src/utils/urls/appUrlHelper';
import { customEncodeURI, orgsListUrl } from 'src/utils/urls/urlHelper';
import type { IncomingApplicationMetadata } from 'src/features/applicationMetadata/types';
import type { DataPostResponse } from 'src/features/attachments';
import type { IDataList } from 'src/features/dataLists';
import type { IFooterLayout } from 'src/features/footer/types';
import type { IFormDynamics } from 'src/features/form/dynamics';
import type {
IDataModelMultiPatchRequest,
IDataModelMultiPatchResponse,
IDataModelPatchRequest,
IDataModelPatchResponse,
} from 'src/features/formData/types';
import type { Instantiation } from 'src/features/instantiate/useInstantiation';
import type { ITextResourceResult } from 'src/features/language/textResources';
import type { OrderDetails, PaymentResponsePayload } from 'src/features/payment/types';
import type { IPdfFormat } from 'src/features/pdf/types';
import type {
BackendValidationIssue,
BackendValidationIssuesWithSource,
IExpressionValidationConfig,
} from 'src/features/validation';
import type { ILayoutSets, ILayoutSettings, IRawOption } from 'src/layout/common.generated';
import type { ActionResult } from 'src/layout/CustomButton/CustomButtonComponent';
import type { ILayoutCollection } from 'src/layout/layout';
import type { ISimpleInstance, LooseAutocomplete } from 'src/types';
import type {
IActionType,
IAltinnOrgs,
IAppLanguage,
IApplicationSettings,
IData,
IInstance,
IParty,
IProcess,
IProfile,
} from 'src/types/shared';
export const doSetSelectedParty = (partyId: number | string) =>
putWithoutConfig<LooseAutocomplete<'Party successfully updated'> | null>(getSetSelectedPartyUrl(partyId));
export const doInstantiateWithPrefill = async (data: Instantiation, language?: string): Promise<IInstance> =>
removeProcessFromInstance((await httpPost<IInstance>(getInstantiateUrl(language), undefined, data)).data);
export const doInstantiate = async (partyId: number, language?: string): Promise<IInstance> =>
removeProcessFromInstance((await httpPost<IInstance>(getCreateInstancesUrl(partyId, language))).data);
export const doProcessNext = async (instanceId: string, language?: string, action?: IActionType) =>
httpPut<IProcess>(getProcessNextUrl(instanceId, language), action ? { action } : null);
export const doAttachmentUploadOld = async (instanceId: string, dataTypeId: string, file: File): Promise<IData> => {
const url = getFileUploadUrlOld(instanceId, dataTypeId);
const contentType = getFileContentType(file);
const config: AxiosRequestConfig = {
headers: {
'Content-Type': contentType,
'Content-Disposition': `attachment; filename=${customEncodeURI(file.name)}`,
},
};
return (await httpPost<IData>(url, config, file)).data;
};
export const doAttachmentUpload = async (
instanceId: string,
dataTypeId: string,
language: string,
file: File,
): Promise<DataPostResponse> => {
const url = getFileUploadUrl(instanceId, dataTypeId, language);
const contentType = getFileContentType(file);
const config: AxiosRequestConfig = {
headers: {
'Content-Type': contentType,
'Content-Disposition': `attachment; filename=${customEncodeURI(file.name)}`,
},
};
return (await httpPost<DataPostResponse>(url, config, file)).data;
};
export const doAttachmentRemoveTag = async (
instanceId: string,
dataElementId: string,
tagToRemove: string,
): Promise<void> => {
await httpDelete(getFileTagUrl(instanceId, dataElementId, tagToRemove));
};
export const doAttachmentAddTag = async (
instanceId: string,
dataElementId: string,
tagToAdd: string,
): Promise<void> => {
const response = await httpPost(
getFileTagUrl(instanceId, dataElementId, undefined),
{
headers: {
'Content-Type': 'application/json',
},
},
JSON.stringify(tagToAdd),
);
if (response.status !== 201) {
throw new Error('Failed to add tag to attachment');
}
return;
};
export type SetTagsRequest = {
tags: string[];
};
type UpdateTagsResponse = {
tags: string[];
validationIssues?: BackendValidationIssuesWithSource[];
};
export const doUpdateAttachmentTags = async ({
instanceId,
dataElementId,
setTagsRequest,
}: {
instanceId: string;
dataElementId: string;
setTagsRequest: SetTagsRequest;
}) => {
const url = getUpdateFileTagsUrl(instanceId, dataElementId);
const response = await httpPut<UpdateTagsResponse, SetTagsRequest>(url, setTagsRequest, {
headers: {
'Content-Type': 'application/json',
},
});
if (response.status !== 200) {
throw new Error('Failed to update tags on attachment');
}
return response.data;
};
type UserActionRequest = {
action?: string;
buttonId?: string;
metadata?: Record<string, string>;
ignoredValidators?: string[];
onBehalfOf?: string;
};
export const doPerformAction = async (
partyId: string,
instanceGuid: string,
actionRequest: UserActionRequest,
language: string,
queryClient: QueryClient,
): Promise<ActionResult> => {
const response = await httpPost<ActionResult>(
getActionsUrl(partyId, instanceGuid, language),
undefined,
actionRequest,
);
if (response.status < 200 || response.status >= 300) {
throw new Error('Failed to perform action');
}
if (actionRequest.action === 'sign') {
queryClient.invalidateQueries({ queryKey: signingQueries.all });
}
return response.data;
};
export const doAttachmentRemove = async (
instanceId: string,
dataElementId: string,
language: string,
): Promise<void> => {
const response = await httpDelete(getDataElementUrl(instanceId, dataElementId, language));
if (response.status < 200 || response.status >= 300) {
throw new Error('Failed to remove attachment');
}
};
export const doSubformEntryAdd = async (instanceId: string, dataType: string, data: unknown): Promise<IData> => {
const response = await httpPost<IData>(getDataModelTypeUrl(instanceId, dataType), undefined, data);
if (response.status >= 300) {
throw new Error('Failed to add sub form');
}
return response.data;
};
export const doSubformEntryDelete = async (instanceId: string, dataElementId: string): Promise<void> => {
const response = await httpDelete(getDataElementIdUrl(instanceId, dataElementId));
if (response.status < 200 || response.status >= 300) {
throw new Error('Failed to delete sub form');
}
};
// When saving data for normal/stateful apps
export const doPatchFormData = (url: string, data: IDataModelPatchRequest) =>
httpPatch<IDataModelPatchResponse>(url, data);
// New multi-patch endpoint for stateful apps
export const doPatchMultipleFormData = (url: string, data: IDataModelMultiPatchRequest) =>
httpPatch<IDataModelMultiPatchResponse>(url, data);
// When saving data for stateless apps
export const doPostStatelessFormData = async (
url: string,
data: object,
options?: AxiosRequestConfig,
): Promise<object> => (await httpPost<object>(url, options, data)).data;
/**
* Query functions (these should use httpGet and start with 'fetch')
*/
export const fetchLogo = async (): Promise<string> =>
(await axios.get('https://altinncdn.no/img/Altinn-logo-blue.svg')).data;
export const fetchActiveInstances = (partyId: number): Promise<ISimpleInstance[]> =>
httpGet(getActiveInstancesUrl(partyId));
export const fetchInstanceData = async (partyId: string, instanceGuid: string): Promise<IInstance> =>
removeProcessFromInstance(
await httpGet<IInstance & { process: unknown }>(`${instancesControllerUrl}/${partyId}/${instanceGuid}`),
);
export const fetchProcessState = (instanceId: string): Promise<IProcess> => httpGet(getProcessStateUrl(instanceId));
export const fetchApplicationMetadata = () => httpGet<IncomingApplicationMetadata>(applicationMetadataApiUrl);
export const fetchApplicationSettings = (): Promise<IApplicationSettings> => httpGet(applicationSettingsApiUrl);
export const fetchSelectedParty = (): Promise<IParty | undefined> => httpGet(selectedPartyUrl);
export const fetchFooterLayout = (): Promise<IFooterLayout | null> => httpGet(getFooterLayoutUrl());
export const fetchLayoutSets = (): Promise<ILayoutSets> => httpGet(getLayoutSetsUrl());
export const fetchLayouts = (layoutSetId: string): Promise<ILayoutCollection> => httpGet(getLayoutsUrl(layoutSetId));
export const fetchLayoutsForInstance = (layoutSetId: string, instanceId: string): Promise<ILayoutCollection> =>
httpGet(getInstanceLayoutsUrl(layoutSetId, instanceId));
export const fetchLayoutSettings = (layoutSetId: string): Promise<ILayoutSettings> =>
httpGet(getLayoutSettingsUrl(layoutSetId));
export const fetchOptions = (url: string): Promise<AxiosResponse<IRawOption[]> | null> => httpGetRaw<IRawOption[]>(url);
export const fetchDataList = (url: string): Promise<IDataList> => httpGet(url);
export const fetchOrgs = (): Promise<{ orgs: IAltinnOrgs }> =>
httpGet(orgsListUrl, {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
});
export const fetchPartiesAllowedToInstantiate = (): Promise<IParty[]> => httpGet(validPartiesUrl);
export const fetchAppLanguages = (): Promise<IAppLanguage[]> => httpGet(applicationLanguagesUrl);
export const fetchReturnUrl = (queryParameterReturnUrl: string): Promise<string> =>
httpGet(getRedirectUrl(queryParameterReturnUrl));
export const fetchRefreshJwtToken = (): Promise<unknown> => httpGet(refreshJwtTokenUrl);
export const fetchCustomValidationConfig = (dataTypeId: string): Promise<IExpressionValidationConfig | null> =>
httpGet(getCustomValidationConfigUrl(dataTypeId));
export const fetchUserProfile = (): Promise<IProfile> => httpGet(profileApiUrl);
export const fetchDataModelSchema = (dataTypeName: string): Promise<JSONSchema7> =>
httpGet(getJsonSchemaUrl() + dataTypeName);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const fetchFormData = (url: string, options?: AxiosRequestConfig): Promise<any> => httpGet(url, options);
export const fetchPdfFormat = (instanceId: string, dataElementId: string): Promise<IPdfFormat> =>
httpGet(getPdfFormatUrl(instanceId, dataElementId));
export const fetchDynamics = (layoutSetId: string): Promise<{ data: IFormDynamics } | null> =>
httpGet(getFetchFormDynamicsUrl(layoutSetId));
export const fetchRuleHandler = (layoutSetId: string): Promise<string | null> =>
httpGet(getRulehandlerUrl(layoutSetId));
export const fetchTextResources = (selectedLanguage: string): Promise<ITextResourceResult> =>
httpGet(textResourcesUrl(selectedLanguage));
export const fetchPaymentInformation = (instanceId: string, language?: string): Promise<PaymentResponsePayload> =>
httpGet(getPaymentInformationUrl(instanceId, language));
export const fetchOrderDetails = (instanceId: string, language?: string): Promise<OrderDetails> =>
httpGet(getOrderDetailsUrl(instanceId, language));
export const fetchBackendValidations = (
instanceId: string,
language: string,
onlyIncrementalValidators?: boolean,
): Promise<BackendValidationIssue[]> => httpGet(getValidationUrl(instanceId, language, onlyIncrementalValidators));
export const fetchBackendValidationsForDataElement = (
instanceId: string,
currentDataElementID: string,
language: string,
): Promise<BackendValidationIssue[]> => httpGet(getDataValidationUrl(instanceId, currentDataElementID, language));
export const fetchLayoutSchema = async (): Promise<JSONSchema7 | undefined> => {
// Hacky (and only) way to get the correct CDN url
const schemaBaseUrl = document
.querySelector('script[src$="altinn-app-frontend.js"]')
?.getAttribute('src')
?.replace('altinn-app-frontend.js', 'schemas/json/layout/');
if (!schemaBaseUrl) {
return Promise.resolve(undefined);
}
return (await axios.get(`${schemaBaseUrl}${LAYOUT_SCHEMA_NAME}`)).data ?? undefined;
};
export function fetchExternalApi({
instanceId,
externalApiId,
}: {
instanceId: string;
externalApiId: string;
}): Promise<unknown> {
const externalApiUrl = `${appPath}/instances/${instanceId}/api/external/${externalApiId}`;
return httpGet(externalApiUrl);
}