Thuta Learning
API Tutorial
BasicWeb Developmentအလယ်အလတ်

API ဆိုတာဘာလဲ?

API Tutorialသင်ခန်းစာ 220 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Library API (function call) နဲ့ web API (HTTP request) ကို ခွဲခြားနိုင်ဖို့
  • Web API တစ်ခုအကြောင်း client က သိရမယ့် အချက်သုံးခု (ဘယ်မှာ ရှိလဲ၊ ဘာပို့ရမလဲ၊ ဘာပြန်လာမလဲ) ကို ပြောနိုင်ဖို့
  • Response ကို status, header, body အဖြစ် ခွဲဖတ်ပြီး JSON body ကို parse လုပ်နိုင်ဖို့
  • API contract ကြောင့် server က အတွင်းပိုင်း ပြောင်းလို့ရပြီး client တွေ မပျက်တာကို ရှင်းပြနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

API ဆိုတာ program တစ်ခုရဲ့ ဧည့်ကြိုကောင်တာလို့ မြင်ကြည့်ပါ။ ဟိုတယ်ဧည့်သည်က အဝတ်လျှော်ခန်း ဒါမှမဟုတ် စာရင်းကိုင်ရုံးထဲ ဝင်မသွားပါဘူး၊ ဧည့်ကြိုကောင်တာကို မေးတာပါ၊ ကောင်တာက နောက်ကွယ်မှာ ဘယ်သူနဲ့ ပြောရမလဲ သိပါတယ်။ Program တွေလည်း အတူတူပါပဲ။ Program တစ်ခုက တောင်းလို့ရတဲ့ အရာတွေကို သေးသေးလေး၊ မှတ်တမ်းတင်ထားတဲ့ စာရင်းတစ်ခုအဖြစ် ဖော်ပြပြီး ကျန်တာအားလုံးကို ဖုံးထားပါတယ်။

Program နှစ်ခုက စက်မတူဘဲ အင်တာနက်ပေါ်မှာ HTTP နဲ့ ပြောကြရင် အဲဒီ interface ကို web API လို့ ခေါ်ပါတယ်။ ဒီ course ရဲ့ အကြောင်းအရာက ဒီအမျိုးအစားပါ၊ URL နဲ့ method ကို ပို့လိုက်ရင် status၊ header အချို့ နဲ့ များသောအားဖြင့် JSON body တစ်ခု ပြန်ရပါတယ်။

Problem → Why → Solution

ပြဿနာ: client က database ကို တိုက်ရိုက် သိနေရင်

ဆိုင်တစ်ဆိုင်ရဲ့ mobile app ကို ထိန်းသိမ်းနေပြီး ပစ္စည်းအချက်အလက်တွေက တခြား team တစ်ခု run တဲ့ server မှာ ရှိတယ်လို့ စဉ်းစားပါ။ API မရှိရင် ကိုယ့် app က database password၊ table တွေရဲ့ ဖွဲ့စည်းပုံနဲ့ ဖတ်ဖို့ SQL ကို သိထားရပါမယ်။ တခြား team က column တစ်ခုကို နာမည်ပြောင်းလိုက်တဲ့နေ့မှာ ကိုယ့် app ပျက်ပြီး ဒီနည်းကိုပဲ ကူးသုံးထားတဲ့ တခြား app အားလုံးလည်း အတူ ပျက်ပါတယ်။

အပြန်အလှန် နည်းလမ်းကတော့ database ကို ဖုံးထားပြီး /products/1 လို URL တစ်ခုကနေ ပစ္စည်းကို JSON နဲ့ ပြန်ပေးတာပါ။ Client တွေက URL နဲ့ JSON ပုံစံကိုပဲ မှီခိုပါတယ်။ အောက်က code က ကြွပ်ဆတ်တဲ့ တိုက်ရိုက်နည်းနဲ့ တည်ငြိမ်တဲ့ နည်းကို ယှဉ်ပြထားပါတယ်။

