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

Refactor: Move api endpoints to app router #93

Merged
merged 1 commit into from
Jun 12, 2023

Conversation

marthendalnunes
Copy link
Member

This PR transitions the API endpoints from the pages router to the new app router, marking TurboETH's complete migration to the app router and eliminating the pages folder. This move also standardizes the implementation of API endpoints for integrations.

Now, every API endpoint associated with an integration should be located inside the integrations folder. For instance, if the openai integration requires an ask endpoint, the corresponding code should be housed in integrations/openai/api/ask.ts. The endpoint functions must maintain the Next convention for route handlers, employing named exports based on the HTTP method (GET, POST, PATCH, etc.):

import { OpenAIStream } from '@/integrations/openai/openai-stream'
import { ModelConfig } from '@/integrations/openai/types'

export async function POST(req: Request) {
  const { prompt, apiKey } = (await req.json()) as {
    prompt?: string
    apiKey?: string
  }
  if (!prompt) {
    return new Response('No prompt in the request', { status: 400 })
  }

  const payload: ModelConfig = {
    model: 'gpt-3.5-turbo',
    messages: [{ role: 'user', content: prompt }],
    temperature: 0.7,
    top_p: 1,
    frequency_penalty: 0,
    presence_penalty: 0,
    max_tokens: 600,
    stream: true,
    n: 1,
  }

  const stream = await OpenAIStream(payload, apiKey)
  return new Response(stream)
}

To expose the endpoint, the route handler file must reside at app/api/(integration-name)/(endpoint-name)/route/ts. For instance, the openai endpoint should be at app/api/openai/ask/route.ts:

export { POST } from '@/integrations/openai/api/ask'

export const runtime = 'edge'

This pattern applies to all migrated API endpoints and should be adhered to for any new additions.

The major obstacle in this migration was the lack of support for app router API endpoints from iron-session. The workaround employed the getIronSession method with an empty Response object to retrieve the session object, instead of using the unsupported withIronSessionApiRoute wrapper. Upon the launch of iron-session V8, which promises first-class support for the app router, the endpoints utilizing it will be updated accordingly.

@vercel
Copy link

vercel bot commented Jun 12, 2023

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

Name Status Preview Updated (UTC)
light-turbo-eth ✅ Ready (Inspect) Visit Preview Jun 12, 2023 6:27pm
turbo-eth ✅ Ready (Inspect) Visit Preview Jun 12, 2023 6:27pm

Copy link
Member

@kamescg kamescg left a comment

Choose a reason for hiding this comment

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

LGTM

@marthendalnunes marthendalnunes merged commit 76afd29 into integrations Jun 12, 2023
@marthendalnunes marthendalnunes deleted the refactor/remove-pages-dir branch June 12, 2023 23:24
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.

2 participants