| |
|
|
| 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' |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function listPlans( |
| client: Client, |
| req: ListPlansRequest = {}, |
| options?: RequestOptions, |
| ): Promise<Result<ListPlansResponse>> { |
| 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) |
| }) |
| }) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function createPlan( |
| client: Client, |
| req: CreatePlanRequest, |
| options?: RequestOptions, |
| ): Promise<Result<CreatePlanResponse>> { |
| 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) |
| }) |
| }) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function updatePlan( |
| client: Client, |
| req: UpdatePlanRequest, |
| options?: RequestOptions, |
| ): Promise<Result<UpdatePlanResponse>> { |
| 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) |
| }) |
| }) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function getPlan( |
| client: Client, |
| req: GetPlanRequest, |
| options?: RequestOptions, |
| ): Promise<Result<GetPlanResponse>> { |
| 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) |
| }) |
| }) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function deletePlan( |
| client: Client, |
| req: DeletePlanRequest, |
| options?: RequestOptions, |
| ): Promise<Result<DeletePlanResponse>> { |
| 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) |
| }) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function archivePlan( |
| client: Client, |
| req: ArchivePlanRequest, |
| options?: RequestOptions, |
| ): Promise<Result<ArchivePlanResponse>> { |
| 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) |
| }) |
| }) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function publishPlan( |
| client: Client, |
| req: PublishPlanRequest, |
| options?: RequestOptions, |
| ): Promise<Result<PublishPlanResponse>> { |
| 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) |
| }) |
| }) |
| } |
|
|