@pichxyaponn/tw-angpao
Version:
ระบบรับเงินจาก Truewallet (ซองอั่งเปา) ด้วย ElysiaJS
108 lines (103 loc) • 3.27 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/utils.ts
var utils_exports = {};
__export(utils_exports, {
getValidVoucherCode: () => getValidVoucherCode,
isValidThaiPhoneNumber: () => isValidThaiPhoneNumber,
makeApiRequest: () => makeApiRequest,
parseApiResponse: () => parseApiResponse
});
module.exports = __toCommonJS(utils_exports);
// src/type.ts
var import_typebox = require("@sinclair/typebox");
var shape = import_typebox.Type.Object({
mobile: import_typebox.Type.String(),
voucher_hash: import_typebox.Type.String()
});
// src/error.class.ts
var ApiError = class extends Error {
constructor(code, message) {
super(message);
this.code = code;
this.name = "ApiError";
}
};
var JsonParseError = class extends Error {
constructor(message, originalError) {
super(message);
this.code = "INVALID_JSON_RESPONSE";
this.name = "JsonParseError";
this.cause = originalError;
}
};
// src/utils.ts
function getValidVoucherCode(voucherCode) {
const parts = voucherCode.split("?v=");
const codeToTest = parts[1] || parts[0];
const match = codeToTest.match(/[0-9A-Za-z]+/);
return match ? match[0] : "";
}
var thaiPhoneNumberRegex = /^0[689]\d{8}$/;
function isValidThaiPhoneNumber(phoneNumber) {
const cleanedNumber = phoneNumber.replace(/[^\d]/g, "");
if (cleanedNumber.startsWith("66") && cleanedNumber.length === 11) {
return thaiPhoneNumberRegex.test("0" + cleanedNumber.substring(2));
}
return thaiPhoneNumberRegex.test(cleanedNumber);
}
async function makeApiRequest(url, body) {
const response = await fetch(url, {
method: "POST",
headers: {
"content-type": "application/json"
},
body: JSON.stringify(body)
});
return response;
}
async function parseApiResponse(response) {
if (!response?.ok) {
try {
const errorData = await response?.json();
if (errorData) {
return errorData;
}
} catch {
}
throw new ApiError(
`HTTP_ERROR_${response?.ok ? "OK" : "UNKNOWN"}`,
`API request failed: ${response?.statusText || "Unknown"}`
);
}
try {
const data = await response?.json();
if (data?.status?.code === "SUCCESS" && data?.status?.data) return data;
else return data;
} catch (jsonError) {
throw new JsonParseError("API returned invalid JSON", jsonError);
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
getValidVoucherCode,
isValidThaiPhoneNumber,
makeApiRequest,
parseApiResponse
});