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

A self-hosted API Explorer as a new extension #2014

Merged
merged 2 commits into from
Nov 15, 2018
Merged

Conversation

bajtos
Copy link
Member

@bajtos bajtos commented Nov 12, 2018

In this pull request, I am picking up the best parts from #1664 and #1976 and proposing an implementation of a self-hosted API explorer build on the features that are already available. The internal design is not as pretty & clean as it could be, but it works OOTB (as long as the user configures a custom url path, see below).

I expect that the internal details of the explorer component will evolve over time as we keep adding more extension points to our REST layer, but that's way beyond this pull request.

The missing piece: how to disable the built-in redirect to explorer.loopback.io. I'll be opening a new pull request for that (hopefully today). See feat(rest): add config option to disable API Explorer redirects #2016.

Close #1664, close #1976, close #1960.

This pull request is one of the tasks for [API Explorer] Self-hosted API Explorer #559.

@strongloop/loopback-maintainers please review

Checklist

  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

@bajtos bajtos added this to the November 2018 Milestone milestone Nov 12, 2018
@bajtos bajtos self-assigned this Nov 12, 2018
@bajtos bajtos requested review from raymondfeng and a team November 12, 2018 09:18
Copy link
Member Author

@bajtos bajtos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pull request contains three commits to make the review easier:

  1. The first one adds an empty "explorer" package
  2. The second implements self-hosted API explorer
  3. The third one enables self-hosted API explorer in example projects

I'll work on the documentation and CLI templates once the general direction is approved.

packages/explorer/README.md Outdated Show resolved Hide resolved
packages/explorer/docs.json Outdated Show resolved Hide resolved
packages/explorer/src/explorer.component.ts Outdated Show resolved Hide resolved
packages/explorer/src/explorer.controller.ts Outdated Show resolved Hide resolved
packages/explorer/src/explorer.controller.ts Outdated Show resolved Hide resolved
@bajtos
Copy link
Member Author

bajtos commented Nov 12, 2018

@strongloop/loopback-maintainers How should we name the extension providing self-hosted API Explorer?

Currently, I am using @loopback/explorer, even though the component is using "ApiExplorer" name everywhere. Should we rename it to @loopback/api-explorer?

Also, do we want to start some kind of convention for LB extensions? In LB 3.x, extension packages have names starting with loopback-component-, e.g. loopback-component-explorer. Should we do the same in LB4 and call the new package @loopback/extension-api-explorer? Personally, I find such name awfully long and prefer shorter @loopback/api-explorer.

When deciding between explorer and api-explorer (or even something else): Are we expecting other kinds of explorers to be available in LB4 apps in the future? I believe GraphQL has an interactive explorer/client. I think we should use a naming scheme that will make it clear which extension is exploring which protocol. Both explorer and api-explorer seem too generic to me, I am proposing rest-explorer (to go with @loopback/rest component) or perhaps openapi-explorer.

Thoughts?

@raymondfeng
Copy link
Contributor

@bajtos CI is failing.

@bajtos
Copy link
Member Author

bajtos commented Nov 12, 2018

CI is failing.

I know, this is waiting for feat(rest): add config option to disable API Explorer redirects #2016 to be landed first.

@raymondfeng
Copy link
Contributor

The proposal combines the ability of controllers and static assets to serve API explorer with server-side templating. I think it's acceptable first step. LGTM.

Copy link
Contributor

@jannyHou jannyHou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly LGTM, left a few nitpicks. I am good with the name @loopback/rest-explorer.


const response = await request.get('/explorer/').expect(200);
const body = response.body;
expect(body).to.match(/^\s*url: '\/apispec',\s*$/m);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are 3 endpoint mappings above, do we want to assert the other 2 as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is verifying that we are picking the correct endpoints (OpenAPI v3 in JSON format). This test is configuring other mappings to verify that the algorithm picks the right mapping..

@jannyHou How can I improve the test to make this more clear to readers?

packages/explorer/src/explorer.controller.ts Outdated Show resolved Hide resolved
packages/explorer/test/acceptance/explorer.acceptance.ts Outdated Show resolved Hide resolved
packages/explorer/src/explorer.controller.ts Outdated Show resolved Hide resolved
packages/explorer/src/explorer.keys.ts Outdated Show resolved Hide resolved
@marioestradarosa
Copy link
Contributor

How about rest-api-explorer ? (too long?), but rest-explorer LGTM also.

@dhmlau
Copy link
Member

dhmlau commented Nov 13, 2018

Just my two cents.. Between rest-explorer and openapi-explorer, I'd prefer rest-explorer because it's clearer that it's for REST. But no strong preference for either way.

@bajtos
Copy link
Member Author

bajtos commented Nov 13, 2018

Re: package name - thank you @jannyHou @marioestradarosa @dhmlau for the feedback. I am going to use @loopback/rest-explorer going forward.

