Skip to content

Commit 9ed1e90

Browse files
authored
Add Custom Code page (Typescript) (#52)
1 parent 3bc13e3 commit 9ed1e90

File tree

1 file changed

+128
-2
lines changed

1 file changed

+128
-2
lines changed

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

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

6-
Learn how to extend your Fern TypeScript SDK with custom code and utilities.
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 SDK.
710

8-
<Warning>This page is a WIP, please refer to our previous [documentation](https://buildwithfern.com/learn/sdks/capabilities/custom-code).</Warning>
11+
## Adding custom logic
12+
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:
21+
22+
<Steps>
23+
24+
### Create a new file and add your custom logic
25+
26+
```typescript title="src/helper.ts"
27+
export function myHelper(): void {
28+
return console.log("Hello world!");
29+
}
30+
```
31+
32+
### Add your file to `.fernignore`
33+
34+
<Tip>A `.fernignore` file is automatically created in your SDK repository when you use GitHub publishing.</Tip>
35+
36+
37+
```yaml {3} title=".fernignore"
38+
# Specify files that shouldn't be modified by Fern
39+
40+
src/helper.ts
41+
```
42+
43+
### Consume the helper
44+
45+
Now your users can consume the helper function by importing it from the SDK:
46+
47+
```typescript
48+
import { myHelper } from "sdk/helper";
49+
50+
myHelper();
51+
```
52+
</Steps>
53+
54+
## Custom SDK methods
55+
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.
59+
60+
<Note>
61+
See an example from Flatfile using this process in their [TypeScript SDK](https://github.com/FlatFilers/flatfile-node)
62+
</Note>
63+
64+
<Steps>
65+
### Import and extend the generated client
66+
67+
First, import the Fern generated client from `../client` and alias it to `FernClient`.
68+
Next, extend `FernClient` and add whatever methods you want.
69+
70+
```typescript title="src/wrapper/MyClient.ts"
71+
import { MyClient as FernClient } from "../client"; // alias the Fern generated client
72+
73+
export class MyClient extends FernClient { // extend the Fern generated client
74+
75+
public myHelper(): void {
76+
console.log("Hello world!");
77+
}
78+
79+
}
80+
```
81+
82+
<Note>
83+
See an example from Flatfile doing this in their [FlatfileClient](https://github.com/FlatFilers/flatfile-node/blob/main/src/wrapper/FlatfileClient.ts)
84+
</Note>
85+
86+
### Export the extended client
87+
88+
Update your `index.ts` file to export the **extended client** instead of the generated client.
89+
90+
```typescript title="src/index.ts"
91+
export { MyClient } from src/wrapper/MyClient; // instead of `src/Client`
92+
```
93+
94+
<Note>
95+
See an example [index.ts](https://github.com/FlatFilers/flatfile-node/blob/main/src/index.ts) from Flatline
96+
</Note>
97+
98+
### Update `.fernignore`
99+
100+
Add both the `wrapper` directory and `index.ts` to `.fernignore`.
101+
102+
```diff title=".fernignore"
103+
+ src/wrapper
104+
+ src/index.ts
105+
```
106+
107+
<Note>
108+
See an example [.fernignore](https://github.com/FlatFilers/flatfile-node/blob/main/.fernignore) from Flatline
109+
</Note>
110+
111+
112+
### Consume the method
113+
114+
Now your users can consume the helper function by importing it from the SDK:
115+
116+
```typescript
117+
client.myHelper()
118+
```
119+
</Steps>
120+
121+
122+
## Custom dependencies
123+
124+
To add packages that your custom code requires, update your `generators.yml`.
125+
126+
```yaml {4-7} title="generators.yml"
127+
- name: fernapi/fern-typescript-sdk
128+
version: "..."
129+
config:
130+
extraDependencies:
131+
lodash-es: '1.0.0'
132+
extraDevDependencies:
133+
"@types/lodash-es": '1.0.0'
134+
```

0 commit comments

Comments
 (0)