import {z} from 'zod' export type CreateApiMeArgs = {body: {'option[creation_watch_state]'?: string | undefined, 'option[interaction_watch_state]'?: string | undefined, 'option[content_show_signature]'?: boolean | undefined, 'option[email_on_conversation]'?: boolean | undefined, 'option[push_on_conversation]'?: boolean | undefined, 'option[receive_admin_email]'?: boolean | undefined, 'option[show_dob_year]'?: boolean | undefined, 'option[show_dob_date]'?: boolean | undefined, 'profile[location]'?: string | undefined, 'profile[website]'?: string | undefined, 'profile[about]'?: string | undefined, 'profile[signature]'?: string | undefined, 'privacy[allow_view_profile]'?: string | undefined, 'privacy[allow_post_profile]'?: string | undefined, 'privacy[allow_receive_news_feed]'?: string | undefined, 'privacy[allow_send_personal_conversation]'?: string | undefined, 'privacy[allow_view_identities]'?: string | undefined, visible?: boolean | undefined, activity_visible?: boolean | undefined, timezone?: string | undefined, custom_title?: string | undefined, 'custom_fields[]'?: string | undefined}}; export type CreateApiMeOptions = {baseUrl?: string | undefined, XFOAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiMeResponse = z.object({success: z.boolean().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 information about the current user * * POST /me/ * * Also accepts auth: XF-Api-Key */ export const createApiMe = async (args: CreateApiMeArgs, options?: CreateApiMeOptions) => { const {baseUrl = "https://example.com/api", fetch: fetchFn = fetch, XFOAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/x-www-form-urlencoded") headers.set('accept', "application/json") if (XFOAuth2 !== undefined) headers.set('authorization', `Bearer ${XFOAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/me/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: new URLSearchParams(Object.entries(args.body).flatMap(([key, value]) => (value == null ? [] : [[key, String(value)]])))}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiMeResponse, await res.json()) };