import {z} from 'zod' export type BusinessEntityID = string; export type UpdateApiV1OrganizationBusinessEntitiesIdArgs = {id: BusinessEntityID, body: {name: string, description?: string | undefined}}; export type UpdateApiV1OrganizationBusinessEntitiesIdOptions = {baseUrl?: string | undefined, OAuth2ClientCredentialsProduction?: string | undefined, fetch?: typeof fetch | undefined}; export const businessEntityID = z.string(); export const organizationID = z.string(); export const nullableBusinessEntityID = z.string().nullable(); export const countryCode = z.string(); export const nullableIndustryTaxonomySubIndustryCode = z.string().nullable(); export const nullableFacilityTypeID = z.string().nullable(); export const nullableProcessTypeID = z.string().nullable(); export const businessEntityTypeID = z.string(); export const functionalUnitID = z.string(); export const businessEntity = z.object({id: businessEntityID, orgID: organizationID.optional(), parentID: nullableBusinessEntityID.optional(), name: z.string(), description: z.string().optional(), countryCode: countryCode.optional(), subIndustryCode: nullableIndustryTaxonomySubIndustryCode.optional(), facilityTypeID: nullableFacilityTypeID.optional(), processTypeID: nullableProcessTypeID.optional(), businessEntityTypeID: businessEntityTypeID.optional(), defaultFunctionalUnitID: functionalUnitID.optional(), defaultDisplayUnit: z.string().optional(), activeStartTimestamp: z.number().optional(), activeEndTimestamp: z.number().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 }; /** * Update a business entity * * PUT /v1/organization/business_entities/{id} * * Also accepts auth: OAuth2AuthCodeProduction */ export const updateApiV1OrganizationBusinessEntitiesId = async (args: UpdateApiV1OrganizationBusinessEntitiesIdArgs, options?: UpdateApiV1OrganizationBusinessEntitiesIdOptions) => { 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/business_entities/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(businessEntity, await res.json()) };