mirror of
https://github.com/gristlabs/grist-core.git
synced 2026-03-02 04:09:24 +00:00
REQUEST now supports POST (#588)
* REQUEST now supports POST * Add extra flag for enabling REQUEST, also update README and comments Co-authored-by: John Cant <a.jonncant@gmail.com> Co-authored-by: Alex Hall <alex.mojaki@gmail.com>
This commit is contained in:
@@ -72,6 +72,8 @@ export interface SandboxActionBundle {
|
||||
// Represents a unique call to the Python REQUEST function
|
||||
export interface SandboxRequest {
|
||||
url: string;
|
||||
method: string;
|
||||
body?: string;
|
||||
params: Record<string, string> | null;
|
||||
headers: Record<string, string> | null;
|
||||
deps: unknown; // pass back to the sandbox unchanged in the response
|
||||
|
||||
@@ -66,6 +66,11 @@ export async function main() {
|
||||
process.env.GRIST_EXPERIMENTAL_PLUGINS = "1";
|
||||
}
|
||||
|
||||
// Experimental plugins are enabled by default for devs
|
||||
if (!process.env.GRIST_ENABLE_REQUEST_FUNCTION) {
|
||||
process.env.GRIST_ENABLE_REQUEST_FUNCTION = "1";
|
||||
}
|
||||
|
||||
// For tests, it is useful to start with the database in a known state.
|
||||
// If TEST_CLEAN_DATABASE is set, we reset the database before starting.
|
||||
if (process.env.TEST_CLEAN_DATABASE) {
|
||||
|
||||
@@ -80,16 +80,21 @@ export class DocRequests {
|
||||
|
||||
private async _handleSingleRequestRaw(request: SandboxRequest): Promise<Response> {
|
||||
try {
|
||||
if (process.env.GRIST_EXPERIMENTAL_PLUGINS != '1') {
|
||||
if (process.env.GRIST_ENABLE_REQUEST_FUNCTION != '1') {
|
||||
throw new Error("REQUEST is not enabled");
|
||||
}
|
||||
const {url, params, headers} = request;
|
||||
const {url, method, body, params, headers} = request;
|
||||
const urlObj = new URL(url);
|
||||
log.rawInfo("Handling sandbox request", {host: urlObj.host, docId: this._activeDoc.docName});
|
||||
for (const [param, value] of Object.entries(params || {})) {
|
||||
urlObj.searchParams.append(param, value);
|
||||
}
|
||||
const response = await fetch(urlObj.toString(), {headers: headers || {}, agent: proxyAgent(urlObj)});
|
||||
const response = await fetch(urlObj.toString(), {
|
||||
headers: headers || {},
|
||||
agent: proxyAgent(urlObj),
|
||||
method,
|
||||
body
|
||||
});
|
||||
const content = await response.buffer();
|
||||
const {status, statusText} = response;
|
||||
const encoding = httpEncoding(response.headers.get('content-type'), content);
|
||||
|
||||
Reference in New Issue
Block a user