Skip to content

Commit 43085f3

Browse files
authored
Add Java Custom Code Page (#58)
1 parent d700d3c commit 43085f3

File tree

6 files changed

+139
-22
lines changed

6 files changed

+139
-22
lines changed

fern/products/sdks/overview/java/custom-code.mdx

Lines changed: 113 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,117 @@ title: Adding custom code
33
description: Augment your Java SDK with custom utilities
44
---
55

6-
Learn how to extend your Fern Java SDK with custom code and utilities.
6+
<Markdown src="/products/sdks/snippets/custom-code-intro.mdx"/>
77

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+
```java title="src/main/java/<package>/Helper.java"
17+
package com.example.helper;
18+
19+
public class Helper {
20+
21+
public static void myHelper() {
22+
System.out.println("Hello World!");
23+
}
24+
25+
}
26+
```
27+
28+
### Add your file to `.fernignore`
29+
30+
<Tip>A `.fernignore` file is automatically created in your SDK repository when you use GitHub publishing.</Tip>
31+
32+
33+
```yaml {3} title=".fernignore"
34+
# Specify files that shouldn't be modified by Fern
35+
36+
src/main/java/<package>/Helper.java
37+
```
38+
39+
### Consume the helper
40+
41+
<Markdown src="/products/sdks/snippets/consume-method.mdx"/>
42+
43+
```java
44+
import com.example.helper.Helper;
45+
46+
public class Main {
47+
48+
public static void main(String[] args) {
49+
Helper.myHelper();
50+
}
51+
52+
}
53+
```
54+
</Steps>
55+
56+
## Adding custom SDK methods
57+
58+
<Markdown src="/products/sdks/snippets/custom-sdk-methods-intro.mdx"/>
59+
60+
<Steps>
61+
### Update `generators.yml` configuration
62+
Name your Fern-generated client something like `BaseClient` to reflect that this client will be extended.
63+
64+
```yml {4} title="generators.yml"
65+
- name: fernapi/fern-java-sdk
66+
version: "..."
67+
config:
68+
client-class-name: BaseClient
69+
```
70+
71+
### Import and extend the generated client
72+
73+
First, import the Fern generated base client and extend it. Then, add whatever methods you want.
74+
75+
```java title="src/main/java/com/example/MyClient.java"
76+
package com.example;
77+
78+
import com.example.client.BaseClient;
79+
80+
public class MyClient extends BaseClient { // extend the Fern generated client
81+
82+
public void myHelper() {
83+
System.out.println("Hello World!");
84+
}
85+
86+
}
87+
```
88+
89+
### Update `.fernignore`
90+
91+
Add the `MyClient.java` to `.fernignore`.
92+
93+
```diff title=".fernignore"
94+
+ src/main/java/com/example/MyClient.java
95+
```
96+
97+
### Consume the method
98+
99+
<Markdown src="/products/sdks/snippets/consume-method.mdx"/>
100+
101+
```java
102+
client.myHelper();
103+
```
104+
</Steps>
105+
106+
107+
## Adding custom dependencies
108+
109+
To add packages that your custom code requires, update your `generators.yml`.
110+
111+
```yaml {4-7} title="generators.yml"
112+
- name: fernapi/fern-java-sdk
113+
version: "..."
114+
config:
115+
custom-dependencies:
116+
- org.apache.commons:commons-lang3:3.12.0
117+
- org.slf4j:slf4j-api:2.0.7
118+
119+
```

fern/products/sdks/overview/python/custom-code.mdx

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,11 @@ title: Adding custom code
33
description: Augment your Python SDK with custom utilities
44
---
55

6-
Fern-generated SDKs are designed to be extended with custom code. Your custom
7-
code can add additional functionality to the SDK and live in harmony with the
8-
generated code. This page explains how to configure custom logic using a
9-
`.fernignore` file, create custom SDK methods, and add additional dependencies to your Python SDK.
6+
<Markdown src="/products/sdks/snippets/custom-code-intro.mdx"/>
107

118
## Adding custom logic
129

13-
If you want your SDK to do more than just make basic API calls (like combining
14-
multiple calls, processing data, adding utilities), you can use `.fernignore` to
15-
protect your custom code from being overwritten during regeneration.
16-
17-
Simply add your custom files to the SDK repository and list them out in `.fernignore`. Fern
18-
won't override any files that you add in `.fernignore`.
19-
20-
To get started adding custom code:
10+
<Markdown src="/products/sdks/snippets/custom-logic-intro.mdx"/>
2111

2212
<Steps>
2313

@@ -42,7 +32,7 @@ To get started adding custom code:
4232

4333
### Consume the helper
4434

45-
Now your users can consume the helper function by importing it from the SDK:
35+
<Markdown src="/products/sdks/snippets/consume-method.mdx"/>
4636

4737
```python
4838
from package.helper import my_helper
@@ -53,9 +43,7 @@ To get started adding custom code:
5343

5444
## Adding custom SDK methods
5545

56-
Fern also allows you to add custom methods to the SDK itself (e.g.
57-
`client.my_method()` ) by inheriting the Fern generated client and then
58-
extending it.
46+
<Markdown src="/products/sdks/snippets/custom-sdk-methods-intro.mdx"/>
5947

6048
<Note>
6149
See an example from ElevenLabs using this process in their [Python SDK](https://github.com/elevenlabs/elevenlabs-python/blob/main/src/elevenlabs/client.py).
@@ -64,9 +52,9 @@ To get started adding custom code:
6452
<Steps>
6553
### Update `generators.yml` configuration
6654

67-
To add a custom method to the Python SDK, you will need to configure the
68-
generator to output the client in a file called `base_client.py`. Then, you can
69-
extend the base client and add whatever methods you want.
55+
Name your Fern-generated client something like `BaseClient` to reflect
56+
that this client will be extended. Configure the generator to output the
57+
client in a file called `base_client.py`.
7058

7159
```yaml {4-8} title="generators.yml"
7260
- name: fernapi/fern-python-sdk
@@ -121,7 +109,7 @@ To get started adding custom code:
121109

122110
### Consume the method
123111

124-
Now your users can consume the helper function by importing it from the SDK:
112+
<Markdown src="/products/sdks/snippets/consume-method.mdx"/>
125113

126114
```python
127115
client.my_helper()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Now your users can consume the helper function by importing it from the SDK.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Fern-generated SDKs are designed to be extended with custom code. Your custom
2+
code can add additional functionality to the SDK and live in harmony with the
3+
generated code.
4+
5+
This page explains how to configure custom logic using a
6+
`.fernignore` file, create custom SDK methods, and add additional dependencies to your SDK.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
If you want your SDK to do more than just make basic API calls (like combining
2+
multiple calls, processing data, adding utilities), you can use `.fernignore` to
3+
protect your custom code from being overwritten during regeneration.
4+
5+
Simply add your custom files to the SDK repository and list them out in `.fernignore`. Fern
6+
won't override any files that you add in `.fernignore`.
7+
8+
To get started adding custom code:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fern also allows you to add custom methods to the SDK itself (e.g.
2+
`client.my_method()` ) by inheriting the Fern generated client and then
3+
extending it.

0 commit comments

Comments
 (0)