Skip to content

Commit

Permalink
fix: codeclimate code style complains
Browse files Browse the repository at this point in the history
  • Loading branch information
pmstss committed Feb 16, 2022
1 parent d46bd1a commit 30987fb
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 78 deletions.
8 changes: 2 additions & 6 deletions packages/oas/src/converter/BaseUrlParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,13 @@ export class BaseUrlParser {
);
}

let preferredUrls: string[] = urls.filter(
const preferredUrls: string[] = urls.filter(
(x) => x.startsWith('https') || x.startsWith('wss')
);

if (!preferredUrls.length) {
preferredUrls = urls;
}

return this.sampler.sample({
type: 'array',
examples: preferredUrls
examples: preferredUrls.length ? preferredUrls : urls
});
}

Expand Down
111 changes: 61 additions & 50 deletions packages/oas/src/converter/subconverters/HeadersConverter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { isOASV2, isOASV3 } from '../../utils';
import { Sampler } from '../Sampler';
import { SubConverter } from './SubConverter';
import { Header, OpenAPI, OpenAPIV2, OpenAPIV3 } from '@har-sdk/core';

type OperationObject = OpenAPIV2.OperationObject | OpenAPIV3.OperationObject;
type ParameterObject = OpenAPIV3.ParameterObject | OpenAPIV2.Parameter;

