forked from Esri/arcgis-rest-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.ts
More file actions
326 lines (300 loc) · 7.87 KB
/
Copy pathhelpers.ts
File metadata and controls
326 lines (300 loc) · 7.87 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
/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */
import {
IRequestOptions,
IAuthenticatedRequestOptions
} from "@esri/arcgis-rest-request";
import { IItem } from "../helpers.js";
import type { Blob, File } from "@esri/arcgis-rest-request";
/**
* Base options interface for making authenticated requests for items.
*/
export interface IUserItemOptions extends IAuthenticatedRequestOptions {
/**
* Unique identifier of the item.
*/
id: string;
/**
* Item owner username. If not present, `authentication.getUsername()` is utilized.
*/
owner?: string;
}
export interface IFolderIdOptions extends IAuthenticatedRequestOptions {
/**
* Unique identifier of the folder.
*/
folderId: string;
/**
* Item owner username. If not present, `authentication.getUsername()` is utilized.
*/
owner?: string;
}
export type ItemRelationshipType =
| "APIKey2Item"
| "Area2CustomPackage"
| "Area2Package"
| "Item2Attachment"
| "Item2Report"
| "Listed2Provisioned"
| "Map2AppConfig"
| "Map2Area"
| "Map2FeatureCollection"
| "Map2Service"
| "MobileApp2Code"
| "Service2Data"
| "Service2Layer"
| "Service2Route"
| "Service2Service"
| "Service2Style"
| "Solution2Item"
| "Style2Style"
| "Survey2Data"
| "Survey2Service"
| "SurveyAddIn2Data"
| "Theme2Story"
| "TrackView2Map"
| "WebStyle2DesktopStyle"
| "WMA2Code"
| "WorkforceMap2FeatureService";
/**
* Names of methods for reading the body of a fetch response, see:
* https://developer.mozilla.org/en-US/docs/Web/API/Body#Methods
*/
export type FetchReadMethodName =
| "arrayBuffer"
| "blob"
| "formData"
| "json"
| "text";
export interface IItemRelationshipOptions extends IRequestOptions {
/**
* Unique identifier of the item.
*/
id: string;
/**
* The type of relationship between the two items.
*/
relationshipType: ItemRelationshipType | ItemRelationshipType[];
/**
* The direction of the relationship. Either forward (from origin -> destination) or reverse (from destination -> origin).
*/
direction?: "forward" | "reverse";
}
export interface IManageItemRelationshipOptions
extends IAuthenticatedRequestOptions {
originItemId: string;
destinationItemId: string;
relationshipType: ItemRelationshipType;
}
export interface IItemInfoOptions extends IUserItemOptions {
/**
* Subfolder for added information.
*/
folderName?: string;
/**
* Object to store
*/
file: any;
}
export interface IItemResourceOptions extends IUserItemOptions {
/**
* New resource filename.
*/
name?: string;
/**
* Folder in which to store the new resource.
*/
prefix?: string;
/**
* Text input to be added as a file resource.
*/
content?: string;
/**
* Controls whether access to the file resource is restricted to the owner or inherited from the sharing permissions set for the associated item.
*/
private?: boolean;
/**
* Object to store
*/
resource?: any;
}
export interface IRemoveItemResourceOptions extends IUserItemOptions {
/**
* Resource item to be removed. Resource prefix needs to be specified if the file resource has one.
*/
resource?: string;
/**
* If true, all file resources are removed.
*/
deleteAll?: boolean;
}
export interface ICreateUpdateItemOptions extends IAuthenticatedRequestOptions {
/**
* The owner of the item. If this property is not present, `item.owner` will be passed, or lastly `authentication.getUsername()`.
*/
owner?: string;
/**
* Id of the folder to house the item.
*/
folderId?: string;
/**
* The file to be uploaded. If uploading a file, the request must be a multipart request.
*/
file?: Blob | File;
/**
* The URL where the item can be downloaded. The resource will be downloaded and stored as a file type. Similar to uploading a file to be added, but instead of transferring the contents of the file, the URL of the data file is referenced and creates a file item.
*/
dataUrl?: string;
/**
* The text content for the item to be submitted.
*/
text?: string;
/**
* If true, the file is uploaded asynchronously. If false, the file is uploaded synchronously.
*/
async?: boolean;
/**
* If true, the file is uploaded in multiple parts.
*/
multipart?: boolean;
/**
* The filename being uploaded in multipart mode. Required if multipart=true.
*/
filename?: string;
/**
* If true, overwrite the existing file.
*/
overwrite?: boolean;
}
export interface IItemDataOptions extends IRequestOptions {
/**
* Used to request binary data.
*/
file?: boolean;
}
export interface IItemPartOptions extends IUserItemOptions {
/**
* The file part to be uploaded.
*/
file: any;
/**
* Part numbers can be any number from 1 to 10,000, inclusive. A part number uniquely identifies a part and also defines its position within the object being created. If you upload a new part using the same part number that was used with a previous part, the previously uploaded part is overwritten.
*/
partNum: number;
}
export interface IUpdateItemResponse {
success: boolean;
id: string;
}
export interface IItemInfoResponse {
success: boolean;
itemId: string;
owner: string;
folder: string;
}
export interface IItemResourceResponse {
success: boolean;
itemId: string;
owner: string;
folder: string;
}
export interface IAddFolderResponse {
/**
* Success or failure of request.
*/
success: boolean;
/**
* Information about created folder: its alphanumeric id, name, and owner's name.
*/
folder: {
id: string;
title: string;
username: string;
};
}
export interface IMoveItemResponse {
/**
* Success or failure of request.
*/
success: boolean;
/**
* Alphanumeric id of moved item.
*/
itemId: string;
/**
* Name of owner of item.
*/
owner: string;
/**
* Alphanumeric id of folder now housing item.
*/
folder: string;
}
/**
* `requestOptions.owner` is given priority, `requestOptions.item.owner` will be checked next. If neither are present, `authentication.getUserName()` will be used instead.
*/
export function determineOwner(requestOptions: any): Promise<string> {
if (requestOptions.owner) {
return Promise.resolve(requestOptions.owner);
} else if (requestOptions.item && requestOptions.item.owner) {
return Promise.resolve(requestOptions.item.owner);
} else if (
requestOptions.authentication &&
requestOptions.authentication.getUsername
) {
return requestOptions.authentication.getUsername();
} else {
return Promise.reject(
new Error(
"Could not determine the owner of this item. Pass the `owner`, `item.owner`, or `authentication` option."
)
);
}
}
/**
* checks if the extent is a valid BBox (2 element array of coordinate pair arrays)
* @param extent
* @returns
*/
export function isBBox(extent: unknown): boolean {
return (
Array.isArray(extent) &&
Array.isArray(extent[0]) &&
Array.isArray(extent[1])
);
}
/**
* Given a Bbox, convert it to a string. Some api endpoints expect a string
*
* @param {BBox} extent
* @return {*} {string}
*/
export function bboxToString(extent: number[][]): string {
return extent.join(",");
}
/**
* Returns the full thumbnail URL for an item.
*
* @param item - The item to decorate
* @param portal - The portal URL
* @param token - Optional token to append if the item is private
* @returns The item with decorated thumbnail
*/
export function decorateThumbnail(
item: IItem,
portal: string,
token?: string
): IItem {
if (!item) return item;
let thumbnailUrl: string | null = null;
if (typeof item.thumbnail === "string") {
thumbnailUrl = `${portal}/content/items/${item.id}/info/${item.thumbnail}`;
if (thumbnailUrl && item.access !== "public" && token) {
thumbnailUrl += `?token=${token}`;
}
}
return {
...item,
...(thumbnailUrl ? { thumbnailUrl } : {})
};
}