-
Notifications
You must be signed in to change notification settings - Fork 13
/
assets.ts
98 lines (85 loc) · 2.82 KB
/
assets.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
import { TSError, isEmpty, isString } from '@terascope/utils';
import { Teraslice } from '@terascope/types';
import path from 'path';
import autoBind from 'auto-bind';
import Client from './client';
import { PostData, SearchOptions, ClientConfig } from './interfaces';
export default class Assets extends Client {
constructor(config: ClientConfig) {
super(config);
autoBind(this);
}
async upload(
data: PostData,
query: Teraslice.AssetUploadQuery = {}
): Promise<Teraslice.AssetIDResponse> {
if (isEmpty(data)) {
throw new TSError('Asset stream must not be empty', {
statusCode: 400
});
}
const results = await this.post('/assets', data, {
searchParams: query as Record<string, any>
});
return this.parse(results);
}
async remove(
id: string,
searchOptions: SearchOptions = {}
): Promise<Teraslice.AssetIDResponse> {
if (isEmpty(id)) {
throw new TSError('Asset delete requires a ID', {
statusCode: 400
});
}
const results = await this.delete(`/assets/${id}`, searchOptions);
return this.parse(results);
}
async list(
query: Teraslice.SearchQuery = {},
searchOptions: SearchOptions = {}
): Promise<Teraslice.AssetRecord[]> {
const options = { ...searchOptions, searchParams: query };
return this.get('/assets', options);
}
async getAsset(
name: string,
version = '',
searchOptions: SearchOptions = {}
): Promise<Teraslice.AssetRecord[]> {
if (!name || !isString(name)) {
throw new TSError('Name is required, and must be of type string', {
statusCode: 400
});
}
if (version && !isString(version)) {
throw new TSError('Version if provided must be of type string', {
statusCode: 400
});
}
const assetUrl = path.join('/assets', name, version);
return this.get(assetUrl, searchOptions);
}
async txt(
name = '',
version = '',
query: Teraslice.TxtSearchParams = {},
searchOptions: SearchOptions = {}
): Promise<string> {
if (name && !isString(name)) {
throw new TSError('Name must be of type string', {
statusCode: 400
});
}
if (version && !isString(version)) {
throw new TSError('Version must be of type string', {
statusCode: 400
});
}
const options = Object.assign({ responseType: 'text' }, searchOptions, {
searchParams: query
});
const pathing = path.join('/txt/assets', name, version);
return super.get(pathing, options);
}
}