Exercise: Implementing resilience in a TypeScript NestJS application

Objectives
After completing this lesson, you will be able to:

After completing this lesson, you will be able to:

  • Implementing resilience in a TypeScript NestJS application

Implementing resilience in a TypeScript NestJS application

Implementing resilience in SAP Cloud SDK application

In this exercise, you will learn how to implement resilience in SAP Cloud SDK application. We will use the Mock Server instead of the actual SAP S/4HANA Cloud.

Prerequisites

For the complete execution of current exercise, you must execute the following activities first:

  1. Execute the following exercises:
    • Creating your Pay-As-You-Go Account in SAP BTP
    • Run the Mock Server on port 8081
    • Complete the exercise Querying Business Partner API of SAP S/4HANA Cloud

Steps

  1. Checkout the branch tsnestjs_v2.0_resilience in SAP Business Application Studio

    1. Open SAP Business Application Studio.

    2. Run the following commands in the terminal. Make sure current working directory is /home/user/projects/cloud-sdk-learning-journey.

      Code snippet
      cd ~/projects/cloud-sdk-learning-journey
      git checkout tsnestjs_v2.0_resilience
      Copy code
  2. Build and run the project

    1. Run the application by executing the following commands in the terminal

      Code snippet
      cd cloudsdk_bp_ts
      npm i
      npm run start:dev
      Copy code
  3. Query the Business Partner API of SAP S/4HANA Cloud

    1. Open the requests.http file

    2. Click on Send Request in the requests.http file to submit the GET request for business partners

    3. Verify that the list of business partners are returned from the Mock Server. Note: If the Mock Server is not running, make sure to run the Mock Server on port 8081 by following the prerequisites at the top

    4. Stop the Mock Server by pressing Ctrl + C in the Mock Server project terminal

    5. Click on Send Request in the requests.http file to submit the GET request for business partners

    6. Start the Mock Server within 30 seconds. Notice that the resilience configuration allows the application to keep retrying with a time out of 30 seconds. Verify that the list of business partners are returned from the Mock Server since the Mock Server was started within 30 seconds

  4. Optional: Review code in the following source files

    1. Note
      Check out the SAP Cloud SDK documentation on Executing an OData GET request using SAP Cloud SDK for JavaScript. https://sap.github.io/cloud-sdk/docs/js/tutorials/getting-started/execute-an-odata-request

      Code walk-through

      File nameComments on code changes
      business-partner.service.ts

      Code logic is as follows...

      1. Import resilience from the @sap-cloud-sdk/resilience module
      2. Use the middleware function to build a resilience middleware. In our example, we will use the following values
        • timeout: 30 seconds
        • circuitBreaker: true
        • retry: true
      package.jsonNew SAP Cloud SDK modules have been installed for the project
      requests.httpProvides various HTTP requests that can be submitted to query the Business Partner API
      commands.txtProvides various commands that can be run on the terminal
  5. Optional: Manually create the project

    1. Create or update the following files in the table below

      File nameTypeContents
      business-partner.service.ts

      Path: ~/projects/cloudsdk_bp_ts/src/business-partner

      Modified

      Copy contents from here...

Save progress to your learning plan by logging in or creating an account

Login or Register