SAP Cloud SDK for JavaScript

repository·main·Indexed 42 days ago

https://github.com/sap/cloud-sdk-js

A set of libraries designed to reduce development effort when building side-by-side extension applications on SAP Business Technology Platform (BTP) that integrate with SAP solutions like SAP S/4HANA and SAP SuccessFactors. Version 4.9.1 includes packages for OData v2 and v4 clients, OpenAPI generation, HTTP client functionality with destination lookup, and resilience middlewares for timeouts and circuit breakers.

Tokens
61.1K
Snippets
192
Records
343
Agent score
53%

What's inside SAP Cloud SDK for JavaScript

  1. Overview of @sap-cloud-sdk/generator-common

    main

    The @sap-cloud-sdk/generator-common package provides shared utility functions used by both the OData client generator and the OpenAPI client generator within the SAP Cloud SDK.

    Warning: The APIs in this package are considered internal. They are subject to change in newer versions without notice. Use them with caution if you are building custom generator extensions.

  2. Overview of @sap-cloud-sdk/eslint-config

    main

    The @sap-cloud-sdk/eslint-config package provides a shareable ESLint configuration used by the SAP Cloud SDK. It is designed to maintain code quality and consistency.

    Key features include:

    • Extends eslint:recommended.
    • Extends @typescript-eslint/recommended.
    • Integrates eslint-config-prettier to prevent formatting conflicts when using Prettier.
  3. What is @sap-cloud-sdk/http-client?

    main

    The @sap-cloud-sdk/http-client package provides generic HTTP client functionality enhanced with SAP infrastructure capabilities. It sits on top of a standard HTTP client to automate connectivity tasks, including:

    • Destination lookup: Resolving service destinations configured in the SAP BTP environment.
    • SAP S/4HANA On-premise connectivity: Managing connections to on-premise systems.
    • Web proxies: Handling proxy configurations for outbound requests.
  4. OpenAPI Client generation approach

    main

    The SAP Cloud SDK for JavaScript uses a custom, TypeScript-based OpenAPI generator to create API clients. This replaces the previous dependency on the Java-based @openapitools/openapi-generator-cli.

    Key benefits of this approach include:

    • No Java Runtime Required: Users do not need a JRE installed to generate clients.
    • Smaller Package Size: The project no longer needs to bundle a ~24MB .jar file.
    • Faster Generation: Generation is significantly faster (approximately 3.5s compared to 15s for the Java-based version).
    • Improved API Design: The custom generator allows for better handling of unique names and API file structures without requiring manual wrappers or workarounds.
  5. How OData V4 Enums are handled in the SDK

    main

    The SAP Cloud SDK handles OData V4 Enum types by using a specialized EnumField class instead of a generic EdmTypeField. This approach ensures type safety when building filters, preventing developers from passing arbitrary strings or numbers that do not correspond to the defined enum entries.

    Key Characteristics

    • Type Safety: When building filters (e.g., using .equal()), the SDK restricts inputs to the valid members of the specific enum type.
    • Underlying Types: The SDK parses metadata to capture the enum name, underlying type (e.g., Edm.Int32, Edm.Int64), keys, and values.
    • Implementation Detail: For Edm.Int64 underlying types, the SDK uses bignumber.js or string to avoid precision loss associated with the standard JavaScript number type.
  6. Configure resilience options in connectivity

    main

    In version 3.24.0, the @sap-cloud-sdk/connectivity and @sap-cloud-sdk/resilience packages introduced ResilienceOptions and a resilience() function. The resilience() function returns an array of middlewares that can be applied to requests to improve stability. Additionally, a retry option was added to DestinationFetchOption to handle unstable token exchanges performed by the destination service.

    // Concept: Using resilience middlewares (introduced in 3.24.0)
    // The resilience() function returns an array of middlewares based on ResilienceOptions
    const middlewares = resilience(resilienceOptions);
  7. Use @sap-cloud-sdk/odata-v2 or @sap-cloud-sdk/odata-v4 instead of @sap-cloud-sdk/odata-common

    main

    The @sap-cloud-sdk/odata-common package contains shared internal functionality for OData clients but is not intended for direct use.

    To interact with OData services, you should use the specific versioned packages:

    • For OData V2 services: Use @sap-cloud-sdk/odata-v2
    • For OData V4 services: Use @sap-cloud-sdk/odata-v4
  8. How URL encoding is handled in OData and OpenAPI requests

    main

    The SAP Cloud SDK uses encodeURIComponent to ensure that user-provided input values within URLs are correctly encoded, preventing issues with special characters that could break the URL structure. This is particularly important for characters like /, which encodeURI fails to encode properly.

    What is encoded:

    OData Requests

    • Entity Key Values: Values used within entity paths, such as /Entity(KeyName='va/ue'). The value va/ue is encoded to va%2Fue.
    • Navigation Property Key Values: Values used within navigation property paths.
    • $filter Query Parameters: The content of the $filter option. For example, $filter=Gender eq '/Female' is encoded to Gender%20eq%20%27%2FFemale%27.

    OpenAPI Requests

    • Path Parameters: User input values provided for path parameters, such as /entities/{entityId}. If entityId is a b, it is encoded to a%20b.
    • Query Parameters: The values of all query parameters are encoded.

    What is NOT encoded by the SDK:

    • Host: The SDK does not adjust the host (e.g., www.häuser.de). Users should use Internationalized Domain Name (IDN) converters if necessary.
    • Service Path: Assumed to have no special characters; users can configure service mapping files if encoding is required.
    • Entity/Key Names: Names derived from metadata are assumed to be safe.
    • OData Query Options: Standard options like $top, $skip, $orderby, $select, and $expand are not treated as user input for encoding purposes (except for the content within $filter).
  9. Property and query parameter naming behavior

    main

    The generator does not perform camel case renaming on properties or query parameters. It preserves the exact names provided in the OpenAPI specification.

    Implications for Developers:

    • Fidelity: This avoids collisions and complexity when dealing with allOf or overlapping property names (e.g., some_property vs some-property).
    • TypeScript Usage: Generated interfaces and types will use the original casing from the spec. While this may look unconventional in JavaScript/TypeScript (e.g., using snake_case or kebab-case keys), it ensures that request/response bodies and query parameters match the underlying API specification exactly.
  10. Understand the SAP Cloud SDK API categories

    main

    The SAP Cloud SDK categorizes all exported objects (constants, functions, interfaces, types, classes) into three distinct tiers to ensure stability and facilitate refactoring:

    1. Exported Public API: The stable contract you should rely on for your applications. These are reachable via the root package import (e.g., @sap-cloud-sdk/odata-v2).
    2. Exported Internal API: Technical exports not intended for direct consumer usage. These are stable only for path versions and are accessible via the /internal subpath (e.g., @sap-cloud-sdk/odata-v2/internal).
    3. Not exported: The default state for most objects. If an object is not in one of the two categories above, it is not accessible to consumers.

    Note on internal packages: Some packages like @sap-cloud-sdk/odata-common and @sap-cloud-sdk/generator-common are designed for internal use only and do not expose a public API at their root level. You should instead use the public APIs provided by @sap-cloud-sdk/odata-v2, @sap-cloud-sdk/odata-v4, or the generator packages.