@querykitjs/web 2.0.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +41 -15
- package/README.md +34 -8
- package/dist/index.cjs +22 -15
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -6
- package/dist/index.d.ts +6 -6
- package/dist/index.js +22 -15
- package/dist/index.js.map +1 -1
- package/dist/react/index.cjs +11 -4
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.d.cts +1 -1
- package/dist/react/index.d.ts +1 -1
- package/dist/react/index.js +11 -4
- package/dist/react/index.js.map +1 -1
- package/dist/{registry-DPIEOvLW.d.cts → registry-DbZQk28q.d.cts} +19 -13
- package/dist/{registry-DPIEOvLW.d.ts → registry-DbZQk28q.d.ts} +19 -13
- package/package.json +2 -2
package/dist/react/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/url.ts","../../src/react/use-list-params-base.ts","../../src/react/use-list-params.ts"],"names":[],"mappings":";;;;;;AAWA,IAAM,YAAA,GAAoC;AAAA,EACxC,SAAA,EAAW,MAAA;AAAA,EACX,SAAA,EAAW,MAAA;AAAA,EACX,SAAA,EAAW;AACb,CAAA;AAUO,SAAS,WAAW,IAAA,EAAoB;AAC7C,EAAA,IAAI,CAAC,IAAA,CAAK,IAAA,EAAM,OAAO,EAAA;AACvB,EAAA,OAAA,CAAQ,IAAA,CAAK,SAAA,KAAc,MAAA,GAAS,GAAA,GAAM,MAAM,IAAA,CAAK,IAAA;AACvD;AAQO,SAAS,cAAA,CAAe,QAAoB,YAAA,EAAuC;AACxF,EAAA,MAAM,aAA+B,EAAC;AACtC,EAAA,MAAM,SAAuB,EAAC;AAE9B,EAAA,KAAA,MAAW,CAAC,KAAA,EAAO,CAAC,KAAK,MAAA,CAAO,OAAA,CAAQ,MAAM,CAAA,EAAkC;AAC9E,IAAA,MAAM,GAAA,GAAM,EAAE,GAAA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AAAA,IACjI,KAAA,EAAO,WAAA,CAAY,CAAC,IAAA,KAAoB,eAAA,CAAgB,WAAA,CAAY,YAAA,EAAc,IAAI,CAAC,CAAA,EAAG,CAAC,YAAA,EAAc,eAAe,CAAC;AAAA,GAC3H;AACF;;;ACvBO,SAAS,aAAA,CAAwC,UAA0B,OAAA,EAAuD;AACvI,EAAA,MAAM,CAAC,YAAA,EAAc,eAAe,CAAA,GAAI,eAAA,EAAgB;AACxD,EAAA,OAAO,kBAAkB,QAAA,EAAU;AAAA,IACjC,GAAG,OAAA;AAAA,IACH,YAAA;AAAA,IACA,eAAA,EAAiB,CAAA,IAAA,KAAQ,eAAA,CAAgB,IAAI;AAAA,GAC9C,CAAA;AACH","file":"index.js","sourcesContent":["import { buildListParams } from \"./query\";\nimport type { FieldDescriptor, ListSchema } from \"./schema\";\nimport type { FieldCondition, Filter, FilterNode, FilterValue, ListParams, ListPayload, Sort, SortInput } from \"./types\";\n\n/** URL param nomlari (sahifa/o'lcham/sort) — sozlanadigan. */\nexport interface UrlConfig {\n pageParam?: string;\n sizeParam?: string;\n sortParam?: string;\n}\n\nconst URL_DEFAULTS: Required<UrlConfig> = {\n pageParam: \"page\",\n sizeParam: \"size\",\n sortParam: \"sortType\",\n};\n\n/** `\"-createdAt\"` → `{ name, direction }` (bo'sh bo'lsa `undefined`). */\nexport function decodeSort(value: string | null | undefined): Sort | undefined {\n if (!value) return undefined;\n const desc = value.startsWith(\"-\");\n return { name: desc ? value.slice(1) : value, direction: desc ? \"desc\" : \"asc\" };\n}\n\n/** `{ name, direction }` → `\"-createdAt\"` / `\"createdAt\"` (nomsiz bo'lsa `\"\"`). */\nexport function encodeSort(sort: Sort): string {\n if (!sort.name) return \"\";\n return (sort.direction === \"desc\" ? \"-\" : \"\") + sort.name;\n}\n\n/**\n * Schema + searchParams → filter. Applies `range`/`between`/`search`/`split`/\n * `default`/`trim`; empty/absent params are dropped. Returns a flat\n * `FieldCondition[]` (implicit AND), or a tree when a `search` OR-group is\n * present (`{ and: [orGroup, …conditions] }`).\n */\nexport function schemaToFilter(schema: ListSchema, searchParams: URLSearchParams): Filter {\n const conditions: FieldCondition[] = [];\n const groups: FilterNode[] = [];\n\n for (const [param, d] of Object.entries(schema) as [string, FieldDescriptor][]) {\n const key = d.key ?? param;\n\n // range → two inclusive conditions (>= , <=)\n if (d.range) {\n const from = searchParams.get(d.range[0]);\n const to = searchParams.get(d.range[1]);\n if (from) conditions.push(cond(key, \">=\", from));\n if (to) conditions.push(cond(key, \"<=\", to));\n continue;\n }\n\n // between → one condition with a 2-tuple value\n if (d.between) {\n const from = searchParams.get(d.between[0]);\n const to = searchParams.get(d.between[1]);\n if (from && to) conditions.push(cond(key, \"between\", [from, to]));\n continue;\n }\n\n // read value, falling back to `default` only when the param is ABSENT\n // (an empty `?x=` means the user cleared the filter → prune, don't default).\n const raw = searchParams.get(param);\n let value: FilterValue | undefined = raw !== null ? raw : d.default;\n if (typeof value === \"string\" && d.trim) value = value.trim();\n if (value === undefined || value === null || value === \"\") continue;\n if (Array.isArray(value) && value.length === 0) continue;\n\n // search → OR of `contains` across the listed fields (needs a string)\n if (d.search && typeof value === \"string\") {\n groups.push({ or: d.search.map(field => cond(field, \"%_%\", value as string)) });\n continue;\n }\n\n // split → comma-string → array (for `in`/`notIn`); guarded against empties\n if (d.split && typeof value === \"string\") {\n const delimiter = d.split === true ? \",\" : d.split;\n const parts = value\n .split(delimiter)\n .map(s => s.trim())\n .filter(Boolean);\n if (parts.length === 0) continue;\n value = parts;\n }\n\n conditions.push(cond(key, d.operation ?? \"=\", value));\n }\n\n return groups.length === 0 ? conditions : { and: [...groups, ...conditions] };\n}\n\nfunction cond(key: string, operation: FieldCondition[\"operation\"], value: FilterValue): FieldCondition {\n return { key, operation, value };\n}\n\n/** searchParams → normalizatsiyalanmagan {@link ListParams} (filter/sort/page/perPage). */\nexport function readListParams(schema: ListSchema, searchParams: URLSearchParams, url: UrlConfig = {}): ListParams {\n const cfg = { ...URL_DEFAULTS, ...url };\n const size = searchParams.get(cfg.sizeParam);\n return {\n filter: schemaToFilter(schema, searchParams),\n sort: searchParams.get(cfg.sortParam) ?? undefined,\n page: Number(searchParams.get(cfg.pageParam)) || 1,\n perPage: size ? Number(size) : undefined,\n };\n}\n\n/** searchParams → to'liq, yuborishga tayyor {@link ListPayload}. */\nexport function searchParamsToPayload(schema: ListSchema, searchParams: URLSearchParams, url?: UrlConfig): ListPayload {\n return buildListParams(readListParams(schema, searchParams, url));\n}\n\n/* ------------------------- URL yozish (immutable) ------------------------- */\n/* Har biri YANGI URLSearchParams qaytaradi; filter/sort o'zgarsa `page` reset. */\n\nfunction clone(searchParams: URLSearchParams): URLSearchParams {\n return new URLSearchParams(searchParams);\n}\n\n/** Filter param'ini o'rnatadi/o'chiradi va `page`ni reset qiladi. */\nexport function setParam(searchParams: URLSearchParams, name: string, value: string | null | undefined, url: UrlConfig = {}): URLSearchParams {\n const cfg = { ...URL_DEFAULTS, ...url };\n const next = clone(searchParams);\n next.delete(cfg.pageParam);\n if (value === null || value === undefined || value === \"\") next.delete(name);\n else next.set(name, value);\n return next;\n}\n\n/** Sahifani o'rnatadi (`page` reset qilinmaydi). */\nexport function setPage(searchParams: URLSearchParams, page: number, url: UrlConfig = {}): URLSearchParams {\n const cfg = { ...URL_DEFAULTS, ...url };\n const next = clone(searchParams);\n next.set(cfg.pageParam, String(page));\n return next;\n}\n\n/** Sahifa o'lchamini o'rnatadi va `page`ni reset qiladi. */\nexport function setSize(searchParams: URLSearchParams, size: number, url: UrlConfig = {}): URLSearchParams {\n const cfg = { ...URL_DEFAULTS, ...url };\n const next = clone(searchParams);\n next.delete(cfg.pageParam);\n next.set(cfg.sizeParam, String(size));\n return next;\n}\n\n/** Sortni o'rnatadi (`\"-createdAt\"` yoki `{name,direction}`) va `page`ni reset qiladi. */\nexport function setSort(searchParams: URLSearchParams, sort: SortInput, url: UrlConfig = {}): URLSearchParams {\n const cfg = { ...URL_DEFAULTS, ...url };\n const next = clone(searchParams);\n next.delete(cfg.pageParam);\n const encoded = typeof sort === \"string\" ? sort : encodeSort(sort);\n if (encoded) next.set(cfg.sortParam, encoded);\n else next.delete(cfg.sortParam);\n return next;\n}\n\n/** Barcha list param'larini tozalaydi (ixtiyoriy saqlanadigan kalitlar bilan). */\nexport function resetParams(searchParams: URLSearchParams, keep: string[] = []): URLSearchParams {\n const next = new URLSearchParams();\n for (const key of keep) {\n const value = searchParams.get(key);\n if (value !== null) next.set(key, value);\n }\n return next;\n}\n","import { useCallback, useMemo } from \"react\";\n\nimport type { AdapterName, WithFor } from \"../adapter\";\nimport type { Resource, ResourceListParams } from \"../registry\";\nimport type { ListSchema } from \"../schema\";\nimport type { ListPayload, SortInput } from \"../types\";\nimport type { UrlConfig } from \"../url\";\nimport { readListParams, resetParams, setPage, setParam, setSize, setSort } from \"../url\";\n\nexport interface UseListParamsBaseOptions<A extends AdapterName> {\n /** URL↔filter schema ({@link defineListSchema} / `resource.schema`). Memoize it. */\n schema: ListSchema;\n /** Current URL search params (from your router). */\n searchParams: URLSearchParams;\n /** Apply new search params (`(next) => void`). */\n setSearchParams: (next: URLSearchParams) => void;\n /** URL param names (page/size/sortType) — optional. Memoize it. */\n url?: UrlConfig;\n /** Fixed relations to load on every request (not URL-driven). */\n with?: WithFor<A>;\n}\n\nexport interface UseListParamsResult {\n /** Ready-to-send list payload (built from the URL via the resource's defaults). */\n params: ListPayload;\n searchParams: URLSearchParams;\n /** Set/clear a filter param (resets `page`). */\n setParam: (name: string, value: string | null | undefined) => void;\n setPage: (page: number) => void;\n setSize: (size: number) => void;\n setSort: (sort: SortInput) => void;\n reset: (keep?: string[]) => void;\n}\n\n/**\n * Router-agnostic list URL-state hook. Reads the URL via the schema, builds a\n * ready-to-send payload through the `resource` (registry defaults + adapter), and\n * returns URL setters (filter/sort/size changes reset `page`). Bring your own\n * `searchParams`/`setSearchParams` (Next.js, TanStack Router, …).\n */\nexport function useListParamsBase<T, A extends AdapterName>(resource: Resource<T, A>, options: UseListParamsBaseOptions<A>): UseListParamsResult {\n const { schema, searchParams, setSearchParams, url, with: withRel } = options;\n\n const params = useMemo(\n () => resource.list({ ...readListParams(schema, searchParams, url), with: withRel } as ResourceListParams<T, A>),\n [resource, schema, searchParams, url, withRel],\n );\n\n return {\n params,\n searchParams,\n setParam: useCallback((name, value) => setSearchParams(setParam(searchParams, name, value, url)), [searchParams, setSearchParams, url]),\n setPage: useCallback((page: number) => setSearchParams(setPage(searchParams, page, url)), [searchParams, setSearchParams, url]),\n setSize: useCallback((size: number) => setSearchParams(setSize(searchParams, size, url)), [searchParams, setSearchParams, url]),\n setSort: useCallback((sort: SortInput) => setSearchParams(setSort(searchParams, sort, url)), [searchParams, setSearchParams, url]),\n reset: useCallback((keep?: string[]) => setSearchParams(resetParams(searchParams, keep)), [searchParams, setSearchParams]),\n };\n}\n","import { useSearchParams } from \"react-router-dom\";\n\nimport type { AdapterName, WithFor } from \"../adapter\";\nimport type { Resource } from \"../registry\";\nimport type { ListSchema } from \"../schema\";\nimport type { UrlConfig } from \"../url\";\nimport { useListParamsBase, type UseListParamsResult } from \"./use-list-params-base\";\n\nexport interface UseListParamsOptions<A extends AdapterName> {\n /** URL↔filter schema ({@link defineListSchema} / `resource.schema`). Memoize it. */\n schema: ListSchema;\n /** URL param names (page/size/sortType) — optional. Memoize it. */\n url?: UrlConfig;\n /** Fixed relations to load on every request (not URL-driven). */\n with?: WithFor<A>;\n}\n\n/**\n * List URL-state hook — turnkey with **react-router-dom** (uses `useSearchParams`\n * internally). Reads filters/sort/page from the URL via the schema, builds a\n * ready-to-send payload through the `resource`, and returns URL setters (which\n * reset `page` on filter/sort/size change). Does **not** fetch — pass `params`\n * to your own axios / TanStack Query.\n *\n * For other routers (Next.js, TanStack Router), use {@link useListParamsBase} and\n * supply `searchParams`/`setSearchParams` yourself.\n *\n * @example\n * ```tsx\n * const users = qk.resource<IUser>(\"users\");\n * const { params, setPage, setSort } = useListParams(users, { schema: usersSchema });\n * const { data } = useQuery({ queryKey: users.keys.list(params), queryFn: () => post(\"/users/list\", params) });\n * ```\n */\nexport function useListParams<T, A extends AdapterName>(resource: Resource<T, A>, options: UseListParamsOptions<A>): UseListParamsResult {\n const [searchParams, setSearchParams] = useSearchParams();\n return useListParamsBase(resource, {\n ...options,\n searchParams,\n setSearchParams: next => setSearchParams(next),\n });\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/url.ts","../../src/react/use-list-params-base.ts","../../src/react/use-list-params.ts"],"names":[],"mappings":";;;;;;AAWA,IAAM,YAAA,GAAoC;AAAA,EACxC,SAAA,EAAW,MAAA;AAAA,EACX,SAAA,EAAW,MAAA;AAAA,EACX,SAAA,EAAW;AACb,CAAA;AAMO,SAAS,WAAW,KAAA,EAAoD;AAC7E,EAAA,IAAI,CAAC,OAAO,OAAO,MAAA;AACnB,EAAA,MAAM,KAAA,GAAQ,KAAA,CACX,KAAA,CAAM,GAAG,EACT,GAAA,CAAI,CAAA,CAAA,KAAK,CAAA,CAAE,IAAA,EAAM,CAAA,CACjB,MAAA,CAAO,OAAO,CAAA,CACd,IAAI,CAAA,GAAA,KAAO;AACV,IAAA,MAAM,IAAA,GAAO,GAAA,CAAI,UAAA,CAAW,GAAG,CAAA;AAC/B,IAAA,OAAO,EAAE,GAAA,EAAK,IAAA,GAAO,GAAA,CAAI,KAAA,CAAM,CAAC,CAAA,GAAI,GAAA,EAAK,SAAA,EAAY,IAAA,GAAO,MAAA,GAAS,KAAA,EAAwB;AAAA,EAC/F,CAAC,CAAA,CACA,MAAA,CAAO,CAAA,CAAA,KAAK,EAAE,GAAG,CAAA;AACpB,EAAA,OAAO,KAAA,CAAM,SAAS,KAAA,GAAQ,MAAA;AAChC;AAGO,SAAS,WAAW,IAAA,EAAyB;AAClD,EAAA,OAAO,IAAA,CACJ,GAAA,CAAI,CAAA,CAAA,KAAM,OAAO,CAAA,KAAM,WAAW,CAAA,CAAE,IAAA,EAAK,GAAI,CAAA,IAAK,CAAA,CAAE,GAAA,GAAA,CAA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B,GAAG,CAAC,CAAA;AAAA,IAC9H,OAAA,EAAS,WAAA,CAAY,CAAC,IAAA,KAAiB,gBAAgB,OAAA,CAAQ,YAAA,EAAc,IAAA,EAAM,GAAG,CAAC,CAAA,EAAG,CAAC,YAAA,EAAc,eAAA,EAAiB,GAAG,CAAC,CAAA;AAAA,IAC9H,OAAA,EAAS,WAAA,CAAY,CAAC,IAAA,KAAoB,gBAAgB,OAAA,CAAQ,YAAA,EAAc,IAAA,EAAM,GAAG,CAAC,CAAA,EAAG,CAAC,YAAA,EAAc,eAAA,EAAiB,GAAG,CAAC,CAAA;AAAA,IACjI,KAAA,EAAO,WAAA,CAAY,CAAC,IAAA,KAAoB,eAAA,CAAgB,WAAA,CAAY,YAAA,EAAc,IAAI,CAAC,CAAA,EAAG,CAAC,YAAA,EAAc,eAAe,CAAC;AAAA,GAC3H;AACF;;;ACvBO,SAAS,aAAA,CAAwC,UAA0B,OAAA,EAAuD;AACvI,EAAA,MAAM,CAAC,YAAA,EAAc,eAAe,CAAA,GAAI,eAAA,EAAgB;AACxD,EAAA,OAAO,kBAAkB,QAAA,EAAU;AAAA,IACjC,GAAG,OAAA;AAAA,IACH,YAAA;AAAA,IACA,eAAA,EAAiB,CAAA,IAAA,KAAQ,eAAA,CAAgB,IAAI;AAAA,GAC9C,CAAA;AACH","file":"index.js","sourcesContent":["import { buildListParams } from \"./query\";\nimport type { FieldDescriptor, ListSchema } from \"./schema\";\nimport type { FieldCondition, Filter, FilterNode, FilterValue, ListParams, ListPayload, Sort, SortDirection, SortInput } from \"./types\";\n\n/** URL param nomlari (sahifa/o'lcham/sort) — sozlanadigan. */\nexport interface UrlConfig {\n pageParam?: string;\n sizeParam?: string;\n sortParam?: string;\n}\n\nconst URL_DEFAULTS: Required<UrlConfig> = {\n pageParam: \"page\",\n sizeParam: \"size\",\n sortParam: \"sortType\",\n};\n\n/**\n * `\"-createdAt,id,-appNumber\"` → `[{ key, direction }, …]` — comma-separated,\n * order preserved, `-` prefix = `desc` (else `asc`). Empty → `undefined`.\n */\nexport function decodeSort(value: string | null | undefined): Sort | undefined {\n if (!value) return undefined;\n const items = value\n .split(\",\")\n .map(s => s.trim())\n .filter(Boolean)\n .map(tok => {\n const desc = tok.startsWith(\"-\");\n return { key: desc ? tok.slice(1) : tok, direction: (desc ? \"desc\" : \"asc\") as SortDirection };\n })\n .filter(i => i.key); // drop empty keys (e.g. a lone \"-\")\n return items.length ? items : undefined;\n}\n\n/** `[{ key, direction }, …]` (or `\"-field\"` shorthand strings) → `\"-createdAt,id,-appNumber\"` (empty → `\"\"`). */\nexport function encodeSort(sort: SortInput): string {\n return sort\n .map(i => (typeof i === \"string\" ? i.trim() : i && i.key ? (i.direction === \"desc\" ? \"-\" : \"\") + i.key : \"\"))\n .filter(Boolean)\n .join(\",\");\n}\n\n/**\n * Schema + searchParams → filter. Applies `range`/`between`/`search`/`split`/\n * `default`/`trim`; empty/absent params are dropped. Returns a flat\n * `FieldCondition[]` (implicit AND), or a tree when a `search` OR-group is\n * present (`{ and: [orGroup, …conditions] }`).\n */\nexport function schemaToFilter(schema: ListSchema, searchParams: URLSearchParams): Filter {\n const conditions: FieldCondition[] = [];\n const groups: FilterNode[] = [];\n\n for (const [param, d] of Object.entries(schema) as [string, FieldDescriptor][]) {\n const key = d.key ?? param;\n\n // range → two inclusive conditions (>= , <=)\n if (d.range) {\n const from = searchParams.get(d.range[0]);\n const to = searchParams.get(d.range[1]);\n if (from) conditions.push(cond(key, \">=\", from));\n if (to) conditions.push(cond(key, \"<=\", to));\n continue;\n }\n\n // between → one condition with a 2-tuple value\n if (d.between) {\n const from = searchParams.get(d.between[0]);\n const to = searchParams.get(d.between[1]);\n if (from && to) conditions.push(cond(key, \"between\", [from, to]));\n continue;\n }\n\n // read value, falling back to `default` only when the param is ABSENT\n // (an empty `?x=` means the user cleared the filter → prune, don't default).\n const raw = searchParams.get(param);\n let value: FilterValue | undefined = raw !== null ? raw : d.default;\n if (typeof value === \"string\" && d.trim) value = value.trim();\n if (value === undefined || value === null || value === \"\") continue;\n if (Array.isArray(value) && value.length === 0) continue;\n\n // search → OR of `contains` across the listed fields (needs a string)\n if (d.search && typeof value === \"string\") {\n groups.push({ or: d.search.map(field => cond(field, \"%_%\", value as string)) });\n continue;\n }\n\n // split → comma-string → array (for `in`/`notIn`); guarded against empties\n if (d.split && typeof value === \"string\") {\n const delimiter = d.split === true ? \",\" : d.split;\n const parts = value\n .split(delimiter)\n .map(s => s.trim())\n .filter(Boolean);\n if (parts.length === 0) continue;\n value = parts;\n }\n\n conditions.push(cond(key, d.operation ?? \"=\", value));\n }\n\n return groups.length === 0 ? conditions : { and: [...groups, ...conditions] };\n}\n\nfunction cond(key: string, operation: FieldCondition[\"operation\"], value: FilterValue): FieldCondition {\n return { key, operation, value };\n}\n\n/** searchParams → normalizatsiyalanmagan {@link ListParams} (filter/sort/page/perPage). */\nexport function readListParams(schema: ListSchema, searchParams: URLSearchParams, url: UrlConfig = {}): ListParams {\n const cfg = { ...URL_DEFAULTS, ...url };\n const size = searchParams.get(cfg.sizeParam);\n return {\n filter: schemaToFilter(schema, searchParams),\n sort: decodeSort(searchParams.get(cfg.sortParam)),\n page: Number(searchParams.get(cfg.pageParam)) || 1,\n perPage: size ? Number(size) : undefined,\n };\n}\n\n/** searchParams → to'liq, yuborishga tayyor {@link ListPayload}. */\nexport function searchParamsToPayload(schema: ListSchema, searchParams: URLSearchParams, url?: UrlConfig): ListPayload {\n return buildListParams(readListParams(schema, searchParams, url));\n}\n\n/* ------------------------- URL yozish (immutable) ------------------------- */\n/* Har biri YANGI URLSearchParams qaytaradi; filter/sort o'zgarsa `page` reset. */\n\nfunction clone(searchParams: URLSearchParams): URLSearchParams {\n return new URLSearchParams(searchParams);\n}\n\n/** Filter param'ini o'rnatadi/o'chiradi va `page`ni reset qiladi. */\nexport function setParam(searchParams: URLSearchParams, name: string, value: string | null | undefined, url: UrlConfig = {}): URLSearchParams {\n const cfg = { ...URL_DEFAULTS, ...url };\n const next = clone(searchParams);\n next.delete(cfg.pageParam);\n if (value === null || value === undefined || value === \"\") next.delete(name);\n else next.set(name, value);\n return next;\n}\n\n/** Sahifani o'rnatadi (`page` reset qilinmaydi). */\nexport function setPage(searchParams: URLSearchParams, page: number, url: UrlConfig = {}): URLSearchParams {\n const cfg = { ...URL_DEFAULTS, ...url };\n const next = clone(searchParams);\n next.set(cfg.pageParam, String(page));\n return next;\n}\n\n/** Sahifa o'lchamini o'rnatadi va `page`ni reset qiladi. */\nexport function setSize(searchParams: URLSearchParams, size: number, url: UrlConfig = {}): URLSearchParams {\n const cfg = { ...URL_DEFAULTS, ...url };\n const next = clone(searchParams);\n next.delete(cfg.pageParam);\n next.set(cfg.sizeParam, String(size));\n return next;\n}\n\n/** Sortni o'rnatadi (`[{ key, direction }, …]`) va `page`ni reset qiladi. */\nexport function setSort(searchParams: URLSearchParams, sort: SortInput, url: UrlConfig = {}): URLSearchParams {\n const cfg = { ...URL_DEFAULTS, ...url };\n const next = clone(searchParams);\n next.delete(cfg.pageParam);\n const encoded = encodeSort(sort);\n if (encoded) next.set(cfg.sortParam, encoded);\n else next.delete(cfg.sortParam);\n return next;\n}\n\n/** Barcha list param'larini tozalaydi (ixtiyoriy saqlanadigan kalitlar bilan). */\nexport function resetParams(searchParams: URLSearchParams, keep: string[] = []): URLSearchParams {\n const next = new URLSearchParams();\n for (const key of keep) {\n const value = searchParams.get(key);\n if (value !== null) next.set(key, value);\n }\n return next;\n}\n","import { useCallback, useMemo } from \"react\";\n\nimport type { AdapterName, WithFor } from \"../adapter\";\nimport type { Resource, ResourceListParams } from \"../registry\";\nimport type { ListSchema } from \"../schema\";\nimport type { ListPayload, SortInput } from \"../types\";\nimport type { UrlConfig } from \"../url\";\nimport { readListParams, resetParams, setPage, setParam, setSize, setSort } from \"../url\";\n\nexport interface UseListParamsBaseOptions<A extends AdapterName> {\n /** URL↔filter schema ({@link defineListSchema} / `resource.schema`). Memoize it. */\n schema: ListSchema;\n /** Current URL search params (from your router). */\n searchParams: URLSearchParams;\n /** Apply new search params (`(next) => void`). */\n setSearchParams: (next: URLSearchParams) => void;\n /** URL param names (page/size/sortType) — optional. Memoize it. */\n url?: UrlConfig;\n /** Fixed relations to load on every request (not URL-driven). */\n with?: WithFor<A>;\n}\n\nexport interface UseListParamsResult {\n /** Ready-to-send list payload (built from the URL via the resource's defaults). */\n params: ListPayload;\n searchParams: URLSearchParams;\n /** Set/clear a filter param (resets `page`). */\n setParam: (name: string, value: string | null | undefined) => void;\n setPage: (page: number) => void;\n setSize: (size: number) => void;\n setSort: (sort: SortInput) => void;\n reset: (keep?: string[]) => void;\n}\n\n/**\n * Router-agnostic list URL-state hook. Reads the URL via the schema, builds a\n * ready-to-send payload through the `resource` (registry defaults + adapter), and\n * returns URL setters (filter/sort/size changes reset `page`). Bring your own\n * `searchParams`/`setSearchParams` (Next.js, TanStack Router, …).\n */\nexport function useListParamsBase<T, A extends AdapterName>(resource: Resource<T, A>, options: UseListParamsBaseOptions<A>): UseListParamsResult {\n const { schema, searchParams, setSearchParams, url, with: withRel } = options;\n\n const params = useMemo(\n () => resource.list({ ...readListParams(schema, searchParams, url), with: withRel } as ResourceListParams<T, A>),\n [resource, schema, searchParams, url, withRel],\n );\n\n return {\n params,\n searchParams,\n setParam: useCallback((name, value) => setSearchParams(setParam(searchParams, name, value, url)), [searchParams, setSearchParams, url]),\n setPage: useCallback((page: number) => setSearchParams(setPage(searchParams, page, url)), [searchParams, setSearchParams, url]),\n setSize: useCallback((size: number) => setSearchParams(setSize(searchParams, size, url)), [searchParams, setSearchParams, url]),\n setSort: useCallback((sort: SortInput) => setSearchParams(setSort(searchParams, sort, url)), [searchParams, setSearchParams, url]),\n reset: useCallback((keep?: string[]) => setSearchParams(resetParams(searchParams, keep)), [searchParams, setSearchParams]),\n };\n}\n","import { useSearchParams } from \"react-router-dom\";\n\nimport type { AdapterName, WithFor } from \"../adapter\";\nimport type { Resource } from \"../registry\";\nimport type { ListSchema } from \"../schema\";\nimport type { UrlConfig } from \"../url\";\nimport { useListParamsBase, type UseListParamsResult } from \"./use-list-params-base\";\n\nexport interface UseListParamsOptions<A extends AdapterName> {\n /** URL↔filter schema ({@link defineListSchema} / `resource.schema`). Memoize it. */\n schema: ListSchema;\n /** URL param names (page/size/sortType) — optional. Memoize it. */\n url?: UrlConfig;\n /** Fixed relations to load on every request (not URL-driven). */\n with?: WithFor<A>;\n}\n\n/**\n * List URL-state hook — turnkey with **react-router-dom** (uses `useSearchParams`\n * internally). Reads filters/sort/page from the URL via the schema, builds a\n * ready-to-send payload through the `resource`, and returns URL setters (which\n * reset `page` on filter/sort/size change). Does **not** fetch — pass `params`\n * to your own axios / TanStack Query.\n *\n * For other routers (Next.js, TanStack Router), use {@link useListParamsBase} and\n * supply `searchParams`/`setSearchParams` yourself.\n *\n * @example\n * ```tsx\n * const users = qk.resource<IUser>(\"users\");\n * const { params, setPage, setSort } = useListParams(users, { schema: usersSchema });\n * const { data } = useQuery({ queryKey: users.keys.list(params), queryFn: () => post(\"/users/list\", params) });\n * ```\n */\nexport function useListParams<T, A extends AdapterName>(resource: Resource<T, A>, options: UseListParamsOptions<A>): UseListParamsResult {\n const [searchParams, setSearchParams] = useSearchParams();\n return useListParamsBase(resource, {\n ...options,\n searchParams,\n setSearchParams: next => setSearchParams(next),\n });\n}\n"]}
|
|
@@ -24,12 +24,15 @@ type FilterNode<T = Record<string, unknown>> = Core.FilterNode<FieldKey<T>>;
|
|
|
24
24
|
*/
|
|
25
25
|
type Filter<T = Record<string, unknown>> = Core.Filter<FieldKey<T>>;
|
|
26
26
|
type SortDirection = Core.SortDirection;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
27
|
+
type SortItem = Core.SortItem;
|
|
28
|
+
/** Sort — **always an array** of `{ key, direction }` (multi-field). */
|
|
29
|
+
type Sort = Core.Sort;
|
|
30
|
+
/**
|
|
31
|
+
* Sort input — an array where each item is a `{ key, direction }` object OR a
|
|
32
|
+
* `"-field"` / `"field"` shorthand string (`["-createdAt", "id"]`). Builders map
|
|
33
|
+
* it to the canonical {@link Sort} (`SortItem[]`).
|
|
34
|
+
*/
|
|
35
|
+
type SortInput = ReadonlyArray<SortItem | string>;
|
|
33
36
|
interface Params<T = Record<string, unknown>> {
|
|
34
37
|
filter?: Filter<T>;
|
|
35
38
|
sort?: SortInput;
|
|
@@ -242,10 +245,13 @@ interface UrlConfig {
|
|
|
242
245
|
sizeParam?: string;
|
|
243
246
|
sortParam?: string;
|
|
244
247
|
}
|
|
245
|
-
/**
|
|
248
|
+
/**
|
|
249
|
+
* `"-createdAt,id,-appNumber"` → `[{ key, direction }, …]` — comma-separated,
|
|
250
|
+
* order preserved, `-` prefix = `desc` (else `asc`). Empty → `undefined`.
|
|
251
|
+
*/
|
|
246
252
|
declare function decodeSort(value: string | null | undefined): Sort | undefined;
|
|
247
|
-
/** `{
|
|
248
|
-
declare function encodeSort(sort:
|
|
253
|
+
/** `[{ key, direction }, …]` (or `"-field"` shorthand strings) → `"-createdAt,id,-appNumber"` (empty → `""`). */
|
|
254
|
+
declare function encodeSort(sort: SortInput): string;
|
|
249
255
|
/**
|
|
250
256
|
* Schema + searchParams → filter. Applies `range`/`between`/`search`/`split`/
|
|
251
257
|
* `default`/`trim`; empty/absent params are dropped. Returns a flat
|
|
@@ -263,7 +269,7 @@ declare function setParam(searchParams: URLSearchParams, name: string, value: st
|
|
|
263
269
|
declare function setPage(searchParams: URLSearchParams, page: number, url?: UrlConfig): URLSearchParams;
|
|
264
270
|
/** Sahifa o'lchamini o'rnatadi va `page`ni reset qiladi. */
|
|
265
271
|
declare function setSize(searchParams: URLSearchParams, size: number, url?: UrlConfig): URLSearchParams;
|
|
266
|
-
/** Sortni o'rnatadi (`
|
|
272
|
+
/** Sortni o'rnatadi (`[{ key, direction }, …]`) va `page`ni reset qiladi. */
|
|
267
273
|
declare function setSort(searchParams: URLSearchParams, sort: SortInput, url?: UrlConfig): URLSearchParams;
|
|
268
274
|
/** Barcha list param'larini tozalaydi (ixtiyoriy saqlanadigan kalitlar bilan). */
|
|
269
275
|
declare function resetParams(searchParams: URLSearchParams, keep?: string[]): URLSearchParams;
|
|
@@ -378,7 +384,7 @@ interface RegistryDefaults {
|
|
|
378
384
|
perPage?: number;
|
|
379
385
|
/** `infinite` + `cursor` page size. */
|
|
380
386
|
limit?: number;
|
|
381
|
-
/** `list`/`infinite` sort
|
|
387
|
+
/** `list`/`infinite` sort — `[{key,direction}]` or `["-createdAt", "id"]` shorthand. */
|
|
382
388
|
sort?: SortInput;
|
|
383
389
|
/** Cursor-only defaults. */
|
|
384
390
|
cursor?: {
|
|
@@ -407,11 +413,11 @@ interface Registry<A extends AdapterName> {
|
|
|
407
413
|
*
|
|
408
414
|
* @example
|
|
409
415
|
* ```ts
|
|
410
|
-
* export const qk = createRegistry({ adapter: "mongoose", defaults: { perPage: 20, sort: "-createdAt" } });
|
|
416
|
+
* export const qk = createRegistry({ adapter: "mongoose", defaults: { perPage: 20, sort: ["-createdAt"] } });
|
|
411
417
|
* const users = qk.resource<IUser>("users");
|
|
412
418
|
* const body = users.list({ filter: users.f.eq("status", "active"), page });
|
|
413
419
|
* ```
|
|
414
420
|
*/
|
|
415
421
|
declare function createRegistry<A extends AdapterName>(config: RegistryConfig<A>): Registry<A>;
|
|
416
422
|
|
|
417
|
-
export {
|
|
423
|
+
export { readListParams as $, type AdapterName as A, type ResourceCursorParams as B, type CursorParams as C, type DrizzleWith as D, type ResourceInfiniteParams as E, type Filter as F, type ResourceKeys as G, type ResourceListParams as H, type InfiniteParams as I, type ResourceParams as J, type SortDirection as K, type ListParams as L, type Meta as M, type NotGroup as N, type OrGroup as O, type Params as P, type QueryPayload as Q, type RawCursorMeta as R, type Sort as S, type SortItem as T, type UrlConfig as U, createFilters as V, type WithFor as W, createRegistry as X, decodeSort as Y, defineListSchema as Z, encodeSort as _, type CursorPayload as a, resetParams as a0, schemaToFilter as a1, searchParamsToPayload as a2, setPage as a3, setParam as a4, setSize as a5, setSort as a6, type InfinitePayload as b, type ListPayload as c, type FieldCondition as d, type FilterNode as e, type SortInput as f, type AndGroup as g, type CursorMeta as h, type CursorResult as i, type FieldDescriptor as j, type FieldKey as k, type FilterOperator as l, type FilterScalar as m, type FilterValue as n, type InfiniteMeta as o, type InfiniteResult as p, type ListResult as q, type ListSchema as r, type MongoosePopulate as s, type PrismaInclude as t, type RawInfiniteMeta as u, type RawMeta as v, type Registry as w, type RegistryConfig as x, type RegistryDefaults as y, type Resource as z };
|
|
@@ -24,12 +24,15 @@ type FilterNode<T = Record<string, unknown>> = Core.FilterNode<FieldKey<T>>;
|
|
|
24
24
|
*/
|
|
25
25
|
type Filter<T = Record<string, unknown>> = Core.Filter<FieldKey<T>>;
|
|
26
26
|
type SortDirection = Core.SortDirection;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
27
|
+
type SortItem = Core.SortItem;
|
|
28
|
+
/** Sort — **always an array** of `{ key, direction }` (multi-field). */
|
|
29
|
+
type Sort = Core.Sort;
|
|
30
|
+
/**
|
|
31
|
+
* Sort input — an array where each item is a `{ key, direction }` object OR a
|
|
32
|
+
* `"-field"` / `"field"` shorthand string (`["-createdAt", "id"]`). Builders map
|
|
33
|
+
* it to the canonical {@link Sort} (`SortItem[]`).
|
|
34
|
+
*/
|
|
35
|
+
type SortInput = ReadonlyArray<SortItem | string>;
|
|
33
36
|
interface Params<T = Record<string, unknown>> {
|
|
34
37
|
filter?: Filter<T>;
|
|
35
38
|
sort?: SortInput;
|
|
@@ -242,10 +245,13 @@ interface UrlConfig {
|
|
|
242
245
|
sizeParam?: string;
|
|
243
246
|
sortParam?: string;
|
|
244
247
|
}
|
|
245
|
-
/**
|
|
248
|
+
/**
|
|
249
|
+
* `"-createdAt,id,-appNumber"` → `[{ key, direction }, …]` — comma-separated,
|
|
250
|
+
* order preserved, `-` prefix = `desc` (else `asc`). Empty → `undefined`.
|
|
251
|
+
*/
|
|
246
252
|
declare function decodeSort(value: string | null | undefined): Sort | undefined;
|
|
247
|
-
/** `{
|
|
248
|
-
declare function encodeSort(sort:
|
|
253
|
+
/** `[{ key, direction }, …]` (or `"-field"` shorthand strings) → `"-createdAt,id,-appNumber"` (empty → `""`). */
|
|
254
|
+
declare function encodeSort(sort: SortInput): string;
|
|
249
255
|
/**
|
|
250
256
|
* Schema + searchParams → filter. Applies `range`/`between`/`search`/`split`/
|
|
251
257
|
* `default`/`trim`; empty/absent params are dropped. Returns a flat
|
|
@@ -263,7 +269,7 @@ declare function setParam(searchParams: URLSearchParams, name: string, value: st
|
|
|
263
269
|
declare function setPage(searchParams: URLSearchParams, page: number, url?: UrlConfig): URLSearchParams;
|
|
264
270
|
/** Sahifa o'lchamini o'rnatadi va `page`ni reset qiladi. */
|
|
265
271
|
declare function setSize(searchParams: URLSearchParams, size: number, url?: UrlConfig): URLSearchParams;
|
|
266
|
-
/** Sortni o'rnatadi (`
|
|
272
|
+
/** Sortni o'rnatadi (`[{ key, direction }, …]`) va `page`ni reset qiladi. */
|
|
267
273
|
declare function setSort(searchParams: URLSearchParams, sort: SortInput, url?: UrlConfig): URLSearchParams;
|
|
268
274
|
/** Barcha list param'larini tozalaydi (ixtiyoriy saqlanadigan kalitlar bilan). */
|
|
269
275
|
declare function resetParams(searchParams: URLSearchParams, keep?: string[]): URLSearchParams;
|
|
@@ -378,7 +384,7 @@ interface RegistryDefaults {
|
|
|
378
384
|
perPage?: number;
|
|
379
385
|
/** `infinite` + `cursor` page size. */
|
|
380
386
|
limit?: number;
|
|
381
|
-
/** `list`/`infinite` sort
|
|
387
|
+
/** `list`/`infinite` sort — `[{key,direction}]` or `["-createdAt", "id"]` shorthand. */
|
|
382
388
|
sort?: SortInput;
|
|
383
389
|
/** Cursor-only defaults. */
|
|
384
390
|
cursor?: {
|
|
@@ -407,11 +413,11 @@ interface Registry<A extends AdapterName> {
|
|
|
407
413
|
*
|
|
408
414
|
* @example
|
|
409
415
|
* ```ts
|
|
410
|
-
* export const qk = createRegistry({ adapter: "mongoose", defaults: { perPage: 20, sort: "-createdAt" } });
|
|
416
|
+
* export const qk = createRegistry({ adapter: "mongoose", defaults: { perPage: 20, sort: ["-createdAt"] } });
|
|
411
417
|
* const users = qk.resource<IUser>("users");
|
|
412
418
|
* const body = users.list({ filter: users.f.eq("status", "active"), page });
|
|
413
419
|
* ```
|
|
414
420
|
*/
|
|
415
421
|
declare function createRegistry<A extends AdapterName>(config: RegistryConfig<A>): Registry<A>;
|
|
416
422
|
|
|
417
|
-
export {
|
|
423
|
+
export { readListParams as $, type AdapterName as A, type ResourceCursorParams as B, type CursorParams as C, type DrizzleWith as D, type ResourceInfiniteParams as E, type Filter as F, type ResourceKeys as G, type ResourceListParams as H, type InfiniteParams as I, type ResourceParams as J, type SortDirection as K, type ListParams as L, type Meta as M, type NotGroup as N, type OrGroup as O, type Params as P, type QueryPayload as Q, type RawCursorMeta as R, type Sort as S, type SortItem as T, type UrlConfig as U, createFilters as V, type WithFor as W, createRegistry as X, decodeSort as Y, defineListSchema as Z, encodeSort as _, type CursorPayload as a, resetParams as a0, schemaToFilter as a1, searchParamsToPayload as a2, setPage as a3, setParam as a4, setSize as a5, setSort as a6, type InfinitePayload as b, type ListPayload as c, type FieldCondition as d, type FilterNode as e, type SortInput as f, type AndGroup as g, type CursorMeta as h, type CursorResult as i, type FieldDescriptor as j, type FieldKey as k, type FilterOperator as l, type FilterScalar as m, type FilterValue as n, type InfiniteMeta as o, type InfiniteResult as p, type ListResult as q, type ListSchema as r, type MongoosePopulate as s, type PrismaInclude as t, type RawInfiniteMeta as u, type RawMeta as v, type Registry as w, type RegistryConfig as x, type RegistryDefaults as y, type Resource as z };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@querykitjs/web",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Frontend query-building for querykit — typed filters, pagination params & URL sync. Zero-dependency; you send the request.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": {
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"test:smoke": "bun run test/smoke.ts"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@querykitjs/core": "^1.
|
|
53
|
+
"@querykitjs/core": "^1.1.0"
|
|
54
54
|
},
|
|
55
55
|
"peerDependencies": {
|
|
56
56
|
"react": ">=18",
|