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

Commit 5e0eaca

Browse files
committed
Lint fixes
1 parent 0dd338b commit 5e0eaca

File tree

6 files changed

+72
-70
lines changed

6 files changed

+72
-70
lines changed

src/DefinitionGenerator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import _ = require('lodash');
12
// tslint:disable-next-line no-submodule-imports
23
import { validateSync as openApiValidatorSync } from 'swagger2openapi/validate';
34
import * as uuid from 'uuid';
45

6+
import { parseModels } from './parse';
57
import { IDefinition, IDefinitionConfig, IOperation, IParameterConfig, IServerlessFunctionConfig } from './types';
68
import { cleanSchema } from './utils';
7-
import { parseModels } from './parse';
8-
import _ = require('lodash');
99

1010
export class DefinitionGenerator {
1111
// The OpenAPI version we currently validate against

src/ServerlessOpenApiDocumentation.ts

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,55 @@
1-
import * as chalk from 'chalk';
1+
import chalk from 'chalk';
22
import * as fs from 'fs';
33
import * as YAML from 'js-yaml';
4+
import _ = require('lodash');
5+
import * as Serverless from 'serverless';
46
import { inspect } from 'util';
7+
58
import { DefinitionGenerator } from './DefinitionGenerator';
6-
import { IDefinitionType, ILog, Format, IDefinitionConfig } from './types';
7-
import * as Serverless from 'serverless';
8-
import _ = require('lodash');
9+
import { Format, IDefinitionConfig, IDefinitionType, ILog } from './types';
910

10-
interface Options {
11-
indent: number,
12-
format: Format,
13-
output: string,
11+
interface IOptions {
12+
indent: number;
13+
format: Format;
14+
output: string;
1415
}
1516

16-
interface ProcessedInput {
17-
options: Options;
17+
interface IProcessedInput {
18+
options: IOptions;
1819
}
1920

20-
interface CustomVars {
21+
interface ICustomVars {
2122
documentation: IDefinitionConfig;
2223
}
2324

24-
interface Service {
25-
custom: CustomVars;
25+
interface IService {
26+
custom: ICustomVars;
2627
}
2728

28-
interface Variables {
29-
service: Service
29+
interface IVariables {
30+
service: IService;
3031
}
3132

32-
interface FullServerless extends Serverless {
33-
variables: Variables;
34-
processedInput: ProcessedInput;
33+
interface IFullServerless extends Serverless {
34+
variables: IVariables;
35+
processedInput: IProcessedInput;
3536
}
3637

3738
export class ServerlessOpenApiDocumentation {
3839
public hooks;
3940
public commands;
4041
/** Serverless Instance */
41-
private serverless: FullServerless;
42+
private serverless: IFullServerless;
4243

4344
/** Serverless Service Custom vars */
44-
private customVars: CustomVars;
45+
private customVars: ICustomVars;
4546

4647
/**
4748
* Constructor
4849
* @param serverless
4950
* @param options
5051
*/
51-
constructor (serverless: FullServerless, options) {
52+
constructor (serverless: IFullServerless, options) {
5253

5354
// pull the serverless instance into our class vars
5455
this.serverless = serverless;
@@ -93,37 +94,6 @@ export class ServerlessOpenApiDocumentation {
9394
process.stdout.write(str.join(' '));
9495
}
9596

96-
/**
97-
* Processes CLI input by reading the input from serverless
98-
* @returns config IConfigType
99-
*/
100-
private processCliInput (): IDefinitionType {
101-
const config: IDefinitionType = {
102-
format: Format.yaml,
103-
file: 'openapi.yml',
104-
indent: 2,
105-
};
106-
107-
config.indent = this.serverless.processedInput.options.indent || 2;
108-
config.format = this.serverless.processedInput.options.format || Format.yaml;
109-
110-
if ([Format.yaml, Format.json].indexOf(config.format) < 0) {
111-
throw new Error('Invalid Output Format Specified - must be one of "yaml" or "json"');
112-
}
113-
114-
config.file = this.serverless.processedInput.options.output ||
115-
((config.format === 'yaml') ? 'openapi.yml' : 'openapi.json');
116-
117-
this.log(
118-
`${chalk.bold.green('[OPTIONS]')}`,
119-
`format: "${chalk.bold.red(config.format)}",`,
120-
`output file: "${chalk.bold.red(config.file)}",`,
121-
`indentation: "${chalk.bold.red(String(config.indent))}"\n\n`,
122-
);
123-
124-
return config;
125-
}
126-
12797
/**
12898
* Generates OpenAPI Documentation based on serverless configuration and functions
12999
*/
@@ -188,4 +158,35 @@ export class ServerlessOpenApiDocumentation {
188158

189159
this.log(`${chalk.bold.green('[OUTPUT]')} To "${chalk.bold.red(config.file)}"\n`);
190160
}
161+
162+
/**
163+
* Processes CLI input by reading the input from serverless
164+
* @returns config IConfigType
165+
*/
166+
private processCliInput (): IDefinitionType {
167+
const config: IDefinitionType = {
168+
format: Format.yaml,
169+
file: 'openapi.yml',
170+
indent: 2,
171+
};
172+
173+
config.indent = this.serverless.processedInput.options.indent || 2;
174+
config.format = this.serverless.processedInput.options.format || Format.yaml;
175+
176+
if ([Format.yaml, Format.json].indexOf(config.format) < 0) {
177+
throw new Error('Invalid Output Format Specified - must be one of "yaml" or "json"');
178+
}
179+
180+
config.file = this.serverless.processedInput.options.output ||
181+
((config.format === 'yaml') ? 'openapi.yml' : 'openapi.json');
182+
183+
this.log(
184+
`${chalk.bold.green('[OPTIONS]')}`,
185+
`format: "${chalk.bold.red(config.format)}",`,
186+
`output file: "${chalk.bold.red(config.file)}",`,
187+
`indentation: "${chalk.bold.red(String(config.indent))}"\n\n`,
188+
);
189+
190+
return config;
191+
}
191192
}

src/__tests__/DefinitionGenerator.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import _ = require('lodash');
12
import * as path from 'path';
23
import * as Serverless from 'serverless';
34
import { DefinitionGenerator } from '../DefinitionGenerator';
4-
import _ = require('lodash');
55

66
class ServerlessInterface extends Serverless {
77
public service: any = {};
@@ -45,7 +45,7 @@ describe('OpenAPI Documentation Generator', () => {
4545
const docGen = new DefinitionGenerator(sls.service.custom.documentation, servicePath);
4646

4747
// implementation copied from ServerlessOpenApiDocumentation.ts
48-
docGen.parse();
48+
await docGen.parse();
4949

5050
const funcConfigs = sls.service.getAllFunctions().map((functionName) => {
5151
const func = sls.service.getFunction(functionName);

src/parse.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import * as path from 'path';
2-
import * as _ from 'lodash';
31
import { JSONSchema7 } from 'json-schema';
4-
52
import * as $RefParser from 'json-schema-ref-parser';
3+
import * as _ from 'lodash';
4+
import * as path from 'path';
65

76
import { IModel } from './types';
87
import { cleanSchema } from './utils';
98

10-
export async function parseModels(models: IModel[], root: string): Promise<{}> {
9+
export async function parseModels (models: IModel[], root: string): Promise<{}> {
1110
const schemas = {};
1211

1312
if (!_.isArrayLike(models)) {
@@ -23,25 +22,25 @@ export async function parseModels(models: IModel[], root: string): Promise<{}> {
2322
? await $RefParser.bundle(path.resolve(root, model.schema))
2423
: model.schema) as JSONSchema7;
2524

26-
_.assign(schemas, updateReferences(schema.definitions), {
27-
[model.name]: updateReferences(cleanSchema(schema)),
28-
});
25+
_.assign(schemas, updateReferences(schema.definitions), {
26+
[model.name]: updateReferences(cleanSchema(schema)),
27+
});
2928
}
3029

3130
return schemas;
3231
}
3332

3433
function updateReferences (schema: JSONSchema7): JSONSchema7 {
35-
if(!schema) {
34+
if (!schema) {
3635
return schema;
3736
}
3837

39-
const cloned = _.cloneDeep(schema) as JSONSchema7;
38+
const cloned = _.cloneDeep(schema);
4039

4140
if (cloned.$ref) {
4241
return {
4342
...cloned,
44-
$ref: cloned.$ref.replace('#/definitions', '#/components/schemas')
43+
$ref: cloned.$ref.replace('#/definitions', '#/components/schemas'),
4544
};
4645
}
4746

src/types.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { JSONSchema7 } from 'json-schema';
2+
13
export interface IModel {
24
name: string;
35
description: string;
46
contentType: string;
5-
schema: string | object;
7+
schema: string | JSONSchema7;
68
examples: any[];
79
example: object;
810
}
@@ -16,8 +18,8 @@ export interface IDefinitionConfig {
1618

1719
export enum Format {
1820
yaml = 'yaml',
19-
json = 'json'
20-
};
21+
json = 'json',
22+
}
2123

2224
export interface IDefinitionType {
2325
file: string;

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

22
import _ = require('lodash');
33

4-
export const cleanSchema = schema => _.omit(schema, '$schema', 'definitions')
4+
export const cleanSchema = (schema) => _.omit(schema, '$schema', 'definitions');

0 commit comments

Comments
 (0)