Package Exports
- @nuecms/sdk-builder
- @nuecms/sdk-builder/dist/index.es.js
- @nuecms/sdk-builder/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@nuecms/sdk-builder) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
SDK Builder
An advanced, modular SDK Builder for Node.js applications that simplifies API client creation. Supports dynamic endpoints, caching, custom response handling, and more — fully written in TypeScript for type safety and developer-friendly integration.
Features
- Dynamic Endpoints: Easily register and manage API endpoints with placeholder support (
{id}
). - Caching Support: Integrate with caching providers like Redis for optimized performance.
- Flexible Response Formats: Built-in support for
JSON
,XML
, andTEXT
formats with custom transformations. - Error Handling: Graceful error handling and detailed error messages for debugging.
- Path Placeholder Resolution: Automatically replace placeholders like
{token}
with dynamic values. - TypeScript Ready: Fully typed for robust, error-free coding.
Table of Contents
Installation
Install the SDK using pnpm
or yarn
:
pnpm add @nuecms/sdk-builder
# or
yarn add @nuecms/sdk-builder
Quick Start
1. Import and Initialize the SDK Builder
import { SdkBuilder } from '@nuecms/sdk-builder';
const apiClient = new SdkBuilder({
baseUrl: 'https://api.example.com',
defaultHeaders: {
Authorization: 'Bearer {access_token}',
'Content-Type': 'application/json',
},
timeout: 5000,
});
2. Register API Endpoints
apiClient.r('getUser', '/users/{id}', 'GET');
apiClient.r('createUser', '/users', 'POST');
3. Make API Calls
const user = await apiClient.getUser({ id: '12345' });
console.log(user);
Usage Examples
Registering Endpoints
Register endpoints with their HTTP method, path, and dynamic placeholders (e.g., {id}
):
apiClient.r('getUser', '/users/{id}', 'GET');
apiClient.r('deleteUser', '/users/{id}', 'DELETE');
apiClient.r('createUser', '/users', 'POST');
Making API Calls
Call the registered endpoints dynamically with placeholders and additional options:
const userDetails = await apiClient.getUser({ id: '12345' });
console.log(userDetails);
Response Formats
The SDK supports multiple response formats:
- JSON (default)
- Blob
- TEXT
You can specify a format per request or set a global default:
const apiClient = new SdkBuilder({
baseUrl: 'https://api.example.com',
defaultHeaders: {
Authorization: 'Bearer {access_token}',
'Content-Type': 'application/json',
},
timeout: 5000,
responseFormat: 'json', // Default response format
});
Caching with Redis
Integrate caching with ioredis
or any custom provider:
import Redis from 'ioredis';
const redis = new Redis();
apiClient.useCacheProvider(redis);
// Example: Cache a response
await apiClient.cacheProvider.set('user:12345', JSON.stringify(userDetails), 'json', 3600);
// Example: Retrieve a cached response
const cachedUser = await apiClient.cacheProvider.get('user:12345');
console.log(cachedUser);
Advanced Features
Custom Response Transformation
Transform API responses dynamically based on the format:
const customTransformer = (data: any, format: string) => {
if (format === 'json' && data?.user) {
data.user.name = data.user.name.toUpperCase(); // Custom transformation
}
return data;
};
const apiClient = new SdkBuilder({
baseUrl: 'https://api.example.com',
customResponseTransformer: customTransformer,
});
Contributing
We welcome contributions to improve this SDK! To get started:
- Fork the repository.
- Create a new branch (
git checkout -b feature-name
). - Commit your changes (
git commit -m "Add feature X"
). - Push to the branch (
git push origin feature-name
). - Open a pull request.
License
This SDK is released under the MIT License. You’re free to use, modify, and distribute this project. See the LICENSE
file for more details.