@uniai-fe/util-functions 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -269,8 +269,7 @@ var parseData = (data) => {
269
269
  var getValueByKeyChain = (data, keyChain) => {
270
270
  if (isKeyChainRecord(data)) {
271
271
  const firstChainIndex = keyChain.indexOf(".");
272
- if (firstChainIndex === -1)
273
- return data[keyChain];
272
+ if (firstChainIndex === -1) return data[keyChain];
274
273
  const currentKey = keyChain.slice(0, firstChainIndex);
275
274
  const remainKeyChain = keyChain.slice(firstChainIndex + 1);
276
275
  const foundData = data[currentKey];
@@ -290,10 +289,7 @@ var convertObjectToSearchParams = (data) => {
290
289
  }
291
290
  if (isKeyChainRecord(data) && Object.keys(data).length > 0) {
292
291
  const d = Object.fromEntries(
293
- Object.entries(data).map(([key, value]) => [
294
- key,
295
- string(value)
296
- ])
292
+ Object.entries(data).map(([key, value]) => [key, string(value)])
297
293
  );
298
294
  return new URLSearchParams(d);
299
295
  }
@@ -547,7 +543,10 @@ var getMatchRoute = (routes, currentPath) => {
547
543
  const res = { category: null, depth: null };
548
544
  res.category = getClosestRoute(routes, currentPath);
549
545
  if (res.category && res.category.depth) {
550
- res.depth = getClosestRoute(res.category.depth, currentPath);
546
+ res.depth = getClosestRoute(
547
+ res.category.depth,
548
+ currentPath
549
+ );
551
550
  }
552
551
  return res;
553
552
  };
@@ -586,8 +585,8 @@ var sortDateDesc = (a, b) => {
586
585
  return sortDesc(new Date(a).getTime(), new Date(b).getTime());
587
586
  };
588
587
 
589
- // src/api/module.ts
590
- var defaultLogger = (method, routeUrl, queryUrl, ...messages) => {
588
+ // src/api/util.ts
589
+ var nextAPILog = (method, routeUrl, queryUrl, ...messages) => {
591
590
  if (process.env.NODE_ENV === "production") return;
592
591
  const DATE = dateFormat(/* @__PURE__ */ new Date());
593
592
  const TIME = new Intl.DateTimeFormat("en-US", {
@@ -599,9 +598,10 @@ var defaultLogger = (method, routeUrl, queryUrl, ...messages) => {
599
598
  const REQUEST_METHOD = method.split("-")[0]?.toUpperCase() || "-";
600
599
  const RESPONSE_METHOD = method.split("-")[1] ? method.split("-")[1]?.toUpperCase() || "-" : REQUEST_METHOD;
601
600
  return console.log(
601
+ // `⚡️`,
602
602
  `
603
603
  `,
604
- `[Backend API] \u23F1 ${DATE} ( ${TIME} )`,
604
+ `[Next Server API] \u23F1 ${DATE} ( ${TIME} )`,
605
605
  `
606
606
  \u03BB [${REQUEST_METHOD}] FE route`,
607
607
  `
@@ -615,6 +615,7 @@ var defaultLogger = (method, routeUrl, queryUrl, ...messages) => {
615
615
  `
616
616
  );
617
617
  };
618
+ var getQueryString = (searchParams) => typeof searchParams !== "undefined" && convertObjectToSearchParams(searchParams).toString() ? `?${convertObjectToSearchParams(searchParams).toString()}` : "";
618
619
  var getFetchOptions = ({
619
620
  method,
620
621
  headers,
@@ -625,131 +626,219 @@ var getFetchOptions = ({
625
626
  if (typeof body !== "undefined") Object.assign(option, { body });
626
627
  return option;
627
628
  };
628
- var getQueryString = (searchParams) => {
629
- if (typeof searchParams === "undefined" || convertObjectToSearchParams(searchParams).toString() === "")
630
- return "";
631
- return `?${convertObjectToSearchParams(searchParams).toString()}`;
632
- };
633
- var resolveDomain = (infra, explicitDomain, resolver) => {
634
- if (typeof explicitDomain === "string" && explicitDomain)
635
- return explicitDomain;
636
- if (typeof infra === "undefined") {
637
- throw new Error(
638
- "[createBackendApiClient] infra \uAC00 \uC9C0\uC815\uB418\uC9C0 \uC54A\uC544 \uB3C4\uBA54\uC778\uC744 \uD574\uC11D\uD560 \uC218 \uC5C6\uC2B5\uB2C8\uB2E4."
639
- );
640
- }
641
- const domain = resolver(infra);
642
- if (!domain) {
643
- throw new Error(
644
- `[createBackendApiClient] \uB3C4\uBA54\uC778\uC744 \uD574\uC11D\uD560 \uC218 \uC5C6\uC2B5\uB2C8\uB2E4. (infra: ${infra})`
645
- );
646
- }
647
- return domain;
648
- };
649
- var createBackendApiClient = (config) => {
650
- const baseLogger = config.logger ?? defaultLogger;
651
- const generateBackendQueryUrl_GET = ({
652
- infra,
653
- domain,
654
- routeUrl,
655
- queryUrl,
656
- searchParams,
657
- log,
658
- logDisabled,
659
- logger
660
- }) => {
661
- const resolvedDomain = resolveDomain(infra, domain, config.resolveDomain);
662
- const url = `${resolvedDomain}${queryUrl}${getQueryString(searchParams)}`;
663
- const logFn = logger ?? baseLogger;
664
- if (!logDisabled)
665
- logFn("get", routeUrl, url, {
666
- ...log,
667
- ...searchParams ? searchParams : {}
668
- });
669
- return url;
629
+
630
+ // src/api/module.ts
631
+ var generateBackendQueryUrl_GET = ({
632
+ domain,
633
+ routeUrl,
634
+ queryUrl,
635
+ searchParams,
636
+ // 가공이 완료된 파라미터
637
+ log,
638
+ logDisabled
639
+ }) => {
640
+ const url = `${domain}${queryUrl}${getQueryString(searchParams)}`;
641
+ if (!logDisabled)
642
+ nextAPILog("get", routeUrl, url, {
643
+ ...log,
644
+ ...searchParams ? searchParams : {}
645
+ });
646
+ return url;
647
+ };
648
+ var fetchBackendQuery = async ({
649
+ domain,
650
+ method,
651
+ routeUrl,
652
+ queryUrl,
653
+ headers,
654
+ body,
655
+ bodyOriginData,
656
+ queryStringData,
657
+ log,
658
+ logDisabled,
659
+ fetchDisabled
660
+ }) => {
661
+ const url = `${domain}${queryUrl}${getQueryString(queryStringData)}`;
662
+ const errRes = {
663
+ is_ok: false,
664
+ idx: "",
665
+ error: ""
670
666
  };
671
- const fetchBackendQuery = async ({
672
- infra,
673
- domain,
674
- routeUrl,
675
- queryUrl,
667
+ const bodyData = convertObjectToSearchParams(bodyOriginData);
668
+ if (typeof bodyOriginData !== "undefined" && bodyData.toString() === "") {
669
+ if (!logDisabled) {
670
+ nextAPILog(method.toLowerCase(), routeUrl, url, {
671
+ state: "ERROR (BODY - ORIGIN DATA)",
672
+ error: "\uB370\uC774\uD130\uAC00 \uC720\uD6A8\uD558\uC9C0 \uC54A\uC2B5\uB2C8\uB2E4.",
673
+ bodyOriginData,
674
+ ...log
675
+ });
676
+ }
677
+ errRes.error = "\uB370\uC774\uD130\uAC00 \uC720\uD6A8\uD558\uC9C0 \uC54A\uC2B5\uB2C8\uB2E4.";
678
+ return errRes;
679
+ }
680
+ const option = getFetchOptions({
676
681
  method,
677
682
  headers,
678
- body,
679
- bodyOriginData,
680
- queryStringData,
681
- log,
682
- logDisabled,
683
- fetchDisabled,
684
- logger
685
- }) => {
686
- const resolvedDomain = resolveDomain(infra, domain, config.resolveDomain);
687
- const url = `${resolvedDomain}${queryUrl}${getQueryString(queryStringData)}`;
688
- const errRes = {
689
- is_ok: false,
690
- idx: "",
691
- error: ""
692
- };
693
- const bodyData = convertObjectToSearchParams(bodyOriginData);
694
- if (typeof bodyOriginData !== "undefined" && bodyData.toString() === "") {
695
- if (!logDisabled) {
696
- (logger ?? baseLogger)(method.toLowerCase(), routeUrl, url, {
697
- state: "ERROR (BODY - ORIGIN DATA)",
698
- error: "\uB370\uC774\uD130\uAC00 \uC720\uD6A8\uD558\uC9C0 \uC54A\uC2B5\uB2C8\uB2E4.",
699
- bodyOriginData,
700
- ...log
701
- });
702
- }
703
- errRes.error = "\uB370\uC774\uD130\uAC00 \uC720\uD6A8\uD558\uC9C0 \uC54A\uC2B5\uB2C8\uB2E4.";
704
- return errRes;
705
- }
706
- const option = getFetchOptions({
707
- method,
708
- headers,
709
- body
683
+ body
684
+ });
685
+ if (!body && typeof bodyOriginData !== "undefined")
686
+ Object.assign(option, { body: bodyData });
687
+ if (!logDisabled) {
688
+ nextAPILog(method.toLowerCase(), routeUrl, url, {
689
+ state: "READY",
690
+ ...option,
691
+ ...log
710
692
  });
711
- if (!body && typeof bodyOriginData !== "undefined")
712
- Object.assign(option, { body: bodyData });
713
- const logFn = logger ?? baseLogger;
693
+ }
694
+ if (fetchDisabled) {
695
+ errRes.error = "API \uC694\uCCAD\uC81C\uD55C \uD65C\uC131\uD654\uB428.";
696
+ return errRes;
697
+ }
698
+ try {
699
+ const res = await (await fetch(url, option)).json();
700
+ if (!logDisabled) {
701
+ const isSuccess = "is_ok" in res && typeof res?.is_ok === "boolean" && res.is_ok === true;
702
+ nextAPILog(method.toLowerCase(), routeUrl, url, {
703
+ state: isSuccess ? "SUCCESS" : "FAIL",
704
+ ...res
705
+ });
706
+ }
707
+ return res;
708
+ } catch (error) {
709
+ errRes.error = error instanceof Error ? error.message || "\uC54C \uC218 \uC5C6\uB294 \uC624\uB958\uAC00 \uBC1C\uC0DD\uD558\uC600\uC2B5\uB2C8\uB2E4." : String(error);
714
710
  if (!logDisabled) {
715
- logFn(method.toLowerCase(), routeUrl, url, {
716
- state: "READY",
717
- ...option,
711
+ nextAPILog(method.toLowerCase(), routeUrl, url, {
712
+ state: "ERROR (RESPONSE)",
713
+ error,
718
714
  ...log
719
715
  });
720
716
  }
721
- if (fetchDisabled) {
722
- errRes.error = "API \uC694\uCCAD\uC81C\uD55C \uD65C\uC131\uD654\uB428.";
723
- return errRes;
717
+ return errRes;
718
+ }
719
+ };
720
+ var fetchWithBody = async ({
721
+ domain,
722
+ method,
723
+ routeUrl,
724
+ queryUrl,
725
+ searchParams,
726
+ headers,
727
+ body,
728
+ bodyData,
729
+ isRawResponse,
730
+ alternateResponse,
731
+ debug,
732
+ disabled,
733
+ disabledLog
734
+ }) => {
735
+ const url = `${domain}${queryUrl}${getQueryString(searchParams)}`;
736
+ const option = getFetchOptions({
737
+ method,
738
+ headers,
739
+ body
740
+ });
741
+ if (!body && typeof bodyData !== "undefined") {
742
+ if (typeof bodyData !== "undefined" && String(bodyData) === "") {
743
+ nextAPILog(method.toLowerCase(), routeUrl, url, {
744
+ state: "ERROR (BODY - DATA)",
745
+ error: "\uB370\uC774\uD130\uAC00 \uC720\uD6A8\uD558\uC9C0 \uC54A\uC2B5\uB2C8\uB2E4.",
746
+ bodyData,
747
+ ...debug
748
+ });
749
+ return alternateResponse;
724
750
  }
725
- try {
726
- const res = await (await fetch(url, option)).json();
727
- if (!logDisabled) {
728
- const isSuccess = "is_ok" in res && typeof res?.is_ok === "boolean" && res.is_ok === true;
729
- logFn(method.toLowerCase(), routeUrl, url, {
730
- state: isSuccess ? "SUCCESS" : "FAIL",
731
- ...res
732
- });
733
- }
734
- return res;
735
- } catch (error) {
736
- errRes.error = error instanceof Error ? error.message || "\uC54C \uC218 \uC5C6\uB294 \uC624\uB958\uAC00 \uBC1C\uC0DD\uD558\uC600\uC2B5\uB2C8\uB2E4." : String(error);
737
- if (!logDisabled) {
738
- logFn(method.toLowerCase(), routeUrl, url, {
739
- state: "ERROR (RESPONSE)",
740
- error,
741
- ...log
742
- });
743
- }
744
- return errRes;
751
+ Object.assign(option, { body: bodyData });
752
+ }
753
+ if (!disabledLog)
754
+ nextAPILog(method.toLowerCase(), routeUrl, url, {
755
+ state: "READY",
756
+ ...option,
757
+ ...debug
758
+ });
759
+ if (disabled) {
760
+ nextAPILog(method.toLowerCase(), routeUrl, url, {
761
+ state: "FETCH DISABLED",
762
+ message: "API \uC694\uCCAD\uC81C\uD55C \uD65C\uC131\uD654\uB428."
763
+ });
764
+ return alternateResponse;
765
+ }
766
+ try {
767
+ const responseRaw = await fetch(url, option);
768
+ const resCode = responseRaw.status;
769
+ if (!disabledLog) {
770
+ nextAPILog(method.toLowerCase(), routeUrl, url, {
771
+ state: resCode === 200 ? "SUCCESS" : "FAIL",
772
+ code: resCode
773
+ });
745
774
  }
746
- };
747
- return {
748
- getQueryString,
749
- generateBackendQueryUrl_GET,
750
- fetchBackendQuery,
751
- logger: baseLogger
752
- };
775
+ if (isRawResponse) return responseRaw;
776
+ const res = await responseRaw.json();
777
+ return res;
778
+ } catch (error) {
779
+ if (!disabledLog)
780
+ nextAPILog(method.toLowerCase(), routeUrl, url, {
781
+ state: "ERROR (RESPONSE)",
782
+ error,
783
+ ...debug
784
+ });
785
+ return alternateResponse;
786
+ }
787
+ };
788
+
789
+ // src/user-agent/device-patterns.ts
790
+ var apple = ["iPhone", "iPad", "iPod", "Mac", "Macintosh"];
791
+ var tablet = ["Tablet", "iPad", "playbook", "silk"];
792
+ var mobile = [
793
+ "Android",
794
+ "Mobile",
795
+ "iPhone",
796
+ "iPod",
797
+ "iPad",
798
+ "BlackBerry",
799
+ "IEMobile",
800
+ "Kindle",
801
+ "NetFront",
802
+ "Silk-Accelerated",
803
+ "hpwOS",
804
+ "webOS",
805
+ "Fennec",
806
+ "Minimo",
807
+ "Opera Mobi",
808
+ "Opera Mini",
809
+ "Blazer",
810
+ "Dolfin",
811
+ "Dolphin",
812
+ "Skyfire",
813
+ "Zune"
814
+ ];
815
+ var devicePatterns = {
816
+ apple,
817
+ tablet,
818
+ mobile
819
+ };
820
+ var device_patterns_default = devicePatterns;
821
+
822
+ // src/user-agent/module.ts
823
+ var setLowerCase = (v) => String(v).toLowerCase();
824
+ var deviceLowerCasePatterns = Object.fromEntries(
825
+ Object.entries(device_patterns_default).map(([key, value]) => [
826
+ key,
827
+ value.map(setLowerCase)
828
+ ])
829
+ );
830
+ var checkResponsiveDevice = (userAgent) => {
831
+ const { mobile: mobile2, tablet: tablet2 } = deviceLowerCasePatterns;
832
+ const clientAgent = setLowerCase(userAgent);
833
+ const isTablet = tablet2.some((agent) => clientAgent.includes(agent));
834
+ const isMobile = mobile2.some((agent) => clientAgent.includes(agent));
835
+ if (isMobile) return isTablet ? "tablet" : "mobile";
836
+ return "desktop";
837
+ };
838
+ var checkAppleDevice = (userAgent) => {
839
+ const { apple: apple2 } = deviceLowerCasePatterns;
840
+ const clientAgent = setLowerCase(userAgent);
841
+ return apple2.some((agent) => clientAgent.includes(agent));
753
842
  };
754
843
 
755
844
  // src/form/checkbox/module.ts
@@ -797,7 +886,9 @@ var stylePaddingSize = styleSpacingSize;
797
886
  var styleMarginSize = styleSpacingSize;
798
887
 
799
888
  exports.capitalize = capitalize;
889
+ exports.checkAppleDevice = checkAppleDevice;
800
890
  exports.checkDateMoment = checkDateMoment;
891
+ exports.checkResponsiveDevice = checkResponsiveDevice;
801
892
  exports.convert2Digit = convert2Digit;
802
893
  exports.convertDigit = convertDigit;
803
894
  exports.convertLiter = convertLiter;
@@ -807,18 +898,21 @@ exports.convertSelectedRepeatInfo = convertSelectedRepeatInfo;
807
898
  exports.correctTimezone = correctTimezone;
808
899
  exports.covertGram = covertGram;
809
900
  exports.covertKilogram = covertKilogram;
810
- exports.createBackendApiClient = createBackendApiClient;
811
901
  exports.dateFormat = dateFormat;
812
- exports.defaultApiLogger = defaultLogger;
813
902
  exports.deltaFormat = deltaFormat;
903
+ exports.devicePatterns = device_patterns_default;
814
904
  exports.distanceFormat = distanceFormat;
815
905
  exports.durationFormat = durationFormat;
816
906
  exports.escapeQuotes = escapeQuotes;
817
907
  exports.escapeSymbol = escapeSymbol;
818
908
  exports.extractCoreValue = extractCoreValue;
909
+ exports.fetchBackendQuery = fetchBackendQuery;
910
+ exports.fetchWithBody = fetchWithBody;
911
+ exports.generateBackendQueryUrl_GET = generateBackendQueryUrl_GET;
819
912
  exports.generateRepeatCycleDate = generateRepeatCycleDate;
820
913
  exports.getClosestRoute = getClosestRoute;
821
914
  exports.getDateArray = getDateArray;
915
+ exports.getFetchOptions = getFetchOptions;
822
916
  exports.getLastWeeks = getLastWeeks;
823
917
  exports.getMatchRoute = getMatchRoute;
824
918
  exports.getNextDay = getNextDay;
@@ -847,6 +941,7 @@ exports.maskDate = maskDate;
847
941
  exports.maskDecimalSeparator = maskDecimalSeparator;
848
942
  exports.maskPhone = maskPhone;
849
943
  exports.maskResidentCode = maskResidentCode;
944
+ exports.nextAPILog = nextAPILog;
850
945
  exports.parseData = parseData;
851
946
  exports.phoneNumberFormat = phoneNumberFormat;
852
947
  exports.repeatCycleDateKo = repeatCycleDateKo;