/
fetchItemJobRecords.ts
72 lines (64 loc) · 1.97 KB
/
fetchItemJobRecords.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
import HubError from "../HubError";
import {
IHubDownloadJobRecord,
IHubJobRecord,
IHubJobRecordRequestOptions,
JobRecordType,
} from "./types";
/**
* Fetches job records for a specific item.
*
* @param id - The ID of the item.
* @param options - The options for fetching job records.
* @returns A promise that resolves to an array of job records.
* @throws {HubError} If any unimplemented options are provided.
*/
export async function fetchItemJobRecords(
id: string,
options: IHubJobRecordRequestOptions
): Promise<IHubJobRecord[]> {
const unimplementedOptions: Array<keyof IHubJobRecordRequestOptions> = [
"types",
"statuses",
];
if (unimplementedOptions.some((option) => options[option])) {
throw new HubError(
"fetchItemJobRecords",
`The following options are not yet implemented: ${unimplementedOptions.join(
", "
)}`
);
}
const { context } = options;
const requestUrl = `${
context.hubUrl
}/api/download/v1/items/${id}/errors${getQueryString(options)}`;
const { errors: rawErrors } = await fetch(requestUrl).then((response) =>
response.json()
);
// TODO: account for additional row types once the API supports them
return rawErrors.map(rowToDownloadJobRecord);
}
function getQueryString(options: IHubJobRecordRequestOptions): string {
const params = new URLSearchParams();
options.from && params.append("fromDate", options.from);
options.to && params.append("toDate", options.to);
options.limit && params.append("limit", options.limit.toString());
const result = params.toString();
return result && `?${result}`;
}
function rowToDownloadJobRecord(
row: Record<string, string>
): IHubDownloadJobRecord {
return {
type: JobRecordType.DOWNLOAD,
message: row.message,
created: new Date(row.timestamp).getTime(),
layerId: row.layerId,
// implement the rest of the fields once the API supports them
id: null,
status: null,
messageId: null,
modified: null,
};
}