import {z} from 'zod' export type GetLoginInfoRequest = {id_token: string}; export type CreateApiUserGetLoginInfoArgs = {body: GetLoginInfoRequest}; export type CreateApiUserGetLoginInfoOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const namespace = z.enum(["user", "team", "organization", "static-configuration", "dcso-organization"]); export const tenantOwner = z.object({namespace: namespace, owner_id: z.string(), display_name: z.string().nullable().optional()}); export const tenantDescription = z.object({tenant_id: z.string(), owner: tenantOwner, name: z.string(), icon_url: z.string()}); export const getLoginInfoResponse = z.object({user_id: z.string(), user_email: z.string(), allowed_tenants: z.array(tenantDescription)}); 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 }; /** * Get Login Info Endpoint * * POST /user/get-login-info */ export const createApiUserGetLoginInfo = async (args: CreateApiUserGetLoginInfoArgs, options: CreateApiUserGetLoginInfoOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/user/get-login-info${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(getLoginInfoResponse, await res.json()) };