-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathhelpers.ts
More file actions
241 lines (217 loc) · 5.94 KB
/
Copy pathhelpers.ts
File metadata and controls
241 lines (217 loc) · 5.94 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
import {
IRequestOptions,
IAuthenticationManager
} from "@esri/arcgis-rest-request";
import { IRegisteredAppResponse, IApp } from "./types/appType.js";
import { IApiKeyInfo } from "./types/apiKeyType.js";
import { IOAuthAppInfo } from "./types/oAuthType.js";
import { generateApiKeyToken } from "./generateApiKeyToken.js";
/**
* @internal
* Encode special params value (e.g. array type...) in advance in order to make {@linkcode encodeParam} works correctly. Usage is case by case.
*/
export const stringifyArrays = (requestOptions: IRequestOptions) => {
Object.entries(requestOptions.params).forEach((entry) => {
const [key, value] = entry;
if (value.constructor.name === "Array") {
requestOptions.params[key] = JSON.stringify(value);
}
});
};
/**
* @internal
* Used to convert {@linkcode IRegisteredAppResponse} to {@linkcode IApp}.
*/
export function registeredAppResponseToApp(
response: IRegisteredAppResponse
): IApp {
const omittedKeys = [
"apnsProdCert",
"apnsSandboxCert",
"gcmApiKey",
"isBeta",
"customAppLoginShowTriage"
];
const dateKeys = ["modified", "registered"];
return Object.keys(response)
.filter((key) => !omittedKeys.includes(key))
.reduce((obj: any, key) => {
if (dateKeys.includes(key)) {
obj[key] = new Date((response as any)[key]);
} else {
obj[key] = (response as any)[key];
}
return obj;
}, {});
}
/**
* @internal
* Used to convert {@linkcode IApp} to {@linkcode IApiKeyInfo} only if `appType` is "apikey".
*/
export function appToApiKeyProperties(response: IApp): IApiKeyInfo {
delete response.client_secret;
delete response.redirect_uris;
delete response.appType;
delete (response as any).customAppLoginShowTriage;
delete response.apiKey;
return response as IApiKeyInfo;
}
/**
* @internal
* Used to convert {@linkcode IApp} to {@linkcode IOAuthAppInfo}.
*/
export function appToOAuthAppProperties(response: IApp): IOAuthAppInfo {
delete response.appType;
delete response.httpReferrers;
delete response.privileges;
delete response.apiKey;
delete (response as any).customAppLoginShowTriage;
delete response.isPersonalAPIToken;
delete response.apiToken1Active;
delete response.apiToken2Active;
return response as IOAuthAppInfo;
}
/**
* @internal
* Used to extract base request options from a hybrid option and exclude `params` and `authentication`.
*/
export function extractBaseRequestOptions<T extends IRequestOptions>(
options: T
): Partial<IRequestOptions> {
const requestOptionsProperties: Array<keyof T> = [
"portal",
"requestFlags",
"fetchOptions"
];
return filterKeys(options, requestOptionsProperties);
}
/**
* @internal
* Used to create a new object including only specified keys from another object.
*/
export function filterKeys<T extends object>(
object: T,
includedKeys: Array<keyof T>
): any {
return includedKeys.reduce(
(obj: { [key: string | number | symbol]: any }, ele) => {
if (ele in object) {
obj[ele] = object[ele];
}
return obj;
},
{}
);
}
/**
* Used to determine if a generated key is in slot 1 or slot 2 key. The full API key should be passed. `undefined` will be returned if the proper slot could not be identified.
*/
export function slotForKey(key: string) {
const slot = parseInt(key.substring(key.length - 10, key.length - 9));
if (slot === 1 || slot === 2) {
return slot;
}
return undefined;
}
/**
* @internal
* Used to determine which slot to invalidate a key in given a number or a full or patial key.
*/
export function slotForInvalidationKey(param: string | 1 | 2) {
if (param === 1 || param === 2) {
return param;
}
if (typeof param !== "string") {
return undefined;
}
const fullKeySlot = slotForKey(param);
if (fullKeySlot) {
return fullKeySlot;
}
}
interface IGenerateApiKeyTokenOptions extends IRequestOptions {
authentication: IAuthenticationManager;
}
/**
* @internal
* Used to generate tokens in slot 1 and/or 2 of an API key.
*/
export function generateApiKeyTokens(
itemId: string,
slots: Array<1 | 2>,
requestOptions: IGenerateApiKeyTokenOptions
) {
return Promise.all(
slots.map((slot) => {
return generateApiKeyToken({
itemId,
apiKey: slot,
...requestOptions
});
})
).then((responses) => {
return responses
.map((responses) => responses.access_token)
.reduce((obj, token, index) => {
obj[`accessToken${slotForKey(token)}`] = token;
return obj;
}, {} as { [key: string]: string });
});
}
/**
* @internal
* Convert boolean flags to an array of slots for {@linkcode generateApiKeyTokens}.
*/
export function generateOptionsToSlots(
generateToken1: boolean,
generateToken2: boolean
): Array<1 | 2> {
const slots: Array<1 | 2> = [];
if (generateToken1) {
slots.push(1);
}
if (generateToken2) {
slots.push(2);
}
return slots;
}
type expirationDateParams =
| {
apiToken1ExpirationDate: Date | -1;
apiToken2ExpirationDate: Date | -1;
}
| {
apiToken1ExpirationDate: Date | -1;
}
| {
apiToken2ExpirationDate: Date | -1;
}
| {};
/**
* @internal
* Build params for updating expiration dates
*/
export function buildExpirationDateParams(
requestOptions: {
apiToken1ExpirationDate?: Date;
apiToken2ExpirationDate?: Date;
},
fillDefaults?: boolean
): expirationDateParams {
const updateparams: any = {};
if (requestOptions.apiToken1ExpirationDate) {
updateparams.apiToken1ExpirationDate =
requestOptions.apiToken1ExpirationDate;
}
if (requestOptions.apiToken2ExpirationDate) {
updateparams.apiToken2ExpirationDate =
requestOptions.apiToken2ExpirationDate;
}
if (fillDefaults && !updateparams.apiToken1ExpirationDate) {
updateparams.apiToken1ExpirationDate = -1;
}
if (fillDefaults && !updateparams.apiToken2ExpirationDate) {
updateparams.apiToken2ExpirationDate = -1;
}
return updateparams;
}