two-approaches.mjs
// Fragile: the client knows the table layout and holds the database password
const rows = await db.query(
  "SELECT product_name, price_cents FROM tbl_products_v2 WHERE id = 1",
  DB_PASSWORD
);

// Stable: the client knows one URL and the shape of the JSON it returns
// (api.example.com is a placeholder host used for illustration only)
const res = await fetch("https://api.example.com/v1/products/1");

ဘာကြောင့် API လိုတာလဲ

Database ကို တိုက်ရိုက် သုံးရင် server ရဲ့ အတွင်းပိုင်း အသေးစိတ်တွေ client တိုင်းထဲ ပျံ့နှံ့သွားလို့ server အတွင်း ပြောင်းလဲမှုတိုင်းက အပြင်ဘက်မှာ ချိုးဖောက်မှု ဖြစ်လာပါတယ်။ API က မျဉ်းတစ်ကြောင်း ဆွဲပေးပါတယ်၊ server က URL၊ method နဲ့ response ပုံစံကို ကတိပေးပြီး အဲဒီမျဉ်း နောက်က အားလုံးကို လွတ်လပ်စွာ ပြောင်းလို့ရပါတယ်။ ဒီကတိကြောင့်ပဲ API တစ်ခုတည်းက website၊ Android app နဲ့ partner ရဲ့ script ကို တစ်ပြိုင်နက် ဝန်ဆောင်မှု ပေးနိုင်တာပါ။

Core concept: interface ကို သုံး၊ implementation ကို မဖတ်ရ

API ဆိုတဲ့ စကားလုံးက web service တွေထက် ပိုကျယ်ပါတယ်။ Code တစ်ခုက တခြား code ခေါ်ဖို့ function တွေ ဖော်ပြထားရင် ဒါ API ပါပဲ။ Node ရဲ့ built-in crypto module ထဲက function တွေဟာ API တစ်ခုဖြစ်သလို array ပေါ်က method စာရင်းလည်း API ပါ။ အားလုံးမှာ တူတာက implementation ကို မဖတ်ဘဲ သုံးလို့ရတဲ့ interface တစ်ခု ရှိတာပါ။ Web API ကတော့ ဒီအယူအဆကိုပဲ network တစ်လျှောက် ဆန့်ထုတ်ထားတာပါ — function ခေါ်တာအစား HTTP request ပို့ပြီး return value အစား HTTP response ရပါတယ်။

ဒီလို ဆန့်ထုတ်လိုက်တာက အချက်သုံးခု အသစ် ပေါင်းထည့်ပါတယ်။ ပထမ၊ call တွေက အချိန်ယူပြီး ကိုယ့် code ကြောင့် မဟုတ်တဲ့ အကြောင်းရင်းတွေကြောင့် ကျရှုံးနိုင်ပါတယ် — server ပိတ်နေတာ၊ connection ပြတ်တာ၊ အဖြေ နှေးလွန်းတာ။ ဒုတိယ၊ data က network ပေါ်မှာ စာသားအဖြစ် ဖြတ်သွားလို့ object တွေကို တစ်ဖက်မှာ JSON လို format ပြောင်းပြီး အခြားတစ်ဖက်မှာ ပြန် parse လုပ်ရပါတယ်။ တတိယ၊ program နှစ်ခုကို မတူတဲ့ လူတွေက ရေးပြီး ထုတ်တာမို့ contract မှာ ရေးထားတာကိုပဲ အားကိုးလို့ရပါတယ်။

Web API ကို သုံးဖို့ client က အချက်သုံးခု သိရပါမယ်။ ဘယ်မှာ ရှိလဲ — base URL နဲ့ resource ရဲ့ path ပါ။ ဘာပို့ရမလဲ — endpoint က မျှော်လင့်တဲ့ method၊ query parameter၊ header နဲ့ body ပါ။ ဘာပြန်လာမလဲ — ပြန်နိုင်တဲ့ status code တွေနဲ့ JSON ရဲ့ ပုံစံပါ။ API documentation က ဒီမေးခွန်းတွေကိုပဲ ဖြေဖို့ ရှိတာမို့ documentation ကောင်းတဲ့ API က ထက်မြက်ပေမယ့် မှတ်တမ်းမရှိတဲ့ API ထက် အလုပ်လုပ်ရတာ အများကြီး လွယ်ပါတယ်။

Response တိုင်းမှာ အပိုင်းသုံးပိုင်း ရှိပြီး လေ့လာသူတွေက တစ်ပိုင်းတည်းကိုပဲ ကြည့်တတ်ပါတယ်။ Status code က request အောင်မြင်လားကို ပြောပါတယ် — အောင်မြင်ရင် 2xx၊ client တောင်းတာမှားရင် 4xx၊ server ကျရှုံးရင် 5xx ပါ။ Header က response ကို ဖော်ပြပါတယ်၊ ဥပမာ Content-Type: application/json က body ကို ဘယ်လို ဖတ်ရမလဲ ပြောပါတယ်။ Body က data အစစ်ကို သယ်ပါတယ်။ အဲဒီအစဉ်အတိုင်း — status၊ header၊ ပြီးမှ body — ဖတ်ရင် beginner bug အများစုကို ကာကွယ်နိုင်ပါတယ်။ နောက်ဆုံးအနေနဲ့ contract မှာ တည်ငြိမ်တဲ့ ပုံစံ လိုပါတယ်။ Server က field အသစ် ထပ်ထည့်ရင် ကိုယ်မပျက်ပေမယ့် field တစ်ခုကို နာမည်ပြောင်းတာ ဒါမှမဟုတ် ဖယ်လိုက်တာက အဲဒီ field ကို အားကိုးထားတဲ့ client တိုင်းကို ချိုးဖောက်ပါတယ်၊ ဒါကြောင့် ရင့်ကျက်တဲ့ API တွေက ပြောင်းလဲမှုတွေကို version ခွဲပါတယ် (Advanced chapter မှာ ကြည့်ပါမယ်)။

မှတ်ထားရမယ့် ပုံစံ: ဧည့်ကြိုကောင်တာ
  1. Client က ကောင်တာကို မေးတယ်
  2. ကောင်တာက request ကို စစ်တယ်
  3. နောက်ဘက် ရုံးက အလုပ်လုပ်တယ်
  4. Status နဲ့ body နဲ့ အဖြေပြန်တယ်

Client က ကောင်တာကိုပဲ မြင်ရပါတယ်၊ နောက်ဘက်ရုံးထဲမှာ ဘာဖြစ်လဲ မသိရပါဘူး။

အထွေထွေ ပုံစံ: response ရဲ့ အပိုင်းသုံးခု

javascript
const res = await fetch("https://host/path");
res.status;             // number: how it went
res.headers.get(name);  // string or null: describes the body
await res.json();       // the parsed body, when it is JSON

fetch ကို ခေါ်ပြီးရင် res.status က နံပါတ်၊ res.headers.get(name) က header တစ်ခုရဲ့ တန်ဖိုး (မရှိရင် null)၊ res.json() က body ကို parse လုပ်တဲ့ promise ပါ။ Body ကို တစ်ကြိမ်ပဲ ဖတ်လို့ရလို့ res.json() ကို နှစ်ခါ မခေါ်ပါနဲ့။

Example: ကောင်တာနဲ့ နောက်ဘက်ရုံး

ဒီ script ရဲ့ server ထဲမှာ client ဘယ်တော့မှ မမြင်ရတဲ့ field တစ်ခု (passwordHash) ပါပါတယ်။ API က user ကို ပြန်ပေးတဲ့အခါ အဲဒီ field ကို ဖယ်ထုတ်ပါတယ်။ Client က user 1 ကို ခေါ်ရင် 200၊ မရှိတဲ့ user 2 ကို ခေါ်ရင် 404 ပြန်ရပါတယ်။ တစ်ခုချင်းအတွက် status, Content-Type header နဲ့ body ကို သီးခြားစီ print ထုတ်ထားပါတယ်။

