GithubHelp home page GithubHelp logo

aswa-shortener's Introduction

asweb-shortener (Azure Static Web app shortener)

Uses Azure Static Web App's redirect functionality to make a personal URL shortener. It is heavily based on Netifly-URLShortener


The problem

You want a URL shortener for your custom domain and you want an easy way to create and update URLs but you don't want to pay hundreds of dollars a year.

This solution

This relies on Azure Static Web App staticwebapp.config.json file for building a super simple URL shortener where the URLs are managed on GitHub and Azure Static Web App handles the redirecting for you.

The solution is build upon netlify by Kent C. Dodds.

Installation

This module is distributed via [npm][npm] which is bundled with [node][node] and should be installed as one of your project's dependencies:

npm install --save asweb-shortener

Usage

Your project should have a staticwebapp.config.json file that looks like this:

// example
{
  "routes": [
    {
      "route": "/me",
      "redirect": "https://callebaut.io",
      "statusCode": "301"
    }
  ],
  "navigationFallback": {
    "rewrite": "index.html",
    "exclude": ["/images/*.{png,jpg,gif}", "/css/*"]
  },
  "responseOverrides": {},
  "globalHeaders": {
    "content-security-policy": "default-src https: 'unsafe-eval' 'unsafe-inline'; object-src 'none'"
  }
}

This module exposes a binary that you should use in your package.json scripts. You also need to add

  • a homepage to your package.json
  • a config setion to specify your appLocation (defaults to src)
{
  "homepage": "https://stijn.run",
  "config": {
    "appLocation": "src"
  },
  "scripts": {
    "shorten": "aswa-shortener"
  }
}

Then you can run:

npm run shorten # simply formats your _redirects file
npm run shorten https://yahoo.com # generates a short code and adds it for you
npm run shorten https://github.com gh # adds gh as a short URL for you

The aswa-shortener does a few things:

  1. generates a short code if one is not provided
  2. validates your URL is a real URL
  3. adds the URL to the routes section of _redirects
  4. runs a git commit and push (this will trigger your Azure Static Web App CI/CD to deploy your new redirect)
  5. Copies the short URL to your clipboard

This introduces some delay as the rebuild needs to be pushed.

Shell Function

If you want to be able to run this anywhere in the terminal, you can try making a custom function for your shell.

Shell Agnostic

  1. Add the following [executable definition][npm-bin] to your package.json:
    {"bin": {"shorten": "cli.js"}}
  2. Create the cli.js file:
    #!/usr/bin/env node
    require('aswa-shortener')
  3. From your project directory, run the following to register the command globally:
    npm link

aswa-shortener's People

Contributors

stijnc avatar allcontributors[bot] avatar kentcdodds avatar andrewmcodes avatar naton avatar caarlos0 avatar ddbeck avatar edm00se avatar zhouzi avatar jakejarvis avatar j-f1 avatar jdorfman avatar marcuslyons avatar mattferderer avatar michaeldeboey avatar oisinq avatar pjlamb12 avatar sebjones avatar transitive-bullshit avatar

Watchers

 avatar

aswa-shortener's Issues

escaped quotes in route entry

  • asweb-shortener version: 1.0.0
  • node version: 16
  • npm version: 8.1.2

What you did:

ran npm run shorten url code

What happened:

route gets injected, containing escaped quotes.

fix duplicate entry

  • asweb-shortener version: 1.0.0
  • node version: 16
  • npm version: 8.1.2

Relevant code or config

What you did:

ran npm shorten url code

What happened:

shortcode and url get added twice, hence the static web app deployment fails during validation

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.