vintrace-sdk 0.1.0 → 0.1.2
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/dist/index.cjs +140 -45
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +140 -45
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -289,6 +289,52 @@ async function vintraceFetch(baseUrl, organization, token, endpoint, method, opt
|
|
|
289
289
|
}
|
|
290
290
|
}
|
|
291
291
|
|
|
292
|
+
// src/http/pagination.ts
|
|
293
|
+
async function* paginate(fetchFn, options = {}) {
|
|
294
|
+
const limit = options.limit ?? 100;
|
|
295
|
+
let offset = 0;
|
|
296
|
+
let hasMore = true;
|
|
297
|
+
while (hasMore) {
|
|
298
|
+
const [response, error] = await fetchFn(offset, limit);
|
|
299
|
+
if (error) {
|
|
300
|
+
throw error;
|
|
301
|
+
}
|
|
302
|
+
if (response === null) {
|
|
303
|
+
break;
|
|
304
|
+
}
|
|
305
|
+
for (const item of response.results) {
|
|
306
|
+
yield item;
|
|
307
|
+
}
|
|
308
|
+
offset += limit;
|
|
309
|
+
hasMore = response.next !== null;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
async function batchGet(ids, fetchFn, options = {}) {
|
|
313
|
+
const concurrency = options.concurrency ?? 5;
|
|
314
|
+
const errors = [];
|
|
315
|
+
const results = [];
|
|
316
|
+
for (let i = 0; i < ids.length; i += concurrency) {
|
|
317
|
+
const batch = ids.slice(i, i + concurrency);
|
|
318
|
+
const batchResults = await Promise.allSettled(batch.map((id) => fetchFn(id)));
|
|
319
|
+
for (const result of batchResults) {
|
|
320
|
+
if (result.status === "fulfilled") {
|
|
321
|
+
const [data, error] = result.value;
|
|
322
|
+
if (error) {
|
|
323
|
+
errors.push(error);
|
|
324
|
+
} else if (data !== null) {
|
|
325
|
+
results.push(data);
|
|
326
|
+
}
|
|
327
|
+
} else {
|
|
328
|
+
errors.push(new VintraceError(result.reason?.message ?? "Unknown error", 0));
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
if (errors.length > 0) {
|
|
333
|
+
return [null, new VintraceAggregateError(errors)];
|
|
334
|
+
}
|
|
335
|
+
return [results, null];
|
|
336
|
+
}
|
|
337
|
+
|
|
292
338
|
// src/client/utils.ts
|
|
293
339
|
async function batchFetch(ids, fetchFn) {
|
|
294
340
|
const results = await Promise.allSettled(ids.map((id) => fetchFn(id)));
|
|
@@ -1967,13 +2013,75 @@ var VesselDetailsReportClient = class {
|
|
|
1967
2013
|
*
|
|
1968
2014
|
* Returns a vessel details report based on the provided parameters.
|
|
1969
2015
|
*/
|
|
1970
|
-
get(params) {
|
|
1971
|
-
|
|
2016
|
+
async get(params) {
|
|
2017
|
+
const baseOffset = params?.offset ?? 0;
|
|
2018
|
+
const limit = params?.limit ?? 100;
|
|
2019
|
+
const firstResponse = await this.client.request(
|
|
1972
2020
|
"v7/report/vessel-details-report",
|
|
1973
2021
|
"GET",
|
|
1974
2022
|
{ responseSchema: GetBulkWineDetailsReportResponseSchema },
|
|
1975
|
-
params
|
|
2023
|
+
{ ...params, limit: String(limit), offset: String(baseOffset) }
|
|
1976
2024
|
);
|
|
2025
|
+
if (firstResponse[1]) {
|
|
2026
|
+
return [null, firstResponse[1]];
|
|
2027
|
+
}
|
|
2028
|
+
const response = firstResponse[0];
|
|
2029
|
+
if (!response) {
|
|
2030
|
+
return [null, null];
|
|
2031
|
+
}
|
|
2032
|
+
const totalCount = response.totalResults ?? response.results?.length ?? 0;
|
|
2033
|
+
if (totalCount <= limit) {
|
|
2034
|
+
return [response, null];
|
|
2035
|
+
}
|
|
2036
|
+
const allResults = [...response.results ?? []];
|
|
2037
|
+
const pageFetcher = async (relativeOffset, pageLimit) => {
|
|
2038
|
+
const [pageData, pageError] = await this.client.request(
|
|
2039
|
+
"v7/report/vessel-details-report",
|
|
2040
|
+
"GET",
|
|
2041
|
+
{ responseSchema: GetBulkWineDetailsReportResponseSchema },
|
|
2042
|
+
{
|
|
2043
|
+
...params,
|
|
2044
|
+
limit: String(pageLimit),
|
|
2045
|
+
offset: String(baseOffset + limit + relativeOffset)
|
|
2046
|
+
}
|
|
2047
|
+
);
|
|
2048
|
+
if (pageError) {
|
|
2049
|
+
return [null, pageError];
|
|
2050
|
+
}
|
|
2051
|
+
if (!pageData) {
|
|
2052
|
+
return [null, null];
|
|
2053
|
+
}
|
|
2054
|
+
return [
|
|
2055
|
+
{
|
|
2056
|
+
totalResults: pageData.totalResults ?? 0,
|
|
2057
|
+
offset: pageData.offset ?? baseOffset + limit + relativeOffset,
|
|
2058
|
+
limit: pageData.limit ?? pageLimit,
|
|
2059
|
+
first: pageData.first ?? null,
|
|
2060
|
+
previous: pageData.previous ?? null,
|
|
2061
|
+
next: pageData.next ?? null,
|
|
2062
|
+
last: pageData.last ?? null,
|
|
2063
|
+
results: pageData.results ?? []
|
|
2064
|
+
},
|
|
2065
|
+
null
|
|
2066
|
+
];
|
|
2067
|
+
};
|
|
2068
|
+
try {
|
|
2069
|
+
for await (const item of paginate(pageFetcher, { limit })) {
|
|
2070
|
+
allResults.push(item);
|
|
2071
|
+
}
|
|
2072
|
+
} catch (error) {
|
|
2073
|
+
if (error instanceof VintraceError) {
|
|
2074
|
+
return [null, error];
|
|
2075
|
+
}
|
|
2076
|
+
return [null, new VintraceError(error instanceof Error ? error.message : "Unknown error", 0)];
|
|
2077
|
+
}
|
|
2078
|
+
return [
|
|
2079
|
+
{
|
|
2080
|
+
...response,
|
|
2081
|
+
results: allResults
|
|
2082
|
+
},
|
|
2083
|
+
null
|
|
2084
|
+
];
|
|
1977
2085
|
}
|
|
1978
2086
|
};
|
|
1979
2087
|
var CostsClient = class {
|
|
@@ -3032,53 +3140,40 @@ var ProductJobsClient = class {
|
|
|
3032
3140
|
responseSchema: ProductJobResponseSchema
|
|
3033
3141
|
});
|
|
3034
3142
|
}
|
|
3035
|
-
|
|
3036
|
-
|
|
3037
|
-
|
|
3038
|
-
|
|
3039
|
-
|
|
3040
|
-
|
|
3041
|
-
|
|
3042
|
-
while (hasMore) {
|
|
3043
|
-
const [response, error] = await fetchFn(offset, limit);
|
|
3044
|
-
if (error) {
|
|
3045
|
-
throw error;
|
|
3046
|
-
}
|
|
3047
|
-
if (response === null) {
|
|
3048
|
-
break;
|
|
3049
|
-
}
|
|
3050
|
-
for (const item of response.results) {
|
|
3051
|
-
yield item;
|
|
3052
|
-
}
|
|
3053
|
-
offset += limit;
|
|
3054
|
-
hasMore = response.next !== null;
|
|
3143
|
+
/**
|
|
3144
|
+
* Get job history for multiple products by their numeric IDs.
|
|
3145
|
+
* Respects the client's `parallelLimit` option (default 5 concurrent).
|
|
3146
|
+
* Returns `VintraceAggregateError` if any individual request fails.
|
|
3147
|
+
*/
|
|
3148
|
+
getAll(productIds) {
|
|
3149
|
+
return this.batchGet(productIds, (id) => this.get(id));
|
|
3055
3150
|
}
|
|
3056
|
-
|
|
3057
|
-
|
|
3058
|
-
|
|
3059
|
-
|
|
3060
|
-
|
|
3061
|
-
|
|
3062
|
-
|
|
3063
|
-
|
|
3064
|
-
|
|
3065
|
-
|
|
3066
|
-
|
|
3067
|
-
|
|
3068
|
-
|
|
3069
|
-
|
|
3070
|
-
|
|
3151
|
+
async batchGet(ids, fetchFn) {
|
|
3152
|
+
const limit = this.client.options.parallelLimit;
|
|
3153
|
+
const errors = [];
|
|
3154
|
+
const data = [];
|
|
3155
|
+
for (let i = 0; i < ids.length; i += limit) {
|
|
3156
|
+
const chunk = ids.slice(i, i + limit);
|
|
3157
|
+
const results = await Promise.allSettled(chunk.map((id) => fetchFn(id)));
|
|
3158
|
+
for (const result of results) {
|
|
3159
|
+
if (result.status === "fulfilled") {
|
|
3160
|
+
const [item, error] = result.value;
|
|
3161
|
+
if (error) {
|
|
3162
|
+
errors.push(error);
|
|
3163
|
+
} else if (item !== null) {
|
|
3164
|
+
data.push(item);
|
|
3165
|
+
}
|
|
3166
|
+
} else {
|
|
3167
|
+
errors.push(new VintraceError(result.reason?.message ?? "Unknown error", 0));
|
|
3071
3168
|
}
|
|
3072
|
-
} else {
|
|
3073
|
-
errors.push(new VintraceError(result.reason?.message ?? "Unknown error", 0));
|
|
3074
3169
|
}
|
|
3075
3170
|
}
|
|
3171
|
+
if (errors.length > 0) {
|
|
3172
|
+
return [null, new VintraceAggregateError(errors)];
|
|
3173
|
+
}
|
|
3174
|
+
return [data, null];
|
|
3076
3175
|
}
|
|
3077
|
-
|
|
3078
|
-
return [null, new VintraceAggregateError(errors)];
|
|
3079
|
-
}
|
|
3080
|
-
return [results, null];
|
|
3081
|
-
}
|
|
3176
|
+
};
|
|
3082
3177
|
|
|
3083
3178
|
exports.DEFAULT_OPTIONS = DEFAULT_OPTIONS;
|
|
3084
3179
|
exports.VintraceAggregateError = VintraceAggregateError;
|