File size: 11,988 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// 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 {
  ListInvoicesRequest,
  ListInvoicesResponse,
  GetInvoiceRequest,
  GetInvoiceResponse,
  UpdateInvoiceRequest,
  UpdateInvoiceResponse,
  DeleteInvoiceRequest,
  DeleteInvoiceResponse,
  AdvanceInvoiceRequest,
  AdvanceInvoiceResponse,
  ApproveInvoiceRequest,
  ApproveInvoiceResponse,
  RetryInvoiceRequest,
  RetryInvoiceResponse,
  SnapshotQuantitiesInvoiceRequest,
  SnapshotQuantitiesInvoiceResponse,
} from '../models/operations/invoices.js'

/**
 * List billing invoices
 *
 * List billing invoices.
 *
 * Returns a page of invoices. Gathering invoices are never included. Use `filter`
 * to narrow by status, customer, dates, or service period start. Use `sort` to
 * control ordering.
 *
 * GET /openmeter/billing/invoices
 */
export function listInvoices(
  client: Client,
  req: ListInvoicesRequest = {},
  options?: RequestOptions,
): Promise<Result<ListInvoicesResponse>> {
  return request(() => {
    if (client._options.validate && req.sort !== undefined) {
      assertValid(schemas.listInvoicesQueryParams.shape.sort, req.sort)
    }
    const query = toWire(
      {
        page: req.page,
        sort: encodeSort(req.sort, toSnakeCase),
        filter: req.filter,
      },
      schemas.listInvoicesQueryParams,
    )
    if (client._options.validate) {
      assertValid(schemas.listInvoicesQueryParamsWire, query)
    }
    const searchParams = toURLSearchParams(query)
    return http(client)
      .get('openmeter/billing/invoices', { ...options, searchParams })
      .json()
      .then((data) => {
        if (client._options.validate) {
          assertValid(schemas.listInvoicesResponseWire, data)
        }
        return fromWire(data, schemas.listInvoicesResponse)
      })
  })
}

/**
 * Get a billing invoice
 *
 * Get a billing invoice by ID.
 *
 * Returns the full invoice resource including line items, status details, totals,
 * and workflow configuration snapshot.
 *
 * GET /openmeter/billing/invoices/{invoiceId}
 */
export function getInvoice(
  client: Client,
  req: GetInvoiceRequest,
  options?: RequestOptions,
): Promise<Result<GetInvoiceResponse>> {
  return request(() => {
    const pathParamsInput = {
      invoiceId: req.invoiceId,
    }
    const pathParams = client._options.validate
      ? toPathWire(pathParamsInput, schemas.getInvoicePathParams)
      : pathParamsInput
    if (client._options.validate) {
      assertValid(schemas.getInvoicePathParamsWire, pathParams)
    }
    const path = `openmeter/billing/invoices/${(() => {
      if (pathParams.invoiceId === undefined) {
        throw new Error('missing path parameter: invoiceId')
      }
      return encodeURIComponent(String(pathParams.invoiceId))
    })()}`
    return http(client)
      .get(path, options)
      .json()
      .then((data) => {
        if (client._options.validate) {
          assertValid(schemas.getInvoiceResponseWire, data)
        }
        return fromWire(data, schemas.getInvoiceResponse)
      })
  })
}

/**
 * Update a billing invoice
 *
 * Update a billing invoice.
 *
 * Only the mutable fields of the invoice can be edited: description, labels,
 * supplier, customer, workflow settings, and top-level lines. Top-level lines are
 * matched by `id`; lines without an `id` are created, and existing lines omitted
 * from `lines` are deleted. Detailed (child) lines are always computed and cannot
 * be edited directly. Only invoices in draft status can be updated.
 *
 * PUT /openmeter/billing/invoices/{invoiceId}
 */
