> This page is for SDK, version Current (default).
> For other versions, use one of these documentation indexes:
> - Current (default): https://buildwithfern.com/learn/sdks/current/llms.txt
> - Legacy: https://buildwithfern.com/learn/sdks/legacy/llms.txt

> For clean Markdown content of this page, append .md to this URL. For the complete documentation index, see https://buildwithfern.com/learn/llms.txt.

# 添加自定义代码

> 学习如何使用 Fern 向您的 TypeScript SDK 添加自定义逻辑、方法和依赖项。轻松扩展生成的客户端。

本页面介绍如何向您的 TypeScript SDK 添加自定义逻辑、方法和依赖项。

> **Note**
>
> These steps cover adding a new custom file to the SDK. To preserve line-level edits to a generated file, use [Replay](/learn/sdks/overview/custom-code#replay) instead.

## 添加自定义逻辑

开始添加自定义代码：

### 创建新文件并添加您的自定义逻辑

**`src/helper.ts`**

```typescript title="src/helper.ts"
export function myHelper(): void {
    return console.log("Hello world!");
}
```

### 将您的文件添加到 `.fernignore`

**`.fernignore`**

```yaml {3} title=".fernignore"
# 指定不应被 Fern 修改的文件

src/helper.ts
```

### 使用 helper 函数

现在您的用户可以通过从 SDK 导入来使用 helper 函数。

```typescript
import { myHelper } from "sdk/helper";

myHelper();
```

#### 高级：自定义导入路径

默认情况下，TypeScript 生成器只为您的 API 定义在 `package.json` 中自动生成 `exports` 条目。如果您希望消费者通过专用子路径导入您的自定义文件（例如 `import { myHelper } from "@acme/sdk/helper"`），您必须在 `generators.yml` 中使用 [`packageJson.exports`](/learn/zh/sdks/generators/typescript/configuration#packagejson) 手动注册：

**`generators.yml`**

```yaml {5-14} title="generators.yml"
- name: fern-typescript-sdk
  version: 3.108.0
  config:
    packageJson:
      exports:
        "./helper": # 消费者导入路径 — 不是源文件路径
          types: "./dist/cjs/helper.d.ts"
          import:
            types: "./dist/esm/helper.d.mts"
            default: "./dist/esm/helper.mjs"
          require:
            types: "./dist/cjs/helper.d.ts"
            default: "./dist/cjs/helper.js"
```

导出键（例如，`"./helper"`）确定您的用户将使用的导入路径，无需与源文件路径匹配。这些条目会合并到生成的 `exports` 中，因此它们在重新生成时会持久存在。由于 Node.js [强制执行 `exports` 映射](https://nodejs.org/api/packages.html#package-entry-points)，在您在此处添加自定义文件之前，无法通过子路径导入它们。

## 自定义 SDK 方法

Fern also allows you to add custom methods to the SDK itself (e.g.
`client.my_method()` ) by inheriting the Fern generated client and then
extending it.

> **Note**
>
> 查看 Flatfile 在其 [TypeScript SDK](https://github.com/FlatFilers/flatfile-node) 中使用此流程的示例

### 导入并扩展生成的客户端

首先，从 `../client` 导入 Fern 生成的客户端并将其别名为 `FernClient`。
然后，扩展 `FernClient` 并添加您想要的任何方法。

**`src/wrapper/MyClient.ts`**

```typescript title="src/wrapper/MyClient.ts"
import { MyClient as FernClient } from "../client"; // 为 Fern 生成的客户端设置别名

export class MyClient extends FernClient { // 扩展 Fern 生成的客户端

  public myHelper(): void {
    console.log("Hello world!");
  }

}
```

> **Note**
>
> 查看 Flatfile 在其 [FlatfileClient](https://github.com/FlatFilers/flatfile-node/blob/main/src/wrapper/FlatfileClient.ts) 中的示例

### 导出扩展的客户端

更新您的 `index.ts` 文件以导出**扩展的客户端**而不是生成的客户端。

**`src/index.ts`**

```typescript title="src/index.ts"
export { MyClient } from "src/wrapper/MyClient"; // 而不是 "src/client"
```

> **Note**
>
> 查看 Flatfile 的示例 [index.ts](https://github.com/FlatFilers/flatfile-node/blob/main/src/index.ts)

### 更新 `.fernignore`

将 `wrapper` 目录和 `index.ts` 都添加到 `.fernignore`。

**`.fernignore`**

```diff title=".fernignore"
+ src/wrapper
+ src/index.ts
```

> **Note**
>
> 查看 Flatfile 的示例 [.fernignore](https://github.com/FlatFilers/flatfile-node/blob/main/.fernignore)

### 使用方法

Now your users can consume the helper function by importing it from the SDK.

```typescript
client.myHelper()
```

## 自定义依赖项

> **企业功能**
>
> 此功能仅适用于[企业计划](https://buildwithfern.com/pricing)。如需开始使用，请联系 [support@buildwithfern.com](mailto:support@buildwithfern.com)。

要添加您的自定义代码所需的包，请在您的 `generators.yml` 中使用 [`extraDependencies`](/learn/zh/sdks/generators/typescript/configuration#extraDependencies) 和 [`extraDevDependencies`](/learn/zh/sdks/generators/typescript/configuration#extraDevDependencies) 选项。

**`generators.yml`**

```yaml {4-7} title="generators.yml"
- name: fern-typescript-sdk
  version: 3.108.0
  config:
    extraDependencies:
      lodash-es: '1.0.0'
    extraDevDependencies:
      "@types/lodash-es": '1.0.0'
```