Skip to content

Commit

Permalink
refactor(oas): extract LocationParam
Browse files Browse the repository at this point in the history
fixes #120
  • Loading branch information
pmstss committed Feb 18, 2022
1 parent 13fed50 commit 55ea8f4
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 63 deletions.
7 changes: 7 additions & 0 deletions packages/oas/src/converter/parts/LocationParam.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ParameterObject } from '../../types';

export interface LocationParam<T extends ParameterObject> {
readonly param: T;
readonly value: unknown;
readonly jsonPointer: string;
}
11 changes: 6 additions & 5 deletions packages/oas/src/converter/parts/Oas2ValueSerializer.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { ConvertError } from '../../errors';
import { isObject, Flattener } from '../../utils';
import { LocationParam } from './LocationParam';
import { OpenAPIV2 } from '@har-sdk/core';

export class Oas2ValueSerializer {
private readonly flattener = new Flattener();

public serialize(
param: OpenAPIV2.Parameter,
value: unknown,
jsonPointer: string
): unknown {
public serialize({
param,
value,
jsonPointer
}: LocationParam<OpenAPIV2.Parameter>): unknown {
const style = param.collectionFormat;
const explode = param.collectionFormat === 'multi';

Expand Down
19 changes: 11 additions & 8 deletions packages/oas/src/converter/parts/headers/HeadersConverter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { OperationObject, ParameterObject } from '../../../types';
import { filterLocationParams, getParameters } from '../../../utils';
import { LocationParam } from '../LocationParam';
import { Sampler } from '../Sampler';
import { SubConverter } from '../../SubConverter';
import { Header, OpenAPI, OpenAPIV2, OpenAPIV3 } from '@har-sdk/core';
Expand Down Expand Up @@ -32,9 +33,7 @@ export abstract class HeadersConverter<T extends OpenAPI.Document>
protected abstract createAcceptHeaders(pathObj: OperationObject): Header[];

protected abstract convertHeaderParam(
param: ParameterObject,
paramValue: unknown,
paramJsonPointer: string
headerParam: LocationParam<ParameterObject>
): Header;

protected abstract getSecuritySchemes():
Expand Down Expand Up @@ -113,14 +112,18 @@ export abstract class HeadersConverter<T extends OpenAPI.Document>
spec: this.spec
});

return this.convertHeaderParam(
{
return this.convertHeaderParam({
value,
param: {
...param,
name: param.name.toLowerCase()
},
value,
jsonPointer.compile([...tokens, 'parameters', idx.toString(10)])
);
jsonPointer: jsonPointer.compile([
...tokens,
'parameters',
idx.toString(10)
])
});
});
}

Expand Down
13 changes: 4 additions & 9 deletions packages/oas/src/converter/parts/headers/Oas2HeadersConveter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { LocationParam } from '../LocationParam';
import { Oas2ValueSerializer } from '../Oas2ValueSerializer';
import { Sampler } from '../Sampler';
import { HeadersConverter } from './HeadersConverter';
Expand All @@ -21,17 +22,11 @@ export class Oas2HeadersConverter extends HeadersConverter<OpenAPIV2.Document> {
}

protected convertHeaderParam(
param: OpenAPIV2.Parameter,
paramValue: unknown,
jsonPointer: string
headerParam: LocationParam<OpenAPIV2.Parameter>
): Header {
return {
name: param.name,
value: this.oas2ValueSerializer.serialize(
param,
paramValue,
jsonPointer
) as string
name: headerParam.param.name,
value: this.oas2ValueSerializer.serialize(headerParam) as string
};
}

Expand Down
11 changes: 6 additions & 5 deletions packages/oas/src/converter/parts/headers/Oas3HeadersConverter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isObject } from '../../../utils';
import { LocationParam } from '../LocationParam';
import { Sampler } from '../Sampler';
import { UriTemplator } from '../UriTemplator';
import { HeadersConverter } from './HeadersConverter';
Expand Down Expand Up @@ -29,15 +30,15 @@ export class Oas3HeadersConverter extends HeadersConverter<OpenAPIV3.Document> {
return [];
}

