import {z} from 'zod' export type CustomerAddress = {address?: string | undefined, zipCode?: string | undefined, contactName?: string | undefined, city?: string | undefined, district?: string | undefined, country?: string | undefined}; export type CustomerUpdateRequest = {name?: string | undefined, surname?: string | undefined, email?: string | undefined, gsmNumber?: string | undefined, identityNumber?: string | undefined, billingAddress?: CustomerAddress | undefined, shippingAddress?: CustomerAddress | undefined}; export type CreateApiV2SubscriptionCustomersCustomerReferenceCodeArgs = {Authorization: string, 'Content-Type': string, customerReferenceCode: string, body: CustomerUpdateRequest}; export type CreateApiV2SubscriptionCustomersCustomerReferenceCodeOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const customerAddress = z.object({address: z.string().optional(), zipCode: z.string().optional(), contactName: z.string().optional(), city: z.string().optional(), district: z.string().optional(), country: z.string().optional()}); export const customerUpdateResponse = z.object({status: z.enum(["success", "failure"]).optional(), systemTime: z.number().int().optional(), data: z.object({referenceCode: z.string().optional(), createdDate: z.number().int().optional(), status: z.literal("ACTIVE").optional(), name: z.string().optional(), surname: z.string().optional(), identityNumber: z.string().optional(), email: z.string().optional(), gsmNumber: z.string().optional(), contactEmail: z.string().optional(), contactGsmNumber: z.string().optional(), billingAddress: customerAddress.optional(), shippingAddress: customerAddress.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 }; /** * Abone Güncelleme * * POST /v2/subscription/customers/{customerReferenceCode} */ export const createApiV2SubscriptionCustomersCustomerReferenceCode = async (args: CreateApiV2SubscriptionCustomersCustomerReferenceCodeArgs, options?: CreateApiV2SubscriptionCustomersCustomerReferenceCodeOptions) => { 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/subscription/customers/${encodeURIComponent(String(args.customerReferenceCode))}${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(customerUpdateResponse, await res.json()) };