github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/webapp/javascript/models/users.ts (about)

     1  import { z, ZodError } from 'zod';
     2  import { Result } from '@webapp/util/fp';
     3  import { modelToResult } from './utils';
     4  
     5  const zDateTime = z.string().transform((value: string | number | Date) => {
     6    if (typeof value === 'string') {
     7      const date = Date.parse(value);
     8      if (Number.isInteger(date)) {
     9        return date;
    10      }
    11      return value;
    12    }
    13    if (typeof value === 'number') {
    14      return new Date(value);
    15    }
    16    return value;
    17  });
    18  
    19  export const userModel = z.object({
    20    id: z.number(),
    21    name: z.string(),
    22    email: z.optional(z.string()),
    23    fullName: z.optional(z.string()),
    24    role: z.string(),
    25    isDisabled: z.boolean(),
    26    isExternal: z.optional(z.boolean()),
    27    createdAt: zDateTime,
    28    updatedAt: zDateTime,
    29    passwordChangedAt: zDateTime,
    30  });
    31  
    32  export const usersModel = z.array(userModel);
    33  
    34  export type Users = z.infer<typeof usersModel>;
    35  export type User = z.infer<typeof userModel>;
    36  
    37  export function parse(a: unknown): Result<Users, ZodError> {
    38    return modelToResult(usersModel, a);
    39  }
    40  
    41  export const passwordEncode = (p: string) =>
    42    btoa(unescape(encodeURIComponent(p)));