Generators

TypeScript Node.js SDK

Source code
Latest version: 0.9.5

The TypeScript Node.js SDK generator outputs a fully functional TypeScript/JavaScript SDK for server-side use. It can publish the SDK to npmjs.org (or any other Node package repository).

Local

Dump the generated SDK to the local file system. Available on the open source plan.

1
2
3
4
5
6
7
8
groups:
  local:
    generators:
      - name: fernapi/fern-typescript-node-sdk
        version: 0.9.5
        output:
          location: local-file-system
          path: ../generated/sdk/node

Publish an internal package

Publish the generated SDK to a private NPM hosted by Fern. Available on the Starter plan.

1
2
3
4
5
6
7
8
groups:
  internal:
    generators:
      - name: fernapi/fern-typescript-node-sdk
        version: 0.9.5
        output:
          location: npm.buildwithfern.com
          package-name: @fern-imdb/api # replace imdb with your org name

Publish a public package

Publish the generated SDK to npmjs.com. Available on the Starter plan.

1
2
3
4
5
6
7
8
9
10
11
groups:
  publish:
    generators:
      - name: fernapi/fern-typescript-node-sdk
        version: 0.9.5
        output:
          location: npm
          package-name: imdb # replace with your package name
          token: ${NPM_TOKEN}
        github:
          repository: imdb/imdb-node # replace imdb with your org & package name

You can override the registry using the url key.

1
2
3
4
5
6
7
8
9
10
generators:
  groups:
    public:
      - name: fernapi/fern-typescript-node-sdk
        version: 0.7.2
        output:
          location: npm
          package-name: imdb # replace with your package name
          token: ${NPM_TOKEN}
+       url: your-npm-registry.com

Configuration options

You can customize the behavior of the TypeScript generator in generators.yml.

You can include one or more options for config, such as:

1
2
3
4
5
6
7
8
9
10
11
12
- name: fernapi/fern-typescript-node-sdk
  version: 0.9.5
  output:
    location: npm
    package-name: imdb
  github:
    repository: imdb/imdb-node 
+ config: 
+   namespaceExport: IMDb
+   timeoutInSeconds: 120
+   noSerdeLayer: true
+   outputEsm: true

The following options are available:

namespaceExport

  • namespaceExport: Allows you to control the name of the generated namespace export and client class. By default, the exported namespace and client are named based on the organization and API names in the Fern Definition.

Type: string

1
import { IMDbApi, IMDbApiClient } from "imdb";

To customize these names, you can use namespaceExport:

1
2
3
4
5
6
7
8
9
- name: fernapi/fern-typescript-node-sdk
  version: 0.9.5
  output:
    location: npm
    package-name: imdb
  github:
    repository: imdb/imdb-node 
+ config: 
+   namespaceExport: IMDb

The result would be:

1
import { IMDb, IMDbClient } from "imdb";

defaultTimeoutInSeconds

  • defaultTimeoutInSeconds: Allows you to control the timeout of the generated client. The timeout is measured in seconds. This is useful for long-running operations. Set to "infinity" to disable timeouts.

Type: integer or "infinity"
Default: 60

1
2
config: 
  timeoutInSeconds: 120
1
2
config: 
  defaultTimeoutInSeconds: "infinity"

noSerdeLayer

  • noSerdeLayer: Allows you to control whether (de-)serialization code is generated. When true, the client uses JSON.parse() and JSON.stringify() instead.

Type: boolean
Default: false

By default, the generated client includes a layer for serializing requests and deserializing responses. This has three benefits:

  1. The client validates requests and response at runtime, client-side.

  2. The client can support complex types, like Date and Set.

  3. The generated types can stray from the wire/JSON representation to be more idiomatic. For example, when noSerdeLayer is disabled, all properties are camelCase, even if the server is expecting snake_case.

1
2
config: 
  noSerdeLayer: true

outputESM

  • outputEsm: Allows you to control whether the generated TypeScript targets CommonJS or esnext.

Type: boolean
Default: false

By default, the generated TypeScript targets CommonJS. Set outputEsm to true to target esnext instead.

1
2
config: 
  outputEsm: true

outputSourceFiles

  • outputSourceFiles: Allows you to control whether the generator outputs .js and d.ts files.

Type: boolean
Default: false

When disabled (the default), the generator outputs .js and d.ts files.

When enabled, the generator outputs raw TypeScript files.

