From 394ce437f9e166bdd04d2e9881ef1d51591bf045 Mon Sep 17 00:00:00 2001 From: Jason Chuang Date: Fri, 18 Sep 2026 21:13:28 +0800 Subject: [PATCH] npm compile refine --- src/utils/HttpUtils.js | 280 +++++++++++++++++--------------- src/utils/registerValidation.js | 95 +++++------ src/utils/sanitizeHtml.js | 16 +- 3 files changed, 197 insertions(+), 194 deletions(-) diff --git a/src/utils/HttpUtils.js b/src/utils/HttpUtils.js index fe8aea75..e58f219d 100644 --- a/src/utils/HttpUtils.js +++ b/src/utils/HttpUtils.js @@ -1,174 +1,188 @@ -import axios from "axios"; -import { FILE_UP_POST, FILE_DOWN_GET } from "../utils/ApiPathConst"; +import axios from 'axios'; +import { FILE_UP_POST, FILE_DOWN_GET } from '../utils/ApiPathConst'; import qs from 'qs'; export const get = ({ url, params, onSuccess, onFail, onError, onFinally }) => { - axios.get(url, { - params, - paramsSerializer: (p) => qs.stringify(p, { arrayFormat: 'repeat' }) // <-- FIX - }).then( - (response) => { onResponse(response, onSuccess, onFail); } - ).catch((error) => { - return handleError(error, onError); - }).finally(() => { - if (typeof onFinally === 'function') { - onFinally(); - } + axios + .get(url, { + params, + paramsSerializer: (p) => qs.stringify(p, { arrayFormat: 'repeat' }) // <-- FIX + }) + .then((response) => { + onResponse(response, onSuccess, onFail); + }) + .catch((error) => { + return handleError(error, onError); + }) + .finally(() => { + if (typeof onFinally === 'function') { + onFinally(); + } }); }; //TODO export const put = ({ url, params, onSuccess, onFail, onError }) => { - axios.put(url, params).then( - (response) => { onResponse(response, onSuccess, onFail); } - ).catch(error => { - return handleError(error, onError); + axios + .put(url, params) + .then((response) => { + onResponse(response, onSuccess, onFail); + }) + .catch((error) => { + return handleError(error, onError); }); }; export const patch = ({ url, params, onSuccess, onFail, onError }) => { - axios.patch(url, params).then( - (response) => { onResponse(response, onSuccess, onFail); } - ).catch(error => { - return handleError(error, onError); + axios + .patch(url, params) + .then((response) => { + onResponse(response, onSuccess, onFail); + }) + .catch((error) => { + return handleError(error, onError); }); }; export const del = ({ url, params, onSuccess, onFail, onError }) => { - axios.delete(url, { params }).then( - (response) => { onResponse(response, onSuccess, onFail); } - ).catch(error => { - return handleError(error, onError); + axios + .delete(url, { params }) + .then((response) => { + onResponse(response, onSuccess, onFail); + }) + .catch((error) => { + return handleError(error, onError); }); }; export const post = ({ url, params, onSuccess, onFail, onError, headers }) => { - headers = headers ? headers : { - "Content-Type": "application/json" - }; - - axios.post(url, params, - { - headers: headers - }).then( - (response) => { onResponse(response, onSuccess, onFail); } - ).catch(error => { - return handleError(error, onError); - }); + headers = headers + ? headers + : { + 'Content-Type': 'application/json' + }; + + axios + .post(url, params, { + headers: headers + }) + .then((response) => { + onResponse(response, onSuccess, onFail); + }) + .catch((error) => { + return handleError(error, onError); + }); }; export const postWithFiles = ({ url, params, files, onSuccess, onFail, onError }) => { - var formData = new FormData(); - for (let i = 0; i < files.length; i++) { - const file = files[i] - formData.append("multipartFileList", file); + var formData = new FormData(); + for (let i = 0; i < files.length; i++) { + const file = files[i]; + formData.append('multipartFileList', file); + } + if (params) + for (var key in params) { + if (typeof params[key] === 'object') { + formData.append(key, JSON.stringify(params[key])); + } else { + formData.append(key, params[key]); + } } - if (params) - for (var key in params) { - if (typeof (params[key]) === 'object') { - formData.append(key, JSON.stringify(params[key])); - } else { - formData.append(key, params[key]); - } - } - axios.post(url, formData, - { headers: { "Content-Type": "multipart/form-data" } }) - .then( - (response) => { onResponse(response, onSuccess, onFail); } - ).catch(error => { - return handleError(error, onError); - }); + axios + .post(url, formData, { headers: { 'Content-Type': 'multipart/form-data' } }) + .then((response) => { + onResponse(response, onSuccess, onFail); + }) + .catch((error) => { + return handleError(error, onError); + }); }; export const fileDownload = ({ url, fileId, skey, params, method, onResponse, onError }) => { - if (!url) { - url = FILE_DOWN_GET + "/" + fileId + "/" + skey - } - if (method == 'post') { - axios.post(url, params, - { - responseType: 'blob', - headers: { - "Content-Type": "application/json" - } - } - ).then( - (response) => { - fileDownloadResponse(response, onResponse) - } - ).catch(error => { - return handleError(error, onError); - }); - - } else { - axios.get(url, - { - responseType: 'blob', - params: params - } - ).then( - (response) => { - fileDownloadResponse(response, onResponse) - } - ).catch(error => { - return handleError(error, onError); - }); - } + if (!url) { + url = FILE_DOWN_GET + '/' + fileId + '/' + skey; + } + if (method == 'post') { + axios + .post(url, params, { + responseType: 'blob', + headers: { + 'Content-Type': 'application/json' + } + }) + .then((response) => { + fileDownloadResponse(response, onResponse); + }) + .catch((error) => { + return handleError(error, onError); + }); + } else { + axios + .get(url, { + responseType: 'blob', + params: params + }) + .then((response) => { + fileDownloadResponse(response, onResponse); + }) + .catch((error) => { + return handleError(error, onError); + }); + } }; const fileDownloadResponse = (response, onResponse) => { - const cd = response.headers?.['content-disposition']; - const fn = cd?.split('filename=')[1]?.replaceAll('"','')?.trim() || 'export.xlsx'; - - const url = URL.createObjectURL(response.data); - const a = document.createElement('a'); - a.href = url; - a.download = fn; - document.body.appendChild(a); - a.click(); - document.body.removeChild(a); - URL.revokeObjectURL(url); - onResponse?.(); + const cd = response.headers?.['content-disposition']; + const fn = cd?.split('filename=')[1]?.replaceAll('"', '')?.trim() || 'export.xlsx'; + + const url = URL.createObjectURL(response.data); + const a = document.createElement('a'); + a.href = url; + a.download = fn; + document.body.appendChild(a); + a.click(); + document.body.removeChild(a); + URL.revokeObjectURL(url); + onResponse?.(); }; export const fileUpload = ({ refType, refId, files, refCode, onSuccess, onFail, onError }) => { - postWithFiles({ - url: FILE_UP_POST, - params: { - refType: refType, - refId: refId, - refCode: refCode - }, - files: files, - onSuccess: onSuccess, - onFail: onFail, - onError: onError - }); + postWithFiles({ + url: FILE_UP_POST, + params: { + refType: refType, + refId: refId, + refCode: refCode + }, + files: files, + onSuccess: onSuccess, + onFail: onFail, + onError: onError + }); }; - const onResponse = (response, onSuccess, onFail) => { - if (response.status >= 300 || response.status < 200) { - console.log("onFail"); - if (onFail) { - onFail(response); - } else { - console.log(response); - } - return; + if (response.status >= 300 || response.status < 200) { + console.log('onFail'); + if (onFail) { + onFail(response); + } else { + console.log(response); } + return; + } - if (onSuccess) { - onSuccess(response?.data); - } -} + if (onSuccess) { + onSuccess(response?.data); + } +}; const handleError = (error, onError) => { - if (onError) { - return onError(error); - } else { - // console.log(error); - return false; - } -} \ No newline at end of file + if (onError) { + return onError(error); + } else { + // console.log(error); + return false; + } +}; diff --git a/src/utils/registerValidation.js b/src/utils/registerValidation.js index 403c1f88..54cb0772 100644 --- a/src/utils/registerValidation.js +++ b/src/utils/registerValidation.js @@ -1,18 +1,18 @@ export const ADDRESS_LINE_MAX_LENGTH = 40; export function isAddressLineWithinLimit(line, maxLen = ADDRESS_LINE_MAX_LENGTH) { - if (line == null || line === '') return true; - return line.length <= maxLen; + if (line == null || line === '') return true; + return line.length <= maxLen; } /** Step-0 gate: line1 required; lines 1–3 each <= maxLen */ export function isRegisterAddressValid(data, maxLen = ADDRESS_LINE_MAX_LENGTH) { - return ( - data.address1 !== '' && - isAddressLineWithinLimit(data.address1, maxLen) && - isAddressLineWithinLimit(data.address2, maxLen) && - isAddressLineWithinLimit(data.address3, maxLen) - ); + return ( + data.address1 !== '' && + isAddressLineWithinLimit(data.address1, maxLen) && + isAddressLineWithinLimit(data.address2, maxLen) && + isAddressLineWithinLimit(data.address3, maxLen) + ); } /** @@ -20,48 +20,51 @@ export function isRegisterAddressValid(data, maxLen = ADDRESS_LINE_MAX_LENGTH) { * Packs at comma boundaries where possible. */ export function splitAddressIntoLines(address, maxLen = ADDRESS_LINE_MAX_LENGTH) { - const empty = { address1: '', address2: '', address3: '' }; - if (address == null || address === '') return empty; + const empty = { address1: '', address2: '', address3: '' }; + if (address == null || address === '') return empty; - const trimmed = address.trim(); - if (trimmed.length <= maxLen) { - return { address1: trimmed, address2: '', address3: '' }; - } + const trimmed = address.trim(); + if (trimmed.length <= maxLen) { + return { address1: trimmed, address2: '', address3: '' }; + } - const segments = trimmed.split(',').map((s) => s.trim()).filter(Boolean); - const lines = []; - let current = ''; + const segments = trimmed + .split(',') + .map((s) => s.trim()) + .filter(Boolean); + const lines = []; + let current = ''; - const pushCurrent = () => { - if (current) { - lines.push(current); - current = ''; - } - }; + const pushCurrent = () => { + if (current) { + lines.push(current); + current = ''; + } + }; - for (const segment of segments) { - const candidate = current ? `${current}, ${segment}` : segment; - if (candidate.length <= maxLen) { - current = candidate; - } else if (segment.length <= maxLen) { - pushCurrent(); - current = segment; - } else { - pushCurrent(); - let remaining = segment; - while (remaining.length > maxLen) { - lines.push(remaining.slice(0, maxLen)); - remaining = remaining.slice(maxLen); - } - current = remaining; - } - if (lines.length >= 3) break; + for (const segment of segments) { + const candidate = current ? `${current}, ${segment}` : segment; + if (candidate.length <= maxLen) { + current = candidate; + } else if (segment.length <= maxLen) { + pushCurrent(); + current = segment; + } else { + pushCurrent(); + let remaining = segment; + while (remaining.length > maxLen) { + lines.push(remaining.slice(0, maxLen)); + remaining = remaining.slice(maxLen); + } + current = remaining; } - pushCurrent(); + if (lines.length >= 3) break; + } + pushCurrent(); - return { - address1: lines[0] ?? '', - address2: lines[1] ?? '', - address3: lines[2] ?? '', - }; + return { + address1: lines[0] ?? '', + address2: lines[1] ?? '', + address3: lines[2] ?? '' + }; } diff --git a/src/utils/sanitizeHtml.js b/src/utils/sanitizeHtml.js index 97fc0f01..d812e92e 100644 --- a/src/utils/sanitizeHtml.js +++ b/src/utils/sanitizeHtml.js @@ -38,21 +38,7 @@ const ALLOWED_TAGS = [ 'ul' ]; -const ALLOWED_ATTR = [ - 'href', - 'title', - 'target', - 'rel', - 'src', - 'alt', - 'width', - 'height', - 'colspan', - 'rowspan', - 'scope', - 'class', - 'style' -]; +const ALLOWED_ATTR = ['href', 'title', 'target', 'rel', 'src', 'alt', 'width', 'height', 'colspan', 'rowspan', 'scope', 'class', 'style']; if (typeof window !== 'undefined') { DOMPurify.addHook('afterSanitizeAttributes', (node) => {