protected convertHeaderParam(
param: OpenAPIV3.ParameterObject,
paramValue: unknown
): Header {
protected convertHeaderParam({
param,
value
}: LocationParam<OpenAPIV3.ParameterObject>): Header {
return {
name: param.name,
value: decodeURIComponent(
this.uriTemplator.substitute(`{x${param.explode ? '*' : ''}}`, {
x: paramValue
x: value
})
)
};
Expand Down
11 changes: 4 additions & 7 deletions packages/oas/src/converter/parts/path/Oas2PathConverter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LocationParam } from '../LocationParam';
import { Sampler } from '../Sampler';
import { Oas2ValueSerializer } from '../Oas2ValueSerializer';
import { PathConverter, PathParam } from './PathConverter';
import { PathConverter } from './PathConverter';
import { OpenAPIV2 } from '@har-sdk/core';

export class Oas2PathConverter extends PathConverter<OpenAPIV2.Parameter> {
Expand All @@ -12,18 +13,14 @@ export class Oas2PathConverter extends PathConverter<OpenAPIV2.Parameter> {

protected parsePath(
path: string,
pathParams: PathParam<OpenAPIV2.Parameter>[]
pathParams: LocationParam<OpenAPIV2.Parameter>[]
): string {
return encodeURI(
pathParams.reduce(
(res, pathParam) =>
res.replace(
`{${pathParam.param.name}}`,
this.oas2ValueSerializer.serialize(
pathParam.param,
pathParam.value,
pathParam.jsonPointer
) as string
this.oas2ValueSerializer.serialize(pathParam) as string
),
path
)
Expand Down
5 changes: 3 additions & 2 deletions packages/oas/src/converter/parts/path/Oas3PathConverter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ParameterObject } from '../../../types';
import { LocationParam } from '../LocationParam';
import { Sampler } from '../Sampler';
import { UriTemplator } from '../UriTemplator';
import { PathConverter, PathParam } from './PathConverter';
import { PathConverter } from './PathConverter';
import { OpenAPIV3 } from '@har-sdk/core';

export class Oas3PathConverter extends PathConverter<OpenAPIV3.ParameterObject> {
Expand All @@ -13,7 +14,7 @@ export class Oas3PathConverter extends PathConverter<OpenAPIV3.ParameterObject>

protected parsePath(
path: string,
pathParams: PathParam<OpenAPIV3.ParameterObject>[]
pathParams: LocationParam<OpenAPIV3.ParameterObject>[]
): string {
const pathTemplateStr = pathParams.reduce(
(res, { param }) =>
Expand Down
12 changes: 5 additions & 7 deletions packages/oas/src/converter/parts/path/PathConverter.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { ParameterObject } from '../../../types';
import { getParameters, filterLocationParams } from '../../../utils';
import { LocationParam } from '../LocationParam';
import { Sampler } from '../Sampler';
import { SubConverter } from '../../SubConverter';
import jsonPointer from 'json-pointer';
import { OpenAPI } from '@har-sdk/core';

export interface PathParam<T> {
readonly param: T;
readonly value: unknown;
readonly jsonPointer: string;
}

export abstract class PathConverter<T extends ParameterObject>
implements SubConverter<string>
{
Expand All @@ -19,7 +14,10 @@ export abstract class PathConverter<T extends ParameterObject>
private readonly sampler: Sampler
) {}

protected abstract parsePath(path: string, params: PathParam<T>[]): string;
protected abstract parsePath(
path: string,
params: LocationParam<T>[]
): string;

public convert(path: string, method: string): string {
const params: ParameterObject[] = getParameters(this.spec, path, method);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isObject } from '../../../utils';
import { LocationParam } from '../LocationParam';
import { Oas2ValueSerializer } from '../Oas2ValueSerializer';
import { Sampler } from '../Sampler';
import { QueryStringConverter } from './QueryStringConverter';
Expand All @@ -12,16 +13,10 @@ export class Oas2QueryStringConverter extends QueryStringConverter<OpenAPIV2.Par
}

protected convertQueryParam(
param: OpenAPIV2.Parameter,
paramValue: unknown,
paramJsonPointer: string
queryParam: LocationParam<OpenAPIV2.Parameter>
): QueryString[] {
const { name } = param;
const value = this.oas2ValueSerializer.serialize(
param as OpenAPIV2.Parameter,
paramValue,
paramJsonPointer
);
const { name } = queryParam.param;
const value = this.oas2ValueSerializer.serialize(queryParam);

let values: QueryString[];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { LocationParam } from '../LocationParam';
import { Sampler } from '../Sampler';
import { UriTemplator } from '../UriTemplator';
import { QueryStringConverter } from './QueryStringConverter';
Expand All @@ -10,10 +11,10 @@ export class Oas3QueryStringConverter extends QueryStringConverter<OpenAPIV3.Par
super(spec, sampler);
}

protected convertQueryParam(
param: OpenAPIV3.ParameterObject,
paramValue: unknown
): QueryString[] {
protected convertQueryParam({
param,
value: paramValue
}: LocationParam<OpenAPIV3.ParameterObject>): QueryString[] {
const templateStr = this.getQueryParamUriTemplate(param);

let queryString = this.uriTemplator.substitute(templateStr, {
Expand Down
17 changes: 10 additions & 7 deletions packages/oas/src/converter/parts/query/QueryStringConverter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ParameterObject } from '../../../types';
import { getParameters, filterLocationParams } from '../../../utils';
import { LocationParam } from '../LocationParam';
import { Sampler } from '../Sampler';
import { SubConverter } from '../../SubConverter';
import jsonPointer from 'json-pointer';
Expand All @@ -14,9 +15,7 @@ export abstract class QueryStringConverter<T extends ParameterObject>
) {}

protected abstract convertQueryParam(
param: T,
paramValue: unknown,
paramJsonPointer: string
queryParam: LocationParam<T>
): QueryString[];

public convert(path: string, method: string): QueryString[] {
Expand All @@ -31,11 +30,15 @@ export abstract class QueryStringConverter<T extends ParameterObject>
spec: this.spec
});

return this.convertQueryParam(
param as T,
return this.convertQueryParam({
param: param as T,
value,
jsonPointer.compile([...tokens, 'parameters', idx.toString(10)])
);
jsonPointer: jsonPointer.compile([
...tokens,
'parameters',
idx.toString(10)
])
});
});
}
}

0 comments on commit 55ea8f4

Please sign in to comment.