@truto/truto-jsonata 1.0.45 → 1.0.47
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.md +75 -0
- package/dist/browser/index.js +891 -451
- package/dist/browser/index.js.map +25 -10
- package/dist/cjs/index.cjs +911 -471
- package/dist/cjs/index.cjs.map +26 -11
- package/dist/esm/index.js +130 -117
- package/dist/esm/index.js.map +8 -7
- package/dist/functions/dtFromFormat.d.ts +2 -2
- package/dist/functions/jsonToCsv.d.ts +1 -0
- package/package.json +2 -1
package/dist/esm/index.js
CHANGED
|
@@ -3,9 +3,9 @@ import jsonata from "jsonata";
|
|
|
3
3
|
|
|
4
4
|
// src/registerJsonataExtensions.ts
|
|
5
5
|
import {
|
|
6
|
-
castArray as
|
|
6
|
+
castArray as castArray6,
|
|
7
7
|
chunk as chunk2,
|
|
8
|
-
compact as
|
|
8
|
+
compact as compact3,
|
|
9
9
|
difference,
|
|
10
10
|
filter as filter2,
|
|
11
11
|
find as find2,
|
|
@@ -2192,6 +2192,96 @@ var convertMarkdownToSlack = (text) => {
|
|
|
2192
2192
|
};
|
|
2193
2193
|
var convertMarkdownToSlack_default = convertMarkdownToSlack;
|
|
2194
2194
|
|
|
2195
|
+
// src/functions/convertMdToPdf.ts
|
|
2196
|
+
import pRetry, { AbortError } from "p-retry";
|
|
2197
|
+
import { includes, split, trim } from "lodash-es";
|
|
2198
|
+
function getFilenameFromHeaders(resp, fallback) {
|
|
2199
|
+
const cd = resp.headers.get("content-disposition") || "";
|
|
2200
|
+
const matchStar = cd.match(/filename\*=([^;]+)/i);
|
|
2201
|
+
if (matchStar) {
|
|
2202
|
+
const value = trim(matchStar[1]);
|
|
2203
|
+
const parts = split(value, "''");
|
|
2204
|
+
if (parts.length === 2) {
|
|
2205
|
+
try {
|
|
2206
|
+
return decodeURIComponent(parts[1]);
|
|
2207
|
+
} catch {
|
|
2208
|
+
return parts[1];
|
|
2209
|
+
}
|
|
2210
|
+
}
|
|
2211
|
+
return trim(value, `'"`);
|
|
2212
|
+
}
|
|
2213
|
+
const match = cd.match(/filename="?([^";]+)"?/i);
|
|
2214
|
+
if (match)
|
|
2215
|
+
return match[1];
|
|
2216
|
+
return fallback || "document.pdf";
|
|
2217
|
+
}
|
|
2218
|
+
async function convertMdToPdf(markdown, options = {}, assetHeaders = {}) {
|
|
2219
|
+
const documentParserApiUrl = this.environment.lookup("documentParserApiUrl");
|
|
2220
|
+
const documentParserApiKey = this.environment.lookup("documentParserApiKey");
|
|
2221
|
+
if (!documentParserApiKey) {
|
|
2222
|
+
throw new Error("Document parser API key not found in environment");
|
|
2223
|
+
}
|
|
2224
|
+
if (!documentParserApiUrl) {
|
|
2225
|
+
throw new Error("Document parser API URL not found in environment");
|
|
2226
|
+
}
|
|
2227
|
+
if (!markdown || typeof markdown !== "string") {
|
|
2228
|
+
throw new AbortError("Markdown content is required and must be a string");
|
|
2229
|
+
}
|
|
2230
|
+
const requestBody = {
|
|
2231
|
+
markdown,
|
|
2232
|
+
options,
|
|
2233
|
+
assetHeaders
|
|
2234
|
+
};
|
|
2235
|
+
return await pRetry(async () => {
|
|
2236
|
+
const response = await fetch(`${documentParserApiUrl}/md-to-pdf`, {
|
|
2237
|
+
method: "POST",
|
|
2238
|
+
headers: {
|
|
2239
|
+
Authorization: `Bearer ${documentParserApiKey}`,
|
|
2240
|
+
"Content-Type": "application/json",
|
|
2241
|
+
Accept: "application/pdf",
|
|
2242
|
+
"User-Agent": "truto"
|
|
2243
|
+
},
|
|
2244
|
+
body: JSON.stringify(requestBody)
|
|
2245
|
+
});
|
|
2246
|
+
if (!response.ok) {
|
|
2247
|
+
if (response.status === 429) {
|
|
2248
|
+
throw new Error("Rate limit exceeded");
|
|
2249
|
+
}
|
|
2250
|
+
if (response.status >= 500) {
|
|
2251
|
+
throw new Error(`Server error: ${response.status}`);
|
|
2252
|
+
}
|
|
2253
|
+
const errorText = await response.text();
|
|
2254
|
+
let errorMessage = `PDF conversion failed (${response.status})`;
|
|
2255
|
+
try {
|
|
2256
|
+
const errorData = JSON.parse(errorText);
|
|
2257
|
+
errorMessage = errorData.error || errorMessage;
|
|
2258
|
+
if (errorData.details) {
|
|
2259
|
+
errorMessage += `: ${errorData.details}`;
|
|
2260
|
+
}
|
|
2261
|
+
} catch {
|
|
2262
|
+
errorMessage += `: ${errorText}`;
|
|
2263
|
+
}
|
|
2264
|
+
throw new AbortError(errorMessage);
|
|
2265
|
+
}
|
|
2266
|
+
const contentType = response.headers.get("content-type");
|
|
2267
|
+
if (!contentType || !includes(contentType, "application/pdf")) {
|
|
2268
|
+
throw new AbortError(`Expected PDF but received: ${contentType}`);
|
|
2269
|
+
}
|
|
2270
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
2271
|
+
const filename = getFilenameFromHeaders(response, options.filename);
|
|
2272
|
+
const blob2 = new Blob([arrayBuffer], { type: "application/pdf" });
|
|
2273
|
+
blob2.name = filename;
|
|
2274
|
+
return blob2;
|
|
2275
|
+
}, {
|
|
2276
|
+
retries: 5,
|
|
2277
|
+
maxTimeout: 30000,
|
|
2278
|
+
minTimeout: 2500,
|
|
2279
|
+
onFailedAttempt: (error) => {
|
|
2280
|
+
console.warn(`PDF conversion attempt ${error.attemptNumber} failed:`, error.message);
|
|
2281
|
+
}
|
|
2282
|
+
});
|
|
2283
|
+
}
|
|
2284
|
+
|
|
2195
2285
|
// src/functions/convertNotionToMarkdown.ts
|
|
2196
2286
|
import { castArray as castArray3, flatten, join as join2, map as map3, reject, repeat } from "lodash-es";
|
|
2197
2287
|
var formatPlainText = (x) => {
|
|
@@ -2691,17 +2781,17 @@ var digest_default = digest;
|
|
|
2691
2781
|
|
|
2692
2782
|
// src/functions/dtFromFormat.ts
|
|
2693
2783
|
import { DateTime as DateTime2 } from "luxon";
|
|
2694
|
-
function dtFromFormat(date, format) {
|
|
2784
|
+
function dtFromFormat(date, format, options) {
|
|
2695
2785
|
if (format === "RFC2822") {
|
|
2696
|
-
return DateTime2.fromRFC2822(date);
|
|
2786
|
+
return DateTime2.fromRFC2822(date, options);
|
|
2697
2787
|
}
|
|
2698
2788
|
if (format === "ISO") {
|
|
2699
|
-
return DateTime2.fromISO(date);
|
|
2789
|
+
return DateTime2.fromISO(date, options);
|
|
2700
2790
|
}
|
|
2701
2791
|
if (format === "fromSeconds") {
|
|
2702
|
-
return DateTime2.fromSeconds(parseInt(date));
|
|
2792
|
+
return DateTime2.fromSeconds(parseInt(date), options);
|
|
2703
2793
|
}
|
|
2704
|
-
return DateTime2.fromFormat(date, format);
|
|
2794
|
+
return DateTime2.fromFormat(date, format, options);
|
|
2705
2795
|
}
|
|
2706
2796
|
var dtFromFormat_default = dtFromFormat;
|
|
2707
2797
|
|
|
@@ -2725,12 +2815,12 @@ var firstNonEmpty_default = firstNonEmpty;
|
|
|
2725
2815
|
// src/functions/generateEmbeddingsCohere.ts
|
|
2726
2816
|
import { castArray as castArray4, chunk, isEmpty as isEmpty4 } from "lodash-es";
|
|
2727
2817
|
import pMap from "p-map";
|
|
2728
|
-
import
|
|
2818
|
+
import pRetry2, { AbortError as AbortError2 } from "p-retry";
|
|
2729
2819
|
async function generateEmbeddingsCohere(body, api_key) {
|
|
2730
2820
|
if (!isEmpty4(body.texts)) {
|
|
2731
2821
|
const chunks = chunk(castArray4(body.texts), 20);
|
|
2732
2822
|
return await pMap(chunks, async (chunk2) => {
|
|
2733
|
-
return await
|
|
2823
|
+
return await pRetry2(async () => {
|
|
2734
2824
|
const response = await fetch("https://api.cohere.com/v2/embed", {
|
|
2735
2825
|
method: "POST",
|
|
2736
2826
|
headers: {
|
|
@@ -2748,7 +2838,7 @@ async function generateEmbeddingsCohere(body, api_key) {
|
|
|
2748
2838
|
if (response.status >= 500) {
|
|
2749
2839
|
throw new Error("Server error");
|
|
2750
2840
|
}
|
|
2751
|
-
throw new
|
|
2841
|
+
throw new AbortError2(await response.text());
|
|
2752
2842
|
}
|
|
2753
2843
|
return await response.json();
|
|
2754
2844
|
}, { retries: 5, maxTimeout: 5000, minTimeout: 2500 });
|
|
@@ -2756,7 +2846,7 @@ async function generateEmbeddingsCohere(body, api_key) {
|
|
|
2756
2846
|
concurrency: 1
|
|
2757
2847
|
});
|
|
2758
2848
|
} else if (!isEmpty4(body.images)) {
|
|
2759
|
-
return await
|
|
2849
|
+
return await pRetry2(async () => {
|
|
2760
2850
|
const response = await fetch("https://api.cohere.com/v2/embed", {
|
|
2761
2851
|
method: "POST",
|
|
2762
2852
|
headers: {
|
|
@@ -2774,7 +2864,7 @@ async function generateEmbeddingsCohere(body, api_key) {
|
|
|
2774
2864
|
if (response.status >= 500) {
|
|
2775
2865
|
throw new Error("Server error");
|
|
2776
2866
|
}
|
|
2777
|
-
throw new
|
|
2867
|
+
throw new AbortError2(await response.text());
|
|
2778
2868
|
}
|
|
2779
2869
|
return await response.json();
|
|
2780
2870
|
}, { retries: 5, maxTimeout: 5000, minTimeout: 2500 });
|
|
@@ -2831,6 +2921,18 @@ function jsonParse(str) {
|
|
|
2831
2921
|
}
|
|
2832
2922
|
var jsonParse_default = jsonParse;
|
|
2833
2923
|
|
|
2924
|
+
// src/functions/jsonToCsv.ts
|
|
2925
|
+
import { Parser } from "@json2csv/plainjs";
|
|
2926
|
+
import { castArray as castArray5, compact as compact2, isEmpty as isEmpty5 } from "lodash-es";
|
|
2927
|
+
function jsonToCsv(json, options) {
|
|
2928
|
+
const jsonArray = compact2(castArray5(json));
|
|
2929
|
+
if (isEmpty5(jsonArray)) {
|
|
2930
|
+
return "";
|
|
2931
|
+
}
|
|
2932
|
+
const parser = new Parser(options);
|
|
2933
|
+
return parser.parse(jsonArray);
|
|
2934
|
+
}
|
|
2935
|
+
|
|
2834
2936
|
// src/functions/jsToXml.ts
|
|
2835
2937
|
import { js2xml } from "xml-js";
|
|
2836
2938
|
function jsToXml_default(json, options = { compact: true, spaces: 4 }) {
|
|
@@ -2858,14 +2960,14 @@ function mostSimilar(value, possibleValues, threshold = 0.8) {
|
|
|
2858
2960
|
var mostSimilar_default = mostSimilar;
|
|
2859
2961
|
|
|
2860
2962
|
// src/functions/parseDocument.ts
|
|
2861
|
-
import
|
|
2963
|
+
import pRetry3, { AbortError as AbortError3 } from "p-retry";
|
|
2862
2964
|
async function parseDocument(file, fileType) {
|
|
2863
2965
|
const documentParserApiUrl = this.environment.lookup("documentParserApiUrl");
|
|
2864
2966
|
const documentParserApiKey = this.environment.lookup("documentParserApiKey");
|
|
2865
2967
|
if (!documentParserApiKey) {
|
|
2866
2968
|
throw new Error("API key not found in environment");
|
|
2867
2969
|
}
|
|
2868
|
-
return await
|
|
2970
|
+
return await pRetry3(async () => {
|
|
2869
2971
|
const response = await fetch(`${documentParserApiUrl}/parse`, {
|
|
2870
2972
|
method: "POST",
|
|
2871
2973
|
headers: {
|
|
@@ -2883,7 +2985,7 @@ async function parseDocument(file, fileType) {
|
|
|
2883
2985
|
if (response.status >= 500) {
|
|
2884
2986
|
throw new Error("Server error");
|
|
2885
2987
|
}
|
|
2886
|
-
throw new
|
|
2988
|
+
throw new AbortError3(await response.text());
|
|
2887
2989
|
}
|
|
2888
2990
|
const data = await response.json();
|
|
2889
2991
|
return data.content;
|
|
@@ -3068,96 +3170,6 @@ function zipSqlResponse(columns, data, key) {
|
|
|
3068
3170
|
}
|
|
3069
3171
|
var zipSqlResponse_default = zipSqlResponse;
|
|
3070
3172
|
|
|
3071
|
-
// src/functions/convertMdToPdf.ts
|
|
3072
|
-
import pRetry3, { AbortError as AbortError3 } from "p-retry";
|
|
3073
|
-
import { includes, split, trim } from "lodash-es";
|
|
3074
|
-
function getFilenameFromHeaders(resp, fallback) {
|
|
3075
|
-
const cd = resp.headers.get("content-disposition") || "";
|
|
3076
|
-
const matchStar = cd.match(/filename\*=([^;]+)/i);
|
|
3077
|
-
if (matchStar) {
|
|
3078
|
-
const value = trim(matchStar[1]);
|
|
3079
|
-
const parts = split(value, "''");
|
|
3080
|
-
if (parts.length === 2) {
|
|
3081
|
-
try {
|
|
3082
|
-
return decodeURIComponent(parts[1]);
|
|
3083
|
-
} catch {
|
|
3084
|
-
return parts[1];
|
|
3085
|
-
}
|
|
3086
|
-
}
|
|
3087
|
-
return trim(value, `'"`);
|
|
3088
|
-
}
|
|
3089
|
-
const match = cd.match(/filename="?([^";]+)"?/i);
|
|
3090
|
-
if (match)
|
|
3091
|
-
return match[1];
|
|
3092
|
-
return fallback || "document.pdf";
|
|
3093
|
-
}
|
|
3094
|
-
async function convertMdToPdf(markdown, options = {}, assetHeaders = {}) {
|
|
3095
|
-
const documentParserApiUrl = this.environment.lookup("documentParserApiUrl");
|
|
3096
|
-
const documentParserApiKey = this.environment.lookup("documentParserApiKey");
|
|
3097
|
-
if (!documentParserApiKey) {
|
|
3098
|
-
throw new Error("Document parser API key not found in environment");
|
|
3099
|
-
}
|
|
3100
|
-
if (!documentParserApiUrl) {
|
|
3101
|
-
throw new Error("Document parser API URL not found in environment");
|
|
3102
|
-
}
|
|
3103
|
-
if (!markdown || typeof markdown !== "string") {
|
|
3104
|
-
throw new AbortError3("Markdown content is required and must be a string");
|
|
3105
|
-
}
|
|
3106
|
-
const requestBody = {
|
|
3107
|
-
markdown,
|
|
3108
|
-
options,
|
|
3109
|
-
assetHeaders
|
|
3110
|
-
};
|
|
3111
|
-
return await pRetry3(async () => {
|
|
3112
|
-
const response = await fetch(`${documentParserApiUrl}/md-to-pdf`, {
|
|
3113
|
-
method: "POST",
|
|
3114
|
-
headers: {
|
|
3115
|
-
Authorization: `Bearer ${documentParserApiKey}`,
|
|
3116
|
-
"Content-Type": "application/json",
|
|
3117
|
-
Accept: "application/pdf",
|
|
3118
|
-
"User-Agent": "truto"
|
|
3119
|
-
},
|
|
3120
|
-
body: JSON.stringify(requestBody)
|
|
3121
|
-
});
|
|
3122
|
-
if (!response.ok) {
|
|
3123
|
-
if (response.status === 429) {
|
|
3124
|
-
throw new Error("Rate limit exceeded");
|
|
3125
|
-
}
|
|
3126
|
-
if (response.status >= 500) {
|
|
3127
|
-
throw new Error(`Server error: ${response.status}`);
|
|
3128
|
-
}
|
|
3129
|
-
const errorText = await response.text();
|
|
3130
|
-
let errorMessage = `PDF conversion failed (${response.status})`;
|
|
3131
|
-
try {
|
|
3132
|
-
const errorData = JSON.parse(errorText);
|
|
3133
|
-
errorMessage = errorData.error || errorMessage;
|
|
3134
|
-
if (errorData.details) {
|
|
3135
|
-
errorMessage += `: ${errorData.details}`;
|
|
3136
|
-
}
|
|
3137
|
-
} catch {
|
|
3138
|
-
errorMessage += `: ${errorText}`;
|
|
3139
|
-
}
|
|
3140
|
-
throw new AbortError3(errorMessage);
|
|
3141
|
-
}
|
|
3142
|
-
const contentType = response.headers.get("content-type");
|
|
3143
|
-
if (!contentType || !includes(contentType, "application/pdf")) {
|
|
3144
|
-
throw new AbortError3(`Expected PDF but received: ${contentType}`);
|
|
3145
|
-
}
|
|
3146
|
-
const arrayBuffer = await response.arrayBuffer();
|
|
3147
|
-
const filename = getFilenameFromHeaders(response, options.filename);
|
|
3148
|
-
const blob2 = new Blob([arrayBuffer], { type: "application/pdf" });
|
|
3149
|
-
blob2.name = filename;
|
|
3150
|
-
return blob2;
|
|
3151
|
-
}, {
|
|
3152
|
-
retries: 5,
|
|
3153
|
-
maxTimeout: 30000,
|
|
3154
|
-
minTimeout: 2500,
|
|
3155
|
-
onFailedAttempt: (error) => {
|
|
3156
|
-
console.warn(`PDF conversion attempt ${error.attemptNumber} failed:`, error.message);
|
|
3157
|
-
}
|
|
3158
|
-
});
|
|
3159
|
-
}
|
|
3160
|
-
|
|
3161
3173
|
// src/registerJsonataExtensions.ts
|
|
3162
3174
|
function registerJsonataExtensions(expression) {
|
|
3163
3175
|
expression.registerFunction("dtFromIso", dtFromIso_default);
|
|
@@ -3200,10 +3212,10 @@ function registerJsonataExtensions(expression) {
|
|
|
3200
3212
|
expression.registerFunction("jsToXml", jsToXml_default);
|
|
3201
3213
|
expression.registerFunction("generateEmbeddingsCohere", generateEmbeddingsCohere_default);
|
|
3202
3214
|
expression.registerFunction("groupBy", function(array, key) {
|
|
3203
|
-
return groupBy3(
|
|
3215
|
+
return groupBy3(castArray6(array), key);
|
|
3204
3216
|
});
|
|
3205
3217
|
expression.registerFunction("keyBy", function(array, key) {
|
|
3206
|
-
return keyBy(
|
|
3218
|
+
return keyBy(castArray6(array), key);
|
|
3207
3219
|
});
|
|
3208
3220
|
expression.registerFunction("pick", function(obj, keys) {
|
|
3209
3221
|
return pick(obj, keys);
|
|
@@ -3212,25 +3224,25 @@ function registerJsonataExtensions(expression) {
|
|
|
3212
3224
|
return omit2(obj, keys);
|
|
3213
3225
|
});
|
|
3214
3226
|
expression.registerFunction("compact", function(arr) {
|
|
3215
|
-
return
|
|
3227
|
+
return compact3(castArray6(arr));
|
|
3216
3228
|
});
|
|
3217
3229
|
expression.registerFunction("join", function(arr, separator) {
|
|
3218
|
-
return join3(
|
|
3230
|
+
return join3(castArray6(arr), separator);
|
|
3219
3231
|
});
|
|
3220
3232
|
expression.registerFunction("orderBy", function(arr, attr, order) {
|
|
3221
|
-
return orderBy(
|
|
3233
|
+
return orderBy(castArray6(arr), attr, order);
|
|
3222
3234
|
});
|
|
3223
3235
|
expression.registerFunction("find", function(arr, attr) {
|
|
3224
|
-
return find2(
|
|
3236
|
+
return find2(castArray6(arr), attr);
|
|
3225
3237
|
});
|
|
3226
3238
|
expression.registerFunction("lofilter", function(arr, attr) {
|
|
3227
|
-
return filter2(
|
|
3239
|
+
return filter2(castArray6(arr), attr);
|
|
3228
3240
|
});
|
|
3229
3241
|
expression.registerFunction("values", function(obj) {
|
|
3230
3242
|
return values(obj);
|
|
3231
3243
|
});
|
|
3232
3244
|
expression.registerFunction("chunk", function(arr, size) {
|
|
3233
|
-
return chunk2(
|
|
3245
|
+
return chunk2(castArray6(arr), size);
|
|
3234
3246
|
});
|
|
3235
3247
|
expression.registerFunction("wrap", function(value, wrapper, endWrapper) {
|
|
3236
3248
|
return join3([wrapper, value, endWrapper || wrapper], "");
|
|
@@ -3243,14 +3255,15 @@ function registerJsonataExtensions(expression) {
|
|
|
3243
3255
|
expression.registerFunction("parseQuery", parseQuery);
|
|
3244
3256
|
expression.registerFunction("stringifyQuery", stringifyQuery);
|
|
3245
3257
|
expression.registerFunction("flatten", function(arr) {
|
|
3246
|
-
return flatten2(
|
|
3258
|
+
return flatten2(castArray6(arr));
|
|
3247
3259
|
});
|
|
3248
3260
|
expression.registerFunction("flattenDeep", function(arr) {
|
|
3249
|
-
return flattenDeep3(
|
|
3261
|
+
return flattenDeep3(castArray6(arr));
|
|
3250
3262
|
});
|
|
3251
3263
|
expression.registerFunction("flattenDepth", function(arr, depth) {
|
|
3252
|
-
return flattenDepth(
|
|
3264
|
+
return flattenDepth(castArray6(arr), depth);
|
|
3253
3265
|
});
|
|
3266
|
+
expression.registerFunction("jsonToCsv", jsonToCsv);
|
|
3254
3267
|
return expression;
|
|
3255
3268
|
}
|
|
3256
3269
|
|
|
@@ -3262,4 +3275,4 @@ export {
|
|
|
3262
3275
|
trutoJsonata as default
|
|
3263
3276
|
};
|
|
3264
3277
|
|
|
3265
|
-
//# debugId=
|
|
3278
|
+
//# debugId=E5071DB9A293C3B964756E2164756E21
|