UNPKG

@pichxyaponn/tw-angpao

Version:

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

183 lines (177 loc) 5.2 kB
"use strict"; 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/index.ts var index_exports = {}; __export(index_exports, { TWAngpao: () => TWAngpao, default: () => index_default }); module.exports = __toCommonJS(index_exports); var import_elysia = require("elysia"); // src/error.class.ts var ValidationError = class extends Error { constructor(code, message) { super(message); this.code = code; this.name = "ValidationError"; } }; var NetworkError = class extends Error { constructor(code, message) { super(message); this.code = code; this.name = "NetworkError"; } }; 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/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/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); } } // src/index.ts async function redeemVoucher({ phoneNumber, voucherCode }) { const cleanedPhoneNumber = phoneNumber?.trim() || ""; const validVoucherCode = voucherCode ? getValidVoucherCode(voucherCode) : ""; if (!isValidThaiPhoneNumber(cleanedPhoneNumber)) return { status: { code: "INVALID_PHONE_NUMBER", message: "Invalid Thai Phone Number." }, data: null }; if (!validVoucherCode) return { status: { code: "INVALID_VOUCHER_CODE", message: "Invalid Voucher Code." }, data: null }; const url = `https://gift.truemoney.com/campaign/vouchers/${validVoucherCode}/redeem`; const body = { mobile: cleanedPhoneNumber, voucher_hash: validVoucherCode }; try { const response = await makeApiRequest(url, body); return await parseApiResponse(response); } catch (error) { if (error instanceof ValidationError || error instanceof ApiError) return { status: { code: error.code, message: error.message }, data: null }; if (error instanceof NetworkError || error instanceof JsonParseError) return { status: { code: error.code, message: error.message, error: error.cause }, data: null }; console.error("Unexpected error in redeemVoucher:", error); throw new NetworkError( "NETWORK_ERROR", error instanceof Error ? error.message : "Unexpected error" ); } } var TWAngpao = (name = "TWA") => { return new import_elysia.Elysia({ name: "tw-angpao", seed: name }).decorate(name, { async redeem(phoneNumber, voucherCode) { return redeemVoucher({ phoneNumber, voucherCode }); } }); }; var index_default = TWAngpao; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { TWAngpao });