Skip to content

Commit

Permalink
fix: update loopback-connector-mysql to 7.0.5 (latest)
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammad Aaqil <aaqilcs102@gmail.com>
  • Loading branch information
aaqilniz committed Mar 2, 2024
1 parent e031d91 commit 45d6c50
Show file tree
Hide file tree
Showing 9 changed files with 2,086 additions and 2 deletions.
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions packages/cli/.yo-rc.json
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,27 @@
"name": "promote-anonymous-schemas",
"hide": false
},
"readonly": {
"description": "Generate only GET endpoints.",
"required": false,
"type": "Boolean",
"name": "readonly",
"hide": false
},
"exclude": {
"description": "Exclude endpoints with provided regex.",
"required": false,
"type": "String",
"name": "exclude",
"hide": false
},
"include": {
"description": "Only include endpoints with provided regex.",
"required": false,
"type": "String",
"name": "include",
"hide": false
},
"config": {
"type": "String",
"alias": "c",
Expand Down
77 changes: 77 additions & 0 deletions packages/cli/generators/openapi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,24 @@ module.exports = class OpenApiGenerator extends BaseGenerator {
type: Boolean,
});

this.option('readonly', {
description: g.f('Generate only GET endpoints.'),
required: false,
type: Boolean,
});

this.option('exclude', {
description: g.f('Exclude endpoints with provided regex.'),
required: false,
type: String,
});

this.option('include', {
description: g.f('Only include endpoints with provided regex.'),
required: false,
type: String,
});

return super._setupGenerator();
}

Expand Down Expand Up @@ -211,6 +229,57 @@ module.exports = class OpenApiGenerator extends BaseGenerator {
}
}

async askForReadonly() {
if (this.shouldExit()) return;
const prompts = [
{
name: 'readonly',
message: g.f('Generate only GET endpoints.'),
when: false,
// when: !this.options.readonly,
default: false,
},
];
const answers = await this.prompt(prompts);
if (answers.readonly) {
this.options.readonly = answers.readonly;
}
}

async askForExclude() {
if (this.shouldExit()) return;
const prompts = [
{
name: 'exclude',
message: g.f('Exclude endpoints with provided regex.'),
// when: !this.options.exclude,
when: false,
default: false,
},
];
const answers = await this.prompt(prompts);
if (answers.exclude) {
this.options.exclude = answers.exclude;
}
}

async askForInclude() {
if (this.shouldExit()) return;
const prompts = [
{
name: 'include',
message: g.f('Only include endpoints with provided regex.'),
when: false,
// when: !this.options.include,
default: false,
},
];
const answers = await this.prompt(prompts);
if (answers.include) {
this.options.include = answers.include;
}
}

