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: api route #25

Open
hulxv opened this issue Sep 22, 2024 · 0 comments
Open

feat: api route #25

hulxv opened this issue Sep 22, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@hulxv
Copy link
Collaborator

hulxv commented Sep 22, 2024

Description

Introduce API route support in MetaSSR to allow developers to define backend API endpoints directly within the framework. This feature would enable seamless integration of API logic alongside server-side rendering, allowing for dynamic content fetching, user authentication, or data manipulation without the need for external APIs.

Client-Side Example

// ./src/api/user.js

export async function GET(req) {
    const { id } = req.params;
    const user = await database.getUserById(id);

    

    if (user) {
        res.status(200).json(user);
        return {
            status: 200,
            body: Json.stringify(user)
        }
    } else {
        return {
            status: 404,
            body: JSON.stringify({
                message: "User is not found."
            })
        }
    }
}

export async function POST(req) {
    const newUser = await database.createUser(req.body);
    return {
        status: 201,
        body: Json.stringify(newUser)
    }
   
}

Polyglot Programming Support

With the integration of Metacall, API routes can be written in different programming languages, making it easy for developers to use their language of choice to define routes and business logic. This provides flexibility for teams with varying language expertise.

Example in Python

# ./src/api/user.py

async def GET(req):
    user_id = req.params['id']
    user = await database.get_user_by_id(user_id)

    if user:
        return {
            "status": 200,
            "body": json.dumps(user)
        }
    else:
        return {
            "status": 404,
            "body": json.dumps({
                "message": "User not found."
            })
        }

async def POST(req):
    new_user = await database.create_user(req.body)
    return {
        "status": 201,
        "body": json.dumps(new_user)
    }

Benefits

  • Streamlines the process of building backend APIs for server-side rendered applications by co-locating API logic within the same framework.
  • Reduces the need for external services, enabling tighter integration between SSR components and backend API logic.
  • Polyglot support allows backend APIs to be written in different languages, giving developers the flexibility to use the best tools for their particular use case.
@hulxv hulxv added the enhancement New feature or request label Sep 22, 2024
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

No branches or pull requests

1 participant