UNPKG

@pichxyaponn/tw-angpao

Version:

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

78 lines (75 loc) 2.03 kB
// src/type.ts import { Type as t } from "@sinclair/typebox"; var shape = t.Object({ mobile: t.String(), voucher_hash: t.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); } } export { getValidVoucherCode, isValidThaiPhoneNumber, makeApiRequest, parseApiResponse };