File size: 5,652 Bytes
1477a90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Code generated by @openmeter/typespec-typescript. DO NOT EDIT.

import { type Client, http } from '../core.js'
import { type Result, type RequestOptions } from '../lib/types.js'
import { request } from '../lib/request.js'
import { toURLSearchParams, encodeSort } from '../lib/encodings.js'
import {
  toWire,
  toPathWire,
  fromWire,
  assertValid,
  toSnakeCase,
} from '../lib/wire.js'
import * as schemas from '../models/schemas.js'
import type {
  ListCurrenciesRequest,
  ListCurrenciesResponse,
  CreateCustomCurrencyRequest,
  CreateCustomCurrencyResponse,
  ListCostBasesRequest,
  ListCostBasesResponse,
  CreateCostBasisRequest,
  CreateCostBasisResponse,
} from '../models/operations/currencies.js'

/**
 * List currencies
 *
 * List currencies supported by the billing system.
 *
 * GET /openmeter/currencies
 */
export function listCurrencies(
  client: Client,
  req: ListCurrenciesRequest = {},
  options?: RequestOptions,
): Promise<Result<ListCurrenciesResponse>> {
  return request(() => {
    if (client._options.validate && req.sort !== undefined) {
      assertValid(schemas.listCurrenciesQueryParams.shape.sort, req.sort)
    }
    const query = toWire(
      {
        page: req.page,
        sort: encodeSort(req.sort, toSnakeCase),
        filter: req.filter,
      },
      schemas.listCurrenciesQueryParams,
    )
    if (client._options.validate) {
      assertValid(schemas.listCurrenciesQueryParamsWire, query)
    }
    const searchParams = toURLSearchParams(query)
    return http(client)
      .get('openmeter/currencies', { ...options, searchParams })
      .json()
      .then((data) => {
        if (client._options.validate) {
          assertValid(schemas.listCurrenciesResponseWire, data)
        }
        return fromWire(data, schemas.listCurrenciesResponse)
      })
  })
}

/**
 * Create custom currency
 *
 * Create a custom currency. This operation allows defining your own custom
 * currency for billing purposes.
 *
 * POST /openmeter/currencies/custom
 */
export function createCustomCurrency(
  client: Client,
  req: CreateCustomCurrencyRequest,
  options?: RequestOptions,
): Promise<Result<CreateCustomCurrencyResponse>> {
  return request(() => {
    const body = toWire(req, schemas.createCustomCurrencyBody)
    if (client._options.validate) {
      assertValid(schemas.createCustomCurrencyBodyWire, body)
    }
    return http(client)
      .post('openmeter/currencies/custom', { ...options, json: body })
      .json()
      .then((data) => {
        if (client._options.validate) {
          assertValid(schemas.createCustomCurrencyResponseWire, data)
        }
        return fromWire(data, schemas.createCustomCurrencyResponse)
      })
  })
}

/**
 * List cost bases
 *
 * List cost bases for a currency. For custom currencies, there can be multiple
 * cost bases with different `effective_from` dates.
 *
 * GET /openmeter/currencies/custom/{currencyId}/cost-bases
 */
export function listCostBases(
  client: Client,
  req: ListCostBasesRequest,
  options?: RequestOptions,
): Promise<Result<ListCostBasesResponse>> {
  return request(() => {
    const pathParamsInput = {
      currencyId: req.currencyId,
    }
    const pathParams = client._options.validate
      ? toPathWire(pathParamsInput, schemas.listCostBasesPathParams)
      : pathParamsInput
    if (client._options.validate) {
      assertValid(schemas.listCostBasesPathParamsWire, pathParams)
    }
    const path = `openmeter/currencies/custom/${(() => {
      if (pathParams.currencyId === undefined) {
        throw new Error('missing path parameter: currencyId')
      }
      return encodeURIComponent(String(pathParams.currencyId))
    })()}/cost-bases`
    const query = toWire(
      {
        filter: req.filter,
        page: req.page,
      },
      schemas.listCostBasesQueryParams,
    )
    if (client._options.validate) {
      assertValid(schemas.listCostBasesQueryParamsWire, query)
    }
    const searchParams = toURLSearchParams(query)
    return http(client)
      .get(path, { ...options, searchParams })
      .json()
      .then((data) => {
        if (client._options.validate) {
          assertValid(schemas.listCostBasesResponseWire, data)
        }
        return fromWire(data, schemas.listCostBasesResponse)
      })
  })
}

/**
 * Create cost basis
 *
 * Create a cost basis for a currency.
 *
 * POST /openmeter/currencies/custom/{currencyId}/cost-bases
 */
export function createCostBasis(
  client: Client,
  req: CreateCostBasisRequest,
  options?: RequestOptions,
): Promise<Result<CreateCostBasisResponse>> {
  return request(() => {
    const pathParamsInput = {
      currencyId: req.currencyId,
    }
    const pathParams = client._options.validate
      ? toPathWire(pathParamsInput, schemas.createCostBasisPathParams)
      : pathParamsInput
    if (client._options.validate) {
      assertValid(schemas.createCostBasisPathParamsWire, pathParams)
    }
    const path = `openmeter/currencies/custom/${(() => {
      if (pathParams.currencyId === undefined) {
        throw new Error('missing path parameter: currencyId')
      }
      return encodeURIComponent(String(pathParams.currencyId))
    })()}/cost-bases`
    const body = toWire(req.body, schemas.createCostBasisBody)
    if (client._options.validate) {
      assertValid(schemas.createCostBasisBodyWire, body)
    }
    return http(client)
      .post(path, { ...options, json: body })
      .json()
      .then((data) => {
        if (client._options.validate) {
          assertValid(schemas.createCostBasisResponseWire, data)
        }
        return fromWire(data, schemas.createCostBasisResponse)
      })
  })
}