export function updateInvoice(
  client: Client,
  req: UpdateInvoiceRequest,
  options?: RequestOptions,
): Promise<Result<UpdateInvoiceResponse>> {
  return request(() => {
    const pathParamsInput = {
      invoiceId: req.invoiceId,
    }
    const pathParams = client._options.validate
      ? toPathWire(pathParamsInput, schemas.updateInvoicePathParams)
      : pathParamsInput
    if (client._options.validate) {
      assertValid(schemas.updateInvoicePathParamsWire, pathParams)
    }
    const path = `openmeter/billing/invoices/${(() => {
      if (pathParams.invoiceId === undefined) {
        throw new Error('missing path parameter: invoiceId')
      }
      return encodeURIComponent(String(pathParams.invoiceId))
    })()}`
    const body = toWire(req.body, schemas.updateInvoiceBody)
    if (client._options.validate) {
      assertValid(schemas.updateInvoiceBodyWire, body)
    }
    return http(client)
      .put(path, { ...options, json: body })
      .json()
      .then((data) => {
        if (client._options.validate) {
          assertValid(schemas.updateInvoiceResponseWire, data)
        }
        return fromWire(data, schemas.updateInvoiceResponse)
      })
  })
}

/**
 * Delete a billing invoice
 *
 * Delete a billing invoice.
 *
 * Only standard invoices in draft status can be deleted. Deleting an invoice will
 * also delete all associated line items and workflow configuration.
 *
 * DELETE /openmeter/billing/invoices/{invoiceId}
 */
export function deleteInvoice(
  client: Client,
  req: DeleteInvoiceRequest,
  options?: RequestOptions,
): Promise<Result<DeleteInvoiceResponse>> {
  return request(async () => {
    const pathParamsInput = {
      invoiceId: req.invoiceId,
    }
    const pathParams = client._options.validate
      ? toPathWire(pathParamsInput, schemas.deleteInvoicePathParams)
      : pathParamsInput
    if (client._options.validate) {
      assertValid(schemas.deleteInvoicePathParamsWire, pathParams)
    }
    const path = `openmeter/billing/invoices/${(() => {
      if (pathParams.invoiceId === undefined) {
        throw new Error('missing path parameter: invoiceId')
      }
      return encodeURIComponent(String(pathParams.invoiceId))
    })()}`
    await http(client).delete(path, options)
  })
}

/**
 * Advance billing invoice's next status
 *
 * Advance a billing invoice.
 *
 * Advances the invoice to the next workflow state. The next state is determined by
 * the invoice's current status and workflow configuration. Only invoices in draft
 * or issued status can be advanced.
 *
 * POST /openmeter/billing/invoices/{invoiceId}/advance
 */
export function advanceInvoice(
  client: Client,
  req: AdvanceInvoiceRequest,
  options?: RequestOptions,
): Promise<Result<AdvanceInvoiceResponse>> {
  return request(() => {
    const pathParamsInput = {
      invoiceId: req.invoiceId,
    }
    const pathParams = client._options.validate
      ? toPathWire(pathParamsInput, schemas.advanceInvoicePathParams)
      : pathParamsInput
    if (client._options.validate) {
      assertValid(schemas.advanceInvoicePathParamsWire, pathParams)
    }
    const path = `openmeter/billing/invoices/${(() => {
      if (pathParams.invoiceId === undefined) {
        throw new Error('missing path parameter: invoiceId')
      }
      return encodeURIComponent(String(pathParams.invoiceId))
    })()}/advance`
    return http(client)
      .post(path, options)
      .json()
      .then((data) => {
        if (client._options.validate) {
          assertValid(schemas.advanceInvoiceResponseWire, data)
        }
        return fromWire(data, schemas.advanceInvoiceResponse)
      })
  })
}

/**
 * Send the invoice to the customer
 *
 * Approve a billing invoice.
 *
 * This call instantly sends the invoice to the customer using the configured
 * billing profile app.
 *
 * This call is valid in two invoice statuses:
 *
 * - draft: the invoice will be sent to the customer, the invoice state becomes
 * issued
 * - manual_approval_needed: the invoice will be sent to the customer, the invoice
 * state becomes issued
 *
 * POST /openmeter/billing/invoices/{invoiceId}/approve
 */
