使用 SAP Cloud SDK 开发应用时,如何通过环境变量的配置避免硬编码

简介: erry 之前的文章SAP S/4HANA Cloud SDK 入门介绍介绍了如何在第三方应用里,调用 SAP S/4HANA Cloud SDK,消费 S/4HANA 系统的 OData 服务。

当时我的代码里,指向 SAP API Business Hub Sandbox 的 url 是硬编码的,不够灵活。本文介绍如何通过环境变量的方式来配置这些 endpoint.

官网链接

在项目根目录新建一个 .env 文件,内容如下:

destinations=[{"name": "sandbox", 
                "url": "https://sandbox.api.sap.com/s4hanacloud"}]

执行如下命令行:


npm install @nestjs/config


为了加载 .env 文件中定义的环境变量,我们需要将 config 包提供的 ConfigModule 添加到应用程序的 @Module 定义中。 打开 app.module.ts 并使用以下代码更新它:


源代码:

import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { BusinessPartnerController } from './business-partner.controller';
@Module({
  imports: [ConfigModule.forRoot()],
  controllers: [AppController, BusinessPartnerController],
  providers: [AppService],
})
export class AppModule {}

将应用代码里 execute 方法的参数,从硬编码的 url,改成传递一个 .env 文件里维护的 Destination 名称即可。

image.png

相关文章
|
2月前
|
开发工具 Android开发 开发者
SAP ABAP Cloud 发展简史
SAP ABAP Cloud 发展简史
SAP ABAP Cloud 发展简史
|
2月前
|
UED
什么是 SAP Commerce Cloud SmartEdit Product Carousel Component Editor
什么是 SAP Commerce Cloud SmartEdit Product Carousel Component Editor
|
2月前
|
数据库 存储 安全
使用 SAP HANA Cloud 的一些优势
使用 SAP HANA Cloud 的一些优势
使用 SAP HANA Cloud 的一些优势
|
2月前
|
机器学习/深度学习 搜索推荐 UED
SAP Commerce Cloud Context Driven Services 概述
SAP Commerce Cloud Context Driven Services 概述
|
2月前
|
监控 搜索推荐 安全
SAP Commerce Cloud Context Driven Services 里 profile-tag.js 的作用介绍
SAP Commerce Cloud Context Driven Services 里 profile-tag.js 的作用介绍
|
2月前
|
存储 消息中间件 搜索推荐
SAP Commerce Cloud Context Driven Services 的 clickStreamEvents HTTP 请求
SAP Commerce Cloud Context Driven Services 的 clickStreamEvents HTTP 请求
|
2月前
|
机器学习/深度学习 人工智能 搜索推荐
SAP Commerce Cloud 智能销售服务 (Intelligent Selling Services)概述
SAP Commerce Cloud 智能销售服务 (Intelligent Selling Services)概述
|
2月前
|
XML 网络安全 开发工具
如何下载并安装 SAP ABAPGit,并完成 ABAP 服务器上 SSL 证书的配置试读版
如何下载并安装 SAP ABAPGit,并完成 ABAP 服务器上 SSL 证书的配置试读版
|
2月前
|
算法 Oracle Unix
安装完后的SAP有关配置
安装完后的SAP有关配置
22 0
|
2月前
|
安全 UED 开发者
SAP Commerce Cloud 配置项 refreshWithLock=false 的作用介绍
SAP Commerce Cloud 配置项 refreshWithLock=false 的作用介绍