-
Notifications
You must be signed in to change notification settings - Fork 25
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
Adds simple POST endpoint at /runSquiggle, and adds open-api.json schema #2523
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d46637e
Adds simple experimental POST endpoint
OAGr 380b83b
Don't show all of lambdas in API response
OAGr 967568a
Quick sampleSet data
OAGr 1c86c39
Hide calculator fns from API
OAGr 310dbbb
Merge branch 'main' into run-squiggle-post-endpoint
OAGr 3efbd79
Merge branch 'serialization-test' into run-squiggle-post-endpoint
OAGr 61ff33f
Simplifying code, adding schema
OAGr 300f6a3
Minor fixes
OAGr 0dd04ee
Removing select tests for now
OAGr aefb449
Catch all errors in runSquiggle, give 500 if server error
OAGr 5c1f624
Fixed SqValue
OAGr 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
There are no files selected for viewing
This file contains 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,53 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
import { runSquiggle } from "@/graphql/queries/runSquiggle"; | ||
|
||
export async function POST(req: NextRequest) { | ||
// Assuming 'code' is sent in the request body and is a string | ||
try { | ||
const body = await req.json(); | ||
if (body.code) { | ||
let response = await runSquiggle(body.code); | ||
if (response.isOk) { | ||
return new NextResponse( | ||
JSON.stringify({ | ||
result: response.resultJSON, | ||
bindings: response.bindingsJSON, | ||
}), | ||
{ | ||
status: 200, | ||
statusText: "OK", | ||
headers: { "Content-Type": "application/json" }, | ||
} | ||
); | ||
} else { | ||
return new NextResponse( | ||
JSON.stringify({ error: response.errorString }), | ||
{ | ||
status: 400, | ||
statusText: "ERROR", | ||
headers: { "Content-Type": "application/json" }, | ||
} | ||
); | ||
} | ||
} else { | ||
return new NextResponse(JSON.stringify({ error: "No code provided" }), { | ||
status: 400, | ||
statusText: "ERROR", | ||
headers: { "Content-Type": "application/json" }, | ||
}); | ||
} | ||
} catch (error) { | ||
// Catch any errors, including JSON parsing errors | ||
console.error("Error in POST request:", error); | ||
return new NextResponse( | ||
//We could give more information here, but we don't want to leak any information | ||
JSON.stringify({ error: "An internal error occurred" }), | ||
{ | ||
status: 500, | ||
statusText: "Internal Server Error", | ||
headers: { "Content-Type": "application/json" }, | ||
} | ||
); | ||
} | ||
} |
This file contains 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 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,91 @@ | ||
{ | ||
"openapi": "3.0.0", | ||
"info": { | ||
"title": "Squiggle Code Processing API", | ||
"version": "0.0.1" | ||
}, | ||
"servers": [ | ||
{ | ||
"url": "https://squigglehub.org/api" | ||
} | ||
], | ||
"paths": { | ||
"/runSquiggle": { | ||
"post": { | ||
"summary": "Run the given Squiggle code string.", | ||
"operationId": "runSquiggle", | ||
"requestBody": { | ||
"required": true, | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"type": "object", | ||
"properties": { | ||
"code": { | ||
"type": "string", | ||
"description": "The code to be processed." | ||
} | ||
}, | ||
"required": ["code"] | ||
} | ||
} | ||
} | ||
}, | ||
"responses": { | ||
"200": { | ||
"description": "Code processed successfully.", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"type": "object", | ||
"properties": { | ||
"result": { | ||
"type": "string", | ||
"description": "The result of the code processing." | ||
}, | ||
"bindings": { | ||
"type": "string", | ||
"description": "Additional bindings from the code processing." | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"400": { | ||
"description": "Error in processing or no code provided.", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"type": "object", | ||
"properties": { | ||
"error": { | ||
"type": "string", | ||
"description": "Error message explaining the failure." | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"500": { | ||
"description": "Internal Server Error.", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"type": "object", | ||
"properties": { | ||
"error": { | ||
"type": "string", | ||
"description": "A generic error message indicating a server problem." | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains 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
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.
I could see making this configurable later