@bajtos
Copy link
Member Author

bajtos commented Nov 13, 2018

I have rebased the patch on the latest master and addressed all remaining issues & todos. The pull request is ready for final review. I'll squash the commit history before landing.

To make the review easier, I left the first three commits unchanged and added a bunch of new small commits (one for each logical step, kind of):
chore: rename explorer to rest-explorer

  • fix(rest): allow users to disable Explorer redirects after RestServer…
  • fix: disable built-in swagger-ui redirects when a self-hosted compone…
  • docs: describe rest-explorer component
  • fix: apidocs for rest-explorer
  • docs: add a mention of hard-coded visual style to README
  • feat(cli): enable self-hosted REST API explorer

@raymondfeng @jannyHou @marioestradarosa LGTY?

@bajtos bajtos requested a review from jannyHou November 13, 2018 12:00
@bajtos
Copy link
Member Author

bajtos commented Nov 13, 2018

The documentation changes require us to land loopbackio/loopback.io#767, I think that other PR should be landed first.

@bajtos
Copy link
Member Author

bajtos commented Nov 13, 2018

The documentation changes require us to land loopbackio/loopback.io#767, I think that other PR should be landed first.

Actually, I found a better solution - see #2025

@bajtos
Copy link
Member Author

bajtos commented Nov 13, 2018

continuous-integration/travis-ci/pr — The Travis CI build failed

✖   scope must be one of [
    benchmark, docs, example-hello-world, example-log-extension, example-rpc-server, 
    example-soap-calculator, example-todo-list, example-todo, authentication, boot, build, 
    cli, context, core, http-caching-proxy, http-server, metadata, openapi-spec-builder,
    openapi-v3-types, openapi-v3, repository-json-schema, repository, rest-explorer, rest, 
    service-proxy, testlab, sandbox-example
] [scope-enum]
✖   found 1 problems, 0 warnings

Please ignore this build failure. It's just the commit linter using a wrong version of package list to verify scope. This error will get fixed when I clean up the commit history.

@bajtos
Copy link
Member Author

bajtos commented Nov 13, 2018

Liquid Exception: Could not locate the included file
  'readmes/loopback-next/packages/rest-explorer/README.md' in any of
  ["/home/travis/build/strongloop/loopback-next/sandbox/loopback.io/pages/en/lb4"].
  Ensure it exists in one of those directories and, if it is a symlink, does not point 
  outside your site source. in /_layouts/readme.html

The failure of verify-docs build step will be fixed once #2025 is landed and this pull request is rebased on the new master.

Copy link
Contributor

@b-admike b-admike left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! I've left some comments, but LGTM overall.

packages/explorer/test/acceptance/explorer.acceptance.ts Outdated Show resolved Hide resolved
debug('Redirect to swagger-ui was disabled by configuration.');
return;
}

const explorerPaths = ['/swagger-ui', '/explorer'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should explorerPaths take into account custom paths the user configures with our RestExplorer component?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not opposed to that, but I think it's out of scope of this pull request.

packages/rest-explorer/README.md Outdated Show resolved Hide resolved
summary:
A tutorial on using `@loopback/rest-explorer` to add a self-hosted REST API
Explorer
---
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you planning to add docs to this page in this PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This page will automatically include the contents of packages/rest-explorer/README.md, see https://loopback.io/doc/en/lb4/Creating-decorators.html as an example of a page based on README contents.

docs/site/Server.md Outdated Show resolved Hide resolved
… was created

Before this change, the built-in Explorer redirect could be disabled
only via the config object passed to RestServer. Once a RestServer was
instantiated, changes to the configuration were no longer honored.

This commit moves the detection of `apiExplorer.disabled` to the Express
routes handling redirects.
@bajtos bajtos force-pushed the feat/explorer-extension branch from b090e07 to 9ed2384 Compare November 15, 2018 08:20
Add a new package @loopback/rest-explorer that provides a self-hosted
swagger-ui instance and disables the built-in swagger-ui redirect.

Modify example projects to use @loopback/rest-explorer.

Modify the CLI template to include self-hosted REST API Explorer in new
applications.
@bajtos bajtos force-pushed the feat/explorer-extension branch from 9ed2384 to e40b653 Compare November 15, 2018 08:26
@bajtos bajtos merged commit 4c165c7 into master Nov 15, 2018
@bajtos bajtos deleted the feat/explorer-extension branch November 15, 2018 11:57
@cloudwheels
Copy link

@bajtos when will this extension be released? Thanks

@dhmlau dhmlau mentioned this pull request Nov 20, 2018
20 tasks
@bajtos
Copy link
Member Author

bajtos commented Nov 26, 2018

I believe @loopback/[email protected] has been already published on November 17th, see e0fe37d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support local API Explorer
7 participants