import {z} from 'zod' export type BusinessEntityTypeID = string; export type BusinessEntityTypeCategory = 'ORG_LEVEL' | 'MID_LEVEL' | 'FACILITY_LEVEL' | 'PROCESS_LEVEL'; export type GetApiV1OrganizationBusinessEntitiesArgs = {business_entity_type_id?: BusinessEntityTypeID | undefined, business_entity_type_category?: BusinessEntityTypeCategory | undefined}; export type GetApiV1OrganizationBusinessEntitiesOptions = {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 getApiV1OrganizationBusinessEntitiesResponse = z.array(businessEntity); 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 }; /** * List business entities in an org * * GET /v1/organization/business_entities * * Also accepts auth: OAuth2AuthCodeProduction */ export const getApiV1OrganizationBusinessEntities = async (args: GetApiV1OrganizationBusinessEntitiesArgs, options?: GetApiV1OrganizationBusinessEntitiesOptions) => { const {baseUrl = "https://api.sinai.com", fetch: fetchFn = fetch, OAuth2ClientCredentialsProduction} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.business_entity_type_id != null) query.append("business_entity_type_id", String(args.business_entity_type_id)) if (args.business_entity_type_category != null) query.append("business_entity_type_category", String(args.business_entity_type_category)) if (OAuth2ClientCredentialsProduction !== undefined) headers.set('authorization', `Bearer ${OAuth2ClientCredentialsProduction}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/organization/business_entities${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV1OrganizationBusinessEntitiesResponse, await res.json()) };