async askForSpecUrlOrPath() {
if (this.shouldExit()) return;
if (this.dataSourceInfo && this.dataSourceInfo.specPath) {
Expand All @@ -236,11 +305,19 @@ module.exports = class OpenApiGenerator extends BaseGenerator {

async loadAndBuildApiSpec() {
if (this.shouldExit()) return;
if (this.options.exclude && this.options.include) {
this.exit(
new Error('We cannot have include and exclude at the same time.'),
);
}
try {
const result = await loadAndBuildSpec(this.url, {
log: this.log,
validate: this.options.validate,
promoteAnonymousSchemas: this.options['promote-anonymous-schemas'],
readonly: this.options.readonly,
exclude: this.options.exclude,
include: this.options.include,
});
debugJson('OpenAPI spec', result.apiSpec);
Object.assign(this, result);
Expand Down
115 changes: 113 additions & 2 deletions packages/cli/generators/openapi/spec-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {debugJson, cloneSpecObject} = require('./utils');
const {generateControllerSpecs} = require('./spec-helper');
const {generateModelSpecs, registerNamedSchemas} = require('./schema-helper');
const {ResolverError} = require('@apidevtools/json-schema-ref-parser');
const openapiFilter = require('openapi-filter');

/**
* Load swagger specs from the given url or file path; handle yml or json
Expand Down Expand Up @@ -58,9 +59,12 @@ async function loadSpec(specUrlStr, {log, validate} = {}) {

async function loadAndBuildSpec(
url,
{log, validate, promoteAnonymousSchemas} = {},
{log, validate, promoteAnonymousSchemas, readonly, exclude, include} = {},
) {
const apiSpec = await loadSpec(url, {log, validate});
let apiSpec = await loadSpec(url, {log, validate});

apiSpec = filterSpec(apiSpec, readonly, exclude, include);

// First populate the type registry for named schemas
const typeRegistry = {
objectTypeMapping: new Map(),
Expand All @@ -77,6 +81,113 @@ async function loadAndBuildSpec(
};
}

function getIndiciesOf(searchStr, str, caseSensitive) {
const searchStrLen = searchStr.length;
if (searchStrLen === 0) {
return [];
}
let startIndex = 0,
index;
const indices = [];
if (!caseSensitive) {
str = str.toLowerCase();
searchStr = searchStr.toLowerCase();
}
while ((index = str.indexOf(searchStr, startIndex)) > -1) {
indices.push(index);
startIndex = index + searchStrLen;
}
return indices;
}

function insertAtIndex(str, substring, index) {
return str.slice(0, index) + substring + str.slice(index);
}

function applyFilters(stringifiedSpecs, options) {
let specs = JSON.parse(stringifiedSpecs);
const openapiComponent = specs.components;
specs = openapiFilter.filter(specs, options);
specs.components = openapiComponent;
return specs;
}

function findIndexes(stringSpecs, regex) {
let result;
const indices = [];
while ((result = regex.exec(stringSpecs))) {
indices.push(result.index);
}
return indices;
}

function excludeOrIncludeSpec(specs, filter, options) {
let stringifiedSpecs = JSON.stringify(specs);
const regex = new RegExp(filter, 'g');

const indexes = findIndexes(stringifiedSpecs, regex);
let indiciesCount = 0;
while (indiciesCount < indexes.length) {
const ind = indexes[indiciesCount];
for (let i = ind; i < stringifiedSpecs.length; i++) {
const toMatch =
stringifiedSpecs[i] + stringifiedSpecs[i + 1] + stringifiedSpecs[i + 2];
if (toMatch === '":{') {
stringifiedSpecs = insertAtIndex(
stringifiedSpecs,
'"x-filter": true,',
i + 3,
);
indiciesCount++;
break;
}
}
}
return applyFilters(stringifiedSpecs, options);
}

function readonlySpec(specs, options) {
let stringifiedSpecs = JSON.stringify(specs);
const excludeOps = ['"post":', '"patch":', '"put":', '"delete":'];
excludeOps.forEach(operator => {
let indices = getIndiciesOf(operator, stringifiedSpecs);
let indiciesCount = 0;
while (indiciesCount < indices.length) {
indices = getIndiciesOf(operator, stringifiedSpecs);
const index = indices[indiciesCount];
stringifiedSpecs = insertAtIndex(
stringifiedSpecs,
'"x-filter": true,',
index + operator.length + 1,
);
indiciesCount++;
}
});
return applyFilters(stringifiedSpecs, options);
}

function filterSpec(specs, readonly, exclude, include) {
const options = {
valid: true,
info: true,
strip: true,
flags: ['x-filter'],
servers: true,
};
if (readonly) {
specs = readonlySpec(specs, options);
}
if (exclude) {
// exclude only specified - include everything else
specs = excludeOrIncludeSpec(specs, exclude, options);
}
if (include) {
// include only specified - exclude everything else
specs = excludeOrIncludeSpec(specs, include, {...options, inverse: true});
}
return specs;
}

module.exports = {
loadSpec,
loadAndBuildSpec,
Expand Down
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"mkdirp": "^3.0.1",
"natural-compare": "^1.4.0",
"pacote": "^17.0.6",
"openapi-filter": "^3.2.3",
"pluralize": "^8.0.0",
"regenerate": "^1.4.2",
"semver": "^7.5.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,27 @@ exports[`cli saves command metadata to .yo-rc.json 1`] = `
"name": "promote-anonymous-schemas",
"hide": false
},
"readonly": {
"description": "Generate only GET endpoints.",
"required": false,
"type": "Boolean",
"name": "readonly",
"hide": false
},
"exclude": {
"description": "Exclude endpoints with provided regex.",
"required": false,
"type": "String",
"name": "exclude",
"hide": false
},
"include": {
"description": "Only include endpoints with provided regex.",
"required": false,
"type": "String",
"name": "include",
"hide": false
},
"config": {
"type": "String",
"alias": "c",
Expand Down
Loading

0 comments on commit 45d6c50

Please sign in to comment.