Skip to content

An advanced, modular SDK Builder for Node.js applications that simplifies API client creation.

License

Notifications You must be signed in to change notification settings

nuecms/sdk-builder

Repository files navigation

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.

npm GitHub GitHub issues GitHub pull requests

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, and TEXT 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';
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);

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:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature-name).
  3. Commit your changes (git commit -m "Add feature X").
  4. Push to the branch (git push origin feature-name).
  5. 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.

About

An advanced, modular SDK Builder for Node.js applications that simplifies API client creation.

Resources

License

Stars

Watchers

Forks

Packages

No packages published