Skip to content

Commit

Permalink
feat: Added IP Address support (Basic Auth)
Browse files Browse the repository at this point in the history
- SetPrivateKey
- All ip_address in body inseaf of path variable
  • Loading branch information
Tynopia committed Dec 2, 2023
1 parent 6416050 commit 930f727
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 26 deletions.
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ SetWebstoreIdentifier("your_public_token")

```typescript
// Get all categories of the webstore
async GetCategories(includePackages?: boolean, basketIdent?: string, ipAddress?: string)
async GetCategories(includePackages?: boolean, basketIdent?: string, ip_address?: string)

// Get a specific category by an id
async GetCategory(id: number, includePackages?: boolean, basketIdent?: string, ipAddress?: string)
async GetCategory(id: number, includePackages?: boolean, basketIdent?: string, ip_address?: string)

// Apply a coupon, gift card, or creator code
async Apply<T>(t: T, basketIdent: string, type: ApplyType)
Expand All @@ -38,16 +38,16 @@ async Apply<T>(t: T, basketIdent: string, type: ApplyType)
async Remove<T>(t: T, basketIdent: string, type: ApplyType)

// Get a specific package by an id inside a basket
async GetPackage(id: number, basketIdent?: string, ipAddress?: string)
async GetPackage(id: number, basketIdent?: string, ip_address?: string)

// Get all packages of the webstore inside a basket
async GetPackages(basketIdent?: string, ipAddress?: string)
async GetPackages(basketIdent?: string, ip_address?: string)

// Get a basket by an identifier
async GetBasket(basketIdent: string)

// Create a basket and provide complete and cancel URLs
async CreateBasket(complete_url: string, cancel_url: string)
async CreateBasket(complete_url: string, cancel_url: string, ip_address?: string)

// Get all auth URLs by a basket
async GetBasketAuthUrl(basketIdent: string, returnUrl: string)
Expand All @@ -73,6 +73,20 @@ async GetWebstore()
- **Seamless Integration:** Use Tebex functionalities in your project without exposing the fact that you are using Tebex.
- **Custom Storefront:** Build your custom storefront by leveraging Tebex Headless API.


## IP forwarding

If a store uses its own backend but wants to use the IP addresses of the users instead of the server, Tebex a basic requires [authentication](https://documenter.getpostman.com/view/10912536/2s9XxvTEmh#intro).

[Check our following Link to generate a private key](https://creator.tebex.io/developers/api-keys)

```typescript
import { SetWebstoreIdentifier, SetPrivateKey } from "tebex_headless";

