// 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 { CreateMeterRequest, CreateMeterResponse, GetMeterRequest, GetMeterResponse, ListMetersRequest, ListMetersResponse, UpdateMeterRequest, UpdateMeterResponse, DeleteMeterRequest, DeleteMeterResponse, QueryMeterRequest, QueryMeterResponse, QueryMeterCsvRequest, QueryMeterCsvResponse, } from '../models/operations/meters.js' /** * Create meter * * Create a meter. * * POST /openmeter/meters */ export function createMeter( client: Client, req: CreateMeterRequest, options?: RequestOptions, ): Promise> { return request(() => { const body = toWire(req, schemas.createMeterBody) if (client._options.validate) { assertValid(schemas.createMeterBodyWire, body) } return http(client) .post('openmeter/meters', { ...options, json: body }) .json() .then((data) => { if (client._options.validate) { assertValid(schemas.createMeterResponseWire, data) } return fromWire(data, schemas.createMeterResponse) }) }) } /** * Get meter * * Get a meter by ID. * * GET /openmeter/meters/{meterId} */ export function getMeter( client: Client, req: GetMeterRequest, options?: RequestOptions, ): Promise> { return request(() => { const pathParamsInput = { meterId: req.meterId, } const pathParams = client._options.validate ? toPathWire(pathParamsInput, schemas.getMeterPathParams) : pathParamsInput if (client._options.validate) { assertValid(schemas.getMeterPathParamsWire, pathParams) } const path = `openmeter/meters/${(() => { if (pathParams.meterId === undefined) { throw new Error('missing path parameter: meterId') } return encodeURIComponent(String(pathParams.meterId)) })()}` return http(client) .get(path, options) .json() .then((data) => { if (client._options.validate) { assertValid(schemas.getMeterResponseWire, data) } return fromWire(data, schemas.getMeterResponse) }) }) } /** * List meters * * List meters. * * GET /openmeter/meters */ export function listMeters( client: Client, req: ListMetersRequest = {}, options?: RequestOptions, ): Promise> { return request(() => { if (client._options.validate && req.sort !== undefined) { assertValid(schemas.listMetersQueryParams.shape.sort, req.sort) } const query = toWire( { page: req.page, sort: encodeSort(req.sort, toSnakeCase), filter: req.filter, }, schemas.listMetersQueryParams, ) if (client._options.validate) { assertValid(schemas.listMetersQueryParamsWire, query) } const searchParams = toURLSearchParams(query) return http(client) .get('openmeter/meters', { ...options, searchParams }) .json() .then((data) => { if (client._options.validate) { assertValid(schemas.listMetersResponseWire, data) } return fromWire(data, schemas.listMetersResponse) }) }) } /** * Update meter * * Update a meter. * * PUT /openmeter/meters/{meterId} */ export function updateMeter( client: Client, req: UpdateMeterRequest, options?: RequestOptions, ): Promise> { return request(() => { const pathParamsInput = { meterId: req.meterId, } const pathParams = client._options.validate ? toPathWire(pathParamsInput, schemas.updateMeterPathParams) : pathParamsInput if (client._options.validate) { assertValid(schemas.updateMeterPathParamsWire, pathParams) } const path = `openmeter/meters/${(() => { if (pathParams.meterId === undefined) { throw new Error('missing path parameter: meterId') } return encodeURIComponent(String(pathParams.meterId)) })()}` const body = toWire(req.body, schemas.updateMeterBody) if (client._options.validate) { assertValid(schemas.updateMeterBodyWire, body) } return http(client) .put(path, { ...options, json: body }) .json() .then((data) => { if (client._options.validate) { assertValid(schemas.updateMeterResponseWire, data) } return fromWire(data, schemas.updateMeterResponse) }) }) } /** * Delete meter * * Delete a meter. * * DELETE /openmeter/meters/{meterId} */ export function deleteMeter( client: Client, req: DeleteMeterRequest, options?: RequestOptions, ): Promise> { return request(async () => { const pathParamsInput = { meterId: req.meterId, } const pathParams = client._options.validate ? toPathWire(pathParamsInput, schemas.deleteMeterPathParams) : pathParamsInput if (client._options.validate) { assertValid(schemas.deleteMeterPathParamsWire, pathParams) } const path = `openmeter/meters/${(() => { if (pathParams.meterId === undefined) { throw new Error('missing path parameter: meterId') } return encodeURIComponent(String(pathParams.meterId)) })()}` await http(client).delete(path, options) }) } /** * Query meter * * Query a meter for usage. * * Set `Accept: application/json` (the default) to get a structured JSON response. * Set `Accept: text/csv` to download the same data as a CSV file suitable for * spreadsheets. The CSV columns, in order, are: * * `from, to, [subject,] [customer_id, customer_key, customer_name,] , value` * * The `subject` column is emitted only when `subject` is in the query's * `group_by_dimensions`. The three `customer_*` columns are emitted together only * when `customer_id` is in the query's `group_by_dimensions`. * * POST /openmeter/meters/{meterId}/query */ export function queryMeter( client: Client, req: QueryMeterRequest, options?: RequestOptions, ): Promise> { return request(() => { const pathParamsInput = { meterId: req.meterId, } const pathParams = client._options.validate ? toPathWire(pathParamsInput, schemas.queryMeterPathParams) : pathParamsInput if (client._options.validate) { assertValid(schemas.queryMeterPathParamsWire, pathParams) } const path = `openmeter/meters/${(() => { if (pathParams.meterId === undefined) { throw new Error('missing path parameter: meterId') } return encodeURIComponent(String(pathParams.meterId)) })()}/query` const body = toWire(req.body, schemas.queryMeterBody) if (client._options.validate) { assertValid(schemas.queryMeterBodyWire, body) } return http(client) .post(path, { ...options, json: body }) .json() .then((data) => { if (client._options.validate) { assertValid(schemas.queryMeterResponseWire, data) } return fromWire(data, schemas.queryMeterResponse) }) }) } /** POST /openmeter/meters/{meterId}/query */ export function queryMeterCsv( client: Client, req: QueryMeterCsvRequest, options?: RequestOptions, ): Promise> { const headers = new Headers(options?.headers as HeadersInit | undefined) headers.set('accept', 'text/csv') return request(() => { const pathParamsInput = { meterId: req.meterId, } const pathParams = client._options.validate ? toPathWire(pathParamsInput, schemas.queryMeterCsvPathParams) : pathParamsInput if (client._options.validate) { assertValid(schemas.queryMeterCsvPathParamsWire, pathParams) } const path = `openmeter/meters/${(() => { if (pathParams.meterId === undefined) { throw new Error('missing path parameter: meterId') } return encodeURIComponent(String(pathParams.meterId)) })()}/query` const body = toWire(req.body, schemas.queryMeterCsvBody) if (client._options.validate) { assertValid(schemas.queryMeterCsvBodyWire, body) } return http(client) .post(path, { ...options, json: body, headers }) .text() }) }