import Since from '@components/shared/Since.astro' import RailroadDiagram from '@components/RailroadDiagram.astro' import Tabs from '@components/Tabs/Tabs.astro' import TabItem from '@components/Tabs/TabItem.astro'
ALTER API statement
The ALTER API statement can be used to modify an existing defined API.
Statement syntax
SurrealQL Syntax
ALTER API [ IF EXISTS ] @endpoint
[ FOR any [ @api_config ] [ THEN @expression | DROP THEN ] ]
[ FOR @http_method, ... [ MIDDLEWARE @function, ... ] [ THEN @expression ] ]
[ FOR @http_method, ... DROP THEN ]
[ COMMENT @string | DROP COMMENT ]
export const alterAst = { type: "Diagram", padding: [10, 20, 10, 20], children: [ { type: "Sequence", children: [ { type: "Terminal", text: "ALTER API" }, { type: "Optional", child: { type: "Sequence", children: [ { type: "Terminal", text: "IF" }, { type: "Terminal", text: "EXISTS" }, ], }, }, { type: "Terminal", text: "@endpoint" }, { type: "ZeroOrMore", child: { type: "Choice", index: 0, children: [ { type: "Sequence", children: [ { type: "Terminal", text: "FOR" }, { type: "Terminal", text: "any" }, { type: "Optional", child: { type: "NonTerminal", text: "@api_config" }, }, { type: "Optional", child: { type: "Choice", index: 0, children: [ { type: "Sequence", children: [ { type: "Terminal", text: "THEN" }, { type: "NonTerminal", text: "@expression" }, ], }, { type: "Sequence", children: [ { type: "Terminal", text: "DROP" }, { type: "Terminal", text: "THEN" }, ], }, ], }, }, ], }, { type: "Sequence", children: [ { type: "Terminal", text: "FOR" }, { type: "NonTerminal", text: "@http_method" }, { type: "ZeroOrMore", child: { type: "Sequence", children: [ { type: "Terminal", text: "," }, { type: "NonTerminal", text: "@http_method" }, ], }, repeat: { type: "Skip" }, }, { type: "Optional", child: { type: "Sequence", children: [ { type: "Terminal", text: "MIDDLEWARE" }, { type: "NonTerminal", text: "@function" }, { type: "ZeroOrMore", child: { type: "Sequence", children: [ { type: "Terminal", text: "," }, { type: "NonTerminal", text: "@function" }, ], }, repeat: { type: "Skip" }, }, ], }, }, { type: "Optional", child: { type: "Sequence", children: [ { type: "Terminal", text: "THEN" }, { type: "NonTerminal", text: "@expression" }, ], }, }, ], }, { type: "Sequence", children: [ { type: "Terminal", text: "FOR" }, { type: "NonTerminal", text: "@http_method" }, { type: "ZeroOrMore", child: { type: "Sequence", children: [ { type: "Terminal", text: "," }, { type: "NonTerminal", text: "@http_method" }, ], }, repeat: { type: "Skip" }, }, { type: "Terminal", text: "DROP" }, { type: "Terminal", text: "THEN" }, ], }, ], }, repeat: { type: "Skip" }, }, { type: "Optional", child: { type: "Choice", index: 0, children: [ { type: "Sequence", children: [ { type: "Terminal", text: "COMMENT" }, { type: "Terminal", text: "@string" }, ], }, { type: "Sequence", children: [ { type: "Terminal", text: "DROP" }, { type: "Terminal", text: "COMMENT" }, ], }, ], }, }, ], }, ], };
Example usage
-- Define a simple API
DEFINE API "/test"
FOR get
THEN {
{
body: {
some: "data"
}
};
};
-- Make a random function
DEFINE FUNCTION fn::feeling_lucky() -> int {
rand::enum(200, 404)
};
-- Set it as the HTTP status
ALTER API "/test" FOR get THEN {
{
status: fn::feeling_lucky(),
body: {
some: "data"
}
}
};
-- status is either 200 or 404
api::invoke("/test");
Possible output
{
body: {
some: 'data'
},
headers: {
"x-surreal-request-id": '95fd0577-5b97-4dc1-b98e-c284f0e36a63'
},
request_id: '95fd0577-5b97-4dc1-b98e-c284f0e36a63',
status: 404
}