Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions schemas/zzapi-bundle.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,16 @@
}
}
},
"pathParamObject": {
"type": "object",
"description": "A set of path parameters described as key/value pairs",
"properties": {
"": { "$ref": "#/$defs/scalar" }
}
},
"headerObject": {
"type": "object",
"description": "A set ofheaders described as key/value pairs",
"description": "A set of headers described as key/value pairs",
"properties": {
"": { "$ref": "#/$defs/scalar" }
}
Expand Down Expand Up @@ -243,11 +250,17 @@
{ "$ref": "#/$defs/headerObject" }
]
},
"pathParams": {
"description": "path parameters",
"anyOf": [
{ "$ref": "#/$defs/pathParamObject" }
]
},
"params": {
"description": "Query string parameters",
"anyOf": [
{
"type": "array",
"type": "array",
"items": { "$ref": "#/$defs/paramArray" }
},
{ "$ref": "#/$defs/paramObject" }
Expand Down
51 changes: 46 additions & 5 deletions src/executeRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { getStringValueIfDefined } from "./utils/typeUtils";
import { GotRequest, Param, RequestSpec } from "./models";

export function constructGotRequest(allData: RequestSpec): GotRequest {
const completeUrl: string = getURL(
var completeUrl: string = getURL(
allData.httpRequest.baseUrl,
allData.httpRequest.url,
getParamsForUrl(allData.httpRequest.params, allData.options.rawParams),
allData.httpRequest.pathParams
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather pass the pathParams here and get the getURL() function to incorporate it.



const options: OptionsOfTextResponseBody = {
method: allData.httpRequest.method.toLowerCase() as Method,
Expand Down Expand Up @@ -77,9 +79,48 @@ export function getParamsForUrl(params: Param[] | undefined, rawParams: boolean)
return `?${paramString}`;
}

export function getURL(baseUrl: string | undefined, url: string, paramsForUrl: string): string {
export function getURL(baseUrl: string | undefined, url: string, paramsForUrl: string, pathParams?: Param[]): string {
// base url not defined, or url does not start with /, then ignore base url
if (!baseUrl || !url.startsWith("/")) return url + paramsForUrl;
// otherwise, incorporate base url
return baseUrl + url + paramsForUrl;
var completeUrl = ""
if (!baseUrl || !url.startsWith("/")){
completeUrl = url + paramsForUrl;
}else{
// otherwise, incorporate base url
completeUrl = baseUrl + url + paramsForUrl;
}

if(/\/:\w+/.test(completeUrl) && pathParams){
completeUrl = substitutePathParams(completeUrl,pathParams);
}
return completeUrl;
}

function substitutePathParams(url: string,params: Param[]): string {

const {baseUrl,path,query} = splitURL(url)
return baseUrl + path.replace(/:(\w+)/g, (_, key) => {
const param = params.find(p => p.name === key);
if (param) {
return param.value;
}
throw new Error(`Missing value for parameter: ${key}`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if a throw translate to a user-friendly readable message in the output window. Have you tested this path?

Copy link
Contributor Author

@koul1sh koul1sh Mar 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it shows an error message about the missing parameter.
Screenshot from 2025-03-23 01-53-53

}) + query;
}

function splitURL(url: string): { baseUrl: string; path: string; query: string } {
try {
// defaults to http if protocol not given
if (!/^\w+:\/\//.test(url)) {
url = "http://" + url;
}
const parsedUrl = new URL(url);

return {
baseUrl: `${parsedUrl.protocol}//${parsedUrl.host}`,
path: parsedUrl.pathname,
query: parsedUrl.search
};
} catch (error) {
throw new Error("Invalid URL");
}
}
2 changes: 2 additions & 0 deletions src/mergeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ export function getMergedData(commonData: Common, requestData: RawRequest): Requ

const method = requestData.method;
const params = getMergedParams(commonData.params, requestData.params);
const pathParams = getMergedParams(commonData.pathParams,requestData.pathParams)
const headers = getMergedHeaders(commonData.headers, requestData.headers);
const body = requestData.body;
const options = getMergedOptions(commonData.options, requestData.options);
Expand All @@ -226,6 +227,7 @@ export function getMergedData(commonData: Common, requestData: RawRequest): Requ
url: requestData.url,
method,
params,
pathParams,
headers,
body,
},
Expand Down
3 changes: 3 additions & 0 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export interface Common {
baseUrl?: string;
headers: RawHeaders;
params: RawParams;
pathParams: RawParams;
options?: RawOptions;
tests?: RawTests;
}
Expand All @@ -84,6 +85,7 @@ export interface RawRequest {
method: Method;
headers: RawHeaders;
params: RawParams;
pathParams : RawParams;
body?: string;
options?: RawOptions;
tests?: RawTests;
Expand All @@ -98,6 +100,7 @@ export interface RequestSpec {
baseUrl?: string;
url: string;
method: Method;
pathParams : Param[];
params: Param[];
headers: { [key: string]: string };
body?: any;
Expand Down