import {z} from 'zod' export type CreateApiUsersActionsLoginArgs = {'Accept-Language'?: string | undefined, body: {user_external_application_id?: string | undefined, login?: string | undefined, password?: string | undefined}}; export type CreateApiUsersActionsLoginOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const user = z.object({user_id: z.string().optional(), login: z.string(), first_name: z.string(), last_name: z.string(), email: z.string(), phone: z.string().optional(), user_role_ids: z.array(z.string()).optional(), store_id: z.string().optional(), last_login: z.string().optional()}); export const createApiUsersActionsLoginResponse = z.object({data: z.object({bearer_token: z.string().optional(), valid_to: z.string().optional(), user: user.optional()}).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 }; /** * User login * * POST /users/actions/login */ export const createApiUsersActionsLogin = async (args: CreateApiUsersActionsLoginArgs, options?: CreateApiUsersActionsLoginOptions) => { const {baseUrl = "https://sandbox.crmcarecloud.com/webservice/rest-api/enterprise-interface/v1.0", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Accept-Language'] != null) headers.set("Accept-Language", String(args['Accept-Language'])) const url = `${baseUrl.replace(/\/+$/, '')}/users/actions/login${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(createApiUsersActionsLoginResponse, await res.json()) };