Skip to content

Commit

Permalink
feat: change sync-request library to then-request
Browse files Browse the repository at this point in the history
This is needed as using the sync-request in production can stuck the generation as evidenced in jhipster-online

Duplicates c0f988d, e9fe904, 1e64e9f and f09e68f from the master branch.

Related to jhipster/jhipster-online#233
  • Loading branch information
SudharakaP committed Oct 7, 2020
1 parent be5c49e commit 9b06d0f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 51 deletions.
50 changes: 23 additions & 27 deletions generators/openapi-client/prompts.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@
*/
const path = require('path');
const shelljs = require('shelljs');
const request = require('sync-request');
const request = require('then-request');

module.exports = {
askActionType,
askExistingAvailableDocs,
askGenerationInfos,
};

function fetchSwaggerResources(input) {
async function fetchSwaggerResources(input) {
const availableDocs = [];

const baseUrl = input.replace(/\/$/, '');
const swaggerResources = request('GET', `${baseUrl}/swagger-resources`, {
const swaggerResources = await request('GET', `${baseUrl}/swagger-resources`, {
// This header is needed to use the custom /swagger-resources controller
// and not the default one that has only the gateway's swagger resource
headers: { Accept: 'application/json, text/javascript;' },
Expand All @@ -49,11 +49,9 @@ function fetchSwaggerResources(input) {

function askActionType() {
if (this.options.regen) {
return;
return undefined;
}

const done = this.async();

const hasExistingApis = Object.keys(this.openApiClients).length !== 0;

const actionList = [
Expand Down Expand Up @@ -95,9 +93,9 @@ function askActionType() {
name: 'jhipsterEndpoint',
message: 'Enter the URL of the running Jhipster instance',
default: 'http://localhost:8080',
validate: input => {
validate: async input => {
try {
const availableDocs = fetchSwaggerResources(input);
const availableDocs = await fetchSwaggerResources(input);

if (availableDocs.length === 0) {
return `No live doc found at ${input}`;
Expand Down Expand Up @@ -135,10 +133,10 @@ function askActionType() {
message: 'Where is your Swagger/OpenAPI spec (URL or path) ?',
default: 'http://petstore.swagger.io/v2/swagger.json',
store: true,
validate: input => {
validate: async input => {
try {
if (/^((http|https):\/\/)/.test(input)) {
request('GET', `${input}`, {
await request('GET', `${input}`, {
// headers: { Accept: 'application/json, text/javascript;' }
});
} else if (!shelljs.test('-f', input)) {
Expand All @@ -152,31 +150,31 @@ function askActionType() {
},
];

this.prompt(prompts).then(props => {
if (props.jhipsterEndpoint !== undefined) {
props.availableDocs = fetchSwaggerResources(props.jhipsterEndpoint);
} else if (props.jhipsterDirectory !== undefined) {
props.inputSpec = `${props.jhipsterDirectory}/src/main/resources/swagger/api.yml`;
} else if (props.customEndpoint !== undefined) {
props.inputSpec = props.customEndpoint;
}

return this.prompt(prompts).then(props => {
if (newClient) {
props.action = 'new';
}

props.generatorName = this.config.get('reactive') ? 'java' : 'spring';

this.props = props;
done();
if (props.jhipsterEndpoint !== undefined) {
return fetchSwaggerResources(props.jhipsterEndpoint).then(availableDocs => {
props.availableDocs = availableDocs;
});
}
if (props.jhipsterDirectory !== undefined) {
props.inputSpec = `${props.jhipsterDirectory}/src/main/resources/swagger/api.yml`;
} else if (props.customEndpoint !== undefined) {
props.inputSpec = props.customEndpoint;
}
});
}

function askExistingAvailableDocs() {
if (this.options.regen) {
return;
return undefined;
}
const done = this.async();

const prompts = [
{
Expand All @@ -188,21 +186,19 @@ function askExistingAvailableDocs() {
},
];

this.prompt(prompts).then(props => {
return this.prompt(prompts).then(props => {
if (props.availableDoc !== undefined) {
this.props.inputSpec = props.availableDoc.url;
this.props.cliName = props.availableDoc.name === 'default' ? 'apidocs' : props.availableDoc.name; // "default" cannot be used as it's a keyword in java
}
done();
});
}

function askGenerationInfos() {
if (this.options.regen) {
return;
return undefined;
}

const done = this.async();
const prompts = [
{
when:
Expand Down Expand Up @@ -255,7 +251,7 @@ function askGenerationInfos() {
},
];

this.prompt(prompts).then(props => {
return this.prompt(prompts).then(props => {
if (props.cliName !== undefined) {
this.props.cliName = props.cliName;
}
Expand Down
23 changes: 0 additions & 23 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"randexp": "0.5.3",
"semver": "7.3.2",
"shelljs": "0.8.4",
"sync-request": "6.1.0",
"then-request": "6.0.2",
"tabtab": "2.2.2",
"test": "^0.6.0",
"through2": "3.0.2",
Expand Down

0 comments on commit 9b06d0f

Please sign in to comment.