Thuta Learning
Angular
AdvancedWeb Developmentအလယ်အလတ်

HTTP Interceptors

Angularသင်ခန်းစာ 3925 မိနစ်

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

  • HTTP Interceptors ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
  • နမူနာ UI ကို run၊ state/output ခန့်မှန်းပြီး edge/error cases စမ်းနိုင်ရန်
  • လက်တွေ့ project တွင် typed safe pattern နဲ့ common pitfalls ကိုခွဲသိနိုင်ရန်

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

HTTP Interceptors ကို ရိုးရိုးလေးနားလည်မယ်

HTTP Interceptors များသည် HTTP requests/responses များကို intercept လုပ်ပြီး modify, log, သို့မဟုတ် handle errors လုပ်နိုင်သည်။

Problem → Why → Solution

ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ

Interceptor မှာ request body/token အပြည့် log လုပ်ခြင်းက sensitive data ပေါက်ကြားစေနိုင်ပါတယ်။

ဖြစ်လေ့ရှိတဲ့အမှား

Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။

Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်

HTTP interceptor က requests/responses ကိုcross-cutting pipeline အဖြစ်ပြောင်းရန်သုံးပြီး auth header, correlation ID, logging သို့ centralized error normalization အတွက်သင့်တော်ပါတယ်။ Requests က immutable ဖြစ်လို့ clone လုပ်ရပြီး interceptor order နဲ့ retry behavior ကအရေးကြီးပါတယ်။ Token refresh တွင် concurrent failures, loop prevention နဲ့ excluded endpoints ကိုတိတိကျကျကိုင်ပါ။

Mental Model
  1. Input + state
  2. Component + template
  3. Rendered Angular UI

HTTP Interceptors ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

typescript
// auth.interceptor.ts
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler } from '@angular/common/http';

@Injectable()
export class AuthInterceptor implements HttpInterceptor {
  
  intercept(req: HttpRequest<any>, next: HttpHandler) {
    // Add auth token to request
    const token = localStorage.getItem('auth_token');
    
    if (token) {
      req = req.clone({
        setHeaders: {
          Authorization: `Bearer ${token}`
        }
      });
    }
    
    return next.handle(req);
  }
}

// logging.interceptor.ts
export class LoggingInterceptor implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler) {
    console.log('Request:', req.url);
    const started = Date.now();
    
    return next.handle(req).pipe(
      tap(() => {
        const elapsed = Date.now() - started;
        console.log(`Response took ${elapsed}ms`);
      })
    );
  }
}

// app.module.ts
@NgModule({
  providers: [
    { provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true },
    { provide: HTTP_INTERCEPTORS, useClass: LoggingInterceptor, multi: true }
  ]
})

HTTP interceptor က requests/responses ကိုcross-cutting pipeline အဖြစ်ပြောင်းရန်သုံးပြီး auth header, correlation ID, logging သို့ centralized error normalization အတွက်သင့်တော်ပါတယ်။ Requests က immutable ဖြစ်လို့ clone လုပ်ရပြီး interceptor order နဲ့ retry behavior ကအရေးကြီးပါတယ်။ Token refresh တွင် concurrent failures, loop prevention နဲ့ excluded endpoints ကိုတိတိကျကျကိုင်ပါ။

Code ကို အဆင့်လိုက်ဖတ်မယ်

  1. Input + state// auth.interceptor.ts

    HTTP Interceptors များသည် HTTP requests/responses များကို intercept လုပ်ပြီး modify, log, သို့မဟုတ် handle errors လုပ်နိုင်သည်။

  2. Component + templateexport class LoggingInterceptor implements HttpInterc...

    HTTP interceptor က requests/responses ကိုcross-cutting pipeline အဖြစ်ပြောင်းရန်သုံးပြီး auth header, correlation ID, logging သို့ centralized error normalization အတွက်သင့်တော်ပါတယ်။ Requests က immutable ဖြစ်လို့ clone လုပ်ရပြီး interceptor order နဲ့ retry behavior ကအရေးကြီးပါတယ်။ Token refresh တွင် concurrent failures, loop prevention နဲ့ excluded endpoints ကိုတိတိကျကျကိုင်ပါ။

  3. Rendered Angular UI})

    Auth header + timing interceptor နှစ်ခုရေးပြီး public endpoint exclusion, 401, retry, concurrent requests နဲ့ sensitive log redaction ကိုtest လုပ်ပါ။

Execution Flow
  1. Input + state
  2. Component + template
  3. Rendered Angular UI

Success

HTTP Interceptors ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

Failure

Interceptor မှာ request body/token အပြည့် log လုပ်ခြင်းက sensitive data ပေါက်ကြားစေနိုင်ပါတယ်။

HTTP Interceptors များသည် HTTP requests/responses များကို intercept လုပ်ပြီး modify, log, သို့မဟုတ် handle errors လုပ်နိုင်သည်။

typescript
// auth.interceptor.ts
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler } from '@angular/common/http';

@Injectable()
export class AuthInterceptor implements HttpInterceptor {
  
  intercept(req: HttpRequest<any>, next: HttpHandler) {
    // Add auth token to request
    const token = localStorage.getItem('auth_token');
    
    if (token) {
      req = req.clone({
        setHeaders: {
          Authorization: `Bearer ${token}`
        }
      });
    }
    
    return next.handle(req);
  }
}

// logging.interceptor.ts
export class LoggingInterceptor implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler) {
    console.log('Request:', req.url);
    const started = Date.now();
    
    return next.handle(req).pipe(
      tap(() => {
        const elapsed = Date.now() - started;
        console.log(`Response took ${elapsed}ms`);
      })
    );
  }
}

// app.module.ts
@NgModule({
  providers: [
    { provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true },
    { provide: HTTP_INTERCEPTORS, useClass: LoggingInterceptor, multi: true }
  ]
})
You should see
(All HTTP requests များသည် interceptors များမှ pass ဖြစ်ပြီး token auto-add, logging စသည်ဖြင့် handle လုပ်မည်)

Real-world usage

လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ

Auth header + timing interceptor နှစ်ခုရေးပြီး public endpoint exclusion, 401, retry, concurrent requests နဲ့ sensitive log redaction ကိုtest လုပ်ပါ။

Mini challenge

ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ

HTTP Interceptors နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
typescript
// auth.interceptor.ts
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler } from '@angular/common/http';

@Injectable()
export class AuthInterceptor implements HttpInterceptor {
  
  intercept(req: HttpRequest<any>, next: HttpHandler) {
    // Add auth token to request
    const token = localStorage.getItem('auth_token');
    
    if (token) {
      req = req.clone({
        setHeaders: {
          Authorization: `Bearer ${token}`
        }
      });
    }
    
    return next.handle(req);
  }
}

// logging.interceptor.ts
export class LoggingInterceptor implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler) {
    console.log('Request:', req.url);
    const started = Date.now();
    
    return next.handle(req).pipe(
      tap(() => {
        const elapsed = Date.now() - started;
        console.log(`Response took ${elapsed}ms`);
      })
    );
  }
}

// app.module.ts
@NgModule({
  providers: [
    { provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true },
    { provide: HTTP_INTERCEPTORS, useClass: LoggingInterceptor, multi: true }
  ]
})

ရလဒ်: HTTP Interceptors ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

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

Auth header + timing interceptor နှစ်ခုရေးပြီး public endpoint exclusion, 401, retry, concurrent requests နဲ့ sensitive log redaction ကိုtest လုပ်ပါ။

Quick Quiz 1 / 3

HTTP Interceptors ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

ဘယ်အမှားကို အရင်ဆုံးရှောင်သင့်လဲ။

Quick Quiz 3 / 3

Real project မှာ ဘယ်လိုအသုံးချသင့်လဲ။

ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ

  • Input + state → Component + template → Rendered Angular UI
  • HTTP interceptor က requests/responses ကိုcross-cutting pipeline အဖြစ်ပြောင်းရန်သုံးပြီး auth header, correlation ID, logging သို့ centralized error normalization အတွက်သင့်တော်ပါတယ်။ Requests က immutable ဖြစ်လို့ clone လုပ်ရပြီး interceptor order နဲ့ retry behavior ကအရေးကြီးပါတယ်။ Token refresh တွင် concurrent failures, loop prevention နဲ့ excluded endpoints ကိုတိတိကျကျကိုင်ပါ။
  • Interceptor မှာ request body/token အပြည့် log လုပ်ခြင်းက sensitive data ပေါက်ကြားစေနိုင်ပါတယ်။
  • Auth header + timing interceptor နှစ်ခုရေးပြီး public endpoint exclusion, 401, retry, concurrent requests နဲ့ sensitive log redaction ကိုtest လုပ်ပါ။

Angular — HTTP Client — Angular

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Interceptor မှာ request body/token အပြည့် log လုပ်ခြင်းက sensitive data ပေါက်ကြားစေနိုင်ပါတယ်။
  • Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။

လက်တွေ့လေ့ကျင့်ခန်း

HTTP Interceptors နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: (All HTTP requests များသည် interceptors များမှ pass ဖြစ်ပြီး token auto-add, logging စသည်ဖြင့် handle လုပ်မည်)

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

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

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

HTTP Interceptors | Thuta Learning