-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathdataService.tsx
143 lines (121 loc) · 4.65 KB
/
dataService.tsx
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
import { CommonServiceIds, IExtensionDataManager, IExtensionDataService } from 'azure-devops-extension-api';
import { appInsights } from '../utilities/telemetryClient';
import { SDKContext } from './azureDevOpsContextProvider';
import React from 'react';
let extensionDataManager: IExtensionDataManager;
async function getDataService(): Promise<IExtensionDataManager> {
if (!extensionDataManager) {
const { SDK } = React.useContext(SDKContext);
const accessToken = await SDK.getAccessToken();
const extensionDataService = await SDK.getService<IExtensionDataService>(CommonServiceIds.ExtensionDataService);
extensionDataManager = await extensionDataService.getExtensionDataManager(SDK.getExtensionContext().id, accessToken);
}
return extensionDataManager;
}
/**
* Read user/account scoped documents.
*/
export async function readDocuments<T>(
collectionName: string, isPrivate?: boolean, throwCollectionDoesNotExistException?: boolean): Promise<T[]> {
const dataService: IExtensionDataManager = await getDataService();
let data: T[];
try {
data = await dataService.getDocuments(collectionName, isPrivate ? { scopeType: 'User' } : undefined);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
console.error(e);
appInsights.trackException(e);
if (e.serverError?.typeKey === 'DocumentCollectionDoesNotExistException') {
if (throwCollectionDoesNotExistException) {
throw e;
}
}
appInsights.trackException(e);
data = [];
}
return data;
}
/**
* Read a specific user/account scoped document.
*/
export async function readDocument<T>(collectionName: string, id: string, isPrivate?: boolean): Promise<T> {
if (id === "emptyFeedbackItem") {
return undefined;
}
const dataService: IExtensionDataManager = await getDataService();
let data: T;
try {
data = await dataService.getDocument(collectionName, id, isPrivate ? { scopeType: 'User' } : undefined);
} catch (e) {
appInsights.trackException(e);
console.error('An exception occurred while trying to read the document: ', e);
data = undefined;
}
return data;
}
/**
* Create user/account scoped document.
*/
export async function createDocument<T>(collectionName: string, data: T, isPrivate?: boolean): Promise<T> {
const dataService: IExtensionDataManager = await getDataService();
return dataService.createDocument(collectionName, data, isPrivate ? { scopeType: 'User' } : undefined);
}
/**
* Create or Update user/account scoped document.
*/
export async function createOrUpdateDocument<T>(collectionName: string, data: T, isPrivate?: boolean): Promise<T> {
const dataService: IExtensionDataManager = await getDataService();
return dataService.setDocument(collectionName, data, isPrivate ? { scopeType: 'User' } : undefined);
}
/**
* Update user/account scoped document.
*/
export async function updateDocument<T>(collectionName: string, data: T, isPrivate?: boolean): Promise<T> {
const dataService: IExtensionDataManager = await getDataService();
let updatedData: T;
try {
updatedData = await dataService.updateDocument(collectionName, data, isPrivate ? { scopeType: 'User' } : undefined);
} catch (e) {
appInsights.trackException(e);
console.error('An exception occurred while trying to update the document: ', e);
updatedData = undefined;
}
return updatedData;
}
/**
* Delete user/account scoped document.
*/
export async function deleteDocument(collectionName: string, id: string, isPrivate?: boolean): Promise<void> {
const dataService: IExtensionDataManager = await getDataService();
return dataService.deleteDocument(collectionName, id, isPrivate ? { scopeType: 'User' } : undefined);
}
/**
* Set user/account scoped value.
*/
export async function setValue<T>(id: string, data: T, isPrivate?: boolean): Promise<T> {
const dataService: IExtensionDataManager = await getDataService();
let updatedData: T;
try {
return dataService.setValue(id, data, isPrivate ? { scopeType: 'User' } : undefined);
} catch (e) {
appInsights.trackException(e);
console.error('An exception occurred while trying to read the value: ', e);
updatedData = undefined;
}
return updatedData;
}
/**
* Get user/account scoped value.
*/
export async function getValue<T>(id: string, isPrivate?: boolean): Promise<T> {
const dataService: IExtensionDataManager = await getDataService();
let data: T;
try {
data = await dataService.getValue<T>(id, isPrivate ? { scopeType: 'User' } : undefined);
} catch (e) {
appInsights.trackException(e);
console.error('An exception occurred while trying to read the value: ', e);
data = undefined;
}
return data;
}