-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(oas): extract uri template logic
relates-to #120
- Loading branch information
Showing
7 changed files
with
147 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { isObject, Flattener } from '../utils'; | ||
import { OpenAPIV2 } from '@har-sdk/core'; | ||
|
||
export class Oas2ValueSerializer { | ||
private readonly flattener = new Flattener(); | ||
|
||
public serialize( | ||
param: OpenAPIV2.Parameter, | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
value: any | ||
): any { | ||
const style = param.collectionFormat; | ||
const explode = param.collectionFormat === 'multi'; | ||
|
||
if (explode) { | ||
return value; | ||
} | ||
|
||
const delimiter = this.getDelimiter(style); | ||
if (Array.isArray(value)) { | ||
return value.join(delimiter); | ||
} else if (isObject(value)) { | ||
return this.flattener.toFlattenArray(value).join(delimiter); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
private getDelimiter(style: string): string { | ||
switch (style) { | ||
case 'ssv': | ||
return ' '; | ||
case 'tsv': | ||
return '\t'; | ||
case 'pipes': | ||
return '|'; | ||
case 'csv': | ||
default: | ||
return ','; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import template from 'url-template'; | ||
|
||
export class UriTemplator { | ||
public substitute(templateStr: string, values: Record<string, any>): string { | ||
return template.parse(templateStr).expand(values); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.