Giter Club home page Giter Club logo

shorty's Introduction

Requirements

This package has the following requirements:

  • PHP 8.0 or higher
  • Node.js 14.15.0 or higher

Installation

You can easily install this package using Composer, by running the following command:

git clone https://github.com/kvlknctk/shorty.git
cp .env.example .env

Notice: You need to set tiny and bitly api keys in .env file.

composer update
npm install 

Run Project

You can easily install this package using Composer, by running the following command:

php artisan serve 

Hot reload for React

You can use hot reload for a good developer experience. For this, you need to have your project up and running with php artisan serve first.

npm dev

To add new shortener service

Create your own URL shorter provider file

You can create your own shortener service as follows. To do this, you can go to app/Http/Shorter folder. You have to return string value from your provider

<?php

    namespace App\Http\Shorter;

    use Illuminate\Support\Facades\Http;

    class TinyProvider implements ShortenerInterface
    {
        /**
         * Generate short url from tinyurl.com
         *
         * @param string $url
         * @return object
         */
        public function shortUrl(string $url): string
        {
            $responseData = Http::withHeaders([
                'Authorization' => 'Bearer ' . config('services.tiny.token'),
                'Content-Type'  => 'application/json',
                'Accept'        => 'application/json',
            ])->post('https://api.tinyurl.com/create', [
                "url"    => $url,
                "domain" => config('services.tiny.domain'),
            ]);

            // Return PHP object for further processing
            return $responseData->object()->data->tiny_url;

        }
    }

Connect your new service to factory

then you need to connect the provider you added to the factory. For this, add as shown as an example. Use this file to do this. app/Http/Shorter/Shorter.php

public function setShorter(string $provider): ShortenerInterface
{
    return match ($provider) {
        'tiny' => new TinyProvider(),
        'bitly' => new BitlyProvider(),
        // Add here your new provider with your custom key
        // for example 'myprovider' => new MyProvider()
        default => throw new \InvalidArgumentException('Shorter provider service not found.'),
    };
}

Edit Request Validation

Users need to specify the provider in order to make a request. We have a Request file for this. You need to specify the myprovider name you have added here. app/Http/Requests/ShorterRequest.php

/**
 * Get the validation rules that apply to the request.
 */
public function rules()
{
    return [
        'url'      => 'required|url',
        'provider' => 'required|in:bitly,tiny', // Add here your new provider with your custom key
    ];
}

Available shorter services

Usable Shorter Provider Service Provider Name Api Verison
TinyURL tiny 3.0.0
Bit.ly bitly 4.0.0

shorty's People

Contributors

kvlknctk avatar

Watchers

 avatar

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.