Skip to content

samestrin/url-shortening-api-netlify-edge-supabase

Repository files navigation

url-shortening-api-netlify-edge-supabase

Star on GitHubFork on GitHubWatch on GitHub

Version 0.0.1License: MITBuilt with Node.js

url-shortening-api-netlify-edge-supabase is a URL shortener service. It is a serverless application that provides URL shortening and retrieval functionalities. Utilizing Netlify Edge Functions and Supabase, a cloud-based database, the application offers a high performance, efficient and scalable solution for creating short URLs that redirect to the original, longer URLs.

This replaces the legacy url-shortening-api-netlify-supabase project.

Features

  • URL Shortening: Convert long URLs into short, manageable links that are easier to share.
  • URL Validation: Ensures that only valid URLs with proper protocols are processed.
  • URL Redirection: Redirect users to the original long URL based on the short URL.
  • Link Tracking: Track which short URLs are served.
  • Retrieve Latest Shortened URLs: Access the most recently created short URLs.
  • URL Count: Get the total number of URLs shortened.
  • API Versioning: Retrieve the current version of the API.
  • CORS Support: Handle cross-origin resource sharing with appropriate headers.
  • Error Handling: Graceful error handling with appropriate HTTP status codes and error messages.

Dependencies

  • Node.js: The script runs in a Node.js environment.
  • Deno: A secure runtime for JavaScript and TypeScript.
  • Supabase (REST API): A backend-as-a-service providing a Postgres database, authentication, storage, and more.
  • Netlify Edge Functions: Serverless functions that run at the edge, closer to your users.
  • multiParser: Parses multipart form data.

Installation

To set up the project locally, follow these steps:

  1. Clone the Repository:
git clone https://github.com/samestrin/url-shortening-api-netlify-edge-supabase.git cd url-shortening-api-netlify-edge-supabase
  1. Install Dependencies: Ensure you have the required dependencies installed. Use npm or yarn to install any necessary packages.
npm install
  1. Set Up Netlify CLI: Install the Netlify CLI to deploy and test the functions locally.
npm install -g netlify-cli
  1. Run the Functions Locally: Use the Netlify CLI to run the edge functions locally.
netlify dev

Configuration

The netlify.toml file contains the configuration for the edge functions. Each function is mapped to a specific endpoint:

[build] publish = "public" [build.environment] NODE_VERSION = "20" [[edge_functions]] path = "/shorten" function = "shorten" [[edge_functions]] path = "/count" function = "count" [[edge_functions]] path = "/latest" function = "latest" [[edge_functions]] path = "/version" function = "version" [[edge_functions]] path = "/*" function = "redirect" 

Endpoints

Shorten URL

Endpoint: /shorten Method: POST

Shorten a long URL and return the shortened URL.

  • url: The URL to be shortened.

Example Usage

Use a tool like Postman or curl to make a request:

curl -X POST \ https://localhost/shorten \ -H 'Content-Type: application/x-www-form-urlencoded' \ --data-urlencode 'url=https://www.google.com'

The server responds with:

{"shortUrl":"lqywv6P"}

Forward URL

Endpoint: /[shortId] Method: GET

Based on shortened URL, e.g. /lqywv6P HTTP 301 forward to a long url.

This endpoint is accessed by navigating directly to the shortened URL.

Example Usage

Use curl to make a request:

curl http://localhost/[shortId]

Retrieve Latest Shortened Links

Endpoint: /latest Method: GET

Retrieve the latest URLs shortened.

This endpoint is accessed by navigating directly to /latest.

Example Usage

Use curl to make a request:

curl http://localhost/latest

Retrieve Count

Endpoint: /count Method: GET

Retrieve the number of URLs shortened.

This endpoint is accessed by navigating directly to /count.

Example Usage

Use curl to make a request:

curl http://localhost/count

Retrieve Version

Endpoint: /version Method: GET

Retrieve the current version of the API.

This endpoint is accessed by navigating directly to /version.

Example Usage

Use curl to make a request:

curl http://localhost/version

The server responds with:

{ "name": "url-shortening-api-netlify-edge-supabase", "version": "0.0.1", "description": "url-shortening-api-netlify-edge-supabase is a URL shortener service using Netlify Edge Functions and the Supabase REST API.", "author": "Sam Estrin", "homepage": "https://github.com/samestrin/url-shortening-api-netlify-edge-supabase#readme" }

CORS

The server responds with appropriate CORS headers such as Access-Control-Allow-Origin.

Error Handling

The API handles errors gracefully and returns appropriate error responses:

  • 400 Bad Request: Invalid request parameters.
  • 404 Not Found: Resource not found.
  • 405 Method Not Allowed: Invalid request method (not GET or POST).
  • 500 Internal Server Error: Unexpected server error.

Contribute

Contributions to this project are welcome. Please fork the repository and submit a pull request with your changes or improvements.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Share

Twitter Facebook LinkedIn

About

A TypeScript Netlify Edge Function/Supabase based URL shortener REST API. (This was the original API for https://frwrd.ing/)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •