import {z} from 'zod' export type CreateApiCompanyDomainProductsArgs = {companyDomain: string, body: {name?: string | undefined, visibility?: number | undefined, billingCurrency?: string | undefined, displayName?: string | undefined, description?: string | undefined, isShippingAddressRequired?: boolean | undefined, isShippingAddressValidationRequired?: boolean | undefined, isBillingAddressValidationRequired?: boolean | undefined, isAdditionalEmailRequired?: boolean | undefined, isPhoneNumberRequired?: boolean | undefined, 'is MarketingConsentRequired'?: boolean | undefined, alertEmail?: string | undefined, orderedCustomFields?: ({customFieldID?: number | undefined}) | undefined, typeOfProduct?: number | undefined, productCountryLimitation?: ({isAnyCountryAllowed?: boolean | undefined, countrySettings?: Record | undefined}) | undefined, isAnyCountryAllowed?: boolean | undefined}}; export type CreateApiCompanyDomainProductsOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiCompanyDomainProductsResponse = z.object({productCountryLimitation: z.object({isAnyCountryAllowed: z.boolean().optional(), countrySettings: z.array(z.object({countryIso3: z.string().optional(), addressPermission: z.number().int().optional()})).optional()}).optional(), productId: z.number().int().optional(), name: z.string().optional(), visibility: z.number().int().optional(), billingCurrency: z.string().optional(), billingCurrencyTemplate: z.string().optional(), displayName: z.string().optional(), description: z.string().optional(), typeOfProduct: z.number().int().optional(), hasPlans: z.boolean().optional(), isShippingAddressRequired: z.boolean().optional(), isShippingAddressValidationRequired: z.boolean().optional(), isBillingAddressValidationRequired: z.boolean().optional(), isAdditionalEmailRequired: z.boolean().optional(), isPhoneNumberRequired: z.boolean().optional(), isMarketingConsentRequired: z.boolean().optional(), numberOfPlans: z.number().int().optional(), alertEmail: 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 }; /** * Create a product * * POST /{companyDomain}/products */ export const createApiCompanyDomainProducts = async (args: CreateApiCompanyDomainProductsArgs, options?: CreateApiCompanyDomainProductsOptions) => { const {baseUrl = "https://public.billsby.com/api/v1/rest/core", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (sec0 !== undefined) headers.set("ApiKey", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.companyDomain))}/products${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(createApiCompanyDomainProductsResponse, await res.json()) };