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
/
categories.ts
44 lines (36 loc) · 1.54 KB
/
categories.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
import { OrderParameter, ListCategoriesResponse, GetCategoryResponse, Category } from './types/helpscout-docs'
import { get } from './request'
type CategoriesSortParameter = 'number' | 'order' | 'name' | 'articleCount' | 'createdAt' | 'updatedAt'
interface ListCategoriesOptions {
page?: number
sort?: CategoriesSortParameter
order?: OrderParameter
}
export async function listCategories(apiToken: string, collectionId: string, options?: ListCategoriesOptions) {
const path = `collections/${collectionId}/categories`
const response = await get<ListCategoriesResponse>(apiToken, path, options)
return response.body
}
interface ListAllCategoriesOptions {
sort?: CategoriesSortParameter
order?: OrderParameter
}
export async function listAllCategories(apiToken: string, collectionId: string, options?: ListAllCategoriesOptions) {
const categories: Category[] = []
const baseOptions = Object.assign({}, options)
let currentPage = 1
let totalPages = 1
while (currentPage <= totalPages) {
const currentOptions = Object.assign({}, baseOptions, { page: currentPage })
const categoriesList = await listCategories(apiToken, collectionId, currentOptions)
categories.splice(-1, 0, ...categoriesList.categories.items)
totalPages = categoriesList.categories.pages
currentPage += 1
}
return categories
}
export async function getCategory(apiToken: string, categoryIdOrNumber: string | number) {
const path = `categories/${categoryIdOrNumber}`
const response = await get<GetCategoryResponse>(apiToken, path)
return response.body
}