import {z} from 'zod' export type Quarter = 1 | 2 | 3 | 4; export type Year = number; export type Currency = 'USD' | 'EUR' | 'SGD' | 'GBP' | 'PLN' | 'THB' | 'MYR' | 'SEK'; export type VisaQuarterlySchemeReportParameters = {network: 'visa', bid: string, quarter: Quarter, year: Year, currency: Currency}; export type QuarterlySchemeReportReference = string; export type QuarterlySchemeReportMimeType = 'application/vnd.ms-excel'; export type DistributionKey = string; export type Distributions = Array; export type VisaQuarterlySchemeReportRequest = {parameters: VisaQuarterlySchemeReportParameters, reference?: QuarterlySchemeReportReference | undefined, mimeType: QuarterlySchemeReportMimeType, distributions?: Distributions | undefined}; export type MastercardQuarterlySchemeReportParameters = {network: 'mastercard', acquirerIdCode: string, quarter: Quarter, year: Year, currency: Currency}; export type MastercardQuarterlySchemeReportRequest = {parameters: MastercardQuarterlySchemeReportParameters, reference?: QuarterlySchemeReportReference | undefined, mimeType: QuarterlySchemeReportMimeType, distributions?: Distributions | undefined}; export type CreateApiReportsQuarterlySchemeReportArgs = {body: VisaQuarterlySchemeReportRequest | MastercardQuarterlySchemeReportRequest}; export type CreateApiReportsQuarterlySchemeReportOptions = {baseUrl?: string | undefined, ApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const quarter = z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4)]); export const year = z.number().int(); export const currency = z.enum(["USD", "EUR", "SGD", "GBP", "PLN", "THB", "MYR", "SEK"]); export const visaQuarterlySchemeReportParameters = z.object({network: z.literal("visa"), bid: z.string().min(1).max(120), quarter: quarter, year: year, currency: currency}); export const quarterlySchemeReportMimeType = z.literal("application/vnd.ms-excel"); export const reportKey = z.string().min(10).max(120); export const agentKey = z.string(); export const quarterlySchemeReportReference = z.string().max(120); export const createReportStatus = z.literal("scheduled"); export const openapiComponentsSchemasCreated2 = z.string(); export const distributionKey = z.string().min(10).max(120); export const distributions = z.array(distributionKey); export const visaQuarterlySchemeReportResponse = z.object({reportName: z.literal("VisaQuarterlySchemeReport"), parameters: visaQuarterlySchemeReportParameters, mimeType: quarterlySchemeReportMimeType, key: reportKey, agentKey: agentKey, reference: quarterlySchemeReportReference.optional(), status: createReportStatus, created: openapiComponentsSchemasCreated2, distributions: distributions.optional()}); export const mastercardQuarterlySchemeReportParameters = z.object({network: z.literal("mastercard"), acquirerIdCode: z.string(), quarter: quarter, year: year, currency: currency}); export const mastercardQuarterlySchemeReportResponse = z.object({reportName: z.literal("MastercardQuarterlySchemeReport"), parameters: mastercardQuarterlySchemeReportParameters, mimeType: quarterlySchemeReportMimeType, key: reportKey, agentKey: agentKey, reference: quarterlySchemeReportReference.optional(), status: createReportStatus, created: openapiComponentsSchemasCreated2, distributions: distributions.optional()}); export const createApiReportsQuarterlySchemeReportResponse = z.union([visaQuarterlySchemeReportResponse, mastercardQuarterlySchemeReportResponse]); 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 Quarterly Scheme Report * * POST /reports/quarterlySchemeReport * * Also accepts auth: BearerToken */ export const createApiReportsQuarterlySchemeReport = async (args: CreateApiReportsQuarterlySchemeReportArgs, options?: CreateApiReportsQuarterlySchemeReportOptions) => { 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/json") headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${ApiKey.username}:${ApiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/reports/quarterlySchemeReport${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(createApiReportsQuarterlySchemeReportResponse, await res.json()) };