import {z} from 'zod' export type IyziLinkCreateRequest = {conversationId?: string | undefined, locale?: string | undefined, name: string, description: string, price: unknown, currencyCode: 'TRY' | 'USD' | 'EUR' | 'GBP' | 'RUB' | 'CHF' | 'NOK', encodedImageFile: string, addressIgnorable?: boolean | undefined, installmentRequested?: boolean | undefined, stockEnabled?: boolean | undefined, stockCount?: number | undefined, flexibleLink?: boolean | undefined, presetPriceValues?: (10 | 20 | 30) | undefined, categoryType?: ('GOLD' | 'FOOD' | 'PHONE' | 'UNKNOWN' | 'PC' | 'TABLET') | undefined}; export type CreateApiV2IyzilinkProductsArgs = {Authorization: string, 'Content-Type': string, body: IyziLinkCreateRequest}; export type CreateApiV2IyzilinkProductsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const iyziLinkCreateOrUpdateResponse = z.object({status: z.string().optional(), locale: z.string().optional(), systemTime: z.number().int().optional(), conversationId: z.string().optional(), data: z.object({token: z.string().optional(), url: z.string().optional(), imageUrl: z.string().optional()}).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 }; /** * iyzico Link Oluşturma * * POST /v2/iyzilink/products */ export const createApiV2IyzilinkProducts = async (args: CreateApiV2IyzilinkProductsArgs, options?: CreateApiV2IyzilinkProductsOptions) => { const {baseUrl = "https://sandbox-api.iyzipay.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) const url = `${baseUrl.replace(/\/+$/, '')}/v2/iyzilink/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(iyziLinkCreateOrUpdateResponse, await res.json()) };