Skip to content

Commit

Permalink
feat(openapi): add --create flag (#575)
Browse files Browse the repository at this point in the history
* feat(openapi): add `--create` flag

* docs: copy

* test: update snapshot

* fix: ignore `--id` parameter if it's also passed in
  • Loading branch information
kanadgupta authored Aug 20, 2022
1 parent 486a8b0 commit ff3a8b3
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
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 @@ -42,6 +43,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 @@ -69,6 +71,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.");
}

// 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

0 comments on commit ff3a8b3

Please sign in to comment.