-
Notifications
You must be signed in to change notification settings - Fork 9
feat(api): add GET /api/subscriptions/status #505
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
Open
ahmednahima0-beep
wants to merge
5
commits into
test
Choose a base branch
from
feature/api-get-subscription-status
base: test
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a617b68
feat(api): add GET /api/subscriptions/status
ahmednahima0-beep eeeb22a
refactor(stripe): update subscription handling and validation
ahmednahima0-beep 55c73ea
refactor(sandbox): update sandbox property references to use sandboxId
ahmednahima0-beep 0ec5ff9
refactor(sandbox): update sandbox property references to use name
ahmednahima0-beep 24a5b5a
refactor(sandbox): standardize sandbox property naming to sandboxId
ahmednahima0-beep File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import { getCorsHeaders } from "@/lib/networking/getCorsHeaders"; | ||
| import { getSubscriptionStatusHandler } from "@/lib/stripe/getSubscriptionStatusHandler"; | ||
|
|
||
| /** | ||
| * OPTIONS handler for CORS preflight requests. | ||
| * | ||
| * @returns A 200 NextResponse carrying the CORS headers. | ||
| */ | ||
| export async function OPTIONS() { | ||
| return new NextResponse(null, { | ||
| status: 200, | ||
| headers: getCorsHeaders(), | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * GET /api/subscriptions/status?accountId= | ||
| * | ||
| * Returns whether the account has an active paid subscription (direct or via organization). | ||
| * Requires `x-api-key` or `Authorization: Bearer`; the caller must be allowed to access | ||
| * the requested account (same rules as account_id override on other routes). | ||
| * | ||
| * @param request - The incoming HTTP request (query `accountId`, auth headers). | ||
| * @returns JSON `{ isPro }`, or 400/401/403 with `{ error }` per API docs. | ||
| */ | ||
| export async function GET(request: NextRequest) { | ||
| return getSubscriptionStatusHandler(request); | ||
| } | ||
|
|
||
| export const dynamic = "force-dynamic"; | ||
| export const fetchCache = "force-no-store"; | ||
| export const revalidate = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -9,10 +9,10 @@ import type { SandboxCreatedResponse } from "./createSandbox"; | |||||
| */ | ||||||
| export async function getSandboxStatus(sandboxId: string): Promise<SandboxCreatedResponse | null> { | ||||||
| try { | ||||||
| const sandbox = await Sandbox.get({ name: sandboxId }); | ||||||
| const sandbox = await Sandbox.get({ sandboxId }); | ||||||
|
|
||||||
| return { | ||||||
| sandboxId: sandbox.name, | ||||||
| sandboxId: sandbox.sandboxId, | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Read Prompt for AI agents
Suggested change
|
||||||
| sandboxStatus: sandbox.status, | ||||||
| timeout: sandbox.timeout, | ||||||
| createdAt: sandbox.createdAt.toISOString(), | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import { getSubscriptionStatusHandler } from "@/lib/stripe/getSubscriptionStatusHandler"; | ||
| import { validateGetSubscriptionStatusRequest } from "@/lib/stripe/validateGetSubscriptionStatusRequest"; | ||
| import { getSubscriptionIsPro } from "@/lib/stripe/getSubscriptionIsPro"; | ||
|
|
||
| vi.mock("@/lib/networking/getCorsHeaders", () => ({ | ||
| getCorsHeaders: vi.fn(() => ({ "Access-Control-Allow-Origin": "*" })), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/stripe/validateGetSubscriptionStatusRequest", () => ({ | ||
| validateGetSubscriptionStatusRequest: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/stripe/getSubscriptionIsPro", () => ({ | ||
| getSubscriptionIsPro: vi.fn(), | ||
| })); | ||
|
|
||
| const ACCOUNT = "123e4567-e89b-12d3-a456-426614174000"; | ||
|
|
||
| describe("getSubscriptionStatusHandler", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| vi.spyOn(console, "error").mockImplementation(() => undefined); | ||
| }); | ||
| afterEach(() => vi.mocked(console.error).mockRestore()); | ||
|
|
||
| it("returns validation response unchanged", async () => { | ||
| const err = NextResponse.json({ error: "accountId is required" }, { status: 400 }); | ||
| vi.mocked(validateGetSubscriptionStatusRequest).mockResolvedValue(err); | ||
| const req = new NextRequest(`http://localhost/api/subscriptions/status`); | ||
| expect(await getSubscriptionStatusHandler(req)).toBe(err); | ||
| expect(getSubscriptionIsPro).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("returns 200 { isPro: true }", async () => { | ||
| vi.mocked(validateGetSubscriptionStatusRequest).mockResolvedValue({ accountId: ACCOUNT }); | ||
| vi.mocked(getSubscriptionIsPro).mockResolvedValue(true); | ||
|
|
||
| const res = await getSubscriptionStatusHandler( | ||
| new NextRequest(`http://localhost/api/subscriptions/status?accountId=${ACCOUNT}`), | ||
| ); | ||
| expect(res.status).toBe(200); | ||
| await expect(res.json()).resolves.toEqual({ isPro: true }); | ||
| expect(getSubscriptionIsPro).toHaveBeenCalledWith(ACCOUNT); | ||
| }); | ||
|
|
||
| it("returns 200 { isPro: false }", async () => { | ||
| vi.mocked(validateGetSubscriptionStatusRequest).mockResolvedValue({ accountId: ACCOUNT }); | ||
| vi.mocked(getSubscriptionIsPro).mockResolvedValue(false); | ||
|
|
||
| const res = await getSubscriptionStatusHandler( | ||
| new NextRequest(`http://localhost/api/subscriptions/status?accountId=${ACCOUNT}`), | ||
| ); | ||
| expect(res.status).toBe(200); | ||
| await expect(res.json()).resolves.toEqual({ isPro: false }); | ||
| }); | ||
|
|
||
| it("returns 500 when getSubscriptionIsPro throws", async () => { | ||
| vi.mocked(validateGetSubscriptionStatusRequest).mockResolvedValue({ accountId: ACCOUNT }); | ||
| vi.mocked(getSubscriptionIsPro).mockRejectedValue(new Error("stripe down")); | ||
|
|
||
| const res = await getSubscriptionStatusHandler( | ||
| new NextRequest(`http://localhost/api/subscriptions/status?accountId=${ACCOUNT}`), | ||
| ); | ||
| expect(res.status).toBe(500); | ||
| await expect(res.json()).resolves.toEqual({ error: "Internal server error" }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1: Use the beta SDK's
namefield here; this pinned version expectsSandbox.get({ name }).Prompt for AI agents