import {z} from 'zod' export type JTLDataContractsKeysKundenGruppeKey = string; export type JTLWawiRestContractsModelsV1CommonCreateAddress = {company?: string | undefined, company2?: string | undefined, formOfAddress?: string | undefined, title?: string | undefined, firstName?: string | undefined, lastName?: string | undefined, street?: string | undefined, address2?: string | undefined, postalCode?: string | undefined, city?: string | undefined, state?: string | undefined, countryIso?: string | undefined, vatID?: string | undefined, phoneNumber?: string | undefined, mobilePhoneNumber?: string | undefined, emailAddress?: string | undefined, fax?: string | undefined}; export type JTLWawiRestContractsModelsV1CustomerCreateOtherAddresses = {otherBillingAddresses?: Array | undefined, otherShippingAddresses?: Array | undefined, otherCustomerAddresses?: Array | undefined}; export type JTLDataContractsKeysZahlungsartKey = string; export type JTLWawiRestContractsModelsV1CustomerCreatePaymentSettings = {paymentMethodId?: JTLDataContractsKeysZahlungsartKey | undefined, discount?: number | undefined, paymentTarget?: number | undefined, creditLimit?: number | undefined, stopPaymentRequest?: boolean | undefined}; export type JTLDataContractsKeysFirmaKey = string; export type JTLDataContractsKeysKundenKategorieKey = string; export type JTLWawiRestContractsModelsV1CustomerCreateCustomer = {number?: string | undefined, customerGroupId?: JTLDataContractsKeysKundenGruppeKey | undefined, billingAddress?: JTLWawiRestContractsModelsV1CommonCreateAddress | undefined, shipmentaddress?: JTLWawiRestContractsModelsV1CommonCreateAddress | undefined, customAddress?: JTLWawiRestContractsModelsV1CommonCreateAddress | undefined, otherAddresses?: JTLWawiRestContractsModelsV1CustomerCreateOtherAddresses | undefined, paymentSettings?: JTLWawiRestContractsModelsV1CustomerCreatePaymentSettings | undefined, customerSince?: string | undefined, lastChange?: string | undefined, languageIso?: string | undefined, internalCompanyId?: JTLDataContractsKeysFirmaKey | undefined, customerCategoryId?: JTLDataContractsKeysKundenKategorieKey | undefined, taxIdentificationNumber?: string | undefined, accountsReceivableNumber?: number | undefined, commercialRegisterNumber?: string | undefined, website?: string | undefined, initialContact?: string | undefined, ebayUsername?: string | undefined, birthday?: string | undefined, isLocked?: boolean | undefined, isCashRegisterBased?: boolean | undefined}; export type CreateApiV2CustomersArgs = {'x-tenant-id': string, disableAutomaticWorkflows?: boolean | undefined, 'x-runas'?: string | undefined, 'x-companyid'?: string | undefined, body: JTLWawiRestContractsModelsV1CustomerCreateCustomer}; export type CreateApiV2CustomersOptions = {baseUrl?: string | undefined, oauth2AuthorizationCode?: string | undefined, fetch?: typeof fetch | undefined}; export const jTLDataContractsKeysKundeKey = z.string(); export const jTLDataContractsKeysKundenGruppeKey = z.string(); export const jTLDataContractsKeysAdresseKey = z.string(); export const jTLWawiRestContractsModelsV1CommonAddress = z.object({id: jTLDataContractsKeysAdresseKey.optional(), company: z.string().optional(), company2: z.string().optional(), formOfAddress: z.string().optional(), title: z.string().optional(), firstName: z.string().optional(), lastName: z.string().optional(), street: z.string().optional(), address2: z.string().optional(), postalCode: z.string().optional(), city: z.string().optional(), state: z.string().optional(), countryIso: z.string().optional(), vatID: z.string().optional(), phoneNumber: z.string().optional(), mobilePhoneNumber: z.string().optional(), emailAddress: z.string().optional(), fax: z.string().optional()}); export const jTLWawiRestContractsModelsV1CustomerOtherAddresses = z.object({otherBillingAddresses: z.array(jTLWawiRestContractsModelsV1CommonAddress).optional(), otherShippingAddresses: z.array(jTLWawiRestContractsModelsV1CommonAddress).optional(), otherCustomerAddresses: z.array(jTLWawiRestContractsModelsV1CommonAddress).optional()}); export const jTLDataContractsKeysZahlungsartKey = z.string(); export const jTLWawiRestContractsModelsV1CustomerPaymentSettings = z.object({paymentMethodId: jTLDataContractsKeysZahlungsartKey.optional(), discount: z.number().optional(), paymentTarget: z.number().int().optional(), creditLimit: z.number().int().optional(), stopPaymentRequest: z.boolean().optional()}); export const jTLDataContractsKeysFirmaKey = z.string(); export const jTLDataContractsKeysKundenKategorieKey = z.string(); export const jTLWawiRestContractsModelsV1CustomerCustomer = z.object({id: jTLDataContractsKeysKundeKey.optional(), number: z.string().optional(), customerGroupId: jTLDataContractsKeysKundenGruppeKey.optional(), billingAddress: jTLWawiRestContractsModelsV1CommonAddress.optional(), shipmentaddress: jTLWawiRestContractsModelsV1CommonAddress.optional(), customAddress: jTLWawiRestContractsModelsV1CommonAddress.optional(), otherAddresses: jTLWawiRestContractsModelsV1CustomerOtherAddresses.optional(), paymentSettings: jTLWawiRestContractsModelsV1CustomerPaymentSettings.optional(), customerSince: z.string().optional(), lastChange: z.string().optional(), languageIso: z.string().optional(), internalCompanyId: jTLDataContractsKeysFirmaKey.optional(), customerCategoryId: jTLDataContractsKeysKundenKategorieKey.optional(), taxIdentificationNumber: z.string().optional(), accountsReceivableNumber: z.number().int().optional(), commercialRegisterNumber: z.string().optional(), website: z.string().optional(), initialContact: z.string().optional(), ebayUsername: z.string().optional(), birthday: z.string().optional(), isLocked: z.boolean().optional(), isCashRegisterBased: z.boolean().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 Customer * * POST /v2/customers * * Also accepts auth: oauth2-client-credentials */ export const createApiV2Customers = async (args: CreateApiV2CustomersArgs, options?: CreateApiV2CustomersOptions) => { const {baseUrl = "https://scx-sbx.api.jtl-software.com", fetch: fetchFn = fetch, oauth2AuthorizationCode} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-tenant-id'] != null) headers.set("x-tenant-id", String(args['x-tenant-id'])) if (args.disableAutomaticWorkflows != null) query.append("disableAutomaticWorkflows", String(args.disableAutomaticWorkflows)) if (args['x-runas'] != null) headers.set("x-runas", String(args['x-runas'])) if (args['x-companyid'] != null) headers.set("x-companyid", String(args['x-companyid'])) if (oauth2AuthorizationCode !== undefined) headers.set('authorization', `Bearer ${oauth2AuthorizationCode}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/customers${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(jTLWawiRestContractsModelsV1CustomerCustomer, await res.json()) };