front-desk.mjs
import http from "node:http";

// The back office: data the client never sees.
const users = new Map([
  [1, { id: 1, name: "Alice", email: "alice@example.com", passwordHash: "x9f2" }],
]);

// The front desk: the API exposes only what it chooses to.
const server = http.createServer((req, res) => {
  const match = req.url.match(/^\/users\/(\d+)$/);
  const user = match && users.get(Number(match[1]));
  if (req.method === "GET" && user) {
    const { passwordHash, ...publicUser } = user;
    res.writeHead(200, { "Content-Type": "application/json" });
    res.end(JSON.stringify(publicUser));
  } else {
    res.writeHead(404, { "Content-Type": "application/json" });
    res.end(JSON.stringify({ error: "user_not_found" }));
  }
});
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
const base = `http://127.0.0.1:${server.address().port}`;

// The client sees only a request, a status, headers and a body.
for (const id of [1, 2]) {
  const res = await fetch(`${base}/users/${id}`);
  console.log(`GET /users/${id} -> ${res.status}`);
  console.log("  content-type:", res.headers.get("content-type"));
  console.log("  body:", JSON.stringify(await res.json()));
}

server.close();

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Hidden data[1, { id: 1, name: "Alice", email: "alice@example.com", passwordHash: "x9f2" }],

    Map ထဲက record မှာ passwordHash လို အတွင်းပိုင်း field တစ်ခု ပါပါတယ်။ Client က ဒီ data ကို တိုက်ရိုက် မထိနိုင်ပါဘူး၊ API ကပဲ ကြားခံပါတယ်။

  2. Route matchingconst match = req.url.match(/^\/users\/(\d+)$/);

    Regular expression က /users/ နောက်က ဂဏန်းတွေကို ဖမ်းယူပါတယ်။ ဥပမာ /users/1 ဆိုရင် match[1] က "1" ဖြစ်ပြီး /users/abc ကတော့ match မဖြစ်လို့ 404 ဖြစ်ပါတယ်။

  3. Choosing what to exposeconst { passwordHash, ...publicUser } = user;

    Destructuring နဲ့ passwordHash ကို ခွာထုတ်ပြီး ကျန်တဲ့ field တွေကို publicUser ထဲ ထည့်ပါတယ်။ ဒါကြောင့် response မှာ id, name, email ပဲ ပါပါတယ်။

  4. Request from the clientconst res = await fetch(`${base}/users/${id}`);

    Client ဘက်က URL တစ်ခုကို ခေါ်ရုံပါပဲ။ Server က Map သုံးလား၊ database သုံးလားဆိုတာ client က မသိပါဘူး၊ သိဖို့လည်း မလိုပါဘူး။

  5. Three parts, one at a timeconsole.log(" content-type:", res.headers.get("content-type"));

    Status, header နဲ့ body ကို သီးခြားစီ ဖတ်တာကို ကြည့်ပါ။ 404 မှာလည်း Content-Type က application/json ဖြစ်ပြီး body က error ဖော်ပြချက်ပါ၊ ဒါက ပုံမှန် API အဖြေတစ်မျိုးပါပဲ။

You should see
GET /users/1 -> 200
  content-type: application/json
  body: {"id":1,"name":"Alice","email":"alice@example.com"}
GET /users/2 -> 404
  content-type: application/json
  body: {"error":"user_not_found"}

Library API နဲ့ web API ယှဉ်ကြည့်ရအောင်

Node ရဲ့ crypto module က library API ရဲ့ ဥပမာ ကောင်းတစ်ခုပါ။ createHash ကို ခေါ်တာက အတူတူပဲ အလုပ်လုပ်နေတဲ့ process ထဲက function call ဖြစ်ပြီး ရလဒ်ကို ချက်ချင်း ရပါတယ်။ အောက်က table မှာ web API နဲ့ ဘာတွေ ကွာလဲ ကြည့်ပါ။

