/
assetsApi.ts
132 lines (122 loc) · 3.66 KB
/
assetsApi.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
// Copyright 2020 Cognite AS
import { BaseResourceAPI, CursorAndAsyncIterator } from '@cognite/sdk-core';
import { RevertableArraySorter } from '@cognite/sdk-core';
import {
Asset,
AssetAggregate,
AssetAggregateQuery,
AssetChange,
AssetIdEither,
AssetListScope,
AssetRetrieveParams,
AssetSearchFilter,
ExternalAssetItem,
IdEither,
} from '../../types';
import { sortAssetCreateItems } from './assetUtils';
export class AssetsAPI extends BaseResourceAPI<Asset> {
/**
* @hidden
*/
protected getDateProps() {
return this.pickDateProps(['items'], ['createdTime', 'lastUpdatedTime']);
}
/**
* [Creates new assets](https://doc.cognitedata.com/api/v1/#operation/createAssets)
*
* ```js
* const assets = [
* { name: 'First asset' },
* { name: 'Second asset', description: 'Another asset', externalId: 'anotherAsset' },
* { name: 'Child asset', parentExternalId: 'anotherAsset'},
* ];
* const createdAssets = await client.assets.create(assets);
* ```
*/
public create = (items: ExternalAssetItem[]): Promise<Asset[]> => {
const { sort, unsort } = new RevertableArraySorter(sortAssetCreateItems);
return super.createEndpoint(items, undefined, sort, unsort);
};
/**
* [List assets](https://doc.cognitedata.com/api/v1/#operation/listAssets)
* <!-- or [similar](https://doc.cognitedata.com/api/v1/#operation/getAssets) -->
*
* ```js
* const assets = await client.assets.list({ filter: { name: '21PT1019' } });
* ```
*/
public list = (scope?: AssetListScope): CursorAndAsyncIterator<Asset> => {
return super.listEndpoint(this.callListEndpointWithPost, scope);
};
/**
* [Aggregate assets](https://docs.cognite.com/api/v1/#operation/aggregateAssets)
*
* ```js
* const aggregates = await client.assets.aggregate({ filter: { root: true } });
* console.log('Number of root assets: ', aggregates[0].count)
* ```
*/
public aggregate = (
query: AssetAggregateQuery
): Promise<AssetAggregate[]> => {
return super.aggregateEndpoint(query);
};
/**
* [Retrieve assets](https://doc.cognitedata.com/api/v1/#operation/byIdsAssets)
* <!-- or [similar](https://doc.cognitedata.com/api/v1/#operation/getAsset) -->
*
* ```js
* const assets = await client.assets.retrieve([{id: 123}, {externalId: 'abc'}]);
* ```
*/
public retrieve = (
ids: IdEither[],
params: AssetRetrieveParams = {}
): Promise<Asset[]> => {
return super.retrieveEndpoint(ids, params);
};
/**
* [Update assets](https://doc.cognitedata.com/api/v1/#operation/updateAssets)
*
* ```js
* const assets = await client.assets.update([{id: 123, update: {name: {set: 'New name'}}}]);
* ```
*/
public update = (changes: AssetChange[]): Promise<Asset[]> => {
return super.updateEndpoint(changes);
};
/**
* [Search for assets](https://doc.cognitedata.com/api/v1/#operation/searchAssets)
*
* ```js
* const assets = await client.assets.search({
* filter: {
* parentIds: [1, 2]
* },
* search: {
* query: '21PT1019'
* }
* });
* ```
*/
public search = (query: AssetSearchFilter): Promise<Asset[]> => {
return super.searchEndpoint(query);
};
/**
* [Delete assets](https://doc.cognitedata.com/api/v1/#operation/deleteAssets)
*
* ```js
* await client.assets.delete([{id: 123}, {externalId: 'abc'}]);
* ```
*/
public delete = (ids: AssetIdEither[], params: AssetDeleteParams = {}) => {
const paramsWithIgnoreUnknownIds = {
...params,
ignoreUnknownIds: true,
};
return super.deleteEndpoint(ids, paramsWithIgnoreUnknownIds);
};
}
export interface AssetDeleteParams {
recursive?: boolean;
}