import {z} from 'zod' export type GetApiV2WhoamiArgs = {'x-api-version'?: string | undefined, 'x-company-guid'?: string | undefined}; export type GetApiV2WhoamiOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2WhoamiResponse = z.object({object: z.literal("whoami"), data: z.object({identity_id: z.number().int(), users: z.array(z.object({id: z.number().int(), identity_id: z.number().int().nullable().optional(), company_id: z.number().int(), first_name: z.string().max(80), last_name: z.string().max(80), preferred_first_name: z.string().max(150).nullable().optional(), preferred_last_name: z.string().max(150).nullable().optional(), pronouns: z.string().max(80).nullable().optional(), email: z.string().max(155), mobile_number: z.string().nullable(), home_number: z.string().nullable(), birth_date: z.string().nullable(), address: z.string().max(100).nullable(), postal_zip: z.string().max(20).nullable(), city: z.string().max(50).nullable(), prov_state: z.string().max(25).nullable(), invite_status: z.enum(["accepted", "pending", "required", "missing_contact_info"]), active: z.boolean(), photo: z.string().nullable(), notes: z.string().nullable(), timezone: z.string().nullable(), type: z.enum(["employee", "asst_manager", "manager", "employer"]), employee_id: z.string().max(190).nullable(), max_weekly_hours: z.string(), punch_id: z.string().max(40).nullable(), created: z.string().optional(), modified: z.string().optional(), invited: z.string().nullable(), invite_accepted: z.string().nullable(), is_new: z.boolean(), appear_as_employee: z.boolean().optional(), subscribe_to_updates: z.boolean().optional(), language: z.string().optional(), push: z.boolean().optional(), skill_level: z.number().int().nullable().optional(), hourly_wage: z.number().int().nullable().optional(), wage_type: z.string().nullable().optional(), sms_me_schedules: z.boolean().nullable().optional(), notify_ot_risk: z.boolean().nullable().optional(), permissions_template_id: z.number().int().nullable().optional(), reactivation_status: z.enum(["accepted", "pending", "expired"]).nullable().optional(), meta: z.object({readonly_fields: z.array(z.string()).optional(), identity_status: z.enum(["created", "updated", "merging_identities"]).nullable().optional()}).nullable().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 }; /** * Retrieve Identity * * GET /v2/whoami * * Also accepts auth: cookieAuth */ export const getApiV2Whoami = async (args: GetApiV2WhoamiArgs, options?: GetApiV2WhoamiOptions) => { const {baseUrl = "https://api.7shifts.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-api-version'] != null) headers.set("x-api-version", String(args['x-api-version'])) if (args['x-company-guid'] != null) headers.set("x-company-guid", String(args['x-company-guid'])) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/whoami${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(getApiV2WhoamiResponse, await res.json()) };