|
1 | 1 | --- |
2 | 2 | title: Adding custom code |
3 | | -description: Augment your TypeScript SDK with custom utilities |
| 3 | +description: Augment your Go SDK with custom utilities |
4 | 4 | --- |
5 | 5 |
|
6 | | -Learn how to extend your Fern Go SDK with custom code and utilities. |
| 6 | +<Markdown src="/products/sdks/snippets/custom-code-intro.mdx"/> |
7 | 7 |
|
8 | | -<Warning>This page is a WIP, please refer to our previous [documentation](https://buildwithfern.com/learn/sdks/capabilities/custom-code).</Warning> |
| 8 | +## Adding custom logic |
| 9 | + |
| 10 | +<Markdown src="/products/sdks/snippets/custom-logic-intro.mdx"/> |
| 11 | + |
| 12 | +<Steps> |
| 13 | + |
| 14 | + ### Create a new file and add your custom logic |
| 15 | + |
| 16 | + ```go title="helper.go" |
| 17 | + func MyHelper() { |
| 18 | + fmt.Println("Hello World!") |
| 19 | + } |
| 20 | + ``` |
| 21 | + |
| 22 | + ### Add your file to `.fernignore` |
| 23 | + |
| 24 | + <Tip>A `.fernignore` file is automatically created in your SDK repository when you use GitHub publishing.</Tip> |
| 25 | + |
| 26 | + |
| 27 | + ```yaml {3} title=".fernignore" |
| 28 | + # Specify files that shouldn't be modified by Fern |
| 29 | + |
| 30 | + helper.go |
| 31 | + ``` |
| 32 | + |
| 33 | + ### Consume the helper |
| 34 | + |
| 35 | + <Markdown src="/products/sdks/snippets/consume-method.mdx"/> |
| 36 | + |
| 37 | + ```go |
| 38 | + import "github.com/package/example" |
| 39 | + |
| 40 | + example.MyHelper(); |
| 41 | + ``` |
| 42 | +</Steps> |
| 43 | + |
| 44 | + ## Adding custom SDK methods |
| 45 | + |
| 46 | + <Markdown src="/products/sdks/snippets/custom-sdk-methods-intro.mdx"/> |
| 47 | + |
| 48 | + <Steps> |
| 49 | + ### Update `generators.yml` configuration |
| 50 | + Name your Fern-generated client something like `BaseClient` to reflect that this client will be extended. |
| 51 | + |
| 52 | + ```yml {4} title="generators.yml" |
| 53 | + - name: fernapi/fern-java-sdk |
| 54 | + version: "..." |
| 55 | + config: |
| 56 | + client-class-name: BaseClient |
| 57 | + ``` |
| 58 | +
|
| 59 | + ### Import and extend the generated client |
| 60 | +
|
| 61 | + First, import the Fern generated base client and extend it. Then, add whatever methods you want. |
| 62 | +
|
| 63 | + ```go title="client/my_client.go" |
| 64 | + type MyClient struct { |
| 65 | + *Client // Embed the Fern generated client. |
| 66 | + } |
| 67 | + |
| 68 | + func NewMyClient(opts ...option.RequestOption) *MyClient { |
| 69 | + return &MyClient{ |
| 70 | + Client: NewClient(opts...), |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + func (m *MyClient) MyHelper() { |
| 75 | + fmt.Println("Hello World!") |
| 76 | + } |
| 77 | + ``` |
| 78 | + |
| 79 | + ### Update `.fernignore` |
| 80 | + |
| 81 | + Add the `client/my_client.go.` to `.fernignore`. |
| 82 | + |
| 83 | + ```diff title=".fernignore" |
| 84 | + + client/my_client.go |
| 85 | + ``` |
| 86 | + |
| 87 | + ### Consume the method |
| 88 | + |
| 89 | + Instead of constructing the generated client, your users will want to construct the extended client. |
| 90 | + |
| 91 | + ```go title="main.go" |
| 92 | + import exampleclient "github.com/package/example/client" |
| 93 | + |
| 94 | + client := exampleclient.NewMyClient(): |
| 95 | + ``` |
| 96 | + |
| 97 | + <Markdown src="/products/sdks/snippets/consume-method.mdx"/> |
| 98 | + |
| 99 | + ```go |
| 100 | + client.MyHelper() |
| 101 | + ``` |
| 102 | + </Steps> |
0 commit comments