From f7186ec0909f820945f1cfb77bf83a12935f62d8 Mon Sep 17 00:00:00 2001 From: Marco Kellershoff <1384938+gorillamoe@users.noreply.github.com> Date: Tue, 8 Oct 2024 18:05:19 +0200 Subject: [PATCH] feat(scripts): add request to post_request script (#273) --- lua/kulala/globals/init.lua | 2 +- .../engines/javascript/lib/rollup.config.ts | 1 + .../javascript/lib/src/lib/PostRequest.ts | 125 ++++++++++++++++++ .../lib/src/lib/PreRequestRequest.ts | 6 + .../javascript/lib/src/post_request.ts | 3 +- 5 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 lua/kulala/parser/scripts/engines/javascript/lib/src/lib/PostRequest.ts diff --git a/lua/kulala/globals/init.lua b/lua/kulala/globals/init.lua index ffd6472..58e1822 100644 --- a/lua/kulala/globals/init.lua +++ b/lua/kulala/globals/init.lua @@ -4,7 +4,7 @@ local M = {} local plugin_tmp_dir = FS.get_plugin_tmp_dir() -M.VERSION = "4.1.1" +M.VERSION = "4.2.0" M.UI_ID = "kulala://ui" M.SCRATCHPAD_ID = "kulala://scratchpad" M.HEADERS_FILE = plugin_tmp_dir .. "/headers.txt" diff --git a/lua/kulala/parser/scripts/engines/javascript/lib/rollup.config.ts b/lua/kulala/parser/scripts/engines/javascript/lib/rollup.config.ts index 5b07caa..a08f28b 100644 --- a/lua/kulala/parser/scripts/engines/javascript/lib/rollup.config.ts +++ b/lua/kulala/parser/scripts/engines/javascript/lib/rollup.config.ts @@ -36,6 +36,7 @@ export default [ reserved: [ 'client', 'response', + 'request', ], }, }), diff --git a/lua/kulala/parser/scripts/engines/javascript/lib/src/lib/PostRequest.ts b/lua/kulala/parser/scripts/engines/javascript/lib/src/lib/PostRequest.ts new file mode 100644 index 0000000..3201e2b --- /dev/null +++ b/lua/kulala/parser/scripts/engines/javascript/lib/src/lib/PostRequest.ts @@ -0,0 +1,125 @@ +import * as fs from 'fs'; +import * as path from 'path'; +const _REQUEST_FILEPATH = path.join(__dirname, '..', '..', 'request.json'); +const _REQUEST_VARIABLES_FILEPATH = path.join(__dirname, 'request_variables.json'); + +type RequestVariables = Record; + +interface RequestJson { + headers: Record, + headers_raw: Record, + body_raw: string, + body_computed: string | undefined, + body: string | object, + method: string, + url_raw: string, + url: string, + environment: Record, +}; + +const getRequestVariables = (): RequestVariables => { + let reqVariables: RequestVariables = {}; + try { + reqVariables = JSON.parse(fs.readFileSync(_REQUEST_VARIABLES_FILEPATH, { encoding: 'utf8' })) as RequestVariables; + } catch (e) { + // do nothing + } + return reqVariables; +}; + +interface HeaderObject { + name: () => string, + getRawValue: () => string, + tryGetSubstituted: () => string, +}; + +const getHeaderObject = (headerName: string, headerRawValue: string, headerValue: string | undefined): HeaderObject | null => { + if (headerValue === undefined) { + return null; + } + return { + name: () => { + return headerName + }, + getRawValue: () => { + return headerRawValue; + }, + tryGetSubstituted: () => { + return headerValue; + }, + }; +}; + +const req = JSON.parse(fs.readFileSync(_REQUEST_FILEPATH, { encoding: 'utf8' })) as RequestJson; + +export const Request = { + body: { + getRaw: () => { + return req.body_raw; + }, + tryGetSubstituted: () => { + return req.body; + }, + getComputed: () => { + return req.body_computed; + }, + }, + headers: { + findByName: (headerName: string) => { + return getHeaderObject(headerName, req.headers_raw[headerName], req.headers[headerName]); + }, + all: function (): HeaderObject[] { + const h = []; + for (const [key, value] of Object.entries(req.headers)) { + const item = getHeaderObject(key, req.headers_raw[key], value); + if (item !== null) { + h.push(item); + } + } + return h; + }, + }, + environment: { + getName: (name: string): string | null => { + if (name in req.environment) { + return req.environment[name]; + } + return null; + }, + get: (name: string): string | null => { + if (name in req.environment) { + return req.environment[name]; + } + return null; + } + }, + method: req.method, + url: { + getRaw: () => { + return req.url_raw; + }, + tryGetSubstituted: () => { + return req.url + }, + }, + status: null, + contentType: { + mimeType: null, + charset: null, + }, + variables: { + set: function (key: string, value: string) { + const reqVariables = getRequestVariables(); + reqVariables[key] = value; + fs.writeFileSync(_REQUEST_VARIABLES_FILEPATH, JSON.stringify(reqVariables)); + }, + get: function (key: string) { + const reqVariables = getRequestVariables(); + if (key in reqVariables) { + return reqVariables[key]; + } + return null; + } + }, +}; + diff --git a/lua/kulala/parser/scripts/engines/javascript/lib/src/lib/PreRequestRequest.ts b/lua/kulala/parser/scripts/engines/javascript/lib/src/lib/PreRequestRequest.ts index d8039db..3201e2b 100644 --- a/lua/kulala/parser/scripts/engines/javascript/lib/src/lib/PreRequestRequest.ts +++ b/lua/kulala/parser/scripts/engines/javascript/lib/src/lib/PreRequestRequest.ts @@ -85,6 +85,12 @@ export const Request = { return req.environment[name]; } return null; + }, + get: (name: string): string | null => { + if (name in req.environment) { + return req.environment[name]; + } + return null; } }, method: req.method, diff --git a/lua/kulala/parser/scripts/engines/javascript/lib/src/post_request.ts b/lua/kulala/parser/scripts/engines/javascript/lib/src/post_request.ts index 208add9..1a32cae 100644 --- a/lua/kulala/parser/scripts/engines/javascript/lib/src/post_request.ts +++ b/lua/kulala/parser/scripts/engines/javascript/lib/src/post_request.ts @@ -1,6 +1,7 @@ import { Client } from './lib/Client'; import { Response } from './lib/PostRequestResponse'; +import { Request } from './lib/PostRequest'; export const client = Client; export const response = Response; - +export const request = Request;