SetWebstoreIdentifier("your_public_token")
SetPrivateKey("your_private_key")
```

## Contributing

We welcome contributions from the community! If you'd like to contribute to this project, please follow these steps:
Expand Down
88 changes: 67 additions & 21 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios, { Method } from "axios";
import axios, { AxiosRequestConfig, Method } from "axios";

/**
* @constant baseUrl
Expand Down Expand Up @@ -28,6 +28,26 @@ export function SetWebstoreIdentifier(identifier: string): void {
webstoreIdentifier = identifier;
}

/**
* @var privateKey
* @description The private key of the webstore
*
* @type {string | null}
*/
export let privateKey: string | null = null;

/**
* @function SetPrivateKey
* @description A function to set the private key
*
* @param {string} key The private key of the webstore
*
* @returns {void}
*/
export function SetPrivateKey(key: string): void {
privateKey = key;
}

/**
* @interface Data
* @description The data returned from the Tebex Headless API
Expand Down Expand Up @@ -126,6 +146,16 @@ export interface CreatorCode {
creator_code: string
}

/**
* @interface IPAddress
* @description The IP address object for the body of the request
*
* @param {string} ip_address The IP address of the user
*/
export interface IPAddress {
ip_address?: string
}

/**
* @function Request
* @description A function to make a request to the Tebex Headless API
Expand All @@ -146,15 +176,24 @@ export async function Request<T, Body>(method: Method, identifier: string | null
}
}

const response = await axios.request<T>({
const config: AxiosRequestConfig = {
url: `${baseUrl}/api/${route}/${identifier}${path ?? ""}`,
params: params,
method: method,
data: body,
headers: {
"Content-Type": "application/json"
}
});
}

if (webstoreIdentifier && privateKey) {
config.auth = {
username: webstoreIdentifier,
password: privateKey
}
}

const response = await axios.request<T>(config);

return response.data;
}
Expand Down Expand Up @@ -185,15 +224,16 @@ export type Category = BaseItem & {
*
* @param {boolean} includePackages Whether to include the packages in the categories
* @param {string} basketIdent The identifier of the basket
* @param {string} ipAddress The IP address of the user
* @param {string} ip_address The IP address of the user
*
* @returns {Promise<Category[]>}
*/
export async function GetCategories(includePackages?: boolean, basketIdent?: string, ipAddress?: string): Promise<Category[]> {
export async function GetCategories(includePackages?: boolean, basketIdent?: string, ip_address?: string): Promise<Category[]> {
const { data }: Data<Category[]> = await Request("get", webstoreIdentifier, "accounts", "/categories", {
includePackages,
basketIdent,
ipAddress
basketIdent
}, {
ip_address
})

return data;
Expand All @@ -206,15 +246,16 @@ export async function GetCategories(includePackages?: boolean, basketIdent?: str
* @param {number} id The ID of the category
* @param {boolean} includePackages Whether to include the packages in the category
* @param {string} basketIdent The identifier of the basket
* @param {string} ipAddress The IP address of the user
* @param {string} ip_address The IP address of the user
*
* @returns {Promise<Category>}
*/
export async function GetCategory(id: number, includePackages?: boolean, basketIdent?: string, ipAddress?: string): Promise<Category> {
export async function GetCategory(id: number, includePackages?: boolean, basketIdent?: string, ip_address?: string): Promise<Category> {
const { data }: Data<Category> = await Request("get", webstoreIdentifier, "accounts", `/categories/${id}`, {
includePackages,
basketIdent,
ipAddress
basketIdent
}, {
ip_address
})

return data;
Expand Down Expand Up @@ -299,14 +340,15 @@ export type Package = BaseItem & {
*
* @param {number} id The ID of the package
* @param {string} basketIdent The identifier of the basket
* @param {string} ipAddress The IP address of the user
* @param {string} ip_address The IP address of the user
*
* @returns {Promise<Package>}
*/
export async function GetPackage(id: number, basketIdent?: string, ipAddress?: string): Promise<Package> {
export async function GetPackage(id: number, basketIdent?: string, ip_address?: string): Promise<Package> {
const { data }: Data<Package> = await Request("get", webstoreIdentifier, "accounts", `/packages/${id}`, {
basketIdent,
ipAddress
basketIdent
}, {
ip_address
})

return data;
Expand All @@ -317,14 +359,15 @@ export async function GetPackage(id: number, basketIdent?: string, ipAddress?: s
* @description A function to get all packages from the Tebex Headless API
*
* @param {string} basketIdent The identifier of the basket
* @param {string} ipAddress The IP address of the user
* @param {string} ip_address The IP address of the user
*
* @returns {Promise<Package[]>}
*/
export async function GetPackages(basketIdent?: string, ipAddress?: string): Promise<Package[]> {
export async function GetPackages(basketIdent?: string, ip_address?: string): Promise<Package[]> {
const { data }: Data<Package[]> = await Request("get", webstoreIdentifier, "accounts", `/packages`, {
basketIdent,
ipAddress
basketIdent
}, {
ip_address
})

return data;
Expand Down Expand Up @@ -450,13 +493,16 @@ export type Urls = {
*
* @param {string} complete_url The complete url
* @param {string} cancel_url The cancel url
* @param {string} ip_address The IP address of the user
*
* @returns {Promise<Basket>}
*/
export async function CreateBasket(complete_url: string, cancel_url: string): Promise<Basket> {
const { data }: Data<Basket> = await Request<Data<Basket>, Urls>("post", webstoreIdentifier, "accounts", "/baskets", {
export async function CreateBasket(complete_url: string, cancel_url: string, ip_address?: string): Promise<Basket> {
const { data }: Data<Basket> = await Request("post", webstoreIdentifier, "accounts", "/baskets", {
complete_url,
cancel_url
}, {
ip_address
});

return data;
Expand Down

0 comments on commit 930f727

Please sign in to comment.