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.
- 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.
Install the SDK using pnpm
or yarn
:
pnpm add @nuecms/sdk-builder
# or
yarn add @nuecms/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,
});
apiClient.r('getUser', '/users/{id}', 'GET');
apiClient.r('createUser', '/users', 'POST');
const user = await apiClient.getUser({ id: '12345' });
console.log(user);
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');
Call the registered endpoints dynamically with placeholders and additional options:
const userDetails = await apiClient.getUser({ id: '12345' });
console.log(userDetails);
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
});
Integrate caching with ioredis
or any custom provider:
import Redis from 'ioredis';
import { RedisCacheProvider } from '@nuecms/sdk-builder';
const apiClient = new SdkBuilder({
baseUrl: 'https://api.example.com',
defaultHeaders: {
Authorization: 'Bearer {access_token}',
'Content-Type': 'application/json',
},
cacheProvider: new RedisCacheProvider(new 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);
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,
});
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.
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.