forked from microsoftgraph/microsoft-graph-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.ts
More file actions
245 lines (209 loc) · 6.34 KB
/
base.ts
File metadata and controls
245 lines (209 loc) · 6.34 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
// ------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
// ------------------------------------------------------------------------------
import { Tokens } from "./tokens";
import { isAuthenticated } from "./auth";
export interface ExplorerOptions {
AuthUrl?: string
GraphUrl?: string
ClientId?: string
Language?: string
RedirectUrl?: string
DefaultUserScopes?: string
GraphVersions?: string[]
PathToBuildDir: string
}
export let Methods:RequestType[] = [
'GET',
'POST',
'PUT',
'PATCH',
'DELETE'
];
export type GraphApiVersion = "v1.0" | "beta";
export let GraphApiVersions:GraphApiVersion[] = ["v1.0", "beta"];
export type AuthenticationStatus = "anonymous" | "authenticating" | "authenticated";
export type RequestType = "GET" | "PUT" | "POST" | "GET_BINARY" | "POST" | "PATCH" | "DELETE";
export interface PermissionScope {
name: string
description: string
longDescription: string
preview: boolean
admin: boolean
enabled?: boolean
/**
* Used in the scopes dialog for checking/unchecking before scope is actually enabled in the token.
*/
enabledTarget?: boolean
}
export interface GraphApiCall {
statusCode?: number
duration?: number
method?: RequestType
humanName?: string
requestUrl?: string
postBody?: string
headers?: GraphRequestHeader[]
requestSentAt?: Date
relativeDate?: string
}
export interface SampleQuery extends GraphApiCall {
docLink?: string
AAD?: boolean
MSA?: boolean
category: string
tip?: string
}
export interface SampleQueryCategory {
title: string
enabled?: boolean
queries?: SampleQuery[]
}
export interface ExplorerValues {
selectedOption?: RequestType
selectedVersion?: GraphApiVersion
endpointUrl?: string
authentication?: {
status?: AuthenticationStatus
user?: {
displayName?: string
emailAddress?: string
profileImageUrl?: string
}
}
showImage?: boolean
requestInProgress?: boolean,
headers?: GraphRequestHeader[]
postBody?: string
}
export interface GraphRequestHeader {
name: string
value: string
enabled?: boolean
readonly?: boolean
}
export interface AutoCompleteItem {
url: string
fullUrl: string
}
export const CommonHeaders = [
"Accept",
"Accept-Charset",
"Accept-Encoding",
"Accept-Language",
"Accept-Datetime",
"Authorization",
"Cache-Control",
"Connection",
"Cookie",
"Content-Length",
"Content-MD5",
"Content-Type",
"Date",
"Expect",
"Forwarded",
"From",
"Host",
"If-Match",
"If-Modified-Since",
"If-None-Match",
"If-Range",
"If-Unmodified-Since",
"Max-Forwards",
"Origin",
"Pragma",
"Proxy-Authorization",
"Range",
"User-Agent",
"Upgrade",
"Via",
"Warning"
];
/**
* Tokens are used as placeholder values in sample queries to cover many scenarios:
* - ID tokens for sample tenant nodes like user IDs, file IDs and other string constants
* - Tokens that must be determined at runtime like the current date
* - Tokens that are determined from the authenticated users session
* - Tokens can be in the POST body or part of the URL
*
* The token fields are split into default, demo and authenticated. If neither the demo or
* auth values are supplied, the token falls back to the default value.
*
* Tokens are maintained in tokens.ts.
*/
export interface Token {
placeholder: string
// base defaults to replace the placeholder with. Not used if any of the below are defined
defaultValue?: string
defaultValueFn?: Function
// when the user is not authenticated, use these values for the demo tenant
demoTenantValue?: string
demoTenantValueFn?: Function
// when the user is authenticated with MSA or AAD, replace token with these values
authenticatedUserValue?: string
authenticatedUserValueFn?: Function
}
/*
* Given a token, go through each of the possible replacement scenarios and find which value to
* replace the token with.
* Order: Authenticated user values, demo tenant replacament values, default replacement values.
*/
function getTokenSubstituteValue(token:Token) {
let priorityOrder = []; // desc
if (isAuthenticated()) {
priorityOrder.push(token.authenticatedUserValueFn);
priorityOrder.push(token.authenticatedUserValue);
} else {
priorityOrder.push(token.demoTenantValueFn);
priorityOrder.push(token.demoTenantValue);
}
priorityOrder.push(token.defaultValueFn);
priorityOrder.push(token.defaultValue);
for (let tokenVal of priorityOrder) {
if (!tokenVal) {
continue;
}
if (typeof tokenVal === "string") {
return tokenVal;
} else if (typeof tokenVal === "function") {
return tokenVal();
}
}
}
/**
* Given a query, replace all tokens in the request URL and the POST body with thier
* values. When a token is found, use getTokenSubstituteValue() to find the right
* value to substitute based on the session.
*/
export function substituteTokens(query:SampleQuery) {
type QueryFields = keyof GraphApiCall;
for (let token of Tokens) {
let queryFieldsToCheck:QueryFields[] = ['requestUrl', 'postBody'];
for (let queryField of queryFieldsToCheck) {
if (!query[queryField]) { // if the sample doesn't have a post body, don't search for tokens in it
continue;
}
if ((query[queryField] as string).indexOf(`{${token.placeholder}}`) !== -1) {
let substitutedValue = getTokenSubstituteValue(token);
if (!substitutedValue) {
continue;
}
query[queryField] = (query[queryField] as string).replace(`{${token.placeholder}}`, substitutedValue);
}
}
}
}
export interface Message {
title: string
body: string
}
export interface MessageBarContent {
icon: string
backgroundClass: string
text: string
}
export let AllowedGraphDomains = [
"https://graph.microsoft.com",
"https://canary.graph.microsoft.com",
"https://microsoftgraph.chinacloudapi.cn"
]