import { withLeadingSlash, joinURL, withoutTrailingSlash } from 'ufo'; import { hash } from 'ohash'; import { u as useRuntimeConfig } from '../server.mjs'; import { w as withContentBase, a as addPrerenderPath, s as shouldUseClientDB } from './utils-6d756e03.mjs'; import { u as useContentPreview } from './preview-38d6e135.mjs'; const get = (obj, path) => path.split(".").reduce((acc, part) => acc && acc[part], obj); const _pick = (obj, condition) => Object.keys(obj).filter(condition).reduce((newObj, key) => Object.assign(newObj, { [key]: obj[key] }), {}); const apply = (fn) => (data) => Array.isArray(data) ? data.map((item) => fn(item)) : fn(data); const detectProperties = (keys) => { const prefixes = []; const properties = []; for (const key of keys) { if (["$", "_"].includes(key)) { prefixes.push(key); } else { properties.push(key); } } return { prefixes, properties }; }; const withoutKeys = (keys = []) => (obj) => { if (keys.length === 0 || !obj) { return obj; } const { prefixes, properties } = detectProperties(keys); return _pick(obj, (key) => !properties.includes(key) && !prefixes.includes(key[0])); }; const withKeys = (keys = []) => (obj) => { if (keys.length === 0 || !obj) { return obj; } const { prefixes, properties } = detectProperties(keys); return _pick(obj, (key) => properties.includes(key) || prefixes.includes(key[0])); }; const sortList = (data, params) => { const comperable = new Intl.Collator(params.$locale, { numeric: params.$numeric, caseFirst: params.$caseFirst, sensitivity: params.$sensitivity }); const keys = Object.keys(params).filter((key) => !key.startsWith("$")); for (const key of keys) { data = data.sort((a, b) => { const values = [get(a, key), get(b, key)].map((value) => { if (value === null) { return void 0; } if (value instanceof Date) { return value.toISOString(); } return value; }); if (params[key] === -1) { values.reverse(); } return comperable.compare(values[0], values[1]); }); } return data; }; const assertArray = (value, message = "Expected an array") => { if (!Array.isArray(value)) { throw new TypeError(message); } }; const ensureArray = (value) => Array.isArray(value) ? value : value ? [value] : []; const arrayParams = ["sort", "where", "only", "without"]; const createQuery = (fetcher, intitialParams) => { const queryParams = { ...intitialParams }; for (const key of arrayParams) { if (queryParams[key]) { queryParams[key] = ensureArray(queryParams[key]); } } const $set = (key, fn = (v) => v) => { return (...values) => { queryParams[key] = fn(...values); return query; }; }; const query = { params: () => ({ ...queryParams, ...queryParams.where ? { where: [...ensureArray(queryParams.where)] } : {}, ...queryParams.sort ? { sort: [...ensureArray(queryParams.sort)] } : {} }), only: $set("only", ensureArray), without: $set("without", ensureArray), where: $set("where", (q) => [...ensureArray(queryParams.where), ...ensureArray(q)]), sort: $set("sort", (sort) => [...ensureArray(queryParams.sort), ...ensureArray(sort)]), limit: $set("limit", (v) => parseInt(String(v), 10)), skip: $set("skip", (v) => parseInt(String(v), 10)), // find find: () => fetcher(query), findOne: () => { queryParams.first = true; return fetcher(query); }, findSurround: (surroundQuery, options) => { queryParams.surround = { query: surroundQuery, ...options }; return fetcher(query); }, // locale locale: (_locale) => query.where({ _locale }) }; return query; }; function jsonStringify(value) { return JSON.stringify(value, regExpReplacer); } function regExpReplacer(_key, value) { if (value instanceof RegExp) { return `--REGEX ${value.toString()}`; } return value; } const encodeQueryParams = (params) => { let encoded = jsonStringify(params); encoded = typeof Buffer !== "undefined" ? Buffer.from(encoded).toString("base64") : btoa(encoded); encoded = encoded.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, ""); const chunks = encoded.match(/.{1,100}/g) || []; return chunks.join("/"); }; const createQueryFetch = () => async (query) => { const { content } = useRuntimeConfig().public; const params = query.params(); const apiPath = content.experimental.stripQueryParameters ? withContentBase(`/query/${`${hash(params)}.${content.integrity}`}/${encodeQueryParams(params)}.json`) : withContentBase(`/query/${hash(params)}.${content.integrity}.json`); { addPrerenderPath(apiPath); } if (shouldUseClientDB()) { const db = await import('./client-db-d3e6e918.mjs').then((m) => m.useContentDatabase()); return db.fetch(query); } const data = await $fetch(apiPath, { method: "GET", responseType: "json", params: content.experimental.stripQueryParameters ? void 0 : { _params: jsonStringify(params), previewToken: useContentPreview().getPreviewToken() } }); if (typeof data === "string" && data.startsWith("")) { throw new Error("Not found"); } return data; }; function queryContent(query, ...pathParts) { const { content } = useRuntimeConfig().public; const queryBuilder = createQuery(createQueryFetch(), typeof query !== "string" ? query : {}); let path; if (typeof query === "string") { path = withLeadingSlash(joinURL(query, ...pathParts)); } const originalParamsFn = queryBuilder.params; queryBuilder.params = () => { var _a, _b, _c; const params = originalParamsFn(); if (path) { params.where = params.where || []; if (params.first && (params.where || []).length === 0) { params.where.push({ _path: withoutTrailingSlash(path) }); } else { params.where.push({ _path: new RegExp(`^${path.replace(/[-[\]{}()*+.,^$\s/]/g, "\\$&")}`) }); } } if (!((_a = params.sort) == null ? void 0 : _a.length)) { params.sort = [{ _file: 1, $numeric: true }]; } if (content.locales.length) { const queryLocale = (_c = (_b = params.where) == null ? void 0 : _b.find((w) => w._locale)) == null ? void 0 : _c._locale; if (!queryLocale) { params.where = params.where || []; params.where.push({ _locale: content.defaultLocale }); } } return params; }; return queryBuilder; } export { assertArray as a, ensureArray as b, createQuery as c, apply as d, encodeQueryParams as e, withKeys as f, get as g, jsonStringify as j, queryContent as q, sortList as s, withoutKeys as w }; //# sourceMappingURL=query-e0164f92.mjs.map