-
Notifications
You must be signed in to change notification settings - Fork 4
/
admin.ts
62 lines (59 loc) · 2.09 KB
/
admin.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
// Inventory Report API
export type InventoryReportCollectionInfo = {
id: number;
name: string;
};
export type InventoryReportInfo = {
collections: InventoryReportCollectionInfo[];
};
export type InventoryReportRequestResponse = {
message: string;
};
export type InventoryReportRequestParams = {
library: string;
baseEndpointUrl?: string;
};
export const DEFAULT_BASE_ENDPOINT_URL = "/admin/reports/inventory_report";
/**
* Get information about the inventory report that would be generated for the
* given library, if requested.
*
* @param library -- the library for which the report information is requested.
* @param baseEndpointUrl -- an optional baseURL (for testing). If not provided,
* the `defaultBaseEndpointUrl` will be used.
* @returns an object converted from the information JSON response, if successful.
* @throws an error, if the request is not successful.
*/
export const getInventoryReportInfo = async ({
library,
baseEndpointUrl = DEFAULT_BASE_ENDPOINT_URL,
}: InventoryReportRequestParams): Promise<InventoryReportInfo> => {
const endpointUrl = `${baseEndpointUrl}/${library}`;
const res = await fetch(endpointUrl);
if (!res.ok) {
throw new Error(`Request failed with status ${res.status}: GET ${res.url}`);
}
return res.json();
};
/**
* Request an inventory report for the given library.
*
* @param library -- the library for which the report information is requested.
* @param baseEndpointUrl -- an optional baseURL (for testing). If not provided,
* the `defaultBaseEndpointUrl` will be used.
* @returns an object converted from the message JSON response, if successful.
* @throws an error, if the request is not successful.
*/
export const requestInventoryReport = async ({
library,
baseEndpointUrl = DEFAULT_BASE_ENDPOINT_URL,
}: InventoryReportRequestParams): Promise<InventoryReportRequestResponse> => {
const endpointUrl = `${baseEndpointUrl}/${library}`;
const res = await fetch(endpointUrl, { method: "POST" });
if (!res.ok) {
throw new Error(
`Request failed with status ${res.status}: POST ${res.url}`
);
}
return res.json();
};