/
usage.ts
77 lines (66 loc) · 1.47 KB
/
usage.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
import { freezeSys, toPlainObject } from 'contentful-sdk-core'
import copy from 'fast-copy'
import {
DefaultElements,
MakeRequest,
MetaLinkProps,
MetaSysProps,
QueryOptions,
} from '../common-types'
import { wrapCollection } from '../common-utils'
import enhanceWithMethods from '../enhance-with-methods'
export type UsageMetricEnum = 'cda' | 'cma' | 'cpa' | 'gql'
export interface UsageQuery extends QueryOptions {
'metric[in]'?: string
'dateRange.startAt'?: string
'dateRange.endAt'?: string
}
export type UsageProps = {
/**
* System metadata
*/
sys: MetaSysProps & {
organization?: { sys: MetaLinkProps }
}
/**
* Type of usage
*/
metric: UsageMetricEnum
/**
* Unit of usage metric
*/
unitOfMeasure: string
/**
* Range of usage
*/
dateRange: {
startAt: string
endAt: string
}
/**
* Value of the usage
*/
usage: number
/**
* Usage per day
*/
usagePerDay: {
[key: string]: number
}
}
export interface Usage extends UsageProps, DefaultElements<UsageProps> {}
/**
* @private
* @param makeRequest - function to make requests via an adapter
* @param data - Raw data
* @return Normalized usage
*/
export function wrapUsage(_makeRequest: MakeRequest, data: UsageProps): Usage {
const usage = toPlainObject(copy(data))
const usageWithMethods = enhanceWithMethods(usage, {})
return freezeSys(usageWithMethods)
}
/**
* @private
*/
export const wrapUsageCollection = wrapCollection(wrapUsage)