Skip to main content

Improved TypeScript Code Samples

· 6 min read

The generated code samples are now idiomatic with no errors. This improves the developer experience by allowing developers to run the code samples without any errors in their applications and to get started quickly.

Details

This release introduces significant improvements to the code samples generated for your API. We've made several updates to the code samples to ensure that they are idiomatic, easier to understand, and production-ready.

The new code samples are designed to enhance the developer experience which will allow users to grasp the intention of an endpoint as to what the parameters are and how they are being initialized. This will let them get started with your API quickly by skimming through the code samples.

What Has Changed?

To meet the standards, the updated code samples are passed through the linter i.e. eslint (click here to see the configuration being used).

The following major improvements are now made part of the TypeScript code samples:

  • Simplified code samples to make them easier to read and understand with the help of properly indented code samples and inline-initialization of the parameters.
  • Fixed linting issues in the updated code samples to ensure that they comply with the best practices in terms of coding styles and standards.
  • Fixed bugs and errors in the updated code samples to ensure that they work as expected.

Parameter Initialization Section

Model Initialization

Made the model's field initialization in-line which ensures that the object structure is intact, improving code readability.

Before
const modelBoss: Person = {
age: BigInt(30),
name: 'Joe',
};


const model: Employee = {
name: 'John',
hiredAt: 'Mon, 15 Jun 2009 20:45:30 GMT',
boss: modelBoss
}
After
const model: Employee = {
name: 'John',
hiredAt: 'Mon, 15 Jun 2009 20:45:30 GMT',
boss: {
age: BigInt(30),
name: 'Joe’
},
}

Array Initialization

Updated the array initialization step to multi-line to improve code readability.

Before
const numberArray: number[] = [210, 211, 212];
After
const numberArray: number[] = [
210,
211,
212
];

Map Initialization

Updated the map initialization step to multi-line to improve code readability.

Before
const numberMap: Record<string, number> = {'key0': 110, 'key1': 111, 'key2': 112 };
After
const numberMap: Record<string, number> = {
'key0': 110,
'key1': 111,
'key2': 112
};

Client Initialization Section

Server Configuration Parameters are now configurable through portal.

Before
const client = new Client({
timeout: 0,
});
After
const client = new Client({
timeout: 0,
port: '8080',
suites: SuiteCode.Spades,
});

Imports Section Improvements​

Previously, model imports were added even if they were not required in the parameters initialization sections. The inclusion of unnecessary imports was causing compilation issues but now we are adding imports on demand for optional parameters.

Before
import {
ApiError,
Client,
Person,
ResponseTypesController,
} from 'testerlib';

# By removing the Person optional parameter, Person import is not removed from import section

const { result, ...httpResponse } = await responseTypesController.send(numberArray, numberMap);
After
import {
ApiError,
Client,
ResponseTypesController,
} from 'testerlib';

# By removing the Person optional parameter, Person import is also removed from import section

const { result, ...httpResponse } = await responseTypesController.send(
numberArray,
numberMap
);

API Call Section

With Parameters

Addressed the styling of an API call method with parameters and made it multi-line in case the parameters count is more than 1. This will improves the overall readability of an endpoint call.

Before
const responseTypesController = new ResponseTypesController(client);

try {
const { result, ...httpResponse } = await responseTypesController.send(employeeModel, numberArray, numberMap);
// Get more response info...
// const { statusCode, headers } = httpResponse;
} catch(error) {
if (error instanceof ApiError) {
const errors = error.result;
// const { statusCode, headers } = error;
}
}
After
const responseTypesController = new ResponseTypesController(client);

try {
const { result, ...httpResponse } = await responseTypesController.send(
employeeModel,
numberArray,
numberMap
);
// Get more response info...
// const { statusCode, headers } = httpResponse;
} catch (error) {
if (error instanceof ApiError) {
const errors = error.result;
// const { statusCode, headers } = error;
}
}

A Comparison of Complete Example Code Sample

The following example shows the complete comparison between the code sample generation for initializing an employee model as an input parameter for ResponseTypesController Send endpoint.

Employee Model

"employee_model": {
"name": "John",
"hiredAt": "Mon, 15 Jun 2009 20:45:30 GMT",
"boss": {
"name": "Joe",
"age": 30
}
}
Before
import { ApiError, Client, Person, ResponseTypesController } from 'testerlib';

const client = new Client({
timeout: 0,
});
const responseTypesController = new ResponseTypesController(client);
const array = null;
const numberArray: number[] = [210, 211, 212];
const numberMap: Record<string, number> = {'key0' : 110, 'key1' : 111, 'key2' : 112 };
const boss: Person = {
name: 'Joe',
age: BigInt(30),
};
const employeeModel: Employee = {
name: 'John',
hiredAt: 'Mon, 15 Jun 2009 20:45:30 GMT',
boss: boss,
};

try {
const { result, ...httpResponse } = await responseTypesController.send(employeeModel, numberArray, numberMap);
// Get more response info...
// const { statusCode, headers } = httpResponse;
} catch(error) {
if (error instanceof ApiError) {
const errors = error.result;
// const { statusCode, headers } = error;
}
}
After
import {
ApiError,
Client,
Employee,
ResponseTypesController,
} from 'testerlib';

const client = new Client({
timeout: 0,
});

const responseTypesController = new ResponseTypesController(client);

const employeeModel: Employee = {
name: 'John',
hiredAt: 'Mon, 15 Jun 2009 20:45:30 GMT',
boss: {
name: 'Joe',
age: BigInt(30),
},
};

const numberArray: number[] = [
210,
211,
212
];

const numberMap: Record<string, number> = {
'key0': 110,
'key1': 111,
'key2': 112
};

try {
const { result, ...httpResponse } = await responseTypesController.send(
employeeModel,
numberArray,
numberMap
);
// Get more response info...
// const { statusCode, headers } = httpResponse;
} catch (error) {
if (error instanceof ApiError) {
const errors = error.result;
// const { statusCode, headers } = error;
}
}

Bugs Fixed

  • Fixed an issue with DateTime initialization for Unix Timestamp formats.
  • Fixed a missing semicolon and extra line issue at the end of a map field.
  • Fixed Enum, Enum Array initialization and import issues.
  • Fixed multiple indentation issues in parameters initialization section.
  • Fixed the inconsistent behavior SDK in case of CollapseToArray CodeGen setting enabled.