Generate an SDK
Generate a typed SDK from an OpenAPI document, then call its operations with checked requests and responses.
The SDK is a client and types. The client checks and converts each request and each response, so your code never calls a conversion function. The SDK has no model classes.
Install
npm install @exprtlabs/golem
npm install --save-dev @exprtlabs/golem-openapi-ts typescript@~7.1.0-betaWith the MobX extension, install @exprtlabs/golem-openapi-ts in dependencies, because generated files import its MobX runtime.
Requests and responses are validated by default (validation profiles). Choose how to build in Build tools. Include the generated sdk/ directory in the TypeScript program that builds your app. Ignore it in git with the other generated files.
Point to your OpenAPI document
Add golem.yaml at the project root. The example keeps it in server/. Paths are relative to this file. name: Provider names the client ProviderClient. This excerpt leaves out two keys. The validation root key sets runtime validation. The extensions key of the block turns on MobX.
- input: provider/provider.yaml
output: provider/sdk
name: ProviderThe document is the contract. Keep it in the project or point input at the file that your API already owns. The generator reads the features in OpenAPI support. The example's provider.yaml declares createCharge (POST /charges), and list, get, text and event operations:
post:
operationId: createCharge
parameters:
- in: header
name: Idempotency-Key
schema: { type: string }
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Money'
responses:
'201':
description: Created charge
content:
application/json:
schema:
$ref: '#/components/schemas/Charge'Generation stops on an invalid document and reports each problem.
Generate the SDK
npx golem generateThe command writes the sdk/ directory under output. The example writes provider/sdk/.
What to import
Import ProviderClient and the data types from the sdk/index.js file under your output directory. The example's provider/client.ts imports ./sdk/index.js. Import operation factories from the public paths in Generated files.
Golem replaces only the files it generated. Keep your own code, including models over SDK types, outside sdk/. A file there that golem did not generate stops generation.
Change provider.yaml and generate again when the contract changes.
Choose a mode
An SDK job has a mode key (SDK modes). The calls are the same in both modes.
| Mode | Use it when | What the SDK holds |
|---|---|---|
standalone (default) | You get a ready SDK and have no golem setup, or you publish the SDK | The client, the types, the checks and conversions, and a copy of the golem runtime that it uses |
golem | Your app runs golem and has models in the same golem.yaml | The client and the types. The models job writes the checks and conversions with your models |
- A standalone SDK imports nothing from
@exprtlabs/golem. Withdates: temporal, installtemporal-polyfilland load it before the first call. - A golem mode SDK needs a
modelblock in the same file. Addmode: golemto the job. - An SDK in a block with the
mobxextension needsmode: golem(MobX and React).
First call
export async function listCharges(baseUrl: string, token: string) {
const client = new ProviderClient({ baseUrl, auth: { bearerAuth: token } });
const operation = client.listCharges();
return operation.execute({});
}Set baseUrl to your service and pass credentials as in Authentication. client.listCharges() creates a reusable operation. .execute({}) sends GET /charges and returns the response. Add parameters and a body, and read the response and check the status before you read the body.
Full example: examples/payments.
