import {z} from 'zod' export type Unit = string; export type NullableIndustryTaxonomySubIndustryCode = string | null; export type FunctionalUnitMeasurementType = 'COUNTER' | 'GAUGE'; export type CreateApiV1OrganizationFunctionalUnitsArgs = {body: {name: string, description?: string | undefined, defaultDisplayUnit: Unit, subIndustryCode?: NullableIndustryTaxonomySubIndustryCode | undefined, measurementType?: FunctionalUnitMeasurementType | undefined}}; export type CreateApiV1OrganizationFunctionalUnitsOptions = {baseUrl?: string | undefined, OAuth2ClientCredentialsProduction?: string | undefined, fetch?: typeof fetch | undefined}; export const functionalUnitID = z.string(); export const unit = z.string(); export const nullableOrganizationID = z.string().nullable(); export const nullableIndustryTaxonomySubIndustryCode = z.string().nullable(); export const functionalUnitMeasurementType = z.enum(["COUNTER", "GAUGE"]); export const functionalUnit = z.object({id: functionalUnitID, name: z.string(), description: z.string().optional(), defaultDisplayUnit: unit, supportedUnits: z.array(unit), orgID: nullableOrganizationID.optional(), subIndustryCode: nullableIndustryTaxonomySubIndustryCode, measurementType: functionalUnitMeasurementType.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 }; /** * Create a functional unit * * POST /v1/organization/functional_units * * Also accepts auth: OAuth2AuthCodeProduction */ export const createApiV1OrganizationFunctionalUnits = async (args: CreateApiV1OrganizationFunctionalUnitsArgs, options?: CreateApiV1OrganizationFunctionalUnitsOptions) => { const {baseUrl = "https://api.sinai.com", fetch: fetchFn = fetch, OAuth2ClientCredentialsProduction} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2ClientCredentialsProduction !== undefined) headers.set('authorization', `Bearer ${OAuth2ClientCredentialsProduction}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/organization/functional_units${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(functionalUnit, await res.json()) };