library-api.mjs
import { createHash } from "node:crypto";

// A library API: a function call inside the same process.
const digest = createHash("sha256").update("hello").digest("hex");
console.log(digest.slice(0, 12));
အပိုင်းLibrary APIWeb API
ခေါ်ပုံFunction callHTTP request
ကျရှုံးပုံကိုယ့် process ထဲမှာ exceptionNetwork ပြတ်တာ၊ timeout၊ error status
Data ပုံစံMemory ထဲက valueNetwork ပေါ်က စာသား (JSON)
ပြောင်းလဲမှုကိုယ်က dependency ကို update လုပ်မှProvider က ကိုယ့်အချိန်ဇယားနဲ့ version ထုတ်

Data က စာသားအဖြစ် ဖြတ်သွားတယ်

Network ပေါ်မှာ JavaScript object မသွားပါဘူး၊ စာသားပဲ သွားပါတယ်။ JSON.stringify က object ကို စာသားအဖြစ် ပြောင်းပြီး JSON.parse က ပြန်ပြောင်းပါတယ်။ ဒီအတွင်းမှာ JSON မှာ မရှိတဲ့ အမျိုးအစားတွေ ပျောက်ဆုံးတတ်ပါတယ်။ Date က string ဖြစ်သွားပြီး undefined value ရှိတဲ့ key တွေ ပျောက်သွားပါတယ်။

json-roundtrip.mjs
const order = {
  id: 7,
  paid: true,
  shippedAt: new Date("2025-01-15T10:00:00Z"),
  note: undefined,
};

const text = JSON.stringify(order);
console.log(text);

const back = JSON.parse(text);
console.log(typeof back.shippedAt, back.note);

ဒါကြောင့် API တစ်ခုက ရက်စွဲကို "2025-01-15T10:00:00.000Z" လို string နဲ့ ပို့တာ တွေ့ရင် ထူးဆန်းတာ မဟုတ်ပါဘူး။ Client ဘက်ကမှ new Date(value) နဲ့ ပြန်ပြောင်းရတာပါ။

လက်တွေ့ project တွေမှာ

Real project တွေမှာ API ရဲ့ အခန်းကဏ္ဍ

Company တစ်ခုမှာ website၊ iOS app၊ Android app နဲ့ admin tool တွေ ရှိတတ်ပါတယ်။ အားလုံးက backend API တစ်ခုတည်းကို ခေါ်ကြပြီး business rule တွေ (ဥပမာ ဈေးနှုန်း တွက်ပုံ) ကို နေရာတစ်ခုတည်းမှာပဲ ထိန်းထားနိုင်ပါတယ်။ Service အသေးလေးတွေအဖြစ် ခွဲထားတဲ့ system တွေမှာလည်း service တစ်ခုက တစ်ခုကို အတွင်းပိုင်း API နဲ့ ခေါ်ပါတယ်။

တခြားကုမ္ပဏီတွေရဲ့ API တွေကိုလည်း ကိုယ့်ထုတ်ကုန်ထဲ ပေါင်းထည့်ကြပါတယ် — ငွေပေးချေမှု၊ အီးမေးလ်ပို့တာ၊ မြေပုံ၊ AI model တွေပါ။ Provider အများစုက curl ဥပမာတွေနဲ့ အတူ JavaScript, Python စတဲ့ ဘာသာတွေအတွက် SDK (အဆင်သင့် ရေးထားတဲ့ client library) ကို ပေးတတ်ပါတယ်။ SDK ကလည်း နောက်ကွယ်မှာ HTTP request တွေပဲ ပို့တာမို့ request၊ status နဲ့ body ကို နားလည်ထားရင် SDK ကို ယုံကြည်စွာ သုံးနိုင်ပြီး ပြဿနာတက်ရင်လည်း ရှာနိုင်ပါတယ်။ Documentation ကောင်းတာက developer experience ကို တိုက်ရိုက် မြှင့်တင်ပေးလို့ API ရွေးတဲ့အခါ documentation ရဲ့ အရည်အသွေးကိုပါ ထည့်စဉ်းစားပါ။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. Network call ကို ရိုးရိုး function call လို သဘောထားတာ

