The official link
To create an application that already contains all the files and configuration you need to use the SAP Cloud SDK for JavaScript, you can use the SDK’s command-line interface (CLI). To get the CLI, run the following command:
npm install -g @sap-cloud-sdk/cli
This will install the CLI globally on your machine, allowing you to use it anywhere. Now you can create a new project by running the CLI init command:
sap-cloud-sdk init my-sdk-project
Installation completed:
In the generated project, package.json defines the following dependencies:
Run the command to start the initialized project, and you will see the message that the application started successfully:
npm run start:dev
website
Set up the API Server
There are several ways to set up an API Server, you can set up your own Mock Server, or you can use the Sandbox API.
In order to invoke an OData service, there needs to be a service to invoke. You can follow the instructions here to set up a local mock server that simulates business partners and custom services. This mock server does not support all the functionality of the real OData service, but it is sufficient to try it out locally.
Once it’s up and running, you should see a list of services at http://localhost:3000/.
Alternatively, you can use the SAP API Business Hub’s sandbox to test many APIs. To use the sandbox, you need an API key. Go to https://api.sap.com and click “Login” in the upper right corner. If you don’t have an account, you need to register first. Once logged in, click ‘Hi’ in the upper right corner, and then click ‘Preferences’ in the drop-down menu that just opened. On the preferences page, click Show API Key.
Add a custom route
Initially, the application contained only index and hello-world routing. We will add another route for business partners, which will list all available business partners.
First, create a new file in the SRC/directory, business-partner.controller.ts, and add an implementation for this route, as shown below:
The @Controller() decorator marks our class as a Controller(that is, the thing that handles requests), The @GET (‘business-partners’) decorator marks the getBusinessSpartners method as the handler of the GET request on the path /business-partners.
In order for the controller to work, we also need to register it in our application. Open app.module.ts, import the controller class you just created and add it to the controller declaration.
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { BusinessPartnerController } from './business-partner.controller';
@Module({
imports: [],
controllers: [AppController, BusinessPartnerController],
providers: [AppService],
})
export class AppModule {}
To invoke the OData service using the SAP Cloud SDK for JavaScript, add the virtual data model (VDM) of this service to your dependency. In this article, we use VDM for business partner services. Install it with the following command:
npm install @sap/cloud-sdk-vdm-business-partner-service
SAP Cloud SDK for JavaScript provides packages for each OData service exposed by SAP S/4HANA Cloud. You can find a list of these services in the SAP API Business Center, and a list of the corresponding packages in our documentation.
Create a function getAllBusinessARTners in business-partner.controller.ts and implement it according to your API server:
In the following code snippet, we assume that you have a mock server running locally on port 3000. Documentation on the mock server can be found here.
function getAllBusinessPartners(): Promise<BusinessPartner[]> {
return BusinessPartner.requestBuilder()
.getAll()
.execute({
url: 'http://localhost:3000'
});
}
- In line 2, we are creating a request builder for the business partner entity.
- Line 3 indicates that we want to create a request to get all business partners.
- Line 4 is responsible for executing and sending a request to the URL based on the given target URL.
Because network requests are asynchronous in nature, the return value of this function is a Promise to the list of business partners (Promise<BusinessPartner[]>).
import { Controller, Get, HttpException } from '@nestjs/common';
import { BusinessPartner } from '@sap/cloud-sdk-vdm-business-partner-service';
@Controller()
export class BusinessPartnerController {
@Get('business-partners')
getBusinessPartners() {
return getAllBusinessPartners()
.catch(error => {
throw new HttpException(`Failed to get business partners - ${error.message}`, 500);
});
}
}
function getAllBusinessPartners(): Promise<BusinessPartner[]> {
return BusinessPartner.requestBuilder()
.getAll()
.execute({
url: 'https://sandbox.api.sap.com/s4hanacloud',
});
}
The final result: Successfully obtained the Business Partners data from the SAP API sandbox system:
More of Jerry’s original articles can be found on “Wang Zixi “: