跳转至

Custom aliases(自定义别名(Custom aliases))

Custom aliases are named references that store string values such as configuration parameters, feature flags, or environment-specific settings. By using custom aliases instead of hard-coding values, you can decouple your function logic from specific configurations and make your functions portable across environments.

Define a custom alias

To define a custom alias, open a TypeScript v2 or Python code repository and follow the steps below:

  1. Open the Resource imports side panel and select the Platform SDK tab. You will see a section for custom aliases.

The Resource imports side panel showing the Platform SDK tab with the custom aliases option.

  1. Select New alias to open the alias creation dialog. Provide a Key for the alias name and a Value to associate with it, then select Create.

The new alias dialog with a key set to myAlias and a value set to someValue.

:::callout{theme="neutral"} Alias keys must be unique within the repository. :::

  1. The custom alias will appear in the Custom aliases section.

Edit a custom alias

To edit an existing custom alias, navigate to the Custom aliases section in the Platform SDK tab. Select the pen icon next to the alias to edit its value inline. You can also click on the 3 dots to edit the alias key or delete the alias altogether.

The custom aliases list showing a created alias with an option to edit the value.

Use a custom alias in code

To use a custom alias in your function, import the alias utility and reference the alias by its key:

```typescript tab="TypeScript v2" import { Aliases } from "@osdk/functions";

export default function getCustomValue(): string { return Aliases.custom("myAlias"); }

```python tab="Python"
from functions.aliases import custom
from functions.api import function

@function
def get_custom_value() -> str:
    return custom("myAlias")

Use custom aliases with Marketplace

When you add a function that uses custom aliases to a Marketplace product, the aliases automatically appear as configurable parameters under Inputs. Installers can set the alias values appropriate for their environment without modifying the function source code.

A Marketplace product showing the custom alias as a configurable parameter input alongside the function output.

Set a description

To help installers understand how to configure the alias, you can add a description to the alias parameter. Select the alias under Inputs to open the Details panel, then enter a description on the General tab.

The Details panel for a custom alias parameter showing a description field.

Add presets

You can define preset values for the alias that installers can choose from during installation. In the Details panel, select the Presets tab and choose Manual overrides to define a set of allowed values.

The Presets tab showing manual override configuration with preset values for the alias.

Installation experience

During installation, the installer will see the alias description and can choose from the preset values or configure the alias manually. After installation, the function resolves the alias to the value configured by the installer.

The installation view showing the custom alias parameter with its description and preset value options.


中文翻译

自定义别名(Custom aliases)

自定义别名是存储字符串值(如配置参数、功能开关或环境特定设置)的命名引用。通过使用自定义别名而非硬编码值,您可以将函数逻辑与特定配置解耦,使函数能够在不同环境间移植。

定义自定义别名

要定义自定义别名,请打开 TypeScript v2 或 Python 代码仓库,并按照以下步骤操作:

  1. 打开 资源导入(Resource imports) 侧面板,选择 平台SDK(Platform SDK) 选项卡。您将看到自定义别名部分。

资源导入侧面板显示平台SDK选项卡,其中包含自定义别名选项。

  1. 选择 新建别名(New alias) 打开别名创建对话框。为别名名称提供 键(Key),并关联一个 值(Value),然后选择 创建(Create)

新建别名对话框,键设置为myAlias,值设置为someValue。

:::callout{theme="neutral"} 别名键在仓库内必须唯一。 :::

  1. 自定义别名将出现在 自定义别名(Custom aliases) 部分。

编辑自定义别名

要编辑现有自定义别名,请导航至 平台SDK(Platform SDK) 选项卡中的 自定义别名(Custom aliases) 部分。选择别名旁边的笔图标可内联编辑其值。您也可以点击三个点来编辑别名键或完全删除别名。

自定义别名列表显示已创建的别名,并带有编辑值的选项。

在代码中使用自定义别名

要在函数中使用自定义别名,请导入别名工具并通过键引用别名:

```typescript tab="TypeScript v2" import { Aliases } from "@osdk/functions";

export default function getCustomValue(): string { return Aliases.custom("myAlias"); }

```python tab="Python"
from functions.aliases import custom
from functions.api import function

@function
def get_custom_value() -> str:
    return custom("myAlias")

在Marketplace中使用自定义别名

当您将使用自定义别名的函数添加到 Marketplace产品 时,别名会自动作为可配置参数出现在 输入(Inputs) 下。安装者可以设置适合其环境的别名值,而无需修改函数源代码。

Marketplace产品显示自定义别名作为可配置参数输入,以及函数输出。

设置描述

为帮助安装者理解如何配置别名,您可以为别名参数添加描述。在 输入(Inputs) 下选择别名以打开 详情(Details) 面板,然后在 常规(General) 选项卡中输入描述。

自定义别名参数的详情面板,显示描述字段。

添加预设值

您可以定义别名的预设值,安装者在安装过程中可以选择这些值。在 详情(Details) 面板中,选择 预设(Presets) 选项卡,然后选择 手动覆盖(Manual overrides) 来定义一组允许的值。

预设选项卡显示手动覆盖配置,包含别名的预设值。

安装体验

在安装过程中,安装者将看到别名描述,并可以从预设值中选择或手动配置别名。安装完成后,函数会将别名解析为安装者配置的值。

安装视图显示自定义别名参数及其描述和预设值选项。