Skip to content
This repository has been archived by the owner on Dec 3, 2021. It is now read-only.

Commit

Permalink
feat(parser): add support of dynamic variables (#7)
Browse files Browse the repository at this point in the history
closes #6
  • Loading branch information
derevnjuk authored Jul 2, 2020
1 parent 547e352 commit ca148f7
Show file tree
Hide file tree
Showing 12 changed files with 1,109 additions and 25 deletions.
62 changes: 62 additions & 0 deletions src/helpers/directories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
export default [
'/Applications',
'/bin',
'/boot',
'/boot/defaults',
'/dev',
'/etc',
'/etc/defaults',
'/etc/mail',
'/etc/namedb',
'/etc/periodic',
'/etc/ppp',
'/home',
'/home/user',
'/home/user/dir',
'/lib',
'/Library',
'/lost+found',
'/media',
'/mnt',
'/net',
'/Network',
'/opt',
'/opt/bin',
'/opt/include',
'/opt/lib',
'/opt/sbin',
'/opt/share',
'/private',
'/private/tmp',
'/private/var',
'/proc',
'/rescue',
'/root',
'/sbin',
'/selinux',
'/srv',
'/sys',
'/System',
'/tmp',
'/Users',
'/usr',
'/usr/X11R6',
'/usr/bin',
'/usr/include',
'/usr/lib',
'/usr/libdata',
'/usr/libexec',
'/usr/local/bin',
'/usr/local/src',
'/usr/obj',
'/usr/ports',
'/usr/sbin',
'/usr/share',
'/usr/src',
'/var',
'/var/log',
'/var/mail',
'/var/spool',
'/var/tmp',
'/var/yp'
];
2 changes: 2 additions & 0 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as directories } from './directories';
export { default as locales } from './locales';
130 changes: 130 additions & 0 deletions src/helpers/locales.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
export default [
'af',
'am',
'an',
'ar',
'ast',
'az',
'be',
'bg',
'bh',
'bn',
'br',
'bs',
'ca',
'ceb',
'ckb',
'co',
'cs',
'cy',
'da',
'de',
'el',
'en',
'eo',
'es',
'et',
'eu',
'fa',
'fi',
'fil',
'fo',
'fr',
'fy',
'ga',
'gd',
'gl',
'gn',
'gu',
'ha',
'haw',
'he',
'hi',
'hmn',
'hr',
'ht',
'hu',
'hy',
'ia',
'id',
'ig',
'is',
'it',
'ja',
'jv',
'ka',
'kk',
'km',
'kn',
'ko',
'ku',
'ky',
'la',
'lb',
'ln',
'lo',
'lt',
'lv',
'mg',
'mi',
'mk',
'ml',
'mn',
'mo',
'mr',
'ms',
'mt',
'my',
'nb',
'ne',
'nl',
'nn',
'no',
'ny',
'oc',
'om',
'or',
'pa',
'pl',
'ps',
'pt',
'qu',
'rm',
'ro',
'ru',
'sd',
'sh',
'si',
'sk',
'sl',
'sm',
'sn',
'so',
'sq',
'sr',
'st',
'su',
'sv',
'sw',
'ta',
'te',
'tg',
'th',
'ti',
'tk',
'to',
'tr',
'tt',
'tw',
'ug',
'uk',
'ur',
'uz',
'vi',
'wa',
'xh',
'yi',
'yo',
'zh',
'zu'
];
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DefaultValidator } from './validator';
import { DefaultConverter } from './converter';
import { DefaultVariableParserFactory } from './parser';
import { DefaultGenerators, DefaultVariableParserFactory } from './parser';
import Har from 'har-format';
import { ok } from 'assert';

Expand All @@ -13,7 +13,10 @@ export const postman2har = async (
ok(collection, `Please provide a valid Postman Collection.`);

const validator: DefaultValidator = new DefaultValidator();
const parserFactory: DefaultVariableParserFactory = new DefaultVariableParserFactory();
const generators: DefaultGenerators = new DefaultGenerators();
const parserFactory: DefaultVariableParserFactory = new DefaultVariableParserFactory(
generators
);
const converter: DefaultConverter = new DefaultConverter(
validator,
parserFactory,
Expand Down
29 changes: 18 additions & 11 deletions src/parser/BaseVariableParser.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
import { VariableParser } from './VariableParser';
import faker from 'faker';
import { Generators } from './Generators';

export abstract class BaseVariableParser implements VariableParser {
protected constructor(private readonly variables: Postman.Variable[]) {}
protected constructor(
private readonly variables: Postman.Variable[],
private readonly generators: Generators
) {}

public abstract parse(value: string): string;

public find(key: string): Postman.Variable | undefined {
return this.variables.find((x: Postman.Variable) => x.key === key);
public find(key: string): Postman.Variable | (() => any) | undefined {
const variable: Postman.Variable | undefined = this.variables.find(
(x: Postman.Variable) => x.key === key
);

if (!variable && key.startsWith('$')) {
return this.generators[key.replace(/^\$/, '')];
}

return variable;
}

protected sample(variable?: Postman.Variable): string {
switch (variable?.type?.toLowerCase()) {
case 'string':
case 'text':
return faker.random.word();
return this.generators.randomWord();
case 'number':
return String(faker.random.number({ min: 1, max: 99 }));
return String(this.generators.randomInt());
case 'any':
default:
return faker.random.arrayElement([
faker.random.alphaNumeric(10),
String(faker.random.number({ min: 1, max: 99 })),
faker.random.uuid()
]);
return this.generators.randomAlphaNumeric();
}
}
}
Loading

0 comments on commit ca148f7

Please sign in to comment.