/netlify-cli-template

A robust and adaptable Netlify CLI template for building serverless functions and static sites, featuring TypeScript, Yarn, and a modular structure.

Primary LanguageHTMLMIT LicenseMIT

Netlify CLI Template ๐Ÿš€

A robust and adaptable template for building serverless applications and static sites with Netlify CLI. This project offers a modern setup with TypeScript, multiple serverless functions, and a customizable static frontendโ€”perfect for blogs, APIs, or dynamic web apps! โœจ

License: MIT Netlify Status

โœจ Features

  • Serverless Functions: Four TypeScript-based functions (hello, time, echo, neon) with robust error handling and CORS support. ๐Ÿ› ๏ธ
  • Static Site: A simple, customizable HTML frontend in the public directory, ready for frameworks like React or Vue. ๐ŸŒ
  • Netlify CLI Integration: Streamlined local development and deployment with netlify dev. โš™๏ธ
  • TypeScript Support: Type-safe functions using AWS Lambda types for reliability. ๐Ÿ“
  • Yarn Workflow: Optimized package management with Yarn. ๐Ÿ“ฆ
  • Modular Design: Easily extend with new functions, frontends, or integrations. ๐Ÿ”ง
  • Neon-Ready: Prepared for Neon Postgres integration with the neon function. ๐Ÿ—„๏ธ

๐Ÿ“‚ Project Structure

netlify-cli-template/
โ”œโ”€โ”€ netlify/
โ”‚   โ””โ”€โ”€ functions/
โ”‚       โ”œโ”€โ”€ hello.ts       # Greets users with query params
โ”‚       โ”œโ”€โ”€ time.ts        # Returns current server time
โ”‚       โ”œโ”€โ”€ echo.ts        # Echoes POST request body
โ”‚       โ””โ”€โ”€ neon.ts        # Placeholder for Neon Postgres integration
โ”œโ”€โ”€ public/
โ”‚   โ””โ”€โ”€ index.html         # Static site entry point
โ”œโ”€โ”€ netlify.toml           # Netlify configuration
โ”œโ”€โ”€ package.json           # Dependencies and scripts
โ”œโ”€โ”€ tsconfig.json          # TypeScript configuration
โ””โ”€โ”€ README.md              # Project documentation

๐Ÿ› ๏ธ Prerequisites

  • Node.js: Version 14 or higher. ๐ŸŸข
  • Yarn: Install with npm install -g yarn. ๐Ÿ“ฆ
  • Netlify CLI: Install globally with yarn global add netlify-cli or locally via yarn add --dev netlify-cli. ๐ŸŒ
  • Git: For version control and Netlify deployment. ๐Ÿ“š

๐Ÿš€ Installation

  1. Clone the Repository:

    git clone https://github.com/lizzyman04/netlify-cli-template.git
    cd netlify-cli-template
  2. Install Dependencies:

    yarn install
  3. Link to Netlify:

    yarn netlify init

    Follow the prompts to connect to your Netlify site or create a new one.

๐ŸŽฎ Usage

  • Local Development:

    yarn start

    Starts a local server at http://localhost:8888. Functions are accessible at http://localhost:8888/.netlify/functions/.

  • Test Functions:

    • hello: GET /api/hello?name=YourName
      Example: Returns Hello, YourName!
    • time: GET /api/time
      Example: Returns current UTC time.
    • echo: POST /api/echo with JSON body
      Example: Echoes back the payload.
    • neon: GET /extensions/neon
      Example: Placeholder for Neon integration.

    Example curl for echo:

    curl -X POST http://localhost:8888/api/echo -d '{"test": "Hello"}' -H "Content-Type: application/json"

    Example curl for neon:

    curl http://localhost:8888/extensions/neon
  • Deploy to Netlify:

    yarn deploy

    Or push to GitHub for continuous deployment. ๐Ÿš€

  • Type Checking:

    yarn check-types

๐ŸŽจ Customization

  • Add Functions: Create new .ts files in netlify/functions/. For custom folders (e.g., netlify/extensions/), configure netlify.toml or use a build script. ๐Ÿ› ๏ธ
  • Frontend: Swap public/ with frameworks like React, Vue.js, or Astro. ๐ŸŒ
  • Environment Variables: Add to .env or Netlifyโ€™s UI (e.g., NETLIFY_TOKEN, DATABASE_URL for Neon). ๐Ÿ”‘
  • Build Process: Modify yarn build for static site generation. ๐Ÿ—๏ธ
  • Redirects: Update netlify.toml for custom routing rules. ๐Ÿ›ค๏ธ

๐Ÿ”ง Available Functions

  • hello: Greets users with a name query param (e.g., /api/hello?name=Arlindo). ๐Ÿ‘‹
  • time: Returns current UTC time in multiple formats (e.g., /api/time). โฐ
  • echo: Echoes POST request body (e.g., POST /api/echo). ๐Ÿ“ข
  • neon: Placeholder for Neon Postgres integration (e.g., GET /extensions/neon). ๐Ÿ—„๏ธ

๐Ÿ—„๏ธ Neon Integration

The neon function is ready for Neon Postgres integration. To connect:

  1. Install the pg package: yarn add pg.
  2. Set DATABASE_URL in .env or Netlifyโ€™s UI with your Neon connection string.
  3. Update neon.ts with database logic (see commented example in the function).
  4. Test queries locally and deploy. ๐Ÿš€

๐Ÿค Contributing

  1. Fork the repository. ๐Ÿด
  2. Create a feature branch (git checkout -b feature/your-feature). ๐ŸŒฟ
  3. Commit changes (git commit -m 'Add your feature'). ๐Ÿ“
  4. Push to the branch (git push origin feature/your-feature). ๐Ÿš€
  5. Open a Pull Request. ๐Ÿ™Œ

๐Ÿ“œ License

MIT License. See LICENSE for details. ๐Ÿ“„

๐Ÿ‘ค Author

  • Arlindo Abdul
  • GitHub: Lizzyman ๐Ÿง‘โ€๐Ÿ’ป

๐Ÿ“š Resources


Built with โค๏ธ using Netlify CLI and Yarn. Happy coding! ๐ŸŽ‰