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: Add plugins apis support #2664

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

ArthurTriis1
Copy link
Contributor

@ArthurTriis1 ArthurTriis1 commented Feb 3, 2025

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:

  • Adding new integrations with backend services within Faststore
  • Defining API routes that can access HTTP-only hooks in the Faststore domain
  • Enabling plugins to expose custom business logic via API endpoints

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:

// plugin.config.js
module.exports = {
  name: "plugin-name",
  pages: {
    ...
  },
  apis: {
    "new-route": {
      path: "/new-route",
    },
  },
};

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:

import { NextApiRequest, NextApiResponse } from "next/types";

export default function handle(req: NextApiRequest, res: NextApiResponse) {
  return res.json({ ok: "from plugin" });
}

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

  • PR title and commit messages follow the Conventional Commits specification
    • Available prefixes: feat, fix, chore, docs, style, refactor, ci and test

PR Description

  • Added a label according to the PR goal - breaking change, bug, contributing, performance, documentation..

Dependencies

  • Committed the yarn.lock file when there were changes to the packages

Documentation

  • PR description
  • For documentation changes, ping @Mariana-Caetano to review and update (Or submit a doc request)

@ArthurTriis1 ArthurTriis1 requested a review from a team as a code owner February 3, 2025 17:16
@ArthurTriis1 ArthurTriis1 requested review from eduardoformiga and lucasfp13 and removed request for a team February 3, 2025 17:16
Copy link

vercel bot commented Feb 3, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Updated (UTC)
faststore-site ⬜️ Ignored (Inspect) Visit Preview Feb 3, 2025 5:59pm

Copy link

codesandbox-ci bot commented Feb 3, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@ArthurTriis1 ArthurTriis1 marked this pull request as ready for review February 4, 2025 21:03
@ArthurTriis1 ArthurTriis1 self-assigned this Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant