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.
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:
mapper.js.GetProductById and PutProductsById).transformResponse if applicable).@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.BifrostError class helps you handle errors encountered during API calls.Binder class allows you to attach custom functions to modify request and response data.API class encapsulates the core functionality of Bifrost, including sending API requests and transforming data.