import {z} from 'zod' export type JTLDataContractsKeysArtikelKey = string; export type JTLDataContractsKeysFirmaKey = string; export type GetApiV2TaxItemItemIdCompanyIdDepartureCountryISOShipmentCountryISOArgs = {'x-tenant-id': string, departureCountryISO: string, shipmentCountryISO: string, taxNumber?: string | undefined, invoiceCountryISO?: string | undefined, invoiceCountryStateISO?: string | undefined, shipmentCountryStateISO?: string | undefined, itemId: JTLDataContractsKeysArtikelKey, companyId: JTLDataContractsKeysFirmaKey, 'x-runas'?: string | undefined, 'x-companyid'?: string | undefined}; export type GetApiV2TaxItemItemIdCompanyIdDepartureCountryISOShipmentCountryISOOptions = {baseUrl?: string | undefined, oauth2AuthorizationCode?: string | undefined, fetch?: typeof fetch | undefined}; export const jTLDataContractsKeysSteuerklasseKey = z.string(); export const jTLWawiRestContractsModelsV1TaxTaxRate = z.object({rate: z.number().optional(), taxClassId: jTLDataContractsKeysSteuerklasseKey.optional(), departureCountryISO: z.string().optional(), shipmentCountryISO: 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 }; /** * Get Tax For Item * * GET /v2/tax/item/{itemId}/{companyId}/{departureCountryISO}/{shipmentCountryISO} * * Also accepts auth: oauth2-client-credentials */ export const getApiV2TaxItemItemIdCompanyIdDepartureCountryISOShipmentCountryISO = async (args: GetApiV2TaxItemItemIdCompanyIdDepartureCountryISOShipmentCountryISOArgs, options?: GetApiV2TaxItemItemIdCompanyIdDepartureCountryISOShipmentCountryISOOptions) => { const {baseUrl = "https://scx-sbx.api.jtl-software.com", fetch: fetchFn = fetch, oauth2AuthorizationCode} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-tenant-id'] != null) headers.set("x-tenant-id", String(args['x-tenant-id'])) if (args.taxNumber != null) query.append("taxNumber", String(args.taxNumber)) if (args.invoiceCountryISO != null) query.append("invoiceCountryISO", String(args.invoiceCountryISO)) if (args.invoiceCountryStateISO != null) query.append("invoiceCountryStateISO", String(args.invoiceCountryStateISO)) if (args.shipmentCountryStateISO != null) query.append("shipmentCountryStateISO", String(args.shipmentCountryStateISO)) 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/tax/item/${encodeURIComponent(String(args.itemId))}/${encodeURIComponent(String(args.companyId))}/${encodeURIComponent(String(args.departureCountryISO))}/${encodeURIComponent(String(args.shipmentCountryISO))}${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(jTLWawiRestContractsModelsV1TaxTaxRate, await res.json()) };