npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rayou/cdk-url-shortener

v0.1.2

Published

Deploy a URL shortener with custom domain support in just a few lines of code.

Downloads

2

Readme

cdk-url-shortener

Release npm PyPI Maven Central Nuget

Deploy a URL shortener with custom domain support in just a few lines of code.

cdk-url-shortener is an AWS CDK L3 construct that will create a URL shortener with custom domain support. The service uses nanoid to generate URL-friendly unique IDs and will retry if an ID collision occurs.

Additionally, you can enable DynamoDB streams to capture changes to items stored in the DynamoDB table.

Table of Contents

Features

  • 🚀 Easy to Start - One-liner code to have your own URL shortener.
  • 🏢 Custom Domain - Bring your custom domain name that fits your brand.
  • 📡 DynamoDB Streams - Capture table activity with DynamoDB Streams.

Installation

TypeScript/JavaScript

$ npm install @rayou/cdk-url-shortener

Python

$ pip install rayou.cdk-url-shortener

.Net

$ nuget install CDK.URLShortener

# See more: https://www.nuget.org/packages/CDK.URLShortener/

Usage

Basic

import { URLShortener } from '@rayou/cdk-url-shortener';

new URLShortener(this, 'myURLShortener');

Custom Domain

import * as route53 from '@aws-cdk/aws-route53';
import * as acm from '@aws-cdk/aws-certificatemanager';
import { URLShortener } from '@rayou/cdk-url-shortener';

const zone = route53.HostedZone.fromLookup(this, 'HostedZone', {
  domainName: 'mydomain.com',
});

// Optional, a DNS validated certificate will be created if not provided.
const certificate = acm.Certificate.fromCertificateArn(
  this,
  'Certificate',
  'arn:aws:acm:region:123456789012:certificate/12345678-1234-1234-1234-123456789012',
);

new URLShortener(this, 'myURLShortener').addDomainName({
  domainName: 'foo.mydomain.com',
  zone,
  certificate,
});

Multiple Custom Domains

import * as route53 from '@aws-cdk/aws-route53';
import { URLShortener } from '@rayou/cdk-url-shortener';

const zone = route53.HostedZone.fromLookup(this, 'HostedZone', {
  domainName: 'mydomain.com',
});

new URLShortener(this, 'myURLShortener')
  .addDomainName({
    domainName: 'foo.mydomain.com',
    zone,
  })
  .addDomainName({
    domainName: 'bar.mydomain.com',
    zone,
  });

⚠️ Please note that although we have added two custom domains, they are pointed to the same URL shortener instance sharing the same DynamoDB table, if you need both domains run independently, create a new URL shortener instance.

Enable DynamoDB Streams

import * as lambda from '@aws-cdk/aws-lambda';
import * as dynamodb from '@aws-cdk/aws-dynamodb';
import * as lambdaEventSources from '@aws-cdk/aws-lambda-event-sources';

import { URLShortener } from '@rayou/cdk-url-shortener';

const table = new dynamodb.Table(this, 'Table', {
  partitionKey: {
    name: 'id',
    type: dynamodb.AttributeType.STRING,
  },
  stream: dynamodb.StreamViewType.NEW_AND_OLD_IMAGES,
});

new URLShortener(this, 'myURLShortener', {
  dynamoTable: table,
});

const streamHandlerCode = `'use strict';
    exports.handler = async (event) => {
      console.log('Received event:', JSON.stringify(event, null, 2));
      for (const record of event.Records) {
        console.log(record.eventID);
        console.log(record.eventName);
        console.log('DynamoDB Record: %j', record.dynamodb);
      }
      console.log(\`Successfully processed \${event.Records.length} records.\`);
    };`;

const lambdaFn = new lambda.Function(this, 'myStreamHandler', {
  runtime: lambda.Runtime.NODEJS_12_X,
  handler: 'index.handler',
  code: lambda.Code.fromInline(streamHandlerCode),
});

lambdaFn.addEventSource(
  new lambdaEventSources.DynamoEventSource(table, {
    startingPosition: lambda.StartingPosition.LATEST,
  }),
);

Create your first short URL

  1. After the deployment, you'll see ApiKeyURL and ApiEndpoint in CDK Outputs, visit ApiKeyURL to get your API key.

    Outputs:
    stack.CustomDomainApiEndpointcc4157 = https://mydomain.com
    stack.myURLShortenerApiEndpoint47185311 = https://yrzxcvbafk.execute-api.us-west-2.amazonaws.com/prod/
    stack.ApiKeyURL = https://console.aws.amazon.com/apigateway/home?#/api-keys/k2zxcvbafw6
  2. Run this cURL command to create your first short URL, an ID will be returned in the response.

    $ curl https://{API_ENDPOINT} /
        -X POST \
        -H 'content-type: application/json' \
        -H 'x-api-key: {API_KEY}' \
        -d '{
          "url": "https://github.com/rayou/cdk-url-shortener"
        }'
    
    {"id":"LDkPh"}
  3. Visit https://{API_ENDPOINT}/{ID} then you'll be redirected to the destination URL.

    $ curl -v https://{API_ENDPOINT}/{ID} # e.g. https://mydomain.com/LDkPh
    
    < HTTP/2 301
    < content-type: text/html; charset=UTF-8
    < content-length: 309
    < location: https://github.com/rayou/cdk-url-shortener
    
    <!DOCTYPE html><html><head><meta charset="UTF-8" /><meta http-equiv="refresh" content="0;url=https://github.com/rayou/cdk-url-shortener" /><title>Redirecting to https://github.com/rayou/cdk-url-shortener</title></head><body>Redirecting to <a href="https://github.com/rayou/cdk-url-shortener">https://github.com/rayou/cdk-url-shortener</a>.</body></html>

Documentation

Construct API Reference

See API.md.

URL Shortener API Endpoints

Shorten a Link

HTTP REQUEST

POST /

HEADERS

| Name | Value | Required | | -------------- | -------------------------- | -------- | | content-type | application/json | Required | | x-api-key | Get your api key here | Required |

ARGUMENTS

| Parameter | Type | Required | Description | | --------- | ------ | -------- | --------------- | | url | string | Required | Destination URL |

Example Request

curl https://mydomain.com /
  -X POST \
  -H 'content-type: application/json' \
  -H 'x-api-key: v3rYsEcuRekey' \
  -d '{
    "url": "https://github.com/rayou/cdk-url-shortener"
  }'

Response (201)

{
  "id": "LDkPh"
}

Visit a shortened URL

HTTP REQUEST

GET /:id

Example Request

curl https://mydomain.com/:id

Response (301)

< HTTP/2 301
< content-type: text/html; charset=UTF-8
< content-length: 309
< location: https://github.com/rayou/cdk-url-shortener

<!DOCTYPE html><html><head><meta charset="UTF-8" /><meta http-equiv="refresh" content="0;url=https://github.com/rayou/cdk-url-shortener" /><title>Redirecting to https://github.com/rayou/cdk-url-shortener</title></head><body>Redirecting to <a href="https://github.com/rayou/cdk-url-shortener">https://github.com/rayou/cdk-url-shortener</a>.</body></html>

Supporting this project

I'm working on this project in my free time, if you like my project, or found it helpful and would like to support me, you can buy me a coffee, any contributions are much appreciated! ❤️

License

This project is distributed under the Apache License, Version 2.0.