All files identity.ts

100% Statements 11/11
100% Branches 0/0
100% Functions 11/11
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135                                                                                                                                                    3x   1x           1x           1x           1x           1x     1x     1x           1x                 1x           1x              
import type { ApiClient, BusinessId } from "./api";
 
export interface UserProfile {
  id: BusinessId;
  email: string;
  displayName: string;
  status: string;
  roles: string[];
}
 
export interface AuthTokens {
  tokenType: "Bearer" | string;
  accessToken: string;
  expiresIn: number;
  refreshToken: string;
}
 
export interface Address {
  id: BusinessId;
  recipientName: string;
  phone: string;
  province: string;
  provinceCode: string;
  city: string;
  cityCode: string;
  district: string;
  districtCode: string;
  detailAddress: string;
  postalCode: string | null;
  defaultAddress: boolean;
  version: number;
  createdAt: string;
  updatedAt: string;
}
 
export interface RegisterInput {
  email: string;
  password: string;
  displayName: string;
}
 
export interface LoginInput {
  email: string;
  password: string;
}
 
export interface AddressInput {
  recipientName: string;
  phone: string;
  province: string;
  provinceCode: string;
  city: string;
  cityCode: string;
  district: string;
  districtCode: string;
  detailAddress: string;
  postalCode: string | null;
  setDefault: boolean;
}
 
export interface IdentityApi {
  register(input: RegisterInput): Promise<UserProfile>;
  login(input: LoginInput): Promise<AuthTokens>;
  refresh(refreshToken: string): Promise<AuthTokens>;
  logout(refreshToken: string): Promise<void>;
  currentUser(): Promise<UserProfile>;
  addresses(): Promise<Address[]>;
  createAddress(input: AddressInput): Promise<Address>;
  updateAddress(addressId: BusinessId, input: AddressInput): Promise<Address>;
  setDefaultAddress(addressId: BusinessId): Promise<Address>;
  deleteAddress(addressId: BusinessId): Promise<void>;
}
 
export function createIdentityApi(client: ApiClient): IdentityApi {
  return {
    register(input) {
      return client.request<UserProfile>("/api/v1/identity/auth/register", {
        method: "POST",
        body: JSON.stringify(input),
      });
    },
    login(input) {
      return client.request<AuthTokens>("/api/v1/identity/auth/login", {
        method: "POST",
        body: JSON.stringify(input),
      });
    },
    refresh(refreshToken) {
      return client.request<AuthTokens>("/api/v1/identity/auth/refresh", {
        method: "POST",
        body: JSON.stringify({ refreshToken }),
      });
    },
    logout(refreshToken) {
      return client.request<void>("/api/v1/identity/auth/logout", {
        method: "POST",
        body: JSON.stringify({ refreshToken }),
      });
    },
    currentUser() {
      return client.request<UserProfile>("/api/v1/identity/me");
    },
    addresses() {
      return client.request<Address[]>("/api/v1/identity/addresses");
    },
    createAddress(input) {
      return client.request<Address>("/api/v1/identity/addresses", {
        method: "POST",
        body: JSON.stringify(input),
      });
    },
    updateAddress(addressId, input) {
      return client.request<Address>(
        `/api/v1/identity/addresses/${encodeURIComponent(addressId)}`,
        {
          method: "PUT",
          body: JSON.stringify(input),
        },
      );
    },
    setDefaultAddress(addressId) {
      return client.request<Address>(
        `/api/v1/identity/addresses/${encodeURIComponent(addressId)}/default`,
        { method: "POST" },
      );
    },
    deleteAddress(addressId) {
      return client.request<void>(
        `/api/v1/identity/addresses/${encodeURIComponent(addressId)}`,
        { method: "DELETE" },
      );
    },
  };
}