Skip to content
Draft
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions docs-js/rpt.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ Use the `predictWithSchema()` method to make predictions with a schema explicitl
Pass the data schema, and the prediction data including the prediction configuration and input data to the method.
Prefer providing the data schema if it is known instead of relying on the auto-inference solely based on the input data.

For information about custom request options such as compression, custom headers, or timeout, refer to the [Advanced Usage](#advanced-usage) section.

```typescript
const prediction = await client.predictWithSchema(
// Data schema
Expand Down Expand Up @@ -146,6 +148,8 @@ To make predictions without providing a schema, use the `predictWithoutSchema()`
Pass only the prediction data as a parameter.
The data schema will be inferred from the input data.

For information about custom request options such as compression, custom headers, or timeout, refer to the [Advanced Usage](#advanced-usage) section.

```typescript
const prediction = await client.predictWithoutSchema(
// Prediction data
Expand Down Expand Up @@ -180,3 +184,142 @@ const prediction = await client.predictWithoutSchema(
}
);
```

## Advanced Usage

The RPT client supports advanced features for specialized use cases.

### Request Compression

The RPT client automatically compresses prediction requests to improve performance for large datasets.
By default, requests with a body of 1024 bytes or larger are automatically compressed using the `gzip` algorithm.

To customize compression settings, pass a `customRequest` parameter with a `requestCompression` configuration to the `predictWithSchema()` or `predictWithoutSchema()` methods:

<!-- vale off -->

```typescript
import * as zlib from 'zlib';

const prediction = await client.predictWithSchema(schema, predictionData, {
requestCompression: {
algorithm: 'gzip', // Only 'gzip' is currently supported for RPT
mode: true, // Force compression regardless of payload size (default 'auto')
autoCompressMinSize: 2048, // Only compress payloads >= 2048 bytes in 'auto' mode (default 1024)
compressOptions: {
level: zlib.constants.Z_BEST_SPEED // Custom compression level
}
}
});
```

<!-- vale on -->

### Custom Request Options

The `customRequest` parameter also supports additional HTTP request configuration options from the SAP Cloud SDK for JavaScript, such as:

- `headers`: Custom HTTP headers.
- `timeout`: Request timeout in milliseconds.
- `middleware`: Custom HTTP middleware functions.

:::warning

The compression middleware is automatically prepended to the middleware chain, unless disabled by setting `mode: false`.

:::

The following example demonstrates how to include custom HTTP headers in your request configuration:

```typescript
const prediction = await client.predictWithSchema(schema, predictionData, {
headers: {
'x-custom-header': 'value'
},
timeout: 30000, // 30 seconds
requestCompression: {
mode: true
}
});
```

## Resilience

The `@sap-cloud-sdk/resilience` package provides built-in resilience features to handle transient failures and improve reliability.

For adding basic resilience features, include the `resilience()` middleware in your request configuration to enable default circuit breaker, and timeout middlewares with default settings:

```typescript
import { resilience } from '@sap-cloud-sdk/resilience';

const prediction = await client.predictWithSchema(schema, predictionData, {
middleware: [...resilience()]
});
```

:::tip

Using the `resilience()` function is the recommended way to build resilience-related middleware, it ensures the correct order of middlewares.

:::

For advanced resilience patterns, refer to the [SAP Cloud SDK documentation on resilience](https://sap.github.io/cloud-sdk/docs/js/guides/resilience).

### Retry

The HTTP client automatically retries failed requests on transient errors such as network issues or temporary server errors.
Customize retry behavior using the resilience library.

Set a custom retry count:

```typescript
import { retry } from '@sap-cloud-sdk/resilience';

const prediction = await client.predictWithSchema(schema, predictionData, {
middleware: [
retry({ maxRetries: 3, backoffType: 'exponential', initialDelay: 1000 })
]
});
```

### Timeout

Configure timeout for requests to prevent hanging connections.
The default timeout is 10 seconds.

```typescript
import { requestCompression } from '@sap-cloud-sdk/http-client';
import { timeout } from '@sap-cloud-sdk/resilience';

const prediction = await client.predictWithSchema(schema, predictionData, {
middleware: [
timeout(30000), // 30 seconds
requestCompression({ mode: true }) // Compression middleware with custom position in the middleware chain
],
requestCompression: {
mode: false // Disable automatic compression, compression middleware will not be prepended
}
});
```

### Combining Resilience Patterns

Combine multiple resilience patterns for comprehensive protection:

<!-- vale off -->

```typescript
import { resilience } from '@sap-cloud-sdk/resilience';

const prediction = await client.predictWithSchema(schema, predictionData, {
middleware: [
...resilience({
timeout: 5000,
curcuitBreaker: { failureThreshold: 5, recoveryTimeout: 60000 },
retry: 5
})
]
});
```

<!-- vale on -->