Uname: Linux premium294.web-hosting.com 4.18.0-553.45.1.lve.el8.x86_64 #1 SMP Wed Mar 26 12:08:09 UTC 2025 x86_64
Software: LiteSpeed
PHP version: 8.1.32 [ PHP INFO ] PHP os: Linux
Server Ip: 104.21.32.1
Your Ip: 216.73.216.223
User: mjbynoyq (1574) | Group: mjbynoyq (1570)
Safe Mode: OFF
Disable Function:
NONE

name : utils.ts
import { Num, Obj, Str } from "@brizy/readers";
import { match } from "fp-utilities";
import { Dictionary } from "../types/utils";
import { MValue } from "../utils/types";
import { ErrorResponse } from "./types";

export const makeUrl = (
  baseUrl: string,
  params: Record<string, string> = {}
): string => {
  const url = new URL(baseUrl);

  Object.entries(params).forEach(([key, value]) => {
    url.searchParams.append(key, value);
  });

  return url.toString();
};

type StrNum = string | number;

type Rec = Dictionary<StrNum>;

const isStrNum = (e: unknown): e is StrNum => {
  return Str.read(e) !== undefined || Num.read(e) !== undefined;
};

const isRec = (e: StrNum | Rec): e is Rec => {
  return Obj.isObject(e);
};

export type Entries = StrNum | Array<StrNum | Rec> | undefined;

export const makeFormEncode = (
  res: Record<string, Entries>
): Record<string, string> => {
  const r: Record<string, string> = {};

  Object.entries(res).forEach(([key, value]) => {
    if (isStrNum(value)) {
      r[key] = `${value}`;
    }

    if (Array.isArray(value)) {
      value.forEach((v, i) => {
        match(
          [
            isStrNum,
            (v) => {
              r[`${key}[${i}]`] = `${v}`;
            }
          ],
          [
            isRec,
            (v) => {
              Object.entries(v).forEach(([key1, value1]) => {
                if (isStrNum(value1)) {
                  r[`${key}[${i}][${key1}]`] = `${value1}`;
                }
              });
            }
          ]
        )(v);
      });
    }
  });

  return r;
};

export const getErrorMessage = (e: unknown): MValue<ErrorResponse> => {
  if (Obj.isObject(e) && "data" in e) {
    return e as unknown as ErrorResponse;
  }
  return undefined;
};
© 2025 XylotrechusZ