import {z} from 'zod' export type GetApiApiV2AccountsNfcNfcArgs = {nfc: string, nfcFormat?: ('NFC_EGYM_APPLE' | 'NFC_EGYM_GOOGLE' | 'NFC_EXTERNAL_APPLE' | 'NFC_EXTERNAL_GOOGLE') | undefined, collectorId?: number | undefined, passTypeId?: string | undefined}; export type GetApiApiV2AccountsNfcNfcOptions = {baseUrl?: string | undefined, mms?: string | undefined, fetch?: typeof fetch | undefined}; export const contactDTO = z.object({phone: z.string().optional(), street: z.string().optional(), streetNumber: z.string().optional(), zipCode: z.string().optional(), city: z.string().optional(), state: z.string().optional(), country: z.string().optional()}); export const corporateFitnessDTO = z.object({startTimestamp: z.string().optional(), endTimestamp: z.string().optional()}); export const membershipDTO = z.object({membershipId: z.string().min(1), agreementNumber: z.string().optional(), membershipType: z.enum(["BASIC", "PREMIUM", "PROSPECT", "CORPORATE_FITNESS"]), membershipSubType: z.string().max(128).optional(), endOfContract: z.string().optional(), startOfContract: z.string(), referringMemberId: z.string().optional(), barcode: z.string().optional(), verificationTAN: z.string().optional(), corporateFitness: corporateFitnessDTO.optional()}); export const memberAccountDTO = z.object({accountId: z.string().optional(), email: z.string().min(1), firstName: z.string().min(1), lastName: z.string().min(1), dateOfBirth: z.string().optional(), gender: z.enum(["MALE", "FEMALE", "NON_BINARY"]), contact: contactDTO.optional(), membership: membershipDTO}); 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 }; /** * Retrieve an account by NFC * * GET /api/v2/accounts/nfc/{nfc} */ export const getApiApiV2AccountsNfcNfc = async (args: GetApiApiV2AccountsNfcNfcArgs, options?: GetApiApiV2AccountsNfcNfcOptions) => { const {baseUrl = "https://mms.api.ed.ts.egym.coffee", fetch: fetchFn = fetch, mms} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.nfcFormat != null) query.append("nfcFormat", String(args.nfcFormat)) if (args.collectorId != null) query.append("collectorId", String(args.collectorId)) if (args.passTypeId != null) query.append("passTypeId", String(args.passTypeId)) if (mms !== undefined) headers.set("x-api-key", mms) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/accounts/nfc/${encodeURIComponent(String(args.nfc))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(memberAccountDTO, await res.json()) };