import {z} from 'zod' export type DimensionType = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 100 | 198 | 199 | 200; export type ItemCondition = {propertyKey?: (string | null) | undefined, propertyValue?: (string | null) | undefined}; export type CreateDimensionRequest = {accountId?: (number | null) | undefined, name?: (string | null) | undefined, shortName?: (string | null) | undefined, type?: DimensionType | undefined, rank?: number | undefined, properties?: ({[key: string]: string} | null) | undefined, itemCondition?: ItemCondition | undefined}; export type CreateApiV1DimensionsArgs = {Account: unknown, body: CreateDimensionRequest}; export type CreateApiV1DimensionsOptions = {baseUrl: string, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1DimensionsResponse = z.number().int(); 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 new dimenson * * POST /v1/Dimensions */ export const createApiV1Dimensions = async (args: CreateApiV1DimensionsArgs, options: CreateApiV1DimensionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json-patch+json; x-api-version=1.0") headers.set('accept', "application/json") if (args.Account != null) query.append("Account", String(args.Account)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/Dimensions${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(createApiV1DimensionsResponse, await res.json()) };