sdkwork-commons-typescript
v1.0.8
Published
Common TypeScript library for SDK work
Readme
SDKWORK TypeScript Commons
A comprehensive TypeScript library for building SDKs with common utilities and base classes.
Overview
This library provides a foundation for building consistent and robust SDKs in TypeScript. It includes base classes, HTTP utilities, type definitions, and examples to help you quickly create your own SDKs.
Features
- Base SDK Classes: Foundation classes for building consistent SDKs
- HTTP Client: Configurable HTTP client with retry logic and middleware support
- Type Safety: Comprehensive TypeScript type definitions
- Extensible Design: Hook system for customizing request behavior
- Promise Wrapper: Enhanced promise handling with additional utility methods
Project Structure
src/
├── core/ # Core SDK classes
│ ├── BaseSdkApi.ts # Base API class
│ └── index.ts # Core module exports
├── http/ # HTTP-related functionality
│ ├── BaseSdkClient.ts # Base SDK client with HTTP methods
│ ├── HttpTool.ts # HTTP utility functions
│ └── index.ts # HTTP module exports
├── types/ # Type definitions
│ └── index.ts # All type definitions
├── examples/ # Example implementations
│ ├── ExampleApi.ts # Example API implementation
│ ├── enhanced-usage-example.ts # Enhanced usage examples
│ ├── usage-example.ts # Basic usage examples
│ └── index.ts # Examples module exports
└── index.ts # Main entry pointInstallation
npm installBuilding
npm run buildThis will compile the TypeScript code into JavaScript in the dist/ directory.
Usage
Basic Client Setup
import { BaseSdkClient } from 'sdkwork-typescript-commons';
const client = new BaseSdkClient({
baseUrl: 'https://api.example.com',
apiKey: 'your-api-key',
timeout: 5000
});Making Requests
The client provides convenient methods for all HTTP verbs:
// GET request
const users = await client.get('/users');
// POST request
const newUser = await client.post('/users', {
body: JSON.stringify({ name: 'John Doe' })
});
// PUT request
const updatedUser = await client.put('/users/1', {
body: JSON.stringify({ name: 'Jane Doe' })
});
// DELETE request
await client.delete('/users/1');Advanced Usage with Hooks
Extend the BaseSdkClient to add custom behavior:
class CustomSdkClient extends BaseSdkClient {
protected async prepareOptions(options) {
// Add custom headers
options.headers = {
...options.headers,
'X-Custom-Header': 'custom-value'
};
}
protected async prepareRequest(request, { url, options }) {
// Log requests
console.log(`Making ${options.method} request to ${url}`);
}
}API
BaseSdkClient
The main client class with the following methods:
get(path, opts?)- Make a GET requestpost(path, opts?)- Make a POST requestput(path, opts?)- Make a PUT requestpatch(path, opts?)- Make a PATCH requestdelete(path, opts?)- Make a DELETE requestrequest(options, remainingRetries?)- Generic request methodsendRequest(requestOptions)- Send a request with full options
Hooks
prepareOptions(options)- Called before each request to modify optionsprepareRequest(request, { url, options })- Called before each request to modify the RequestInit object
Types
The library provides comprehensive type definitions:
SdkClientOptions- Client configuration optionsSdkRequestOptions- Request configuration optionsSdkResponse<T>- Response structureHTTPMethod- Type for HTTP methodsFinalRequestOptions- Internal request optionsAPIPromise<T>- Enhanced promise with additional methods
Examples
See the examples directory for detailed usage examples:
Development
Prerequisites
- Node.js (v12 or higher)
- npm or yarn
Setup
- Clone the repository
- Install dependencies:
npm install
Building
npm run buildPublishing
To publish the package to npm registry, use the following command:
npm publish --registry=https://registry.npmjs.org/This ensures the package is published to the official npm registry, even if you have configured a different default registry.
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests
- Submit a pull request
License
MIT