type SecurityRequirementObject =
| OpenAPIV2.SecurityRequirementObject
Expand All @@ -27,37 +27,47 @@ export class HeadersConverter implements SubConverter<Header[]> {
const headers: Header[] = [];
const pathObj = this.spec.paths[path][method];

if (Array.isArray(pathObj.consumes)) {
for (const value of pathObj.consumes) {
headers.push(this.createHeader('content-type', value));
}
} else if (pathObj.requestBody?.content) {
for (const value of Object.keys(pathObj.requestBody.content)) {
headers.push(this.createHeader('content-type', value));
}
}
headers.push(...this.createContentTypeHeaders(pathObj));
headers.push(...this.createAcceptHeaders(pathObj));

if (Array.isArray(pathObj.produces)) {
for (const value of pathObj.produces) {
headers.push(this.createHeader('accept', value));
}
headers.push(...this.parsePathParams(pathObj, ['paths', path, method]));
headers.push(...this.parseSecurityRequirements(pathObj));

return headers;
}

private createContentTypeHeaders(pathObj: OperationObject): Header[] {
const values = [];

if ('consumes' in pathObj && Array.isArray(pathObj.consumes)) {
values.push(...pathObj.consumes);
} else if (
'requestBody' in pathObj &&
'content' in pathObj.requestBody &&
pathObj.requestBody?.content
) {
values.push(...Object.keys(pathObj.requestBody.content));
}

headers.push(
...this.createFromPathParams(pathObj, ['paths', path, method])
);
headers.push(...this.createFromSecurityRequirements(pathObj));
return this.createHeaders('content-type', values);
}

return headers;
private createAcceptHeaders(pathObj: OperationObject): Header[] {
return this.createHeaders(
'accept',
'produces' in pathObj && Array.isArray(pathObj.produces)
? pathObj.produces
: []
);
}

private createFromPathParams(
pathObj: OpenAPIV2.OperationObject | OpenAPIV3.OperationObject,
private parsePathParams(
pathObj: OperationObject,
tokens: string[]
): Header[] {
const params: (OpenAPIV3.ParameterObject | OpenAPIV2.Parameter)[] = (
const params: ParameterObject[] = (
Array.isArray(pathObj.parameters) ? pathObj.parameters : []
) as (OpenAPIV3.ParameterObject | OpenAPIV2.Parameter)[];
) as ParameterObject[];

return params
.filter(
Expand Down Expand Up @@ -94,17 +104,17 @@ export class HeadersConverter implements SubConverter<Header[]> {
private getSecuritySchemes():
| Record<string, SecuritySchemeObject>
| undefined {
if (isOASV2(this.spec) && this.spec.securityDefinitions) {
if ('securityDefinitions' in this.spec) {
return this.spec.securityDefinitions;
} else if (isOASV3(this.spec) && this.spec.components) {
} else if ('components' in this.spec) {
return this.spec.components.securitySchemes as Record<
string,
SecuritySchemeObject
>;
}
}

private createFromSecurityRequirements(pathObj: OperationObject): Header[] {
private parseSecurityRequirements(pathObj: OperationObject): Header[] {
const secRequirementObjects = this.getSecurityRequirementObjects(pathObj);
if (!secRequirementObjects) {
return [];
Expand All @@ -116,42 +126,39 @@ export class HeadersConverter implements SubConverter<Header[]> {
}

for (const obj of secRequirementObjects) {
const header = this.createFromSecurityRequirement(obj, securitySchemes);
const header = this.parseSecurityRequirement(obj, securitySchemes);
if (header) {
return [header];
}
}
}

private createFromSecurityRequirement(
private parseSecurityRequirement(
obj: SecurityRequirementObject,
securitySchemes: Record<string, SecuritySchemeObject>
): Header | undefined {
const schemeName = Object.keys(obj)[0];
const securityScheme = securitySchemes[schemeName];
const authType = securityScheme.type.toLowerCase();
switch (authType) {
case 'http':
switch (
(securityScheme as OpenAPIV3.HttpSecurityScheme).scheme?.toLowerCase()
) {
case 'bearer':
return this.createBearerAuthHeader();
case 'basic':
return this.createBasicAuthHeader();
}
break;
case 'basic':
return this.createBasicAuthHeader();
case 'apikey':
if ((securityScheme as SecuritySchemeApiKey).in === 'header') {
return this.createApiKeyHeader(
securityScheme as SecuritySchemeApiKey
);
}
break;
case 'oauth2':
return this.createBearerAuthHeader();
const httpScheme =
'scheme' in securityScheme
? securityScheme.scheme.toLowerCase()
: undefined;

if (authType === 'basic' || httpScheme === 'basic') {
return this.createBasicAuthHeader();
}

if (authType === 'oauth2' || httpScheme === 'bearer') {
return this.createBearerAuthHeader();
}

if (
authType === 'apikey' &&
'in' in securityScheme &&
securityScheme.in === 'header'
) {
return this.createApiKeyHeader(securityScheme as SecuritySchemeApiKey);
}
}

Expand All @@ -173,4 +180,8 @@ export class HeadersConverter implements SubConverter<Header[]> {
value
};
}

private createHeaders(name: string, values: string[]): Header[] {
return values.map((value) => this.createHeader(name, value));
}
}
39 changes: 17 additions & 22 deletions packages/oas/src/converter/subconverters/QueryStringConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,50 @@ import { OpenAPI, OpenAPIV2, OpenAPIV3, QueryString } from '@har-sdk/core';
export class QueryStringConverter implements SubConverter<QueryString[]> {
private readonly flattener = new Flattener();

private readonly oas3: boolean;

constructor(
private readonly spec: OpenAPI.Document,
private readonly sampler: Sampler,
private readonly paramsSerializer: ParamsSerializer
) {}
) {
this.oas3 = isOASV3(this.spec);
}

public convert(path: string, method: string): QueryString[] {
const pathObj = this.spec.paths[path][method];
const tokens = ['paths', path, method];
const params: (OpenAPIV2.Parameter | OpenAPIV3.ParameterObject)[] =
Array.isArray(pathObj.parameters) ? pathObj.parameters : [];
const oas3 = isOASV3(this.spec);

return params
.filter(
(param) =>
typeof param.in === 'string' && param.in.toLowerCase() === 'query'
)
.flatMap((param) => {
const value = oas3
? this.getOas3ParameterValue(param as OpenAPIV3.ParameterObject)
: this.getOas2ParameterValue(param as OpenAPIV2.Parameter);

return this.convertQueryParam(
.flatMap((param) =>
this.convertQueryParam(
param.name,
this.paramsSerializer.serializeValue(
param,
value ??
this.getParameterValue(param) ??
this.sampler.sampleParam(param, {
spec: this.spec,
tokens,
idx: params.indexOf(param)
}),
oas3
this.oas3
)
);
});
)
);
}

private getOas2ParameterValue(param: OpenAPIV2.Parameter): any {
if (param.default !== 'undefined') {
return param.default;
}
}

private getOas3ParameterValue(param: OpenAPIV3.ParameterObject): any {
if (param.example !== 'undefined') {
return param.example;
}
private getParameterValue(
param: OpenAPIV2.Parameter | OpenAPIV3.ParameterObject
): any {
return this.oas3
? (param as OpenAPIV3.ParameterObject).example
: (param as OpenAPIV2.Parameter).default;
}

private convertQueryParam(name: string, value: any): QueryString[] {
Expand Down

0 comments on commit 30987fb

Please sign in to comment.