This config is only applied when dumping the generated SDK to the local file system. It does not apply when publishing to GitHub or npm.

1
2
config: 
  outputSourceFiles: true

includeCredentialsOnCrossOriginRequests

  • includeCredentialsOnCrossOriginRequests: Allows you to set withCredentials to true when making network requests.

Type: boolean
Default: false

To set withCredentials to true when making network requests, set this config to true.

1
2
config: 
  includeCredentialsOnCrossOriginRequests: true

allowCustomFetcher

  • allowCustomFetcher: Allows the end user to specify a custom fetcher implementation.

Type: boolean
Default: false

1
2
config: 
  allowCustomFetcher: true
1
2
3
4
5
const imdb = new IMDbClient({
  fetcher: (args) => {
    ...
  },
});

requireDefaultEnvironment

  • requireDefaultEnvironment: When enabled, the generated client doesn't allow the user to specify a server URL.

Type: boolean
Default: false

1
2
config: 
  requireDefaultEnvironment: true

When disabled (the default), the generated client includes an option to override the server URL:

1
2
3
const imdb = new IMDbClient({
  environment: "localhost:8080",
});

skipResponseValidation

  • skipResponseValidation: When enabled, the generated client will never throw if the response is misshapen. Rather, the client will log the issue using console.warn and return the data (cast to the expected response type).

Type: boolean
Default: false

By default, this config is set to false and the client will throw an error if the response from the server doesn't match the expected type (based on how the response is modeled in the API definition). Set this config to true to never throw an error and log the issue instead.

1
2
config: 
  skipResponseValidation: true

extraDependencies

  • extraDependencies: Allows you to specify extra dependencies in the generated package.json.

Type: map<string,string>
Default: {}

This only applies when publishing to GitHub.

You can use extraDependencies to specify extra dependencies in the generated package.json. This is useful when you utilize .fernignore to supplement the generated client with custom code.

1
2
3
4
5
config: 
  extraDependencies: 
    jest: "^29.7.0" # <-- examples
    "@types/jest": "^29.5.5"
    ts-jest: "^29.1.1"

treatUnknownAsAny

  • treatUnknownAsAny: When enabled, unknown types from Fern are generated into TypeScript using any.

Type: boolean
Default: false

In Fern, there's an unknown type that represents data that isn't knowable at runtime. By default, these types are generated into TypeScript as the unknown type.

1
2
config: 
  treatUnknownAsAny: true

noOptionalProperties

  • noOptionalProperties: Allows you to prevent generating optional properties.

Type: boolean
Default: false

When enabled, the generated properties are never optional. Instead, the type is generated with | undefined.

For example, let's say you have the following Fern Definition:

1
2
3
4
5
types:
  Person:
    properties:
      name: string
      age: optional<integer>

By default, Fern's optional<> properties translate to optional TypeScript properties:

1
2
3
4
interface Person {
  name: string;
  age?: number;
}

When noOptionalProperties is enabled (set to true):

1
2
3
4
interface Person {
  name: string;
  age: number | undefined;
}

useBrandedStringAliases

  • useBrandedStringAliases: When enabled, string aliases are generated as branded strings. This makes each alias feel like its own type and improves compile-time safety.

Type: boolean
Default: false

For example, let's say you have the following Fern Definition:

1
2
3
types:
  MyString: string
  OtherString: string
1
2
3
4
5
export type MyString = string & { __MyString: void };
export const MyString = (value: string): MyString => value as MyString;

export type OtherString = string & { __OtherString: void };
export const OtherString = (value: string): OtherString => value as OtherString;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
function printMyString(s: MyString): void {
  console.log("MyString: " + s);
}

// doesn't compile, "foo" is not assignable to MyString
printMyString("foo");

const otherString = OtherString("other-string");
// doesn't compile, otherString is not assignable to MyString
printMyString(otherString);

// compiles
const myString = MyString("my-string");
printMyString(myString);

When useBrandedStringAliases is disabled (the default), string aliases are generated as normal TypeScript aliases:

1
2
3
export type MyString = string;

export type OtherString = string;

neverThrowErrors

  • neverThrowErrors: When enabled, the client doesn't throw errors when a non-200 response is received from the server. Instead, the response is wrapped in an ApiResponse.

Type: boolean
Default: false

1
2
3
4
5
6
const response = await client.callEndpoint(...);
if (response.ok) {
  console.log(response.body)
} else {
  console.error(response.error)
}