export function approveInvoice(
  client: Client,
  req: ApproveInvoiceRequest,
  options?: RequestOptions,
): Promise<Result<ApproveInvoiceResponse>> {
  return request(() => {
    const pathParamsInput = {
      invoiceId: req.invoiceId,
    }
    const pathParams = client._options.validate
      ? toPathWire(pathParamsInput, schemas.approveInvoicePathParams)
      : pathParamsInput
    if (client._options.validate) {
      assertValid(schemas.approveInvoicePathParamsWire, pathParams)
    }
    const path = `openmeter/billing/invoices/${(() => {
      if (pathParams.invoiceId === undefined) {
        throw new Error('missing path parameter: invoiceId')
      }
      return encodeURIComponent(String(pathParams.invoiceId))
    })()}/approve`
    return http(client)
      .post(path, options)
      .json()
      .then((data) => {
        if (client._options.validate) {
          assertValid(schemas.approveInvoiceResponseWire, data)
        }
        return fromWire(data, schemas.approveInvoiceResponse)
      })
  })
}

/**
 * Retry advancing the invoice after a failed attempt
 *
 * Retry sending a billing invoice.
 *
 * Retry advancing the invoice after a failed attempt.
 *
 * The action can be called when the invoice's statusDetails' actions field contain
 * the "retry" action.
 *
 * POST /openmeter/billing/invoices/{invoiceId}/retry
 */
export function retryInvoice(
  client: Client,
  req: RetryInvoiceRequest,
  options?: RequestOptions,
): Promise<Result<RetryInvoiceResponse>> {
  return request(() => {
    const pathParamsInput = {
      invoiceId: req.invoiceId,
    }
    const pathParams = client._options.validate
      ? toPathWire(pathParamsInput, schemas.retryInvoicePathParams)
      : pathParamsInput
    if (client._options.validate) {
      assertValid(schemas.retryInvoicePathParamsWire, pathParams)
    }
    const path = `openmeter/billing/invoices/${(() => {
      if (pathParams.invoiceId === undefined) {
        throw new Error('missing path parameter: invoiceId')
      }
      return encodeURIComponent(String(pathParams.invoiceId))
    })()}/retry`
    return http(client)
      .post(path, options)
      .json()
      .then((data) => {
        if (client._options.validate) {
          assertValid(schemas.retryInvoiceResponseWire, data)
        }
        return fromWire(data, schemas.retryInvoiceResponse)
      })
  })
}

/**
 * Snapshot quantities for usage based line items
 *
 * Snapshot quantities for usage-based line items.
 *
 * This call will snapshot the quantities for all usage based line items in the
 * invoice.
 *
 * This call is only valid in draft.waiting_for_collection status, where the
 * collection period can be skipped using this action.
 *
 * POST /openmeter/billing/invoices/{invoiceId}/snapshot-quantities
 */
export function snapshotQuantitiesInvoice(
  client: Client,
  req: SnapshotQuantitiesInvoiceRequest,
  options?: RequestOptions,
): Promise<Result<SnapshotQuantitiesInvoiceResponse>> {
  return request(() => {
    const pathParamsInput = {
      invoiceId: req.invoiceId,
    }
    const pathParams = client._options.validate
      ? toPathWire(pathParamsInput, schemas.snapshotQuantitiesInvoicePathParams)
      : pathParamsInput
    if (client._options.validate) {
      assertValid(schemas.snapshotQuantitiesInvoicePathParamsWire, pathParams)
    }
    const path = `openmeter/billing/invoices/${(() => {
      if (pathParams.invoiceId === undefined) {
        throw new Error('missing path parameter: invoiceId')
      }
      return encodeURIComponent(String(pathParams.invoiceId))
    })()}/snapshot-quantities`
    return http(client)
      .post(path, options)
      .json()
      .then((data) => {
        if (client._options.validate) {
          assertValid(schemas.snapshotQuantitiesInvoiceResponseWire, data)
        }
        return fromWire(data, schemas.snapshotQuantitiesInvoiceResponse)
      })
  })
}