import {z} from 'zod' export type ContactDTO = {phone?: string | undefined, street?: string | undefined, streetNumber?: string | undefined, zipCode?: string | undefined, city?: string | undefined, state?: string | undefined, country?: string | undefined}; export type CorporateFitnessDTO = {startTimestamp?: string | undefined, endTimestamp?: string | undefined}; export type MembershipDTO = {membershipId: string, agreementNumber?: string | undefined, membershipType: 'BASIC' | 'PREMIUM' | 'PROSPECT' | 'CORPORATE_FITNESS', membershipSubType?: string | undefined, endOfContract?: string | undefined, startOfContract: string, referringMemberId?: string | undefined, barcode?: string | undefined, verificationTAN?: string | undefined, corporateFitness?: CorporateFitnessDTO | undefined}; export type MemberAccountDTO = {accountId?: string | undefined, email: string, firstName: string, lastName: string, dateOfBirth?: string | undefined, gender: 'MALE' | 'FEMALE' | 'NON_BINARY', contact?: ContactDTO | undefined, membership: MembershipDTO}; export type CreateApiApiV2AccountsArgs = {body: MemberAccountDTO}; export type CreateApiApiV2AccountsOptions = {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 }; /** * Create an account * * POST /api/v2/accounts */ export const createApiApiV2Accounts = async (args: CreateApiApiV2AccountsArgs, options?: CreateApiApiV2AccountsOptions) => { const {baseUrl = "https://mms.api.ed.ts.egym.coffee", fetch: fetchFn = fetch, mms} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (mms !== undefined) headers.set("x-api-key", mms) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/accounts${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(memberAccountDTO, await res.json()) };