@koadz/bifrost - v1.1.5

Koadz Platform Bifrost

Bifrost a library for making API calls in JavaScript applications like react, nextjs, nodejs etc., with a custom data transformation function to modify API responses before they reach your application code.

  • Customizable data transformation: Adapt API responses to your application's specific needs.
  • Simplified API interactions: Streamline API calls with a concise and intuitive API.
  • Error handling: Handle errors gracefully and provide informative messages.
  • Modular design: Easily extend Bifrost with custom functions and behaviors.
npm install @koadz/bifrost

We'll create two files in this project:

  • mapper.js: Contains the custom data transformation function.
  • app.jsx (or equivalent depending on your framework): Demonstrates Bifrost usage with the transformation function.

Create mapper.js in your project directory and add the following code:

import { Binder } from "@koadz/bifrost/binder";

// Function to transform request data (e.g., adding a timestamp)
Binder.append("transformRequest", function (data) {
return { ...data, description: "description added from mapper function", timestamp: Date.now() };
});

// Function to transform response data, modifies the product object by adding initial discount value, and returns the transformed response.
Binder.append("addInitialDiscount", function (response) {
if (Array.isArray(response?.products)) {
response.products = response.products.map((product: any) => {
product.discount = 12;
return product;
});
} else response.discount = 12;
return response;
});

Create app.jsx (or the appropriate file for your framework) in your project directory and add the following code:

import { Bifrost } from "@koadz/bifrost";
import type { APIConfig } from "@koadz/bifrost/types";

// Bifrost configuration for the Product API endpoints
const brikAPIConfig: APIConfig = {
info: {
title: "Koadz",
version: "v1.0.0",
description: "APIs for Services Of Koadz",
},
servers: {
default: {
url: "https://api.domain.com",
},
},
endpoints: {
"/products/{pid}": {
get: {
method: "GET",
endpointId: "GetProductById",
description: "Product",
url: "/products/{pid}", // Replace with your actual API endpoint
pathParams: {
pid: "1", // Adjust path parameter if your API uses a different format
},
postMappers: ["addInitialDiscount"], // Apply the addInitialDiscount mapper after receiving the response
},
put: {
method: "PUT",
endpointId: "PutProductsById",
description: "Product update",
url: "/products/{pid}", // Replace with your actual API endpoint
headers: {
"Content-Type": "application/json", // Set appropriate headers for your API
},
pathParams: {
pid: "1", // Adjust path parameter if your API uses a different format
},
requestBody: {
data: {
title: "iPhone Galaxy +1",
},
mappers: ["transformRequest"], // Include the transformRequest mapper to modify request data before sending
},
},
},
},
};

// Create an instance of the Bifrost API client
const apiClient = new Bifrost(brikAPIConfig);

// Call the GetProductById API endpoint and handle the response
apiClient
.hit("GetProductById", {
// Can pass configuration here as well
})
.then((response) => {
console.log("GetProductById response:", response.data);
})
.catch((error) => {
console.error("Error:", error); // Handle errors appropriately
});

// Call the PutProductsById API endpoint and handle the response
apiClient
.hit("PutProductsById", {
// Can pass configuration here as well
})
.then((response) => {
console.log("PutProductsById response:", response.data);
})
.catch((error) => {
console.error("Error:", error); // Handle errors appropriately
});

Here's a very concise explanation of app.jsx:

  1. Imports: Grabs tools from Bifrost to make API calls.
  2. Configuration: Sets up how Bifrost interacts with two APIs (creating orders and getting products).
    • Defines URLs, methods (POST/GET), headers, and data for each.
    • Updates the path parameters with dynamic value.
    • Tells Bifrost to transform request/response data using functions in mapper.js.
  3. Create Bifrost Client: Builds an object to handle API calls based on the configuration.
  4. Make API Calls: Uses the client to call the two APIs (GetProductById and PutProductsById).
  5. Handle Responses:
    • Logs the response data (modified by transformResponse if applicable).
    • Logs any errors encountered during the calls.
  • Customize your API interactions: Replace placeholders with your actual API details and modify the data transformation functions in mapper.js to suit your specific needs.
  • Streamlined API calls: Bifrost now leverages the @koadz/fetch library for a more efficient and streamlined approach to making API calls. You can also use @koadz/bifrost/fetch directly to utilize the fetch library.
  • Error handling: The BifrostError class helps you handle errors encountered during API calls.
  • Custom functions: The Binder class allows you to attach custom functions to modify request and response data.
  • Core functionality: The API class encapsulates the core functionality of Bifrost, including sending API requests and transforming data.