import {z} from 'zod' export type PersonUpdateData = {data: {type: 'people', id: string, attributes: {state_of_residence: string}}}; export type PatchApiRestPeoplePersonIdArgs = {body: PersonUpdateData}; export type PatchApiRestPeoplePersonIdOptions = {baseUrl?: string | undefined, oauthUserToken?: string | undefined, fetch?: typeof fetch | undefined}; export const personAttributes = z.object({disallow_reason: z.enum(["INACTIVE", "DELINQUENT", "BANNED", "null"]).nullable(), state_of_residence: z.string().max(2).optional(), products: z.object({dailypay_card: z.object({eligible: z.boolean(), enrolled: z.boolean()})})}); export const personLink = z.string(); export const personLinks = z.object({self: personLink}); export const personResource = z.object({type: z.literal("people"), id: z.string(), attributes: personAttributes, links: personLinks}); export const personData = z.object({data: personResource}); 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 a person * * PATCH /rest/people/{person_id} */ export const patchApiRestPeoplePersonId = async (args: PatchApiRestPeoplePersonIdArgs, options?: PatchApiRestPeoplePersonIdOptions) => { const {baseUrl = "https://api.dailypay.com", fetch: fetchFn = fetch, oauthUserToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/vnd.api+json") headers.set('accept', "application/vnd.api+json") if (oauthUserToken !== undefined) headers.set('authorization', `Bearer ${oauthUserToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/rest/people/${encodeURIComponent(String(args.person_id))}${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(personData, await res.json()) };