跳到导航

Adding custom code

以 Markdown 格式查看
You're viewing the legacy SDK docs

These pages cover SDK generation configured with generators.yml. New projects use sdk-config.yml, so see the Current version of this page for up-to-date guidance. To move an existing project, follow the SDK Config migration guide, which walks through fern sdk migrate, and review the sdk-config.yml reference.

Enterprise feature

This feature is available only for the Enterprise plan. To get started, reach out to support@buildwithfern.com.

This page covers how to add custom logic and methods to your Go SDK.

Adding custom logic

To get started adding custom code:

These steps cover adding a new custom file to the SDK. To preserve line-level edits to a generated file, use Replay instead.
1

Create a new file and add your custom logic

helper.go
func MyHelper() {
fmt.Println("Hello World!")
}
2

Add your file to .fernignore

.fernignore
# Specify files that shouldn't be modified by Fern
helper.go
3

Consume the helper

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

import "github.com/package/example"
example.MyHelper();

Adding custom SDK methods

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.

1

Update generators.yml configuration

Name your Fern-generated client something like BaseClient to reflect that this client will be extended.

generators.yml
- name: fern-go-sdk
version: "..."
config:
clientName: BaseClient
2

Import and extend the generated client

First, import the Fern generated base client and extend it. Then, add whatever methods you want.

client/my_client.go
type MyClient struct {
*Client // Embed the Fern generated client.
}
func NewMyClient(opts ...option.RequestOption) *MyClient {
return &MyClient{
Client: NewClient(opts...),
}
}
func (m *MyClient) MyHelper() {
fmt.Println("Hello World!")
}
3

Update .fernignore

Add the client/my_client.go. to .fernignore.

.fernignore
+ client/my_client.go
4

Consume the method

Instead of constructing the generated client, your users will want to construct the extended client.

main.go
import exampleclient "github.com/package/example/client"
client := exampleclient.NewMyClient():

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

client.MyHelper()