# nestjs-sap-rfc

> NestJS SAP RFC Client, providing convenient ABAP business logic consumption from NestJS

Latest version **4.1.11** (published 2025-04-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install nestjs-sap-rfc
pnpm add nestjs-sap-rfc
yarn add nestjs-sap-rfc
bun add nestjs-sap-rfc
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.1.11 |
| Published | 2025-04-04 |
| First published | 2022-02-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 46.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Grupo Boticário |
| Maintainers | grupoboticario |
| Keywords | nestjs, sap, rfc, nodejs |

## Links

- npm: https://www.npmjs.com/package/nestjs-sap-rfc
- Repository: https://github.com/grupoboticario/nestjs-sap-rfc
- Homepage: https://github.com/grupoboticario/nestjs-sap-rfc#readme
- Issues: https://github.com/grupoboticario/nestjs-sap-rfc/issues
- npm.io page: https://npm.io/package/nestjs-sap-rfc

## Dependencies (1)

- [node-rfc](https://npm.io/package/node-rfc.md) ^3.3.1

## Recent versions

- 4.1.11 (latest) — 2025-04-04
- 4.0.5 — 2024-05-27
- 4.0.4 — 2024-05-08
- 4.0.3 — 2024-03-21
- 4.0.2 — 2024-03-21
- 4.0.1 — 2024-01-30
- 4.0.0 — 2024-01-05
- 3.0.33 — 2023-12-11
- 3.0.32 — 2023-12-11
- 3.0.31 — 2023-12-11
- 3.0.30 — 2023-12-06
- 3.0.29 — 2023-11-22
- 3.0.28 — 2023-11-16
- 3.0.27 — 2023-10-03
- 3.0.26 — 2023-09-20
- … 51 more at https://npm.io/package/nestjs-sap-rfc/versions

## README

<p align="center">
  <a href="http://nestjs.com/" target="blank">
    <img src="https://nestjs.com/img/logo_text.svg" width="320" alt="Nest Logo" />
  </a>
</p>

<p align="center">NestJS SAP RFC Client</p>

[![GitHub](https://img.shields.io/github/license/grupoboticario/nestjs-sap-rfc)](https://github.com/grupoboticario/nestjs-sap-rfc/blob/main/LICENSE)
[![CodeQL](https://github.com/grupoboticario/nestjs-sap-rfc/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/grupoboticario/nestjs-sap-rfc/actions/workflows/codeql-analysis.yml)
[![Sonar](https://github.com/grupoboticario/nestjs-sap-rfc/actions/workflows/sonar.yml/badge.svg)](https://github.com/grupoboticario/nestjs-sap-rfc/actions/workflows/sonar.yml)
[![CI](https://github.com/grupoboticario/nestjs-sap-rfc/actions/workflows/ci.yml/badge.svg)](https://github.com/grupoboticario/nestjs-sap-rfc/actions/workflows/ci.yml)
[![Publish](https://github.com/grupoboticario/nestjs-sap-rfc/actions/workflows/publish.yml/badge.svg)](https://github.com/grupoboticario/nestjs-sap-rfc/actions/workflows/publish.yml)
[![GitHub package.json version](https://img.shields.io/github/package-json/v/grupoboticario/nestjs-sap-rfc)](https://github.com/grupoboticario/nestjs-sap-rfc)
[![GitHub package.json dependency version (prod)](https://img.shields.io/github/package-json/dependency-version/grupoboticario/nestjs-sap-rfc/node-rfc)](https://github.com/SAP/node-rfc)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](https://commitizen.github.io/cz-cli/)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=b15648de-145e-4a88-87e7-b2a57e04eaeb&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=b15648de-145e-4a88-87e7-b2a57e04eaeb)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=b15648de-145e-4a88-87e7-b2a57e04eaeb&metric=coverage)](https://sonarcloud.io/summary/new_code?id=b15648de-145e-4a88-87e7-b2a57e04eaeb)
[![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=b15648de-145e-4a88-87e7-b2a57e04eaeb&metric=code_smells)](https://sonarcloud.io/summary/new_code?id=b15648de-145e-4a88-87e7-b2a57e04eaeb)
[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=b15648de-145e-4a88-87e7-b2a57e04eaeb&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=b15648de-145e-4a88-87e7-b2a57e04eaeb)
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=b15648de-145e-4a88-87e7-b2a57e04eaeb&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=b15648de-145e-4a88-87e7-b2a57e04eaeb)
[![GitHub repo size](https://img.shields.io/github/repo-size/grupoboticario/nestjs-sap-rfc)](https://github.com/grupoboticario/nestjs-sap-rfc)
[![npm](https://img.shields.io/npm/v/nestjs-sap-rfc)](https://www.npmjs.com/package/nestjs-sap-rfc)
[![npm type definitions](https://img.shields.io/npm/types/nestjs-sap-rfc)](https://www.typescriptlang.org)
[![npm](https://img.shields.io/npm/dw/nestjs-sap-rfc)](https://www.npmjs.com/package/nestjs-sap-rfc)

## 📚 Description

NestJS SAP RFC Client, providing convenient ABAP business logic consumption from NestJS

## 🛠️ Installation

[SAP NWRFC SDK installation](https://github.com/SAP/node-rfc/blob/main/doc/installation.md#sap-nwrfc-sdk-installation)

```bash
npm install nestjs-sap-rfc --save
```

## 🏃 Getting Started

Register `SapModule` module in app.module.ts

[Connection Pool](https://github.com/SAP/node-rfc/blob/main/doc/usage.md#connection-pool)

```ts
import { SapModule } from 'nestjs-sap-rfc';
import { Module } from '@nestjs/common';

@Module({
  imports: [
    SapModule.createPool({
      isGlobal: true, // for global module
      name: 'service_name', // for multiple modules (OPTIONAL)
      connectionParameters: {
        /* see RfcConnectionParameters */
      },
      clientOptions: {
        /* see RfcClientOptions */
      },
      poolOptions: {
        /* see RfcPoolOptions */
      },
    }),
  ],
})
export class AppModule {}
```

[Connection Pool](https://github.com/SAP/node-rfc/blob/main/doc/usage.md#connection-pool) (Async Module)

```ts
import { SapModule } from 'nestjs-sap-rfc';
import { Module } from '@nestjs/common';

@Module({
  imports: [
    SapModule.createPoolAsync({
      isGlobal: true, // for global module
      name: 'service_name', // for multiple modules (OPTIONAL)
      useFactory: () => {
        return {
          connectionParameters: {
            /* see RfcConnectionParameters */
          },
          clientOptions: {
            /* see RfcClientOptions */
          },
          poolOptions: {
            /* see RfcPoolOptions */
          },
        };
      },
    }),
  ],
})
export class AppModule {}
```

[Connection Pool](https://github.com/SAP/node-rfc/blob/main/doc/usage.md#connection-pool) (Async Module + ConfigService)

```ts
import { SapModule } from 'nestjs-sap-rfc';
import { Module } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

@Module({
  imports: [
    SapModule.createPoolAsync({
      isGlobal: true, // for global module
      name: 'service_name', // for multiple modules (OPTIONAL)
      useFactory: (config: ConfigService) => {
        return {
          connectionParameters: {
            /* see RfcConnectionParameters */
            /* config.get(...) */
          },
          clientOptions: {
            /* see RfcClientOptions */
            /* config.get(...) */
          },
          poolOptions: {
            /* see RfcPoolOptions */
            /* config.get(...) */
          },
        };
      },
      inject: [ConfigService],
    }),
  ],
})
export class AppModule {}
```

[Direct Client](https://github.com/SAP/node-rfc/blob/main/doc/usage.md#client)

```ts
import { SapModule } from 'nestjs-sap-rfc';
import { Module } from '@nestjs/common';

@Module({
  imports: [
    SapModule.createClient({
      isGlobal: true, // for global module
      name: 'service_name', // for multiple modules (OPTIONAL)
      connectionParameters: {
        /* see RfcConnectionParameters */
      },
      clientOptions: {
        /* see RfcClientOptions */
      },
    }),
  ],
})
export class AppModule {}
```

[Direct Client](https://github.com/SAP/node-rfc/blob/main/doc/usage.md#client) (Async Module)

```ts
import { SapModule } from 'nestjs-sap-rfc';
import { Module } from '@nestjs/common';

@Module({
  imports: [
    SapModule.createClientAsync({
      isGlobal: true, // for global module
      name: 'service_name', // for multiple modules (OPTIONAL)
      useFactory: () => {
        return {
          connectionParameters: {
            /* see RfcConnectionParameters */
          },
          clientOptions: {
            /* see RfcClientOptions */
          },
        };
      },
    }),
  ],
})
export class AppModule {}
```

[Direct Client](https://github.com/SAP/node-rfc/blob/main/doc/usage.md#client) (Async Module + ConfigService)

```ts
import { SapModule } from 'nestjs-sap-rfc';
import { Module } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

@Module({
  imports: [
    SapModule.createClientAsync({
      isGlobal: true, // for global module
      name: 'service_name', // for multiple modules (OPTIONAL)
      useFactory: (config: ConfigService) => {
        return {
          connectionParameters: {
            /* see RfcConnectionParameters */
          },
          clientOptions: {
            /* see RfcClientOptions */
          },
        };
      },
      inject: [ConfigService],
    }),
  ],
})
export class AppModule {}
```

Inject SapService

```ts
import { InjectSapService, SapService, SapRfcObject, SapRfcStructure } from 'nestjs-sap-rfc';
import { Injectable } from '@nestjs/common';

// RfcStructure
type PositionData = SapRfcStructure; // SAP structure

// RfcStructure
interface NestedData extends SapRfcStructure {
  readonly E_NESTED?: string; // SAP field name
}

// MySapInterface
interface MySapInterface extends SapRfcObject {
  readonly E_NAME?: string; // SAP field name
  readonly E_DATA?: PositionData; // SAP field name
  readonly E_DATA2?: NestedData; // SAP field name
  readonly E_ERROR?: string; // SAP field name
  readonly I_OBJID?: string; // SAP field name
}

@Injectable()
export class MyService {
  /**
   * @param {SapService} sapService
   */
  constructor(
    @InjectSapService()
    private readonly sapService: SapService,
  ) {}

  public async test(): MySapInterface {
    return this.sapService.execute<MySapInterface>('rfcName', {
      ...rfcParams,
    });
  }
}
```

Inject SapService by name

```ts
import { InjectSapService, SapService, SapRfcObject, SapRfcStructure } from 'nestjs-sap-rfc';
import { Injectable } from '@nestjs/common';

// RfcStructure
type PositionData = SapRfcStructure; // SAP structure

// RfcStructure
interface NestedData extends SapRfcStructure {
  readonly E_NESTED?: string; // SAP field name
}

// MySapInterface
interface MySapInterface extends SapRfcObject {
  readonly E_NAME?: string; // SAP field name
  readonly E_DATA?: PositionData; // SAP field name
  readonly E_DATA2?: NestedData; // SAP field name
  readonly E_ERROR?: string; // SAP field name
  readonly I_OBJID?: string; // SAP field name
}

@Injectable()
export class MyService {
  /**
   * @param {SapService} sapService
   */
  constructor(
    @InjectSapService('service_name')
    private readonly sapService: SapService,
  ) {}

  public async test(): MySapInterface {
    return this.sapService.execute<MySapInterface>('rfcName', {
      ...rfcParams,
    });
  }
}
```

### Creating and using transactions

Transactions are created using SapService. Example:

```ts
import { InjectSapService, SapService, SapRfcObject, SapRfcStructure } from 'nestjs-sap-rfc';
import { Injectable } from '@nestjs/common';

// RfcStructure
type PositionData = SapRfcStructure; // SAP structure

// RfcStructure
interface NestedData extends SapRfcStructure {
  readonly E_NESTED?: string; // SAP field name
}

// MySapInterface
interface MySapInterface extends SapRfcObject {
  readonly E_NAME?: string; // SAP field name
  readonly E_DATA?: PositionData; // SAP field name
  readonly E_DATA2?: NestedData; // SAP field name
  readonly E_ERROR?: string; // SAP field name
  readonly I_OBJID?: string; // SAP field name
}

@Injectable()
export class MyService {
  /**
   * @param {SapService} sapService
   */
  constructor(
    @InjectSapService()
    private readonly sapService: SapService,
  ) {}

  public async runTransaction(): MySapInterface {
    await this.sapService.transaction(async (sapClient: SapClient) => {
      // call rfcs using sapClient
    });
  }
}
```

> Everything you want to run in a transaction must be executed in a callback:

```ts
@Injectable()
export class MyService {
  /**
   * @param {SapService} sapService
   */
  constructor(
    @InjectSapService()
    private readonly sapService: SapService,
  ) {}

  public async runTransaction(): MySapInterface {
    await this.sapService.transaction(async (sapClient: SapClient) => {
      await sapClient.call('rfcName_1', {
        ...rfcParams,
      });
      await sapClient.call('rfcName_2', {
        ...rfcParams,
      });
    });
  }
}
```

> The most important restriction when working in a transaction is to ALWAYS use the provided instance of SapClient.
> All operations MUST be executed using the provided SapClient.

## ✅ Test

```bash
# unit tests
$ npm run test

# test coverage
$ npm run test:cov
```

## 💡 Generate Docs

The docs can be generated on-demand. This will produce a **documentation** folder with the required front-end files.

```bash
# generate docs for code
$ npm run doc

# generate docs for code and serve on http://localhost:8080
$ npm run doc:serve
```

## ⬆️ Commitizen

[commitizen](https://github.com/commitizen/cz-cli) is a command line utility that makes it easier to create commit messages following the [conventional commit format](https://conventionalcommits.org) specification.

Use `npm run commit` instead of `git commit` to use commitizen.

## 🔨 Built With

- [NestJS](https://github.com/nestjs/nest)
- [node-rfc](https://github.com/SAP/node-rfc)

## ✔️ Roadmap

The following improvements are currently in progress:

- [x] Dynamic Configuration
- [x] Transaction with auto commit and rollback
- [x] Resource injection by name
- [x] Update to node-rfc 3.x

---
_Source: https://npm.io/package/nestjs-sap-rfc · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
