Skip to main content

External API (Server-side)

CustomAPI

tip

โฟลเดอร์ test-api เป็นเพียงตัวอย่าง API ไว้สำหรับทดสอบ HTTP Request จาก azael_playpass เพื่อตรวจสอบสิทธิ์การอนุญาตสำหรับ Custom API เท่านั้น

File Structure

test-api
├── .gitignore
├── index.js
└── package.json

How to Run

cd test-api
npm i
npm run dev

httpRequest

ฟังก์ชัน HTTP Request ไปยัง Custom API ทำงานเมื่อเปิดใช้งานอ้างอิงสิทธิ์การเชื่อมต่อจาก API แบบกำหนดเองที่ activeAPI ไฟล์ ./config/external_api.lua

บรรทัดที่ 19
function CustomAPI.httpRequest(req, identifier)
local reqUrl <const> = ('%s/%s'):format(req.url, identifier)
local resStatus <const>, resBody <const> = PerformHttpRequestAwait(reqUrl, 'GET', '', {
['Content-Type'] = 'application/json; charset=utf-8',
['Authorization'] = req.auth
})

if resStatus == 200 then
local resData <const> = json.decode(resBody)

if not resData or not resData.success then -- disallow
return false, {
code = resStatus,
message = resData?.message -- หาก resData.message เป็น nil จะเรียกใช้ข้อความ "join_not_permitted" ที่ไฟล์ ./locales/<langcode>.json แทน
}
end

return true
end

return false, {
code = resStatus,
message = ('HTTP status code %d (%s)'):format(resStatus, 'For more details: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/' .. resStatus)
}
end

Parameters

Returns

  • success: boolean
    • ตอบกลับเป็น true หากอนุญาตให้เข้าร่วมเซิร์ฟเวอร์
  • err: table<{ [key]: any }> | nil
    • ข้อมูลการปฏิเสธหรือไม่อนุญาตให้เข้าร่วมเซิร์ฟเวอร์ เมื่อ success ตอบกลับเป็น false
      • code: integer
        • รหัสสถานะ HTTP ที่ตอบกลับโดย Custom API
      • message: string | nil
        • ข้อความการปฏิเสธหรือไม่อนุญาตให้เข้าร่วมเซิร์ฟเวอร์ (หากเป็น nil จะใช้ข้อความจาก join_not_permitted)