feat: Add plugins apis support #2664
Open
+69
−9
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What's the purpose of this pull request?
This PR adds support for creating new APIs in Faststore from plugins.
During the development of the Buyer Portal plugin, we identified the need to define API routes within the plugin structure. Since plugins already support creating new pages in Faststore, we extended this capability to allow the creation of API routes as well.
Some use cases for creating new API routes include:
Note
This pull request does not aim to add the API Override functionality to plugins. Developing this feature would require addressing specific challenges, such as orchestrating GraphQL requests to avoid conflicts while ensuring API stability.
This PR solely allows new REST routes to be added to a store without interfering with or integrating into Faststore's core API functionalities.
How it works?
To add new APIs to a plugin, simply update the plugin.config.js file by adding the apis option with a list of the APIs that should be created, like this:
Next, create a folder named "apis" inside the src directory of your plugin project. Then, add your API files using the same names as the keys defined in plugin.config.js. For example, if you specified "new-route", your file structure should look like this:
plugin-project/
│── src/
│ ├── apis/
│ │ ├── new-route.ts
This ensures that the API routes are correctly registered and accessible within your plugin.
The structure of an API file follows a format similar to Next.js 13 and can leverage Next.js types. For example:
Note
Based on this thread in the Faststore channel, it was discussed that plugin routes should have a prefix for identification. As a result, all routes created in plugins will automatically have the /plugin prefix, making the final structure:
/api/plugin/{route-name}
.How to test it?
To test this functionality locally, you can either link the CLI package to a store or install the preview version. Follow the instructions to create an API and then access the route to verify it.
Starters Deploy Preview
A preview link was create based on b2bfaststoredev store, and the "new-route" api can be accessed in "api/plugin/new-route"
References
Checklist
You may erase this after checking them all 😉
PR Title and Commit Messages
feat
,fix
,chore
,docs
,style
,refactor
,ci
andtest
PR Description
breaking change
,bug
,contributing
,performance
,documentation
..Dependencies
yarn.lock
file when there were changes to the packagesDocumentation
@Mariana-Caetano
to review and update (Or submit a doc request)