Skip to content

Latest commit

ย 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ“ Bangalore Pincode Explorer

A full-stack, responsive web application built with Next.js 14 (App Router), TypeScript, Tailwind CSS, and MongoDB with Prisma ORM. Powered by India Post's official live public API (api.postalpincode.in) paired with a cache-first database strategy.

Live Demo Tech Stack Live API Database Cache Testing


๐ŸŒ Live Demo & Preview


๐Ÿ›๏ธ Architecture & Cache-First Strategy

The application uses a Cache-First, API-Fallback architecture:

[ User Search Request ]
          โ”‚
          โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      Cache Hit (< 30 days)
โ”‚  Next.js API     โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ [ Return Cached Response ] (HTTP 200, X-Cache: HIT)
โ”‚  Proxy Route     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ”‚ Cache Miss / Expired
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  India Post Live Public REST API      โ”‚
โ”‚  (https://api.postalpincode.in)       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ”œโ”€โ”€โ–บ Filter to Bangalore/Karnataka
         โ”‚
         โ”œโ”€โ”€โ–บ Asynchronously Write to MongoDB (PincodeCache Table)
         โ”‚
         โ–ผ
[ Return Fresh Response ] (HTTP 200, X-Cache: MISS)

Why Cache-First?

  1. Performance: Reduces round-trip latency from ~500ms (live API call) down to ~15ms for cached lookups.
  2. Reliability & Rate-Limiting Protection: Protects India Post's free public endpoint from being overwhelmed during heavy traffic spikes while keeping the app online even if the external service experiences downtime.

๐Ÿ› ๏ธ Tech Stack

  • Framework: Next.js 14 (App Router) with TypeScript
  • Live External API: India Post Public API (https://api.postalpincode.in) โ€” No API Key Required
  • Database Caching Layer: MongoDB with Prisma ORM (PincodeCache model)
  • Styling: Tailwind CSS with sleek dark/light design system
  • State Management: React useState & useCallback with 300ms debouncing
  • Icons: Lucide React
  • Testing:
    • Jest (ts-jest) with global.fetch mocks for unit testing API routes & caching paths.
    • Cypress for End-to-End (E2E) search flow & error handling validation.

โœจ Features List

  1. Live India Post Integration:
    • Pincode Lookup: GET /api/pincodes/[code] -> fetches https://api.postalpincode.in/pincode/{code}.
    • Area Reverse Lookup: GET /api/pincodes/search?area=koramangala -> fetches https://api.postalpincode.in/postoffice/{area}.
  2. First-Load Cached Preview:
    • GET /api/pincodes/cached populates the homepage on initial load so the interface is immediately populated with popular/recently searched Bangalore postal codes before the user types.
  3. Smart Filter:
    • Automatically filters all raw India Post responses to ensure only authentic Bangalore / Bengaluru / Karnataka entries are returned.
  4. Distinct Error States:
    • 400 Bad Request: Invalid pincode format (rejected before calling external API).
    • 404 Not Found: No matching post office or area found.
    • 503 Service Unavailable: India Post API timeout or service downtime.
  5. Modern Accessible UI:
    • Mobile-first layout (375px+ responsive).
    • Switchable Cards grid and Table view.
    • Skeleton loading states while network calls resolve.
    • Copy pincode to clipboard with toast feedback.
    • ARIA live region (aria-live="polite") announcing result counts for screen readers.

๐Ÿš€ Local Setup Instructions

1. Clone Repository & Install Dependencies

git clone https://github.com/ayshrosine/Pin_Code.git
cd Pin_Code
npm install

2. Configure Environment Variables

Create a .env file in the root directory:

# MongoDB Connection String
DATABASE_URL="mongodb://localhost:27017/pincode_db"

3. Generate Prisma Client

npx prisma generate

4. Run Development Server

npm run dev

Open http://localhost:3000 in your browser.


๐Ÿ“ก API Documentation

1. GET /api/pincodes/[code]

Fetches pincode details. Checks MongoDB cache first; if cache miss, queries India Post live API and caches result.

  • Parameters: code (6-digit numeric string)

  • Header Response: X-Cache: HIT or X-Cache: MISS

  • Example: GET /api/pincodes/560034

  • Success Response (200 OK):

    {
      "data": [
        {
          "id": "66be18a123f1a23456789034",
          "code": "560034",
          "areaName": "Koramangala",
          "district": "Bangalore",
          "state": "Karnataka"
        }
      ],
      "count": 1,
      "source": "cache",
      "query": "560034"
    }
  • Invalid Format Error (400 Bad Request):

    {
      "error": "Invalid pincode format. Must be a 6-digit numeric string."
    }
  • Not Found Error (404 Not Found):

    {
      "error": "Pincode '560099' not found."
    }
  • Service Error (503 Service Unavailable):

    {
      "error": "Service temporarily unavailable, please try again."
    }

2. GET /api/pincodes/search

Searches post offices by area name.

  • Query Parameters: area (string, e.g., Koramangala, Whitefield)
  • Example: GET /api/pincodes/search?area=Koramangala
  • Success Response (200 OK):
    {
      "data": [
        {
          "code": "560034",
          "areaName": "Koramangala",
          "district": "Bangalore",
          "state": "Karnataka"
        }
      ],
      "count": 1,
      "source": "live-api",
      "query": "Koramangala"
    }

3. GET /api/pincodes/cached

Returns all currently cached Bangalore pincodes for instant first-load presentation.

  • Example: GET /api/pincodes/cached
  • Success Response (200 OK):
    {
      "data": [...],
      "count": 16,
      "message": "Recently searched & cached pincodes retrieved successfully."
    }

๐Ÿงช Running Tests

1. Unit Tests (Jest)

Run unit tests verifying format validation, fetch mocking, 404 mapping, and cache-hit bypass:

npm test

2. End-to-End Tests (Cypress)

Start dev server (npm run dev) and run Cypress:

npx cypress run

๐Ÿ“‚ Project Structure

bangalore-pincode-explorer/
โ”œโ”€โ”€ app/
โ”‚   โ”œโ”€โ”€ api/
โ”‚   โ”‚   โ””โ”€โ”€ pincodes/
โ”‚   โ”‚       โ”œโ”€โ”€ [code]/
โ”‚   โ”‚       โ”‚   โ””โ”€โ”€ route.ts         # GET /api/pincodes/[code] (cache-first + live API)
โ”‚   โ”‚       โ”œโ”€โ”€ search/
โ”‚   โ”‚       โ”‚   โ””โ”€โ”€ route.ts         # GET /api/pincodes/search?area=... (area lookup)
โ”‚   โ”‚       โ””โ”€โ”€ cached/
โ”‚   โ”‚           โ””โ”€โ”€ route.ts         # GET /api/pincodes/cached (initial feed)
โ”‚   โ”œโ”€โ”€ globals.css                  # Tailwind styles
โ”‚   โ”œโ”€โ”€ layout.tsx                   # Metadata & Root layout
โ”‚   โ””โ”€โ”€ page.tsx                     # Main interactive SPA
โ”œโ”€โ”€ components/
โ”‚   โ”œโ”€โ”€ SearchBar.tsx                # Debounced search bar with mode toggle
โ”‚   โ”œโ”€โ”€ ResultCard.tsx               # Responsive card / table row
โ”‚   โ””โ”€โ”€ ErrorState.tsx               # Distinct 400, 404, 503 error UI
โ”œโ”€โ”€ lib/
โ”‚   โ”œโ”€โ”€ db.ts                        # MongoDB cache functions & fallback data
โ”‚   โ”œโ”€โ”€ postalApi.ts                 # Live India Post API wrapper & timeout filter
โ”‚   โ””โ”€โ”€ prisma.ts                    # Prisma client singleton
โ”œโ”€โ”€ prisma/
โ”‚   โ””โ”€โ”€ schema.prisma                # PincodeCache MongoDB model
โ”œโ”€โ”€ __tests__/
โ”‚   โ””โ”€โ”€ pincode-api.test.ts          # Jest test suite (fetch mocks & cache tests)
โ”œโ”€โ”€ cypress/
โ”‚   โ””โ”€โ”€ e2e/
โ”‚       โ””โ”€โ”€ search.cy.ts             # Cypress E2E search flow & error tests
โ”œโ”€โ”€ cypress.config.ts                # Cypress configuration
โ”œโ”€โ”€ jest.config.js                   # Jest configuration
โ””โ”€โ”€ README.md                        # Project documentation

๐Ÿ”ฎ Known Limitations

  1. India Post Rate Limits: India Post does not publish formal API rate limit documentation. The caching layer actively mitigates rate-limit risks by serving repeat queries directly from MongoDB.
  2. District Matching: India Post data sometimes uses "Bangalore", "Bengaluru", or "BANGALORE URBAN". The filterBangaloreOffices helper normalizes these variations.

About

Full-stack Bangalore Pincode Explorer built with Next.js, TypeScript, and Prisma. Search pincodes or area names via India Post's live public API, with a caching layer for faster repeat lookups. Includes Jest + Cypress tests and a documented REST API.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages