// 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 { ListPlansRequest, ListPlansResponse, CreatePlanRequest, CreatePlanResponse, UpdatePlanRequest, UpdatePlanResponse, GetPlanRequest, GetPlanResponse, DeletePlanRequest, DeletePlanResponse, ArchivePlanRequest, ArchivePlanResponse, PublishPlanRequest, PublishPlanResponse, } from '../models/operations/plans.js' /** * List plans * * List all plans. * * GET /openmeter/plans */ export function listPlans( client: Client, req: ListPlansRequest = {}, options?: RequestOptions, ): Promise> { return request(() => { if (client._options.validate && req.sort !== undefined) { assertValid(schemas.listPlansQueryParams.shape.sort, req.sort) } const query = toWire( { page: req.page, sort: encodeSort(req.sort, toSnakeCase), filter: req.filter, }, schemas.listPlansQueryParams, ) if (client._options.validate) { assertValid(schemas.listPlansQueryParamsWire, query) } const searchParams = toURLSearchParams(query) return http(client) .get('openmeter/plans', { ...options, searchParams }) .json() .then((data) => { if (client._options.validate) { assertValid(schemas.listPlansResponseWire, data) } return fromWire(data, schemas.listPlansResponse) }) }) } /** * Create plan * * Create a new plan. * * POST /openmeter/plans */ export function createPlan( client: Client, req: CreatePlanRequest, options?: RequestOptions, ): Promise> { return request(() => { const body = toWire(req, schemas.createPlanBody) if (client._options.validate) { assertValid(schemas.createPlanBodyWire, body) } return http(client) .post('openmeter/plans', { ...options, json: body }) .json() .then((data) => { if (client._options.validate) { assertValid(schemas.createPlanResponseWire, data) } return fromWire(data, schemas.createPlanResponse) }) }) } /** * Update plan * * Update a plan by id. * * PUT /openmeter/plans/{planId} */ export function updatePlan( client: Client, req: UpdatePlanRequest, options?: RequestOptions, ): Promise> { return request(() => { const pathParamsInput = { planId: req.planId, } const pathParams = client._options.validate ? toPathWire(pathParamsInput, schemas.updatePlanPathParams) : pathParamsInput if (client._options.validate) { assertValid(schemas.updatePlanPathParamsWire, pathParams) } const path = `openmeter/plans/${(() => { if (pathParams.planId === undefined) { throw new Error('missing path parameter: planId') } return encodeURIComponent(String(pathParams.planId)) })()}` const body = toWire(req.body, schemas.updatePlanBody) if (client._options.validate) { assertValid(schemas.updatePlanBodyWire, body) } return http(client) .put(path, { ...options, json: body }) .json() .then((data) => { if (client._options.validate) { assertValid(schemas.updatePlanResponseWire, data) } return fromWire(data, schemas.updatePlanResponse) }) }) } /** * Get plan * * Get a plan by id. * * GET /openmeter/plans/{planId} */ export function getPlan( client: Client, req: GetPlanRequest, options?: RequestOptions, ): Promise> { return request(() => { const pathParamsInput = { planId: req.planId, } const pathParams = client._options.validate ? toPathWire(pathParamsInput, schemas.getPlanPathParams) : pathParamsInput if (client._options.validate) { assertValid(schemas.getPlanPathParamsWire, pathParams) } const path = `openmeter/plans/${(() => { if (pathParams.planId === undefined) { throw new Error('missing path parameter: planId') } return encodeURIComponent(String(pathParams.planId)) })()}` return http(client) .get(path, options) .json() .then((data) => { if (client._options.validate) { assertValid(schemas.getPlanResponseWire, data) } return fromWire(data, schemas.getPlanResponse) }) }) } /** * Delete plan * * Delete a plan by id. * * DELETE /openmeter/plans/{planId} */ export function deletePlan( client: Client, req: DeletePlanRequest, options?: RequestOptions, ): Promise> { return request(async () => { const pathParamsInput = { planId: req.planId, } const pathParams = client._options.validate ? toPathWire(pathParamsInput, schemas.deletePlanPathParams) : pathParamsInput if (client._options.validate) { assertValid(schemas.deletePlanPathParamsWire, pathParams) } const path = `openmeter/plans/${(() => { if (pathParams.planId === undefined) { throw new Error('missing path parameter: planId') } return encodeURIComponent(String(pathParams.planId)) })()}` await http(client).delete(path, options) }) } /** * Archive plan version * * Archive a plan version. * * POST /openmeter/plans/{planId}/archive */ export function archivePlan( client: Client, req: ArchivePlanRequest, options?: RequestOptions, ): Promise> { return request(() => { const pathParamsInput = { planId: req.planId, } const pathParams = client._options.validate ? toPathWire(pathParamsInput, schemas.archivePlanPathParams) : pathParamsInput if (client._options.validate) { assertValid(schemas.archivePlanPathParamsWire, pathParams) } const path = `openmeter/plans/${(() => { if (pathParams.planId === undefined) { throw new Error('missing path parameter: planId') } return encodeURIComponent(String(pathParams.planId)) })()}/archive` return http(client) .post(path, options) .json() .then((data) => { if (client._options.validate) { assertValid(schemas.archivePlanResponseWire, data) } return fromWire(data, schemas.archivePlanResponse) }) }) } /** * Publish plan version * * Publish a plan version. * * POST /openmeter/plans/{planId}/publish */ export function publishPlan( client: Client, req: PublishPlanRequest, options?: RequestOptions, ): Promise> { return request(() => { const pathParamsInput = { planId: req.planId, } const pathParams = client._options.validate ? toPathWire(pathParamsInput, schemas.publishPlanPathParams) : pathParamsInput if (client._options.validate) { assertValid(schemas.publishPlanPathParamsWire, pathParams) } const path = `openmeter/plans/${(() => { if (pathParams.planId === undefined) { throw new Error('missing path parameter: planId') } return encodeURIComponent(String(pathParams.planId)) })()}/publish` return http(client) .post(path, options) .json() .then((data) => { if (client._options.validate) { assertValid(schemas.publishPlanResponseWire, data) } return fromWire(data, schemas.publishPlanResponse) }) }) }