import {z} from 'zod' export type DeleteApiOrganizationsOrgIdStatementdefinitionsIdArgs = {orgId: string, id: string}; export type DeleteApiOrganizationsOrgIdStatementdefinitionsIdOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const aggregationFrequency = z.enum(["DAY", "WEEK", "MONTH", "QUARTER", "YEAR", "WHOLE_PERIOD"]); export const dimension = z.object({meterId: z.string().optional(), name: z.string().min(1), attributes: z.array(z.string()).optional(), filter: z.array(z.string())}); export const aggregation = z.enum(["SUM", "MIN", "MAX", "COUNT", "LATEST", "MEAN", "UNIQUE", "CUSTOM_SQL"]); export const measure = z.object({meterId: z.string().optional(), name: z.string().optional(), aggregations: z.array(aggregation).optional()}); export const statementDefinitionResponse = z.object({id: z.string(), version: z.number().int().optional(), name: z.string().optional(), aggregationFrequency: aggregationFrequency.optional(), includePricePerUnit: z.boolean().optional(), dimensions: z.array(dimension).optional(), measures: z.array(measure).optional(), generateSlimStatements: z.boolean().optional(), dtCreated: z.string().optional(), dtLastModified: z.string().optional(), createdBy: z.string().optional(), lastModifiedBy: z.string().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Delete StatementDefinition * * DELETE /organizations/{orgId}/statementdefinitions/{id} */ export const deleteApiOrganizationsOrgIdStatementdefinitionsId = async (args: DeleteApiOrganizationsOrgIdStatementdefinitionsIdArgs, options?: DeleteApiOrganizationsOrgIdStatementdefinitionsIdOptions) => { const {baseUrl = "https://api.m3ter.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/organizations/${encodeURIComponent(String(args.orgId))}/statementdefinitions/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "DELETE", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(statementDefinitionResponse, await res.json()) };