@prismicio/client
Version:
The official JavaScript + TypeScript client library for Prismic
96 lines (94 loc) • 4.19 kB
JavaScript
import { version } from "./package.js";
import { devMsg } from "./lib/devMsg.js";
//#region src/buildQueryURL.ts
/**
* The query parameter used to indicate if the client is in development mode to
* the API.
*/
const PRISMIC_DEV_PARAM = "x-d";
/** The query parameter used to indicate the version of the client to the API. */
const PRISMIC_CLIENT_VERSION_PARAM = "x-c";
/**
* Parameters in this map have been renamed from the official Prismic REST API
* V2 specification for better developer ergonomics.
*
* These parameters are renamed to their mapped value.
*/
const RENAMED_PARAMS = { accessToken: "access_token" };
/**
* Converts an Ordering to a string that is compatible with Prismic's REST API.
* If the value provided is already a string, no conversion is performed.
*
* @param ordering - Ordering to convert.
*
* @returns String representation of the Ordering.
*/
const castOrderingToString = (ordering) => {
if (typeof ordering === "string") {
if (process.env.NODE_ENV === "development") {
const [field, direction] = ordering.split(" ");
const objectForm = direction === "desc" ? `{ field: "${field}", direction: "desc" }` : `{ field: "${field}" }`;
console.warn(`[@prismicio/client] A string value was provided to the \`orderings\` query parameter. Strings are deprecated. Please convert it to the object form: ${objectForm}. For more details, see ${devMsg("orderings-must-be-an-array-of-objects")}`);
}
return ordering;
}
return ordering.direction === "desc" ? `${ordering.field} desc` : ordering.field;
};
/**
* Builds a Prismic Content API URL to request pages from a repository. The
* paginated response for this URL includes pages matching the parameters.
*
* A ref is required to make a request. Request the `endpoint` URL to retrieve a
* list of available refs.
*
* Type the JSON response with `Query`.
*
* @example
*
* ```ts
* const url = buildQueryURL("https://my-repo.cdn.prismic.io/api/v2", {
* ref: "my-ref",
* filters: [filter.at("document.type", "blog_post")],
* })
* ```
*
* @param endpoint - URL to the repository's Content API.
* @param args - Arguments to filter and scope the query.
*
* @returns URL that can be used to request pages from the repository.
*
* @see Prismic Content API technical reference: {@link https://prismic.io/docs/content-api}
*/
const buildQueryURL = (endpoint, args) => {
const { filters, predicates,...params } = args;
if (!endpoint.endsWith("/")) endpoint += "/";
const url = new URL(`documents/search`, endpoint);
if (filters) {
if (process.env.NODE_ENV === "development" && !Array.isArray(filters)) console.warn(`[@prismicio/client] A non-array value was provided to the \`filters\` query parameter (\`${filters}\`). Non-array values are deprecated. Please convert it to an array. For more details, see ${devMsg("filters-must-be-an-array")}`);
for (const filter of castArray(filters)) url.searchParams.append("q", `[${filter}]`);
}
if (predicates) for (const predicate of castArray(predicates)) url.searchParams.append("q", `[${predicate}]`);
for (const k in params) {
const name = RENAMED_PARAMS[k] || k;
let value = params[k];
if (name === "orderings") {
const scopedValue = params[name];
if (scopedValue != null) {
if (process.env.NODE_ENV === "development" && typeof scopedValue === "string") console.warn(`[@prismicio/client] A string value was provided to the \`orderings\` query parameter. Strings are deprecated. Please convert it to an array of objects. For more details, see ${devMsg("orderings-must-be-an-array-of-objects")}`);
value = `[${castArray(scopedValue).map((ordering) => castOrderingToString(ordering)).join(",")}]`;
}
} else if (name === "routes") {
if (typeof params[name] === "object") value = JSON.stringify(castArray(params[name]));
}
if (value != null) url.searchParams.set(name, castArray(value).join(","));
}
url.searchParams.set(PRISMIC_CLIENT_VERSION_PARAM, `js-${version}`);
if (process.env.NODE_ENV === "development") url.searchParams.set(PRISMIC_DEV_PARAM, "1");
return url.toString();
};
function castArray(a) {
return Array.isArray(a) ? a : [a];
}
//#endregion
export { buildQueryURL };
//# sourceMappingURL=buildQueryURL.js.map