This repository has been archived by the owner on Oct 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collections.ts
52 lines (44 loc) · 1.58 KB
/
collections.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
import { get } from './request'
import {
ListCollectionsResponse,
GetCollectionResponse,
Collection,
OrderParameter,
VisibilityParameter,
} from './types/helpscout-docs'
export type CollectionSortParameter = 'number' | 'visibility' | 'order' | 'name' | 'createdAt' | 'updatedAt'
export interface ListCollectionsOptions {
page?: number
siteId?: string
visibility?: VisibilityParameter
sort?: CollectionSortParameter
order?: OrderParameter
}
export async function listCollections(apiToken: string, options?: ListCollectionsOptions) {
const response = await get<ListCollectionsResponse>(apiToken, 'collections', options)
return response.body
}
export interface ListAllcollectionsOptions {
siteId?: string
visibility?: VisibilityParameter
sort?: CollectionSortParameter
order?: OrderParameter
}
export async function listAllCollections(apiToken: string, options?: ListAllcollectionsOptions) {
const collections: Collection[] = []
const baseOptions = Object.assign({}, options)
let currentPage = 1
let totalPages = 1
while (currentPage <= totalPages) {
const currentOptions = Object.assign({}, baseOptions, { page: currentPage })
const result = await listCollections(apiToken, currentOptions)
collections.splice(-1, 0, ...result.collections.items)
totalPages = result.collections.pages
currentPage += 1
}
return collections
}
export async function getCollection(apiToken: string, collectionIdOrNumber: string | number) {
const response = await get<GetCollectionResponse>(apiToken, `collections/${collectionIdOrNumber}`)
return response.body
}