-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathhelper.js
More file actions
102 lines (91 loc) · 3.91 KB
/
helper.js
File metadata and controls
102 lines (91 loc) · 3.91 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// Fetch user authentication API & set user state
export async function fetchAuth(callback) {
if (!process.env.NODE_ENV || process.env.NODE_ENV === 'development') {
console.log('### FETCH: GET /.auth/me (NOT CALLED ON LOCALHOST)');
callback({ "clientPrincipal": null });
}
else {
let apiUrl = "/.auth/me";
console.log(`### FETCH: GET ${apiUrl}`);
const authResponse = await fetch(apiUrl).catch(error => console.error('Error:', error)),
contentType = authResponse && authResponse.headers.get("content-type");
if (contentType && contentType.indexOf("application/json") !== -1) {
await authResponse.json()
.then(authResponse => callback(authResponse))
.catch(error => console.error('Error:', error));
}
}
}
// Fetch AAD token API & set token state
export async function fetchToken(callback) {
let apiUrl = "/api/aad/token";
console.log(`### FETCH: GET ${apiUrl}`);
const tokenResponse = await fetch(apiUrl).catch(error => console.error('Error:', error)),
headers = tokenResponse && tokenResponse.headers,
contentType = headers && headers.get("content-type");
let tokenAccess = null;
if (contentType && contentType.indexOf("application/json") !== -1) {
await tokenResponse.json()
.then(tokenResponse => {
const tokenResponseStatus = (tokenResponse && tokenResponse.status) || null,
tokenResponseData = (tokenResponseStatus===200 && tokenResponse.data) || null,
tokenType = (tokenResponseData && tokenResponseData.token_type) || null;
// Set token value
if (tokenType==='Bearer' && tokenResponseData.access_token) tokenAccess = tokenResponseData.access_token;
})
.catch(error => console.error('Error:', error));
}
callback( (tokenAccess) ? tokenAccess : 'error' );
}
// Fetch API for storage container
export async function fetchContainer(callback) {
let apiUrl = "/api/storage";
console.log(`### FETCH: GET ${apiUrl}`);
const storageResponse = await fetch(apiUrl).catch(error => console.error('Error:', error)),
contentType = storageResponse && storageResponse.headers.get("content-type");
if (contentType && contentType.indexOf("application/json") !== -1) {
await storageResponse.json()
.then(storageResponse => {
const responseStatus = storageResponse && storageResponse.status,
responseData = storageResponse && storageResponse.data,
container = responseData && responseData.name;
if (responseStatus === 200 && container) {
callback(container);
console.log('### container:', container);
}
})
.catch(error => console.error('Error:', error));
}
}
// Fetch API for type definitions using AAD token
export async function fetchTypeDefs(aadToken, callback) {
if (aadToken) {
let apiUrl = "/api/purview/typedefs";
console.log(`### FETCH: GET ${apiUrl}`);
await fetch(apiUrl, {headers:{'Authorization': `Bearer ${aadToken}`}})
.then(response => handleApiTypes(response, callback))
.catch(error => console.error('Error:', error));
}
}
// Function to handle API response & ensure JSON
export async function handleApiTypes(response, callback) {
const responseHeaders = response && response.headers,
contentType = responseHeaders && responseHeaders.get("content-type");
if (contentType && contentType.indexOf("application/json") !== -1) {
await response.json()
.then(response => {
const responseStatus = response && response.status;
if (responseStatus === 200) {
callback(response.data);
console.log('### typeDefsObj:', response.data);
}
})
.catch(error => console.error('Error:', error));
}
else {
console.log('Warning: TypeDef API response is not JSON or null.');
callback({});
}
}