@lokalise/api-common
Advanced tools
Comparing version 3.0.1 to 3.1.0
@@ -67,8 +67,11 @@ import z from 'zod'; | ||
cursor: z.ZodOptional<z.ZodString>; | ||
hasMore: z.ZodOptional<z.ZodBoolean>; | ||
}, "strip", z.ZodTypeAny, { | ||
count: number; | ||
cursor?: string | undefined; | ||
hasMore?: boolean | undefined; | ||
}, { | ||
count: number; | ||
cursor?: string | undefined; | ||
hasMore?: boolean | undefined; | ||
}>; | ||
@@ -75,0 +78,0 @@ export type PaginationMeta = z.infer<typeof zMeta>; |
@@ -1,13 +0,13 @@ | ||
import t from "zod"; | ||
import o from "zod"; | ||
import { decodeCursor as s } from "./cursorCodec.js"; | ||
const i = t.object({ | ||
limit: t.coerce.number().gt(0), | ||
before: t.string().min(1).optional(), | ||
after: t.string().min(1).optional() | ||
const i = o.object({ | ||
limit: o.coerce.number().gt(0), | ||
before: o.string().min(1).optional(), | ||
after: o.string().min(1).optional() | ||
}), u = i.partial({ | ||
limit: !0 | ||
}), n = (o, r) => { | ||
if (!o) | ||
}), n = (t, r) => { | ||
if (!t) | ||
return; | ||
const e = s(o); | ||
const e = s(t); | ||
if (e.result) | ||
@@ -17,22 +17,23 @@ return e.result; | ||
message: "Invalid cursor", | ||
code: t.ZodIssueCode.custom, | ||
code: o.ZodIssueCode.custom, | ||
params: { message: e.error.message } | ||
}); | ||
}, a = (o) => { | ||
const r = t.string().transform(n).pipe(o.optional()).optional(); | ||
return t.object({ | ||
limit: t.coerce.number().gt(0), | ||
}, a = (t) => { | ||
const r = o.string().transform(n).pipe(t.optional()).optional(); | ||
return o.object({ | ||
limit: o.coerce.number().gt(0), | ||
before: r, | ||
after: r | ||
}); | ||
}, l = (o) => a(o).partial({ limit: !0 }), d = t.object({ | ||
count: t.number(), | ||
cursor: t.string().optional().describe("Pagination cursor, a last item id from this result set") | ||
}), g = t.object({ | ||
message: t.string(), | ||
errorCode: t.string(), | ||
details: t.any().optional() | ||
}, l = (t) => a(t).partial({ limit: !0 }), d = o.object({ | ||
count: o.number(), | ||
cursor: o.string().optional().describe("Pagination cursor, a last item id from this result set"), | ||
hasMore: o.boolean().optional().describe("Whether there are more items to fetch") | ||
}), p = o.object({ | ||
message: o.string(), | ||
errorCode: o.string(), | ||
details: o.any().optional() | ||
}); | ||
export { | ||
g as COMMON_ERROR_RESPONSE_SCHEMA, | ||
p as COMMON_ERROR_RESPONSE_SCHEMA, | ||
i as MANDATORY_PAGINATION_CONFIG_SCHEMA, | ||
@@ -39,0 +40,0 @@ u as OPTIONAL_PAGINATION_CONFIG_SCHEMA, |
@@ -1,2 +0,2 @@ | ||
import type { PaginationMeta, OptionalPaginationParams } from './apiSchemas'; | ||
import type { OptionalPaginationParams, PaginationMeta } from './apiSchemas'; | ||
/** | ||
@@ -15,10 +15,15 @@ * Constructs a PaginationMeta object encapsulating the total count and the cursor for fetching the next page. | ||
* the 'id' property. | ||
* @param pageLimit - An optional number that can be used to specify the expected count of items in the current page. | ||
* - If it is provided and currentPageData length is less than or equal to pageLimit, it means that there are no more items to fetch. | ||
* In that case, hasMore flag will be set to false. Otherwise, hasMore flag will be set to true. | ||
* If currentPageData length is greater than pageLimit, count will be set to pageLimit. | ||
* - If the parameter is not provided, hasMore flag will be undefined. | ||
* | ||
* @returns PaginationMeta - An object detailing two crucial properties required for effective pagination: total item | ||
* count and the cursor. | ||
* count, the cursor and has more flag. | ||
*/ | ||
export declare function getMetaForNextPage<T extends { | ||
id: string; | ||
}>(currentPageData: T[], cursorKeys?: undefined): PaginationMeta; | ||
export declare function getMetaForNextPage<T extends Record<string, unknown>, K extends keyof T>(currentPageData: T[], cursorKeys: K[]): PaginationMeta; | ||
}>(currentPageData: T[], cursorKeys?: undefined, pageLimit?: number): PaginationMeta; | ||
export declare function getMetaForNextPage<T extends Record<string, unknown>, K extends keyof T>(currentPageData: T[], cursorKeys: K[], pageLimit?: number): PaginationMeta; | ||
type PaginatedResponse<T> = { | ||
@@ -25,0 +30,0 @@ data: T[]; |
@@ -1,10 +0,10 @@ | ||
import { encodeCursor as o } from "./cursorCodec.js"; | ||
import { encodeCursor as i } from "./cursorCodec.js"; | ||
const r = (e, t) => { | ||
const i = {}; | ||
const o = {}; | ||
let n = 0; | ||
for (; n < t.length; ) | ||
t[n] in e && (i[t[n]] = e[t[n]]), n += 1; | ||
return i; | ||
t[n] in e && (o[t[n]] = e[t[n]]), n += 1; | ||
return o; | ||
}; | ||
function u(e, t) { | ||
function f(e, t, o) { | ||
if (t !== void 0 && t.length === 0) | ||
@@ -14,22 +14,25 @@ throw new Error("cursorKeys cannot be an empty array"); | ||
return { count: 0 }; | ||
const i = e[e.length - 1]; | ||
let n = ""; | ||
return t ? n = t.length === 1 ? i[t[0]] : o(r(i, t)) : n = i.id, { | ||
count: e.length, | ||
cursor: n | ||
const n = e[e.length - 1]; | ||
let l = ""; | ||
t ? l = t.length === 1 ? n[t[0]] : i(r(n, t)) : l = n.id; | ||
const h = o ? e.length > o : void 0; | ||
return { | ||
count: o ? Math.min(e.length, o) : e.length, | ||
cursor: l, | ||
hasMore: h | ||
}; | ||
} | ||
async function c(e, t) { | ||
const i = []; | ||
async function s(e, t) { | ||
const o = []; | ||
let n; | ||
do { | ||
const l = await t({ ...e, after: n }); | ||
i.push(...l.data), n = l.meta.cursor; | ||
o.push(...l.data), n = l.meta.cursor; | ||
} while (n); | ||
return i; | ||
return o; | ||
} | ||
export { | ||
u as getMetaForNextPage, | ||
c as getPaginatedEntries | ||
f as getMetaForNextPage, | ||
s as getPaginatedEntries | ||
}; | ||
//# sourceMappingURL=paginationUtils.js.map |
{ | ||
"name": "@lokalise/api-common", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"files": [ | ||
@@ -42,7 +42,7 @@ "dist" | ||
"rimraf": "^5.0.1", | ||
"typescript": "5.3.3", | ||
"typescript": "5.4.2", | ||
"vite": "^5.0.11", | ||
"vitest": "^1.2.2" | ||
"vitest": "^1.4.0" | ||
}, | ||
"prettier": "@lokalise/prettier-config" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
39713
284
0