Local function က ချက်ချင်း ပြီးပြီး အောင်မြင်တာ သေချာပေမယ့် web API call က နှေးနိုင်တယ်၊ ပြတ်နိုင်တယ်၊ error status ပြန်နိုင်တယ်။ Timeout မသတ်ဘဲ၊ failure branch မရေးဘဲ ခေါ်ထားရင် network တစ်ခါ ပြတ်တာနဲ့ app တစ်ခုလုံး ရပ်သွားနိုင်ပါတယ်။

2. Documentation ထဲက happy-path ဥပမာကိုပဲ ကူးတာ

Docs ရဲ့ ပထမဆုံး ဥပမာက အောင်မြင်တဲ့ အခြေအနေကိုပဲ ပြတတ်ပါတယ်။ Error response အပိုင်း (ဘယ် status တွေ ပြန်နိုင်လဲ၊ error body ပုံစံ) ကိုပါ ဖတ်ပါ၊ မဟုတ်ရင် 401, 404, 429 တွေ ပထမဆုံး ကြုံတဲ့နေ့ production မှာ အံ့အားသင့်ရပါမယ်။

3. မှတ်တမ်းမတင်ထားတဲ့ field ကို အားကိုးတာ

Response ထဲမှာ တွေ့ရတာတိုင်းက contract မဟုတ်ပါဘူး။ Docs မှာ မပါတဲ့ field ကို သုံးထားရင် provider က နောက်တစ်ခါ ပြောင်းတဲ့အခါ သတိပေးချက်မရှိဘဲ ပျက်ပါလိမ့်မယ်။ မှတ်တမ်းတင်ထားတဲ့ field တွေကိုပဲ သုံးပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • API အသစ်တစ်ခုကို code မရေးခင် curl နဲ့ တစ်ကြိမ် ခေါ်ကြည့်ပြီး response အစစ်ကို မျက်စိနဲ့ မြင်ပါ။
  • Base URL ကို constant တစ်ခုထဲမှာ ထားပါ၊ ပြောင်းရင် (staging၊ production) နေရာတစ်ခုတည်း ပြင်ရုံပါပဲ။
  • Response ကို ကိုယ့်ကိုယ်ပိုင် function တစ်ခုထဲမှာ ပိတ်ထားပါ၊ ကျန် code က provider ရဲ့ JSON ပုံစံကို တိုက်ရိုက် မသိစေနဲ့။

Mini challenge

Mini challenge: public view ပါတဲ့ product API

ပစ္စည်းတစ်ခုချင်းမှာ ပြင်ပကို မပြချင်တဲ့ costPrice ပါတဲ့ mock server တစ်ခု ရေးပါ။ API က id, name, price ကိုပဲ ပြန်ပေးပြီး client function getProduct(base, id) က ပစ္စည်းကို ပြန်ပေးရမယ်၊ 404 ဆိုရင် null ပြန်ရမယ်။

  • ပစ္စည်းနှစ်ခုကို Map ထဲ သိမ်းပြီး တစ်ခုစီမှာ id, name, price, costPrice ပါရမယ်
  • GET /products/<id> က id, name, price ကိုပဲ 200 နဲ့ ပြန်ပေးရမယ်၊ မရှိရင် 404 နဲ့ { error: "product_not_found" } ပြန်ရမယ်
  • getProduct က 200 မှာ parsed object ပြန်ပေးပြီး 404 မှာ null ပြန်ပေးရမယ်၊ တခြား status တွေမှာ Error ပစ်ရမယ်
  • ပစ္စည်း 1 နဲ့ မရှိတဲ့ ပစ္စည်း 9 ကို print ထုတ်ပြီး server ကို ပိတ်ရမယ်
products-api.mjs
import http from "node:http";

