@uniqoders/sdk
v1.0.0
Published
Front API:JSON Parser and Deserializer SDK
Downloads
63
Readme
Uniqoders API SDK
The Uniqoders API SDK is a TypeScript-based library designed to enhance the interaction with your API. Offering structured, type-safe requests and responses, this SDK streamlines the process of communicating with the backend.
Features
- Axios for HTTP Requests: Utilizes Axios for robust and efficient HTTP request management.
- TypeScript Integration: Leverages TypeScript for type-safe interactions, reducing runtime errors.
- JSON to TypeScript Object Mapping: Automatically maps JSON responses from the API to TypeScript objects.
- Cancellable Requests: Supports cancelling of ongoing HTTP requests.
- Customizable Configuration: Designed for easy and flexible configuration to fit various API interaction requirements.
- Deserialization: Automatically maps JSON responses from JSON:API snake_case to camelCase without attributes, includes and relations
Installation
Install the SDK into your project using one of the following package managers:
Using Yarn
yarn add uniqoders-sdk
Using NPM or PNPM
npm install uniqoders-sdk
Usage
Initialization
Create an instance of ApiClient
to start interacting with the API:
import ApiClient from '@uniqoders/sdk';
const API_URL = "https://api.example.com";
const HEADERS = {'Accept-Language': 'en'};
const apiClient = new ApiClient(API_URL, HEADERS);
Making API Requests
Use the ApiClient
to perform API requests:
// Example of a GET request
apiClient.request({
method: 'GET',
url: '/users',
}).then(response => {
console.log(response.data);
});
Response Handling
The responses are automatically parsed into the appropriate TypeScript objects:
apiClient.request({method: 'GET', url: '/user/1'})
.then(response => {
// Assuming response is a User object
console.log(response.data.name);
});
Advanced Features
Cancellable Requests
For scenarios such as rapid search where requests need to be cancelled:
apiClient.request({
method: 'GET',
url: '/search',
params: {query: 'example'},
options: {cancellable: true, cancelKey: 'searchRequest'}
});
Custom Object Mapping
You can define custom mappings for your TypeScript models:
class User extends BaseObject {
// User model definition
}
// Set custom object mappings
apiClient.setObjects({
user: attrs => new User(attrs)
});
How to Work
The library includes various npm scripts to facilitate development and build processes. These scripts can be executed
using pnpm
, npm
, or yarn
, based on your preference.
Available Scripts
"_clear"
: Cleans the build directories by removing all files inbuild/compiled
anddist
. Usesrimraf
for safe and effective deletion across different platforms.pnpm run _clear
"_tsc"
: Compiles the TypeScript files of the project using the TypeScript compiler (tsc
). This produces JavaScript files ready to be bundled and deployed.pnpm run _tsc
"_make-bundle"
: Bundles the project usingrollup
with the configuration defined in the Rollup config file.pnpm run _make-bundle
"build"
: Runs a series of scripts in sequence to clean, compile, and bundle the library. This script is a combination of the previous scripts, ideal for preparing the project for production.pnpm run build
"lint"
: Executeseslint
to identify and report on patterns found in ECMAScript/JavaScript code, helping to maintain code quality and consistency.pnpm run lint
"lint:write"
: Similar tolint
, but also attempts to fix automatically fixable issues in the code.pnpm run lint:write
These scripts enhance the development experience by streamlining tasks like cleaning build directories, compiling TypeScript files, bundling the final output, and maintaining code quality with linting.