Skip to content
Merged
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
40 changes: 34 additions & 6 deletions packages/optimizely-cms-sdk/src/graph/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,51 @@
* This is used internally in the SDK
*/

/** Returns two versions of the same path. One with trailing slash and one without it */
function normalizePath(path: string) {
if (path.endsWith('/')) {
return {
pathWithTrailingSlash: path,
pathWithoutTrailingSlash: path.slice(0, -1),
};
} else {
return {
pathWithTrailingSlash: path + '/',
pathWithoutTrailingSlash: path,
};
}
}

/**
* Creates a {@linkcode ContentInput} object that filters results by a specific URL path.
*
* @param path - The URL path to filter by.
* @returns A `GraphQueryArguments` object with a `where` clause that matches the given path.
*/
export function pathFilter(path: string, host?: string): ContentInput {
const { pathWithTrailingSlash, pathWithoutTrailingSlash } =
normalizePath(path);

return {
where: {
_metadata: {
url: {
default: {
eq: path,
_or: [
{
_metadata: {
url: {
default: { eq: pathWithTrailingSlash },
base: host ? { eq: host } : undefined,
},
},
base: host ? { eq: host } : undefined,
},
},
{
_metadata: {
url: {
default: { eq: pathWithoutTrailingSlash },
base: host ? { eq: host } : undefined,
},
},
},
],
},
};
}
Expand Down