npm package: @kodice.one/rapid-api-azure
Description: RapidAPI for Azure Functions library to bind your APIs
Author: KODICE ONE
Latest version: 1.2.5

# RapidAPI library for Azure Functions 🔗

[RapidAPI](https://www.rapidapi.com/) for [Azure Functions Javascript](https://azure.microsoft.com/en-us/services/functions/) has been developed having in mind reusability of the usual tedious code that is needed to authorize and manage RapidAPI calls to your functions.

But, less talk and let see some code.

For example, when we implement a new Azure Function to be used with RapidAPI, we need to go thorugh some steps:

- Validate the Secret Key sent via RapidAPI request headers.
- Check if the Customer performing the request has the right to request a specific feature.

Usually these steps are quite tedious, repetitive, and prone to error.

Enough, let see some code:

```js
//  Let's define some features for the RapidAPI plans, which are: BASIC, PRO, MEGA, ULTRA.
//  Features keys are defined at your discretion.
//  As rule of thumb more features are available as higher the Plan is.
const Plannings = new PlanMapper();
Plannings.Features = [
    new Feature(RapidPlans.BASIC, ['feat-easy']),
    new Feature(RapidPlans.PRO, ['feat-easy', 'feat-medium']),
    new Feature(RapidPlans.MEGA, ['feat-easy', 'feat-medium', 'feat-hard']),
    new Feature(RapidPlans.ULTRA, ['feat-easy', 'feat-medium', 'feat-hard', 'feat-impossible'])
];

//  Azure Function itself index.ts
import { rapidApiHeaders, RapidHeaders, RapidHeadersError } from "rapid-api-azure";

const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {

  //    Load your RapidAPI Secret Key
  const rapidSecret: string = process.env["RapidApiSecret"];

  //    Map headers and Check the SecretKey with 'rapidApiHeaders' 
  //    If the header does not match the RapidAPI secret
  //        a new RapidHeadersError exception is thrown
  const rapidHeaders: RapidHeaders = rapidApiHeaders(req, rapidSecret);

  //    Check plans and features
  const hasFeature: boolean = Plannings.hasFeature(rapidHeaders.Subscription, "feat-medium");

  //    What to do if you Customer doesn't have Plan-to-Feat?
  if (!hasFeature) {
      console.log.error('Oh no!');
  }
};

```

## Install

Simply run:

```bash
npm install rapid-api-azure
```

## Motivation

Biggest benefit of RapidAPI is to have a facing platform for your API, that allows you to create plans and mappings.
This library has been developed having in mind reusability of the usual tedious code that is needed to authorize and manage RapidAPI calls to your Azure Functions Javascript.
For example, when we implement a new Azure Function to be used with RapidAPI, we need to go thorugh some steps:

- Validate the Secret Key sent via RapidAPI request headers.
- Check if the Customer performing the request has the right to request a specific feature.

Usually these steps are quite tedious, repetitive, and prone to error.

## Library

### Functions

#### **rapidApiHeaders** (request: HttpRequest, secret: string): RapidHeaders

Converts Azure Function http request headers in a typed object. Plan is defined within as enumerator. The function will as well check if SecretKey provided matches the Key in your settings.

### Classes

#### **PlanMapper**

Defines features for the RapidAPI plans, which are: BASIC, PRO, MEGA, ULTRA.
Features keys are defined at your discretion.
As rule of thumb more features are available as higher the Plan is.

#### **Feature** 

Plan to Feature map to be used to check if Plan has a specific feature with *PlanMapper*

#### **PlanMapper**.hasFeature (plan: RapidPlans, feature: string): boolean

Check if the Plan sent by RapidAPI match with the features defined in the *PlanMapper*.Features property.

### Errors

#### **RapidHeadersError**

Custom error that is raised when the SecretKey does not match with the one provided by the RapidAPI header.

### Models

#### **RapidPlans** enumerator

NONE,BASIC, PRO, ULTRA, MEGA, CUSTOM

#### **RapidPlansNames** constants

None, Basic, Pro, Ultra, Mega, Custom

## About

This project is maintained by [KODICE](https://www.kodice.one/).

## License

**rapid-api-azure** is available under the MIT license.

RapidAPI is name registered by [RapidAPI](https://rapidapi.com/company/about/)

```
Copyright (c) KODICE

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
```