UNPKG

@pichxyaponn/tw-angpao

Version:

ระบบรับเงินจาก Truewallet (ซองอั่งเปา) ด้วย ElysiaJS

124 lines (118 loc) 4.19 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); 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.d.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/utils.ts var import_json_accelerator = __toESM(require("json-accelerator")); // 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 var import_compiler = require("@sinclair/typebox/compiler"); 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 guard = import_compiler.TypeCompiler.Compile(shape); const encode = (0, import_json_accelerator.default)(shape); const response = await fetch(url, { method: "POST", headers: { "content-type": "application/json" }, body: guard.Check(body) ? encode(body) : JSON.stringify(body) }); return response; } async function parseApiResponse(response) { if (!response?.ok) { try { const errorData = await response?.json(); if (errorData) { return errorData; } } catch (parseError) { } 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 });