From 7917cde3e949db4e1c741e299ccef9de61c9c0d6 Mon Sep 17 00:00:00 2001 From: Gabriel Ratcliff Date: Thu, 23 Jan 2020 07:51:03 -0800 Subject: [PATCH] Prettier updates: Updating prettier to mirror other ReadMe repositories --- .prettierrc | 4 +- __tests__/lib/find-schema-definition.test.js | 8 +--- __tests__/lib/get-schema.test.js | 12 ++--- .../lib/parameters-to-json-schema.test.js | 46 +++++++++---------- __tests__/lib/utils.test.js | 12 ++--- __tests__/oas.test.js | 38 ++++++--------- src/api.js | 12 ++--- src/lib/commands/add.js | 2 +- src/lib/commands/docs.js | 4 +- src/lib/commands/endpoint.js | 4 +- src/lib/commands/host.js | 4 +- src/lib/commands/init.js | 10 +--- src/lib/get-schema.js | 5 +- src/lib/get-user-variable.js | 2 +- src/lib/parameters-to-json-schema.js | 11 ++--- src/lib/utils.js | 6 +-- 16 files changed, 70 insertions(+), 110 deletions(-) diff --git a/.prettierrc b/.prettierrc index 53d7f9b6..9d5ce399 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,5 +1,5 @@ { + "printWidth": 120, "singleQuote": true, - "trailingComma": "all", - "printWidth": 100, + "trailingComma": "es5" } diff --git a/__tests__/lib/find-schema-definition.test.js b/__tests__/lib/find-schema-definition.test.js index 24c5b5f4..5f2fc6c2 100644 --- a/__tests__/lib/find-schema-definition.test.js +++ b/__tests__/lib/find-schema-definition.test.js @@ -1,13 +1,9 @@ const findSchemaDefinition = require('../../src/lib/find-schema-definition'); test('should throw an error if there is an invalid ref', () => { - expect(() => findSchemaDefinition('some-other-ref', {})).toThrow( - /Could not find a definition for/, - ); + expect(() => findSchemaDefinition('some-other-ref', {})).toThrow(/Could not find a definition for/); }); test('should throw an error if there is a missing ref', () => { - expect(() => findSchemaDefinition('#/components/schemas/user', {})).toThrow( - /Could not find a definition for/, - ); + expect(() => findSchemaDefinition('#/components/schemas/user', {})).toThrow(/Could not find a definition for/); }); diff --git a/__tests__/lib/get-schema.test.js b/__tests__/lib/get-schema.test.js index e021c30c..37d3d994 100644 --- a/__tests__/lib/get-schema.test.js +++ b/__tests__/lib/get-schema.test.js @@ -15,7 +15,7 @@ test('should return the first type if there is content', () => { }, }, }, - }), + }) ).toStrictEqual({ type: 'application/json', schema }); expect( @@ -30,7 +30,7 @@ test('should return the first type if there is content', () => { }, }, }, - }), + }) ).toStrictEqual({ type: 'text/xml', schema }); }); @@ -58,8 +58,8 @@ test('should look up the schema if it looks like the first $ref is a request bod components: { requestBodies: { schema: { content: { 'application/json': { schema: { $ref } } } } }, }, - }, - ).schema.$ref, + } + ).schema.$ref ).toStrictEqual($ref); }); @@ -71,7 +71,7 @@ test('should return the inline schema from request body object', () => { }, { components: { requestBodies: { schema: { content: { 'application/json': { schema } } } } }, - }, - ).schema, + } + ).schema ).toStrictEqual(schema); }); diff --git a/__tests__/lib/parameters-to-json-schema.test.js b/__tests__/lib/parameters-to-json-schema.test.js index 51feaebf..512a04f9 100644 --- a/__tests__/lib/parameters-to-json-schema.test.js +++ b/__tests__/lib/parameters-to-json-schema.test.js @@ -35,7 +35,7 @@ describe('parameter type support and sorting', () => { expect( jsonschema.map(js => { return js.type; - }), + }) ).toStrictEqual(['path', 'query', 'cookie', 'formData', 'header']); }); @@ -55,7 +55,7 @@ describe('parameter type support and sorting', () => { expect( jsonschema.map(js => { return js.type; - }), + }) ).toStrictEqual(['path', 'query', 'body', 'cookie', 'header']); }); }); @@ -76,8 +76,8 @@ test('it should work for request body inline (json)', () => { }, }, }, - {}, - ), + {} + ) ).toStrictEqual([ { label: 'Body Params', @@ -108,8 +108,8 @@ test('it should work for request body inline (formData)', () => { }, }, }, - {}, - ), + {} + ) ).toStrictEqual([ { label: 'Form Data', @@ -138,7 +138,7 @@ test('should pass through enum', () => { }, }, ], - }), + }) ).toStrictEqual([ { label: 'Headers', @@ -170,7 +170,7 @@ test('should pass through defaults', () => { }, }, ], - }), + }) ).toStrictEqual([ { label: 'Headers', @@ -201,7 +201,7 @@ test('it should pass through type for non-body parameters', () => { }, }, ], - })[0].schema.properties.checkbox.type, + })[0].schema.properties.checkbox.type ).toBe('boolean'); }); @@ -220,7 +220,7 @@ test('it should pass through type for non-body parameters that are arrays', () = }, }, ], - })[0].schema.properties.options.type, + })[0].schema.properties.options.type ).toBe('array'); }); @@ -237,7 +237,7 @@ test('it should pass through format', () => { }, }, ], - })[0].schema.properties.checkbox.format, + })[0].schema.properties.checkbox.format ).toBe('int32'); }); @@ -254,7 +254,7 @@ test('it should pass through description', () => { }, }, ], - }), + }) ).toStrictEqual([ { label: 'Headers', @@ -289,8 +289,8 @@ test('it should work for top-level request body $ref', () => { }, }, }, - }, - ), + } + ) ).toStrictEqual([ { type: 'body', @@ -339,8 +339,8 @@ test('it should pull out schemas from `components/requestBodies`', () => { $ref: '#/components/requestBodies/Pet', }, }, - oas, - ), + oas + ) ).toStrictEqual([ { type: 'body', @@ -370,7 +370,7 @@ test('it should pass false value as default parameter', () => { }, }, ], - })[0].schema.properties.check, + })[0].schema.properties.check ).toStrictEqual({ default: false, type: 'boolean' }); }); @@ -397,8 +397,8 @@ test('it should fetch $ref parameters', () => { }, ], }, - oas, - )[0].schema.properties.param, + oas + )[0].schema.properties.param ).toStrictEqual(oas.components.parameters.Param.schema); }); @@ -431,8 +431,8 @@ test('it should fetch parameters that have a child $ref', () => { }, ], }, - oas, - )[0].schema.properties.param.items, + oas + )[0].schema.properties.param.items ).toStrictEqual(oas.components.schemas.string_enum); }); @@ -456,7 +456,7 @@ test('it should add common parameter to path params', () => { parametersToJsonSchema({ path: '/pet/{petId}', oas, - })[0].schema.properties.petId.description, + })[0].schema.properties.petId.description ).toBe(oas.paths['/pet/{petId}'].parameters[0].description); }); @@ -488,6 +488,6 @@ test('it should override path-level parameters on the operation level', () => { }, ], oas, - })[0].schema.properties.petId.description, + })[0].schema.properties.petId.description ).toBe('A comma-separated list of pet IDs'); }); diff --git a/__tests__/lib/utils.test.js b/__tests__/lib/utils.test.js index 956a3fae..fe395bd2 100644 --- a/__tests__/lib/utils.test.js +++ b/__tests__/lib/utils.test.js @@ -14,7 +14,7 @@ describe('utils.js', function() { }, { dir: path.join(__dirname, 'fixtures', 'yaml'), - }, + } ); }); }); @@ -30,7 +30,7 @@ describe('utils.js', function() { }, { dir: path.join(__dirname, 'fixtures', 'json'), - }, + } ); }); }); @@ -58,15 +58,11 @@ describe('utils.js', function() { }); it('bad json file is not swagger', function() { - expect(utils.isSwagger(path.join(__dirname, 'fixtures', 'yaml', 'notthefile.json'))).toBe( - false, - ); + expect(utils.isSwagger(path.join(__dirname, 'fixtures', 'yaml', 'notthefile.json'))).toBe(false); }); it('bad yaml file is not swagger', function() { - expect(utils.isSwagger(path.join(__dirname, 'fixtures', 'json', 'wrongfile.yaml'))).toBe( - false, - ); + expect(utils.isSwagger(path.join(__dirname, 'fixtures', 'json', 'wrongfile.yaml'))).toBe(false); }); }); }); diff --git a/__tests__/oas.test.js b/__tests__/oas.test.js index d4fc59b9..47b9a150 100644 --- a/__tests__/oas.test.js +++ b/__tests__/oas.test.js @@ -47,7 +47,7 @@ describe('server variables', () => { expect( new Oas({ servers: [{ url: 'https://example.com/{path}', variables: { path: { default: 'path' } } }], - }).url(), + }).url() ).toBe('https://example.com/path'); }); @@ -55,12 +55,10 @@ describe('server variables', () => { expect( new Oas( { - servers: [ - { url: 'https://{username}.example.com', variables: { username: { default: 'demo' } } }, - ], + servers: [{ url: 'https://{username}.example.com', variables: { username: { default: 'demo' } } }], }, - { username: 'domh' }, - ).url(), + { username: 'domh' } + ).url() ).toBe('https://domh.example.com'); }); @@ -68,12 +66,10 @@ describe('server variables', () => { expect( new Oas( { - servers: [ - { url: 'https://{username}.example.com', variables: { username: { default: 'demo' } } }, - ], + servers: [{ url: 'https://{username}.example.com', variables: { username: { default: 'demo' } } }], }, - { keys: [{ name: 1, username: 'domh' }] }, - ).url(), + { keys: [{ name: 1, username: 'domh' }] } + ).url() ).toBe('https://domh.example.com'); }); @@ -81,9 +77,7 @@ describe('server variables', () => { expect( new Oas( { - servers: [ - { url: 'https://{username}.example.com', variables: { username: { default: 'demo' } } }, - ], + servers: [{ url: 'https://{username}.example.com', variables: { username: { default: 'demo' } } }], }, { keys: [ @@ -91,16 +85,14 @@ describe('server variables', () => { { name: 2, username: 'readme' }, ], }, - 2, - ).url(), + 2 + ).url() ).toBe('https://readme.example.com'); }); // Test encodeURI it('should pass through if no default set', () => { - expect(new Oas({ servers: [{ url: 'https://example.com/{path}' }] }).url()).toBe( - 'https://example.com/{path}', - ); + expect(new Oas({ servers: [{ url: 'https://example.com/{path}' }] }).url()).toBe('https://example.com/{path}'); }); }); @@ -108,7 +100,7 @@ test('should be able to access properties on oas', () => { expect( new Oas({ info: { version: '1.0' }, - }).info.version, + }).info.version ).toBe('1.0'); }); @@ -128,7 +120,7 @@ describe('operation.getSecurity()', () => { }, }) .operation('/things', 'post') - .getSecurity(), + .getSecurity() ).toBe(security); }); @@ -144,7 +136,7 @@ describe('operation.getSecurity()', () => { security, }) .operation('/things', 'post') - .getSecurity(), + .getSecurity() ).toBe(security); }); @@ -159,7 +151,7 @@ describe('operation.getSecurity()', () => { }, }) .operation('/things', 'post') - .getSecurity(), + .getSecurity() ).toStrictEqual([]); }); }); diff --git a/src/api.js b/src/api.js index ca65970a..9b2e62bb 100644 --- a/src/api.js +++ b/src/api.js @@ -59,9 +59,7 @@ exports.api = function(args, opts) { return; } - let apiId = swagger.info.title - ? uslug(swagger.info.title) - : crypto.randomBytes(7).toString('hex'); + let apiId = swagger.info.title ? uslug(swagger.info.title) : crypto.randomBytes(7).toString('hex'); request.get(`${config.host.url}/check/${apiId}`, { json: true }, (err, check) => { if (!swagger['x-api-id']) { @@ -72,7 +70,7 @@ exports.api = function(args, opts) { } console.log( - 'Your Swagger file needs a unique "x-api-id" property to work. Do you want us to add it automatically?', + 'Your Swagger file needs a unique "x-api-id" property to work. Do you want us to add it automatically?' ); const add = prompt(`Add automatically? ${'(y/n) '.grey}`); @@ -81,7 +79,7 @@ exports.api = function(args, opts) { console.log( `Okay! To do it yourself, edit ${ file.split('/').slice(-1)[0].yellow - } and add the following 'x-api-id' line:`, + } and add the following 'x-api-id' line:` ); exampleId(file, apiId); @@ -95,7 +93,7 @@ exports.api = function(args, opts) { console.log( `${ 'Success! '.green - }We added it to your Swagger file! Make sure you commit the changes so your team is all using the same ID.`, + }We added it to your Swagger file! Make sure you commit the changes so your team is all using the same ID.` ); console.log(''); @@ -104,7 +102,7 @@ exports.api = function(args, opts) { console.log( `We weren't able to add the ID automatically. In ${ file.split('/').slice(-1)[0].yellow - }, add the following 'x-api-id' line:`, + }, add the following 'x-api-id' line:` ); exampleId(file, apiId); diff --git a/src/lib/commands/add.js b/src/lib/commands/add.js index e2e049a8..06572802 100644 --- a/src/lib/commands/add.js +++ b/src/lib/commands/add.js @@ -24,6 +24,6 @@ exports.run = function(config, info) { function() { console.log(`${'Success! '.green}User has been added.`); process.exit(); - }, + } ); }; diff --git a/src/lib/commands/docs.js b/src/lib/commands/docs.js index 7a71e092..fad0da7a 100644 --- a/src/lib/commands/docs.js +++ b/src/lib/commands/docs.js @@ -8,9 +8,7 @@ exports.category = 'services'; exports.run = function(config, info) { console.log(''); - console.log( - `${'Success! '.green}You can now access your Swagger from the following publicly sharable URL:`, - ); + console.log(`${'Success! '.green}You can now access your Swagger from the following publicly sharable URL:`); console.log(''); console.log(` ${info.swaggerUrl}?docs`); console.log(''); diff --git a/src/lib/commands/endpoint.js b/src/lib/commands/endpoint.js index 6a610a1b..1d4c76d2 100644 --- a/src/lib/commands/endpoint.js +++ b/src/lib/commands/endpoint.js @@ -25,9 +25,7 @@ exports.run = function() { console.log('This will be expanded when the Swagger file is compiled.'); console.log(''); - console.log( - 'For more information on this syntax, see https://github.com/readmeio/swagger-inline', - ); + console.log('For more information on this syntax, see https://github.com/readmeio/swagger-inline'); process.exit(); }; diff --git a/src/lib/commands/host.js b/src/lib/commands/host.js index 323a5bc0..2f48e856 100644 --- a/src/lib/commands/host.js +++ b/src/lib/commands/host.js @@ -8,9 +8,7 @@ exports.category = 'services'; exports.run = function(config, info) { console.log(''); - console.log( - `${'Success! '.green}You can now access your Swagger from the following publicly sharable URL:`, - ); + console.log(`${'Success! '.green}You can now access your Swagger from the following publicly sharable URL:`); console.log(''); console.log(` ${info.swaggerUrl}`); console.log(''); diff --git a/src/lib/commands/init.js b/src/lib/commands/init.js index cfe9c1c6..c4f7f315 100644 --- a/src/lib/commands/init.js +++ b/src/lib/commands/init.js @@ -145,17 +145,11 @@ exports.run = function() { console.log(utils.swaggerInlineExample(utils.guessLanguage())); console.log(''); - console.log( - 'For more information on this syntax, see https://github.com/readmeio/swagger-inline', - ); + console.log('For more information on this syntax, see https://github.com/readmeio/swagger-inline'); console.log(''); console.log(`To see what you can do with your API, type ${'oas help'.yellow}.`); console.log(''); - console.log( - `To generate an OAS file, type ${'oas generate'.yellow}. To publish it, type ${ - 'oas host'.yellow - }!`, - ); + console.log(`To generate an OAS file, type ${'oas generate'.yellow}. To publish it, type ${'oas host'.yellow}!`); console.log(''); process.exit(); diff --git a/src/lib/get-schema.js b/src/lib/get-schema.js index 72c801a1..d0527d2f 100644 --- a/src/lib/get-schema.js +++ b/src/lib/get-schema.js @@ -11,10 +11,7 @@ module.exports = function getSchema(pathOperation, oas) { return { type, schema: pathOperation.requestBody.content[type].schema }; } - if ( - pathOperation.requestBody && - pathOperation.requestBody.$ref.match(/^#\/components\/requestBodies\/.*$/) - ) { + if (pathOperation.requestBody && pathOperation.requestBody.$ref.match(/^#\/components\/requestBodies\/.*$/)) { return getSchema({ requestBody: findSchemaDefinition(pathOperation.requestBody.$ref, oas) }); } diff --git a/src/lib/get-user-variable.js b/src/lib/get-user-variable.js index ecc70bb5..276781dc 100644 --- a/src/lib/get-user-variable.js +++ b/src/lib/get-user-variable.js @@ -7,7 +7,7 @@ function getUserVariable(user, property, selectedApp = false) { if (selectedApp) { return getKey( user.keys.find(key => key.name === selectedApp), - property, + property ); } diff --git a/src/lib/parameters-to-json-schema.js b/src/lib/parameters-to-json-schema.js index bd5e3c2c..8f0ea8a2 100644 --- a/src/lib/parameters-to-json-schema.js +++ b/src/lib/parameters-to-json-schema.js @@ -23,9 +23,7 @@ function getBodyParam(pathOperation, oas) { return { type, label: types[type], - schema: oas.components - ? { definitions: { components: oas.components }, ...schema.schema } - : schema.schema, + schema: oas.components ? { definitions: { components: oas.components }, ...schema.schema } : schema.schema, }; } @@ -42,7 +40,7 @@ function getOtherParams(pathOperation, oas) { const { path } = pathOperation; const commonParams = pathOperation.oas.paths[path].parameters; const commonParamsNotInParams = commonParams.filter( - param => !pathParameters.find(param2 => param2.name === param.name && param2.in === param.in), + param => !pathParameters.find(param2 => param2.name === param.name && param2.in === param.in) ); pathParameters = pathParameters.concat(commonParamsNotInParams || []); @@ -72,10 +70,7 @@ function getOtherParams(pathOperation, oas) { if (current.schema.type === 'array') { schema.type = 'array'; - if ( - Object.keys(current.schema.items).length === 1 && - typeof current.schema.items.$ref !== 'undefined' - ) { + if (Object.keys(current.schema.items).length === 1 && typeof current.schema.items.$ref !== 'undefined') { schema.items = findSchemaDefinition(current.schema.items.$ref, oas); } else { schema.items = current.schema.items; diff --git a/src/lib/utils.js b/src/lib/utils.js index fdecaf3e..dd3c9260 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -37,9 +37,7 @@ exports.findSwagger = function(info, cb) { oas.load(function(err, schema) { if (!schema['x-si-base']) { console.log("We couldn't find a Swagger file.".red); - console.log( - `Don't worry, it's easy to get started! Run ${'oas init'.yellow} to get started.`, - ); + console.log(`Don't worry, it's easy to get started! Run ${'oas init'.yellow} to get started.`); process.exit(1); } @@ -182,7 +180,7 @@ exports.getSwaggerUrl = function(config, info, cb) { } return cb(url); - }, + } ); };