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

     1  import { Result } from '@webapp/util/fp';
     2  import {
     3    Users,
     4    type User,
     5    userModel,
     6    usersModel,
     7    passwordEncode,
     8  } from '@webapp/models/users';
     9  import type { ZodError } from 'zod';
    10  import { modelToResult } from '@webapp/models/utils';
    11  import { request, parseResponse } from './base';
    12  import type { RequestError } from './base';
    13  
    14  export interface FetchUsersError {
    15    message?: string;
    16  }
    17  
    18  export async function fetchUsers(): Promise<
    19    Result<Users, RequestError | ZodError>
    20  > {
    21    const response = await request('/api/users');
    22  
    23    if (response.isOk) {
    24      return parseResponse(response, usersModel);
    25    }
    26  
    27    return Result.err<Users, RequestError>(response.error);
    28  }
    29  
    30  export async function disableUser(
    31    user: User
    32  ): Promise<Result<boolean, RequestError | ZodError>> {
    33    const response = await request(`/api/users/${user.id}/disable`, {
    34      method: 'PUT',
    35    });
    36  
    37    if (response.isOk) {
    38      return Result.ok(true);
    39    }
    40  
    41    return Result.err<false, RequestError>(response.error);
    42  }
    43  
    44  export async function enableUser(
    45    user: User
    46  ): Promise<Result<boolean, RequestError | ZodError>> {
    47    const response = await request(`/api/users/${user.id}/enable`, {
    48      method: 'PUT',
    49    });
    50  
    51    if (response.isOk) {
    52      return Result.ok(true);
    53    }
    54  
    55    return Result.err<false, RequestError>(response.error);
    56  }
    57  
    58  export async function createUser(
    59    user: User
    60  ): Promise<Result<boolean, RequestError | ZodError>> {
    61    const response = await request(`/api/users`, {
    62      method: 'POST',
    63      body: JSON.stringify(user),
    64    });
    65  
    66    if (response.isOk) {
    67      return Result.ok(true);
    68    }
    69  
    70    return Result.err<false, RequestError>(response.error);
    71  }
    72  
    73  export async function loadCurrentUser(): Promise<
    74    Result<User, RequestError | ZodError>
    75  > {
    76    const response = await request(`/api/user`);
    77    if (response.isOk) {
    78      return modelToResult<User>(userModel, response.value);
    79    }
    80  
    81    return Result.err<User, RequestError>(response.error);
    82  }
    83  
    84  export async function logIn({
    85    username,
    86    password,
    87  }: {
    88    username: string;
    89    password: string;
    90  }): Promise<Result<unknown, RequestError | ZodError>> {
    91    const response = await request(`/login`, {
    92      method: 'POST',
    93      body: JSON.stringify({ username, password: passwordEncode(password) }),
    94    });
    95    if (response.isOk) {
    96      return Result.ok(true);
    97    }
    98  
    99    return Result.err<boolean, RequestError>(response.error);
   100  }
   101  
   102  export async function signUp(data: {
   103    username: string;
   104    password: string;
   105    fullName: string;
   106    email: string;
   107  }): Promise<Result<unknown, RequestError | ZodError>> {
   108    const response = await request(`/signup`, {
   109      method: 'POST',
   110      body: JSON.stringify({
   111        ...data,
   112        name: data.username,
   113        password: passwordEncode(data.password),
   114      }),
   115    });
   116    if (response.isOk) {
   117      return Result.ok(true);
   118    }
   119  
   120    return Result.err<boolean, RequestError>(response.error);
   121  }
   122  
   123  export async function changeMyPassword(
   124    oldPassword: string,
   125    newPassword: string
   126  ): Promise<Result<boolean, RequestError | ZodError>> {
   127    const response = await request(`/api/user/password`, {
   128      method: 'PUT',
   129      body: JSON.stringify({
   130        oldPassword: passwordEncode(oldPassword),
   131        newPassword: passwordEncode(newPassword),
   132      }),
   133    });
   134    if (response.isOk) {
   135      return Result.ok(true);
   136    }
   137  
   138    return Result.err<boolean, RequestError>(response.error);
   139  }
   140  
   141  export async function changeUserRole(
   142    userId: number,
   143    role: string
   144  ): Promise<Result<boolean, RequestError | ZodError>> {
   145    const response = await request(`/api/users/${userId}/role`, {
   146      method: 'PUT',
   147      body: JSON.stringify({ role }),
   148    });
   149  
   150    if (response.isOk) {
   151      return Result.ok(true);
   152    }
   153  
   154    return Result.err<boolean, RequestError>(response.error);
   155  }
   156  
   157  export async function editMyUser(
   158    data: Partial<User>
   159  ): Promise<Result<boolean, RequestError | ZodError>> {
   160    const response = await request(`/api/users`, {
   161      method: 'PATCH',
   162      body: JSON.stringify(data),
   163    });
   164  
   165    if (response.isOk) {
   166      return Result.ok(true);
   167    }
   168  
   169    return Result.err<boolean, RequestError>(response.error);
   170  }
   171  
   172  export async function deleteUser(data: {
   173    id: number;
   174  }): Promise<Result<boolean, RequestError | ZodError>> {
   175    const { id } = data;
   176    const response = await request(`/api/users/${id}`, {
   177      method: 'DELETE',
   178    });
   179  
   180    if (response.isOk) {
   181      return Result.ok(true);
   182    }
   183  
   184    return Result.err<boolean, RequestError>(response.error);
   185  }