Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adds pagination to the OpenAPI command #353

Merged
merged 3 commits into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions __tests__/lib/prompts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ const specList = [
},
];

const getSpecs = () => {
return [
{
_id: 'spec3',
title: 'spec3_title',
},
];
};

describe('prompt test bed', () => {
let enquirer;

Expand Down Expand Up @@ -70,23 +79,31 @@ describe('prompt test bed', () => {
await prompt.keypress(null, { name: 'down' });
await prompt.submit();
});
const answer = await enquirer.prompt(promptHandler.createOasPrompt([{}]));
const answer = await enquirer.prompt(promptHandler.createOasPrompt([{}], null, 1, null));

expect(answer.option).toBe('create');
expect(answer.specId).toBe('');
});

it('should return specId if user chooses to update file', async () => {
jest.mock('enquirer');
enquirer.on('prompt', async prompt => {
await prompt.keypress(null, { name: 'down' });
await prompt.keypress(null, { name: 'up' });
await prompt.submit();
await prompt.submit();
});
const answer = await enquirer.prompt(promptHandler.createOasPrompt(specList));
enquirer.prompt = jest.fn();
enquirer.prompt.mockReturnValue('spec1');
const parsedDocs = {
next: {
page: null,
},
prev: {
page: null,
},
};
const answer = await enquirer.prompt(promptHandler.createOasPrompt(specList, parsedDocs, 1, getSpecs));

expect(answer.option).toBe('update');
expect(answer.specId).toBe('spec1');
expect(answer).toBe('spec1');
});
});

Expand Down
21 changes: 18 additions & 3 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"oas": "^14.0.0",
"oas-normalize": "^3.0.4",
"open": "^8.2.1",
"parse-link-header": "^1.0.1",
"read": "^1.0.7",
"semver": "^7.0.0",
"table-layout": "^1.0.0"
Expand Down
22 changes: 16 additions & 6 deletions src/cmds/openapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const APIError = require('../lib/apiError');
const { getProjectVersion } = require('../lib/versionSelect');
const fetch = require('node-fetch');
const FormData = require('form-data');
const parse = require('parse-link-header');

exports.command = 'openapi';
exports.usage = 'openapi [file] [options]';
Expand Down Expand Up @@ -159,19 +160,28 @@ exports.run = async function (opts) {
- If found, prompt user to either create a new spec or update an existing one
*/

if (!id) {
const apiSettings = await fetch(`${config.host}/api/v1/api-specification`, {
function getSpecs(url) {
return fetch(`${config.host}${url}`, {
method: 'get',
headers: {
'x-readme-version': versionCleaned,
Authorization: `Basic ${encodedString}`,
},
}).then(res => res.json());
});
}

if (!id) {
const apiSettings = await getSpecs(`/api/v1/api-specification`);

const totalPages = Math.ceil(apiSettings.headers.get('x-total-count') / 10);
const parsedDocs = parse(apiSettings.headers.get('link'));

if (!apiSettings.length) return createSpec();
const apiSettingsBody = await apiSettings.json();
if (!apiSettingsBody.length) return createSpec();

const { option, specId } = await prompt(promptOpts.createOasPrompt(apiSettings));
return option === 'create' ? createSpec() : updateSpec(specId);
const { option } = await prompt(promptOpts.createOasPrompt(apiSettingsBody, parsedDocs, totalPages, getSpecs));
if (!option) return null;
return option === 'create' ? createSpec() : updateSpec(option);
}

/*
Expand Down
79 changes: 65 additions & 14 deletions src/lib/prompts.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const semver = require('semver');
const { prompt } = require('enquirer');
const parse = require('parse-link-header');

exports.generatePrompts = (versionList, selectOnly = false) => [
{
Expand Down Expand Up @@ -38,7 +40,59 @@ exports.generatePrompts = (versionList, selectOnly = false) => [
},
];

exports.createOasPrompt = specList => [
function specOptions(specList, parsedDocs, currPage, totalPages) {
const specs = specList.map(s => {
return {
message: s.title,
value: s._id, // eslint-disable-line no-underscore-dangle
};
});
if (parsedDocs.prev.page) specs.push({ message: `< Prev (page ${currPage - 1} of ${totalPages})`, value: 'prev' });
if (parsedDocs.next.page) {
specs.push({ message: `Next (page ${currPage + 1} of ${totalPages}) >`, value: 'next' });
}
return specs;
}

const updateOasPrompt = (specList, parsedDocs, currPage, totalPages, getSpecs) => [
{
type: 'select',
name: 'specId',
message: 'Select your desired file to update',
choices: specOptions(specList, parsedDocs, currPage, totalPages),
async result(spec) {
if (spec === 'prev') {
try {
const newSpecs = await getSpecs(`${parsedDocs.prev.url}`);
const newParsedDocs = parse(newSpecs.headers.get('link'));
const newSpecList = await newSpecs.json();
const { specId } = await prompt(
updateOasPrompt(newSpecList, newParsedDocs, currPage - 1, totalPages, getSpecs)
);
return specId;
} catch (e) {
return null;
}
}
if (spec === 'next') {
try {
const newSpecs = await getSpecs(`${parsedDocs.next.url}`);
const newParsedDocs = parse(newSpecs.headers.get('link'));
const newSpecList = await newSpecs.json();
const { specId } = await prompt(
updateOasPrompt(newSpecList, newParsedDocs, currPage + 1, totalPages, getSpecs)
);
return specId;
} catch (e) {
return null;
}
}
return spec;
},
},
];

exports.createOasPrompt = (specList, parsedDocs, totalPages, getSpecs) => [
{
type: 'select',
name: 'option',
Expand All @@ -47,20 +101,17 @@ exports.createOasPrompt = specList => [
{ message: 'Update existing', value: 'update' },
{ message: 'Create a new spec', value: 'create' },
],
},
{
type: 'select',
name: 'specId',
message: 'Select your desired file to update',
skip() {
return this.enquirer.answers.option !== 'update';
async result(picked) {
if (picked === 'update') {
try {
const { specId } = await prompt(updateOasPrompt(specList, parsedDocs, 1, totalPages, getSpecs));
return specId;
} catch (e) {
return null;
}
}
return picked;
},
choices: specList.map(s => {
return {
message: s.title,
value: s._id, // eslint-disable-line no-underscore-dangle
};
}),
},
];

Expand Down