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.
- 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.
- 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.
To set up the project locally, follow these steps:
- Clone the Repository:
git clone https://github.com/samestrin/url-shortening-api-netlify-edge-supabase.git cd url-shortening-api-netlify-edge-supabase- Install Dependencies: Ensure you have the required dependencies installed. Use npm or yarn to install any necessary packages.
npm install- Set Up Netlify CLI: Install the Netlify CLI to deploy and test the functions locally.
npm install -g netlify-cli- Run the Functions Locally: Use the Netlify CLI to run the edge functions locally.
netlify devThe 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" Endpoint: /shorten Method: POST
Shorten a long URL and return the shortened URL.
url: The URL to be shortened.
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"}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.
Use curl to make a request:
curl http://localhost/[shortId]Endpoint: /latest Method: GET
Retrieve the latest URLs shortened.
This endpoint is accessed by navigating directly to /latest.
Use curl to make a request:
curl http://localhost/latestEndpoint: /count Method: GET
Retrieve the number of URLs shortened.
This endpoint is accessed by navigating directly to /count.
Use curl to make a request:
curl http://localhost/countEndpoint: /version Method: GET
Retrieve the current version of the API.
This endpoint is accessed by navigating directly to /version.
Use curl to make a request:
curl http://localhost/versionThe 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" }The server responds with appropriate CORS headers such as Access-Control-Allow-Origin.
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.
Contributions to this project are welcome. Please fork the repository and submit a pull request with your changes or improvements.
This project is licensed under the MIT License - see the LICENSE file for details.