Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/generator/k6Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ function _setDefaultSchemaTitle(context: ContextSpecs) {
}
}

function _generateResponseTypeDefinition(response: GetterResponse): string {
function _getResponseDataType(response: GetterResponse): string {
let responseDataType = ''

if (
response.definition.success &&
!['any', 'unknown'].includes(response.definition.success)
Expand All @@ -44,9 +43,13 @@ function _generateResponseTypeDefinition(response: GetterResponse): string {
responseDataType += 'ResponseBody'
}

return responseDataType
}

function _generateResponseTypeDefinition(response: GetterResponse): string {
return `{
response: Response
data: ${responseDataType}
data: ${_getResponseDataType(response)}
}`
}

Expand Down Expand Up @@ -233,16 +236,18 @@ const generateK6Implementation = (

const options = _getK6RequestOptions(verbOptions)

const responseDataType = _getResponseDataType(response)

return `${operationName}(\n ${toObjectString(props, 'implementation')} requestParameters?: Params): ${_generateResponseTypeDefinition(response)} {\n${bodyForm}
${urlGeneration}
const mergedRequestParameters = this._mergeRequestParameters(requestParameters || {}, this.commonRequestParameters);
const response = http.request(${options});
let data;
let data: ${responseDataType};

try {
data = response.json();
data = response.json() as unknown as ${responseDataType};
} catch {
data = response.body;
data = JSON.parse(response.body as string) as ${responseDataType};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if response.body is not a valid a JSON?

}
return {
response,
Expand Down
3 changes: 2 additions & 1 deletion tests/functional-tests/test-generator/generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const commonSubstringsForAllSDK = [
'Do not edit manually',
'Service version',
'const mergedRequestParameters = this._mergeRequestParameters( requestParameters || {}, this.commonRequestParameters, );',
'try { data = response.json(); } catch { data = response.body; }',
'try { data = response.json() as unknown as',
'catch { data = JSON.parse(response.body as string) as',
'return { response, data, };',
]

Expand Down