import {z} from 'zod' export type MerchantKey = string; export type CountryCodeAlpha3 = string; export type CountryCodeOnlyAlpha3 = {alpha3: CountryCodeAlpha3}; export type CountryCodeAlpha2 = string; export type CountryCodeOnlyAlpha2 = {alpha2: CountryCodeAlpha2}; export type CountryCodeNumeric = string; export type CountryCodeOnlyNumeric = {numeric: CountryCodeNumeric}; export type CountryCodeCreate = CountryCodeOnlyAlpha3 | CountryCodeOnlyAlpha2 | CountryCodeOnlyNumeric; export type StateOrProvince = string; export type City = string; export type Line1 = string; export type Line2 = string; export type PostalCode = string; export type PhoneNumber = string; export type Url = string; export type MerchantReference = string; export type Tags = {[key: string]: string}; export type TaxRegistrationId = string; export type Email = string; export type BusinessType = 'corporation' | 'sole_proprietorship' | 'partnership' | 'non_profit_organization' | 'government_agency' | 'limited_liability_company'; export type MerchantMergeUpdate = {name?: string | undefined, description?: string | undefined, legalName?: string | undefined, registeredAddress?: ({countryCode: CountryCodeCreate, stateOrProvince?: StateOrProvince | undefined, city: City, line1: Line1, line2?: Line2 | undefined, postalCode: PostalCode}) | undefined, customerInquiry?: PhoneNumber | undefined, websiteUrl?: Url | undefined, merchantReference?: MerchantReference | undefined, tags?: Tags | undefined, taxRegistrationId?: TaxRegistrationId | undefined, sellerEmail?: Email | undefined, businessType?: BusinessType | undefined}; export type PatchApiMerchantsMerchantKeyArgs = {merchantKey: MerchantKey, 'If-Match'?: number | undefined, body: MerchantMergeUpdate}; export type PatchApiMerchantsMerchantKeyOptions = {baseUrl?: string | undefined, ApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const merchantKey = z.string().min(5).max(120); export const merchantStatus = z.enum(["active", "archived"]); export const merchantReference = z.string().min(1).max(120); export const agentKey = z.string(); export const merchantLegalName = z.string().min(1).max(120); export const countryCodeAlpha3 = z.string().min(3).max(3); export const countryCodeAlpha2 = z.string().min(2).max(2); export const countryCodeNumeric = z.string().min(3).max(3); export const countryCode = z.object({alpha3: countryCodeAlpha3, alpha2: countryCodeAlpha2, numeric: countryCodeNumeric}); export const stateOrProvince = z.string().min(1).max(120); export const city = z.string().min(1).max(120); export const line1 = z.string().min(1).max(120); export const line2 = z.string().min(1).max(120); export const postalCode = z.string().min(1).max(10); export const address = z.object({countryCode: countryCode, stateOrProvince: stateOrProvince.optional(), city: city, line1: line1, line2: line2.optional(), postalCode: postalCode}); export const phoneNumber = z.string().min(5).max(33); export const url = z.string().min(8).max(2048); export const tags = z.record(z.string(), z.string().min(1).max(100)); export const created = z.string(); export const lastModified = z.string(); export const version = z.number().int(); export const taxRegistrationId = z.string().max(20); export const email = z.string(); export const businessType = z.enum(["corporation", "sole_proprietorship", "partnership", "non_profit_organization", "government_agency", "limited_liability_company"]); export const merchant = z.object({key: merchantKey, status: merchantStatus, merchantReference: merchantReference.optional(), agentKey: agentKey, name: z.string().min(1).max(120), description: z.string().min(1).max(160).optional(), legalName: merchantLegalName, registeredAddress: address, customerInquiry: phoneNumber, websiteUrl: url, tags: tags.optional(), created: created, lastModified: lastModified.optional(), version: version, taxRegistrationId: taxRegistrationId.optional(), sellerEmail: email.optional(), businessType: businessType.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 }; /** * Update Merchant * * PATCH /merchants/{merchantKey} * * Also accepts auth: BearerToken */ export const patchApiMerchantsMerchantKey = async (args: PatchApiMerchantsMerchantKeyArgs, options?: PatchApiMerchantsMerchantKeyOptions) => { const {baseUrl = "https://eu-west-1.api.silverflow.com/v1", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/merge-patch+json") headers.set('accept', "application/json") if (args['If-Match'] != null) headers.set("If-Match", String(args['If-Match'])) if (ApiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${ApiKey.username}:${ApiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/merchants/${encodeURIComponent(String(args.merchantKey))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(merchant, await res.json()) };