const products = new Map([
  // TODO: two products with id, name, price and an internal costPrice
]);

const server = http.createServer((req, res) => {
  // TODO: match GET /products/<id>
  // TODO: 200 with only id, name and price, or 404 with { error: "product_not_found" }
});

async function getProduct(base, id) {
  // TODO: fetch the product, return the parsed object for 200 and null for 404
}

// TODO: start the server on port 0, print products 1 and 9, close the server

ရလဒ်: Output က ဒီလို ဖြစ်ရပါမယ်: {"id":1,"name":"Tea","price":1200} null

အရိပ်အမြွက် ကြည့်ရန်

costPrice ကို ဖယ်ဖို့ const { costPrice, ...publicProduct } = product ကို သုံးပါ။ getProduct ထဲမှာ res.status === 404 ကို အရင်စစ်ပြီး ပြီးမှ res.ok ကို စစ်ပါ။

အဖြေ ကြည့်ရန်
products-api.mjs
import http from "node:http";

const products = new Map([
  [1, { id: 1, name: "Tea", price: 1200, costPrice: 700 }],
  [2, { id: 2, name: "Rice", price: 3400, costPrice: 2900 }],
]);

const server = http.createServer((req, res) => {
  const match = req.url.match(/^\/products\/(\d+)$/);
  const product = match && products.get(Number(match[1]));
  res.setHeader("Content-Type", "application/json");
  if (req.method === "GET" && product) {
    const { costPrice, ...publicProduct } = product;
    res.writeHead(200);
    res.end(JSON.stringify(publicProduct));
  } else {
    res.writeHead(404);
    res.end(JSON.stringify({ error: "product_not_found" }));
  }
});

async function getProduct(base, id) {
  const res = await fetch(base + "/products/" + id);
  if (res.status === 404) return null;
  if (!res.ok) throw new Error("HTTP " + res.status);
  return res.json();
}

await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
const base = "http://127.0.0.1:" + server.address().port;
console.log(JSON.stringify(await getProduct(base, 1)));
console.log(JSON.stringify(await getProduct(base, 9)));
server.close();

Quick Quiz 1 / 3

Client နဲ့ database ကြားမှာ API ထည့်ထားရင် အဓိက အကျိုးကျေးဇူးက ဘာလဲ။

Quick Quiz 2 / 3

JSON.parse(JSON.stringify({ n: 1, d: new Date(0), u: undefined })) ရဲ့ ရလဒ်ထဲမှာ d နဲ့ u က ဘာဖြစ်သွားမလဲ။

Quick Quiz 3 / 3

ရိုးရိုး function call နဲ့ ယှဉ်ရင် web API call မှာ ပိုပါလာတာက ဘာလဲ။

မှတ်ထားရမယ့် အချက်များ

  • API ဆိုတာ implementation ကို မဖတ်ဘဲ သုံးလို့ရတဲ့ interface ပါ။ Web API ကတော့ အဲဒါကို HTTP request နဲ့ response အဖြစ် network ပေါ်မှာ ဆန့်ထုတ်ထားတာပါ။
  • Client က ဘယ်မှာရှိလဲ၊ ဘာပို့ရမလဲ၊ ဘာပြန်လာမလဲ ဆိုတဲ့ အချက်သုံးခုကို သိရပြီး response ကို status၊ header၊ body အစဉ်လိုက် ဖတ်ရပါမယ်။
  • Data က network ပေါ်မှာ စာသား (JSON) အဖြစ် သွားလို့ Date လို type တွေ string ဖြစ်သွားပြီး undefined key တွေ ပျောက်သွားပါတယ်။
  • Network call တွေက ကိုယ်မထိန်းနိုင်တဲ့ အကြောင်းရင်းတွေကြောင့် ကျရှုံးနိုင်လို့ error handling ကို ကြိုရေးထားရပါမယ်။

RFC 9110 — HTTP Semantics — IETF

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

API ဆိုတာဘာလဲ? | Thuta Learning