Skip to content

Commit

Permalink
pass pathName to path builder
Browse files Browse the repository at this point in the history
  • Loading branch information
glowcloud committed May 8, 2024
1 parent 14e3e6c commit 21520e5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/execute/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ export function buildRequest(options) {
value,
operation,
spec,
pathName,
});
}
});
Expand Down
14 changes: 5 additions & 9 deletions src/execute/oas3/parameter-builders.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
import { resolve as resolvePathTemplate } from 'openapi-path-templating';

import { DEFAULT_BASE_URL } from '../../constants.js';
import stylize, { encodeCharacters } from './style-serializer.js';
import serialize from './content-serializer.js';

export function path({ req, value, parameter }) {
export function path({ req, value, parameter, pathName }) {
const { name, style, explode, content } = parameter;

if (value === undefined) return;

const url = new URL(req.url, DEFAULT_BASE_URL);
const pathname = decodeURIComponent(url.pathname);

if (content) {
const effectiveMediaType = Object.keys(content)[0];

const resolvedPathname = resolvePathTemplate(
pathname,
pathName,
{ [name]: value },
{ encoder: (val) => encodeCharacters(serialize(val, effectiveMediaType)) }
);

req.url = req.url.replace(pathname, resolvedPathname);
req.url = req.url.replace(pathName, resolvedPathname);
} else {
const resolvedPathname = resolvePathTemplate(
pathname,
pathName,
{ [name]: value },
{
encoder: (val) =>
Expand All @@ -38,7 +34,7 @@ export function path({ req, value, parameter }) {
}
);

req.url = req.url.replace(pathname, resolvedPathname);
req.url = req.url.replace(pathName, resolvedPathname);
}
}

Expand Down
11 changes: 3 additions & 8 deletions src/execute/swagger2/parameter-builders.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { resolve as resolvePathTemplate } from 'openapi-path-templating';

import { DEFAULT_BASE_URL } from '../../constants.js';

// These functions will update the request.
// They'll be given {req, value, paramter, spec, operation}.

Expand Down Expand Up @@ -53,14 +51,11 @@ function headerBuilder({ req, parameter, value }) {
}

// Replace path paramters, with values ( ie: the URL )
function pathBuilder({ req, value, parameter }) {
function pathBuilder({ req, value, parameter, pathName }) {
if (value !== undefined) {
const url = new URL(req.url, DEFAULT_BASE_URL);
const pathname = decodeURIComponent(url.pathname);

const resolvedPathname = resolvePathTemplate(pathname, { [parameter.name]: value });
const resolvedPathname = resolvePathTemplate(pathName, { [parameter.name]: value });

req.url = req.url.replace(pathname, resolvedPathname);
req.url = req.url.replace(pathName, resolvedPathname);
}
}

Expand Down

0 comments on commit 21520e5

Please sign in to comment.