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(openapi): add --create flag #575

Merged
merged 6 commits into from
Aug 20, 2022
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ This will upload `path-to-openapi.json` to your project and return an ID and URL
rdme openapi [path-to-file.json]
```

If you want to bypass the prompt to create or update an API definition, you can pass the `--create` flag:

```sh
rdme openapi [path-to-file.json] --version={project-version} --create
```

#### Editing (Re-Syncing) an Existing API Definition

This will edit (re-sync) an existing API definition (identified by `--id`) within your ReadMe project.
Expand Down
3 changes: 3 additions & 0 deletions __tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Options
uploading an existing API definition.
--version string Project version. If running command in a CI environment and this
option is not passed, the main project version will be used.
--create Bypasses the create/update prompt and creates a new API definition.
--useSpecVersion Uses the version listed in the \`info.version\` field in the API
definition for the project version parameter.
--workingDirectory string Working directory (for usage with relative external references)
Expand Down Expand Up @@ -43,6 +44,7 @@ Options
uploading an existing API definition.
--version string Project version. If running command in a CI environment and this
option is not passed, the main project version will be used.
--create Bypasses the create/update prompt and creates a new API definition.
--useSpecVersion Uses the version listed in the \`info.version\` field in the API
definition for the project version parameter.
--workingDirectory string Working directory (for usage with relative external references)
Expand Down Expand Up @@ -71,6 +73,7 @@ Options
uploading an existing API definition.
--version string Project version. If running command in a CI environment and this
option is not passed, the main project version will be used.
--create Bypasses the create/update prompt and creates a new API definition.
--useSpecVersion Uses the version listed in the \`info.version\` field in the API
definition for the project version parameter.
--workingDirectory string Working directory (for usage with relative external references)
Expand Down
60 changes: 60 additions & 0 deletions __tests__/cmds/openapi/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,66 @@ describe('rdme openapi', () => {
return mock.done();
});

it('should create a new spec via `--create` flag', async () => {
const registryUUID = getRandomRegistryId();

const mock = getAPIMock()
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, [{ version }])
.post('/api/v1/api-registry', body => body.match('form-data; name="spec"'))
.reply(201, { registryUUID, spec: { openapi: '3.0.0' } })
.post('/api/v1/api-specification', { registryUUID })
.delayConnection(1000)
.basicAuth({ user: key })
.reply(201, { _id: 1 }, { location: exampleRefLocation });

const spec = './__tests__/__fixtures__/ref-oas/petstore.json';

await expect(
openapi.run({
key,
version,
spec,
create: true,
})
).resolves.toBe(successfulUpload(spec));

return mock.done();
});

it('should create a new spec via `--create` flag and ignore `--id`', async () => {
const registryUUID = getRandomRegistryId();

const mock = getAPIMock()
.post('/api/v1/api-registry', body => body.match('form-data; name="spec"'))
.reply(201, { registryUUID, spec: { openapi: '3.0.0' } })
.post('/api/v1/api-specification', { registryUUID })
.delayConnection(1000)
.basicAuth({ user: key })
.reply(201, { _id: 1 }, { location: exampleRefLocation });

const spec = './__tests__/__fixtures__/ref-oas/petstore.json';

await expect(
openapi.run({
key,
spec,
id: 'some-id',
create: true,
})
).resolves.toBe(successfulUpload(spec));

expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.info).toHaveBeenCalledTimes(0);

const output = getCommandOutput();

expect(output).toMatch(/the `--id` parameter will be ignored/i);

return mock.done();
});

it('should bundle and upload the expected content', async () => {
let requestBody;
const registryUUID = getRandomRegistryId();
Expand Down
14 changes: 13 additions & 1 deletion src/cmds/openapi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type Options = {
id?: string;
spec?: string;
version?: string;
create?: boolean;
useSpecVersion?: boolean;
workingDirectory?: string;
};
Expand Down Expand Up @@ -53,6 +54,11 @@ export default class OpenAPICommand extends Command {
type: String,
defaultOption: true,
},
{
name: 'create',
type: Boolean,
description: 'Bypasses the create/update prompt and creates a new API definition.',
},
{
name: 'useSpecVersion',
type: Boolean,
Expand All @@ -70,7 +76,7 @@ export default class OpenAPICommand extends Command {
async run(opts: CommandOptions<Options>) {
super.run(opts);

const { key, id, spec, useSpecVersion, version, workingDirectory } = opts;
const { key, id, spec, create, useSpecVersion, version, workingDirectory } = opts;

let selectedVersion = version;
let isUpdate: boolean;
Expand All @@ -86,6 +92,10 @@ export default class OpenAPICommand extends Command {
);
}

if (create && id) {
Command.warn("We'll be using the `--create` option , so the `--id` parameter will be ignored.");
kanadgupta marked this conversation as resolved.
Show resolved Hide resolved
}

// Reason we're hardcoding in command here is because `swagger` command
// relies on this and we don't want to use `swagger` in this function
const { bundledSpec, specPath, specType, specVersion } = await prepareOas(spec, 'openapi');
Expand Down Expand Up @@ -214,6 +224,8 @@ export default class OpenAPICommand extends Command {
});
}

if (create) return createSpec();

if (!id) {
Command.debug('no id parameter, retrieving list of API specs');
const apiSettings = await getSpecs('/api/v1/api-specification');
Expand Down