-
Notifications
You must be signed in to change notification settings - Fork 119
/
items.ts
381 lines (340 loc) · 9.85 KB
/
items.ts
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* Copyright (c) 2017 Environmental Systems Research Institute, Inc.
* Apache-2.0 */
import {
request,
IRequestOptions,
IParams,
getPortalUrl
} from "@esri/arcgis-rest-request";
import { IExtent, IItem, IPagingParams } from "@esri/arcgis-rest-common-types";
import { IUserRequestOptions } from "@esri/arcgis-rest-auth";
export interface IItemRequestOptions extends IUserRequestOptions {
item: IItem;
}
// * @param id - Item Id
// * @param owner - Item owner username
// * @param data - Javascript object to store
export interface IItemIdRequestOptions extends IUserRequestOptions {
/**
* Unique identifier of the item.
*/
id: string;
/**
* Item owner username (by default authentication session will be used).
*/
owner?: string;
}
export interface IItemJsonDataRequestOptions extends IItemIdRequestOptions {
/**
* JSON object to store
*/
data: any;
/**
* Item owner username (by default authentication session will be used).
*/
owner?: string;
}
export interface IItemResourceRequestOptions extends IItemIdRequestOptions {
/**
* New resource filename.
*/
name?: string;
/**
* Text input to be added as a file resource.
*/
content?: string;
resource?: string;
}
export interface IItemCrudRequestOptions extends IUserRequestOptions {
item: IItem;
/**
* Item owner username (by default authentication session will be used).
*/
owner?: string;
/**
* Optional folder to house the item
*/
folder?: string;
}
// this interface still needs to be docced
export interface ISearchRequest extends IPagingParams {
q: string;
[key: string]: any;
// start: number;
// num: number;
}
export interface ISearchRequestOptions extends IRequestOptions {
searchForm?: ISearchRequest;
}
/**
* Options to pass through when searching for items.
*/
export interface ISearchResult {
query: string; // matches the api's form param
total: number;
start: number;
num: number;
nextStart: number;
results: IItem[];
}
/**
* Search for items via the portal api
*
* ```js
* import { searchItems } from '@esri/arcgis-rest-items';
*
* searchItems('water')
* .then((results) => {
* console.log(response.results.total); // 355
* })
* ```
*
* @param search - A string or RequestOptions object to pass through to the endpoint.
* @returns A Promise that will resolve with the data from the response.
*/
export function searchItems(
search: string | ISearchRequestOptions
): Promise<ISearchResult> {
const options: ISearchRequestOptions = {
httpMethod: "GET",
params: {}
};
if (typeof search === "string") {
options.params = { q: search };
} else {
options.params = search.searchForm;
if (search.authentication) {
options.authentication = search.authentication;
}
}
// construct the search url
const url = `${getPortalUrl(options)}/search`;
// send the request
return request(url, options);
}
/**
* Create an item in a folder
*
* @param requestOptions = Options for the request
*/
export function createItemInFolder(
requestOptions: IItemCrudRequestOptions
): Promise<any> {
const owner = requestOptions.owner || requestOptions.authentication.username;
const baseUrl = `${getPortalUrl(requestOptions)}/content/users/${owner}`;
let url = `${baseUrl}/addItem`;
if (requestOptions.folder) {
url = `${baseUrl}/${requestOptions.folder}/addItem`;
}
// serialize the item into something Portal will accept
requestOptions.params = serializeItem(requestOptions.item);
return request(url, requestOptions);
}
/**
* Create an Item in the user's root folder
*
* @param requestOptions - Options for the request
*/
export function createItem(
requestOptions: IItemCrudRequestOptions
): Promise<any> {
// delegate to createItemInFolder placing in the root of the filestore
const options = {
folder: null,
...requestOptions
} as IItemCrudRequestOptions;
return createItemInFolder(options);
}
/**
* Send json to an item to be stored as the `/data` resource
*
* @param requestOptions - Options for the request
*/
export function addItemJsonData(
requestOptions: IItemJsonDataRequestOptions
): Promise<any> {
const owner = requestOptions.owner || requestOptions.authentication.username;
const url = `${getPortalUrl(
requestOptions
)}/content/users/${owner}/items/${requestOptions.id}/update`;
// Portal API requires that the 'data' be stringified and POSTed in
// a `text` form field. It can also be sent with the `.create` call by sending
// a `.data` property.
requestOptions.params = {
text: JSON.stringify(requestOptions.data)
};
return request(url, requestOptions);
}
/**
* Get an item by id
*
* @param id - Item Id
* @param requestOptions - Options for the request
* @returns A Promise that will resolve with the data from the response.
*/
export function getItem(
id: string,
requestOptions?: IRequestOptions
): Promise<IItem> {
const url = `${getPortalUrl(requestOptions)}/content/items/${id}`;
// default to a GET request
const options: IRequestOptions = {
...{ httpMethod: "GET" },
...requestOptions
};
return request(url, options);
}
/**
* Get the /data for an item.
* Note: Some items do not return json from /data
* and this method will throw if that is the case.
*
* @param id - Item Id
* @param requestOptions - Options for the request
* @returns A Promise that will resolve with the json data for the item.
*/
export function getItemData(
id: string,
requestOptions?: IRequestOptions
): Promise<any> {
const url = `${getPortalUrl(requestOptions)}/content/items/${id}/data`;
// default to a GET request
const options: IRequestOptions = {
...{ httpMethod: "GET" },
...requestOptions
};
return request(url, options);
}
/**
* Update an Item
*
* @param item - The item to update.
* @param requestOptions - Options for the request.
* @returns A Promise that resolves with the status of the operation.
*/
export function updateItem(requestOptions: IItemRequestOptions): Promise<any> {
const url = `${getPortalUrl(requestOptions)}/content/users/${requestOptions
.item.owner}/items/${requestOptions.item.id}/update`;
// serialize the item into something Portal will accept
requestOptions.params = serializeItem(requestOptions.item);
return request(url, requestOptions);
}
/**
* Remove an item from the portal
*
* @param requestOptions - Options for the request
* @returns A Promise that deletes an item.
*/
export function removeItem(
requestOptions: IItemIdRequestOptions
): Promise<any> {
const owner = requestOptions.owner || requestOptions.authentication.username;
const url = `${getPortalUrl(
requestOptions
)}/content/users/${owner}/items/${requestOptions.id}/delete`;
return request(url, requestOptions);
}
/**
* Protect an item
*
* @param requestOptions - Options for the request
* @returns A Promise to protect an item.
*/
export function protectItem(
requestOptions: IItemIdRequestOptions
): Promise<any> {
const owner = requestOptions.owner || requestOptions.authentication.username;
const url = `${getPortalUrl(
requestOptions
)}/content/users/${owner}/items/${requestOptions.id}/protect`;
return request(url, requestOptions);
}
/**
* Unprotect an item
*
* @param requestOptions - Options for the request
* @returns A Promise to unprotect an item.
*/
export function unprotectItem(
requestOptions: IItemIdRequestOptions
): Promise<any> {
const owner = requestOptions.owner || requestOptions.authentication.username;
const url = `${getPortalUrl(
requestOptions
)}/content/users/${owner}/items/${requestOptions.id}/unprotect`;
return request(url, requestOptions);
}
/**
* Get the resources associated with an item
*
* @param requestOptions - Options for the request
* @returns A Promise to get some item resources.
*/
export function getItemResources(
requestOptions: IItemIdRequestOptions
): Promise<any> {
const url = `${getPortalUrl(
requestOptions
)}/content/items/${requestOptions.id}/resources`;
requestOptions.params = { num: 1000 };
return request(url, requestOptions);
}
/**
* Update a resource associated with an item
*
* @param requestOptions - Options for the request
* @returns A Promise to unprotect an item.
*/
export function updateItemResource(
requestOptions: IItemResourceRequestOptions
): Promise<any> {
const owner = requestOptions.owner || requestOptions.authentication.username;
const url = `${getPortalUrl(
requestOptions
)}/content/users/${owner}/items/${requestOptions.id}/updateResources`;
requestOptions.params = {
fileName: requestOptions.content,
text: requestOptions.name
};
return request(url, requestOptions);
}
/**
* Remove a resource associated with an item
*
* @param requestOptions - Options for the request
* @returns A Promise to unprotect an item.
*/
export function removeItemResource(
requestOptions: IItemResourceRequestOptions
): Promise<any> {
const owner = requestOptions.owner || requestOptions.authentication.username;
const url = `${getPortalUrl(
requestOptions
)}/content/users/${owner}/items/${requestOptions.id}/removeResources`;
requestOptions.params = { resource: requestOptions.resource };
return request(url, requestOptions);
}
/**
* Serialize an item into a json format accepted by the Portal API
* for create and update operations
*
* @param item IItem to be serialized
* @returns a formatted json object to be sent to Portal
*/
function serializeItem(item: IItem): any {
// create a clone so we're not messing with the original
const clone = JSON.parse(JSON.stringify(item));
// join keywords and tags...
clone.typeKeywords = item.typeKeywords.join(", ");
clone.tags = item.tags.join(", ");
// convert .data to .text
if (clone.data) {
clone.text = JSON.stringify(clone.data);
delete clone.data;
}
// Convert properties to a string
if (clone.properties) {
clone.properties = JSON.stringify(clone.properties);
}
return clone;
}