Код IT
← Каталог

TypeScript и Node.js — Сервис и Result

Фрагмент из «TypeScript и Node.js»: Сервис и Result.

TypeScript main.ts
type ServiceError = "validation" | "conflict";
type ServiceResult<T> =
  | { ok: true; value: T }
  | { ok: false; error: ServiceError };

export function createUser(
  input: CreateUserDto,
  existingEmails: Set<string>,
): ServiceResult<User> {
  if (!input.email.includes("@")) {
    return { ok: false, error: "validation" };
  }
  if (existingEmails.has(input.email)) {
    return { ok: false, error: "conflict" };
  }
  const user: User = {
    id: crypto.randomUUID(),
    ...fromDto(input),
    createdAt: new Date(),
  };
  return { ok: true, value: user };
}
type ServiceError = "validation" | "conflict";
type ServiceResult<T> =
  | { ok: true; value: T }
  | { ok: false; error: ServiceError };

export function createUser(
  input: CreateUserDto,
  existingEmails: Set<string>,
): ServiceResult<User> {
  if (!input.email.includes("@")) {
    return { ok: false, error: "validation" };
  }
  if (existingEmails.has(input.email)) {
    return { ok: false, error: "conflict" };
  }
  const user: User = {
    id: crypto.randomUUID(),
    ...fromDto(input),
    createdAt: new Date(),
  };
  return { ok: true, value: user };
}