npm package: @sstscode/adapter
Description: Adapter module for SSTS system
Author: SSTS Team
Latest version: 1.0.6

# @sstscode/adapter

Adapter module for SSTS system

## Installation

```bash
npm install @sstscode/adapter
```

## Usage

### Basic Usage

```typescript
import { useAdapter } from '@sstscode/adapter'

// Initialize adapter with callbacks
const adapter = useAdapter(
  async (dict, pageName, className, methodName, paramsXml) => {
    console.log('Logging adapter params...')
    return Promise.resolve('12345')
  },
  (tradeDetailId, methodName, result) => console.log('Logging result...', { tradeDetailId, methodName, result }),
  [],
  (url, data) => {
    console.log('Logging request...', { url, data })
  }
  (resultCodeMsg, businessTrace, adapterTrace, returnType, ret) => {
    console.log('Success:', { resultCodeMsg, ret })
  },
  (resultCodeMsg, businessTrace, adapterTrace, error) => {
    console.error('Error:', { resultCodeMsg, error })
  }
)
```

## API Reference

### Adapter Initialization

```typescript
const adapter = useAdapter(
  async (dict, pageName, className, methodName, paramsXml) => {
    console.log('Logging adapter params...')
    return Promise.resolve('12345')
  },
  (tradeDetailId, methodName, result) => console.log('Logging result...', { tradeDetailId, methodName, result }),
  [],
  (url, data) => {
    console.log('Logging request...', { url, data })
  }
  (resultCodeMsg, businessTrace, adapterTrace, returnType, ret) => {
    console.log('Success:', { resultCodeMsg, ret })
  },
  (resultCodeMsg, businessTrace, adapterTrace, error) => {
    console.error('Error:', { resultCodeMsg, error })
  }
)
```

#### Parameters

1. **paramCallback**: Function

   - Called when parameters are received
   - Signature: `(dict: any, pageName: string, className: string, methodName: string, paramsXml: string) => Promise<any>`
   - Returns: Promise resolving to initial parameter value

2. **resultCallback**: Function

   - Called when results are received
   - Signature: `(tradeDetailId: string, methodName: string, result: any) => void`

3. **initialParams**: Array<any>

   - Array of initial parameters to pass to the adapter

4. **successCallback**: Function

   - Called on successful operations
   - Signature: `(resultCodeMsg: string, businessTrace: string, adapterTrace: string, returnType: string, ret: any) => void`

5. **errorCallback**: Function
   - Called on errors
   - Signature: `(resultCodeMsg: string, businessTrace: string, adapterTrace: string, error: any) => void`

### Adapter Services

The adapter provides three main services:

#### HIS Service

```typescript
adapter.his.call(
  fnName: string,
  dict: any,
  isAsync?: boolean,
  callback?: FnSuccessCallback
): Promise<string | object>
```

- Handles HIS system integration
- Returns adapterTrace string if async, otherwise returns result object

#### BANK Service

```typescript
adapter.bank.call(
  fnName: string,
  dict: any,
  isAsync?: boolean,
  callback?: FnSuccessCallback
): Promise<string | object>
```

- Handles banking system integration
- Returns adapterTrace string if async, otherwise returns result object

#### YB Service

```typescript
adapter.yb.call(
  fnName: string,
  dict: any,
  isAsync?: boolean,
  callback?: FnSuccessCallback
): Promise<string | object>
```

- Handles insurance system integration
- Returns adapterTrace string if async, otherwise returns result object

### Common Parameters

All services share these common parameters:

- `fnName`: string - The function name to call
- `dict`: any - Dictionary containing:
  - terminal: { TerminalIp: string, TerminalNo: string }
  - device: object
  - system: object
  - static: object
  - config: object
- `isAsync`: boolean - Whether to call asynchronously (default: true)
- `callback`: Function - Optional success callback with signature:
  ```typescript
  (resultCodeMsg: string, businessTrace: string, adapterTrace: string, returnType: string, ret: any) => void
  ```

## Error Handling

The adapter provides comprehensive error handling through:

1. Callback errors
2. Custom error handling
3. Validation errors

## Development

1. Clone the repository
2. Install dependencies: `npm install`
3. Build the project: `npm run build`
4. Run tests: `npm test`