erpnext-queue-client 1.0.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.
Files changed (73) hide show
  1. package/dist/client.d.ts +25 -0
  2. package/dist/client.js +141 -0
  3. package/dist/constants.d.ts +6 -0
  4. package/dist/constants.js +26 -0
  5. package/dist/erpnext/decryptFromErpNext.server.d.ts +2 -0
  6. package/dist/erpnext/decryptFromErpNext.server.js +17 -0
  7. package/dist/erpnext/decryptFromErpNext.server.test.d.ts +1 -0
  8. package/dist/erpnext/decryptFromErpNext.server.test.js +30 -0
  9. package/dist/erpnext/erpnextRequestWrapper.d.ts +5 -0
  10. package/dist/erpnext/erpnextRequestWrapper.js +91 -0
  11. package/dist/erpnext/erpnextRequests.d.ts +1786 -0
  12. package/dist/erpnext/erpnextRequests.js +339 -0
  13. package/dist/erpnext/model/Address.d.ts +349 -0
  14. package/dist/erpnext/model/Address.js +99 -0
  15. package/dist/erpnext/model/Contact.d.ts +546 -0
  16. package/dist/erpnext/model/Contact.js +118 -0
  17. package/dist/erpnext/model/Country.d.ts +78 -0
  18. package/dist/erpnext/model/Country.js +30 -0
  19. package/dist/erpnext/model/Customer.d.ts +99 -0
  20. package/dist/erpnext/model/Customer.js +42 -0
  21. package/dist/erpnext/model/DateSchema.d.ts +2 -0
  22. package/dist/erpnext/model/DateSchema.js +8 -0
  23. package/dist/erpnext/model/DeliveryNote.d.ts +1648 -0
  24. package/dist/erpnext/model/DeliveryNote.js +219 -0
  25. package/dist/erpnext/model/DispatchRun.d.ts +688 -0
  26. package/dist/erpnext/model/DispatchRun.js +167 -0
  27. package/dist/erpnext/model/DispatcherPreset.d.ts +159 -0
  28. package/dist/erpnext/model/DispatcherPreset.js +43 -0
  29. package/dist/erpnext/model/ERPNextQueue.d.ts +48 -0
  30. package/dist/erpnext/model/ERPNextQueue.js +5 -0
  31. package/dist/erpnext/model/ERPNextRequest.d.ts +29 -0
  32. package/dist/erpnext/model/ERPNextRequest.js +2 -0
  33. package/dist/erpnext/model/ERPNextResponse.d.ts +17 -0
  34. package/dist/erpnext/model/ERPNextResponse.js +18 -0
  35. package/dist/erpnext/model/File.d.ts +146 -0
  36. package/dist/erpnext/model/File.js +36 -0
  37. package/dist/erpnext/model/Fulfiller.d.ts +175 -0
  38. package/dist/erpnext/model/Fulfiller.js +79 -0
  39. package/dist/erpnext/model/FulfillerSettings.d.ts +130 -0
  40. package/dist/erpnext/model/FulfillerSettings.js +34 -0
  41. package/dist/erpnext/model/FulfillmentStation.d.ts +9 -0
  42. package/dist/erpnext/model/FulfillmentStation.js +9 -0
  43. package/dist/erpnext/model/Item.d.ts +1710 -0
  44. package/dist/erpnext/model/Item.js +239 -0
  45. package/dist/erpnext/model/ProjectedQuantityReport.d.ts +281 -0
  46. package/dist/erpnext/model/ProjectedQuantityReport.js +72 -0
  47. package/dist/erpnext/model/PurchaseOrder.d.ts +906 -0
  48. package/dist/erpnext/model/PurchaseOrder.js +248 -0
  49. package/dist/erpnext/model/Receipt.d.ts +790 -0
  50. package/dist/erpnext/model/Receipt.js +212 -0
  51. package/dist/erpnext/model/ReceiptDraft.d.ts +541 -0
  52. package/dist/erpnext/model/ReceiptDraft.js +149 -0
  53. package/dist/erpnext/model/Shipment.d.ts +1139 -0
  54. package/dist/erpnext/model/Shipment.js +191 -0
  55. package/dist/erpnext/model/ShippingProvider.d.ts +434 -0
  56. package/dist/erpnext/model/ShippingProvider.js +204 -0
  57. package/dist/erpnext/model/StockDict.d.ts +3 -0
  58. package/dist/erpnext/model/StockDict.js +7 -0
  59. package/dist/erpnext/model/WarehouseCategory.d.ts +20 -0
  60. package/dist/erpnext/model/WarehouseCategory.js +15 -0
  61. package/dist/index.d.ts +2 -0
  62. package/dist/index.js +18 -0
  63. package/dist/utils/fernet.server.d.ts +150 -0
  64. package/dist/utils/fernet.server.js +344 -0
  65. package/dist/utils/logger.d.ts +5 -0
  66. package/dist/utils/logger.js +45 -0
  67. package/dist/utils/request.d.ts +28 -0
  68. package/dist/utils/request.js +107 -0
  69. package/dist/utils/utils.d.ts +4 -0
  70. package/dist/utils/utils.js +23 -0
  71. package/dist/utils/zodUtils.d.ts +2 -0
  72. package/dist/utils/zodUtils.js +17 -0
  73. package/package.json +32 -0
@@ -0,0 +1,344 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Hex = exports.Base64 = exports.Token = exports.Secret = exports.Buffer = exports.defaults = void 0;
7
+ exports.setSecret = setSecret;
8
+ exports.ArrayToHex = ArrayToHex;
9
+ exports.timeBytes = timeBytes;
10
+ exports.decode64toHex = decode64toHex;
11
+ exports.createHmac = createHmac;
12
+ exports.hexBits = hexBits;
13
+ exports.urlsafe = urlsafe;
14
+ /**
15
+ * This an es6 port of the library found here:
16
+ * https://github.com/csquared/fernet.js
17
+ *
18
+ * This was ported due to issues with the global module scope and caused issues with rollup.js.
19
+ */
20
+ // import URLBase64 from "urlsafe-base64"; // having issues finding buffer
21
+ const buffer_1 = require("buffer");
22
+ Object.defineProperty(exports, "Buffer", { enumerable: true, get: function () { return buffer_1.Buffer; } });
23
+ const randombytes_1 = __importDefault(require("randombytes"));
24
+ const aes_1 = __importDefault(require("crypto-js/aes"));
25
+ const enc_utf8_1 = __importDefault(require("crypto-js/enc-utf8"));
26
+ const enc_hex_1 = __importDefault(require("crypto-js/enc-hex"));
27
+ exports.Hex = enc_hex_1.default;
28
+ const enc_base64_1 = __importDefault(require("crypto-js/enc-base64"));
29
+ exports.Base64 = enc_base64_1.default;
30
+ const hmac_sha256_1 = __importDefault(require("crypto-js/hmac-sha256"));
31
+ const defaults = {
32
+ ttl: 0,
33
+ versionHex: "80",
34
+ secret: undefined,
35
+ };
36
+ exports.defaults = defaults;
37
+ const URLBase64 = {
38
+ encode(buffer) {
39
+ return buffer
40
+ .toString("base64")
41
+ .replace(/\+/g, "-") // Convert '+' to '-'
42
+ .replace(/\//g, "_") // Convert '/' to '_'
43
+ .replace(/=+$/, ""); // Remove ending '='
44
+ },
45
+ decode(base64) {
46
+ // Add removed at end '='
47
+ base64 += Array(5 - (base64.length % 4)).join("=");
48
+ base64 = base64
49
+ .replace(/-/g, "+") // Convert '-' to '+'
50
+ .replace(/_/g, "/"); // Convert '_' to '/'
51
+ return buffer_1.Buffer.from(base64, "base64");
52
+ },
53
+ validate(base64) {
54
+ return /^[A-Za-z0-9\-_]+$/.test(base64);
55
+ },
56
+ };
57
+ /**
58
+ * left pad a string for some hex conversions (changed to function rather than messing with the string prototype)
59
+ *
60
+ * @param {String} str - input string for padding
61
+ * @param {String} padString - pad string to prepend input string
62
+ * @param {Number} length - length of output padded string
63
+ * @return {String} the padded string
64
+ */
65
+ function lpad(str, padString, length) {
66
+ while (str.length < length)
67
+ str = padString + str;
68
+ return str;
69
+ }
70
+ /**
71
+ * Makes a Base64 string a url-safe base64 slertring
72
+ * @param {String} string - input string to make url-safe
73
+ * @return {String} a url-safe base64 string
74
+ */
75
+ function urlsafe(string) {
76
+ return string.replace(/\+/g, "-").replace(/\//g, "_"); //.replace(/=+$/, '')
77
+ }
78
+ /**
79
+ * parse a Hex string to an Int
80
+ * @param {String} hexString - a hexlified string
81
+ * @return {Number} resulting integer from input hex string
82
+ */
83
+ const parseHex = (hexString) => {
84
+ return parseInt("0x" + hexString);
85
+ };
86
+ /**
87
+ * turn bits into number of chars in a hex string
88
+ * @param {Number} bits - input bits to convert to hex
89
+ * @return {Number} number of chars in hex string
90
+ */
91
+ function hexBits(bits) {
92
+ return (bits / 8) * 2;
93
+ }
94
+ /**
95
+ * convert base64 string to hex string
96
+ * @param {String} string - input base64 string to hex
97
+ * @return {String} a hex string
98
+ */
99
+ function decode64toHex(string) {
100
+ const s = URLBase64.decode(string.replace(/=+$/, ""));
101
+ return buffer_1.Buffer.from(s).toString("hex");
102
+ }
103
+ /**
104
+ * convert array to hex string
105
+ * @param {Number[]} array - iv array of integers
106
+ * @return {String} a hex string
107
+ */
108
+ function ArrayToHex(array) {
109
+ let hex = "";
110
+ for (const _byte in array) {
111
+ hex += lpad(Number(_byte).toString(16), "0", 2);
112
+ }
113
+ return hex;
114
+ }
115
+ /**
116
+ * Creates a random hex string
117
+ * @param {Number} size - size of hex string
118
+ * @return {String} a hex string
119
+ */
120
+ function randomHex(size) {
121
+ // return crypto.randomBytes(128/8).toString('hex');
122
+ return (0, randombytes_1.default)(128 / 8).toString("hex");
123
+ }
124
+ /**
125
+ * Will safely create an IV Array of integers
126
+ * @param {Number[]} [iv_array=null] - array of numbers for IV array. If none passed in, a random hex will be created
127
+ * @return {String} a hex string
128
+ */
129
+ function setIV(iv_array) {
130
+ return Array.isArray(iv_array) ? ArrayToHex(iv_array) : randomHex(128 / 8);
131
+ }
132
+ /**
133
+ * convert Time object or now into WordArray
134
+ * @param {Date} time - input Date object to convert to WordArray
135
+ * @return {Number[]} a word array
136
+ */
137
+ function timeBytes(time) {
138
+ if (time) {
139
+ //@ts-ignore
140
+ time = time / 1000;
141
+ }
142
+ else {
143
+ //@ts-ignore
144
+ time = Math.round(new Date() / 1000);
145
+ }
146
+ //@ts-ignore
147
+ const hexTime = lpad(time.toString(16), "0", "16");
148
+ return enc_hex_1.default.parse(hexTime);
149
+ }
150
+ /**
151
+ * convenience function to create a new instance of a Secret() and sets it as the default for all future tokens
152
+ * @param {String} secret64 - base64 encoded secret string
153
+ * @return {Secret} a Secret
154
+ */
155
+ function setSecret(secret64) {
156
+ defaults.secret = new Secret(secret64);
157
+ return defaults.secret;
158
+ }
159
+ /**
160
+ * Encrypts a message using AES
161
+ * @param {String} message - message to encrypt
162
+ * @param {String} encryptionKey - encryption key for AES
163
+ * @param {Number[]} iv - IV array
164
+ * @return {String} encrypted message
165
+ */
166
+ function encryptMessage(message, encryptionKey, iv) {
167
+ const encrypted = aes_1.default.encrypt(message, encryptionKey, { iv });
168
+ return encrypted.ciphertext;
169
+ }
170
+ /**
171
+ * Decrypts an AES Encrypted message
172
+ * @param {String} cipherText - the encrypted message
173
+ * @param {String} encryptionKey - decryption key for AES
174
+ * @param {Number[]} iv - IV array
175
+ * @return {String} decrypted message
176
+ */
177
+ function decryptMessage(cipherText, encryptionKey, iv) {
178
+ const encrypted = {
179
+ ciphertext: cipherText,
180
+ key: encryptionKey,
181
+ iv,
182
+ };
183
+ const decrypted = aes_1.default.decrypt(encrypted, encryptionKey, { iv });
184
+ return decrypted.toString(enc_utf8_1.default);
185
+ }
186
+ /**
187
+ * creates an encryption token
188
+ * @param {Number[]} signingKey - signing key for encyrption
189
+ * @param {Date} time - time stamp for verification
190
+ * @param {Number[]} iv - IV Array
191
+ * @param {String} cipherText - the cipher text
192
+ * @return {String} the url safe encrypted string
193
+ */
194
+ function createToken(signingKey, time, iv, cipherText) {
195
+ const hmac = createHmac(signingKey, time, iv, cipherText);
196
+ let tokenWords = enc_hex_1.default.parse(defaults.versionHex);
197
+ for (let c of [time, iv, cipherText, hmac]) {
198
+ tokenWords = tokenWords.concat(c);
199
+ }
200
+ return urlsafe(tokenWords.toString(enc_base64_1.default));
201
+ }
202
+ /**
203
+ * Creates a SHA256 undigested byte string
204
+ * @param {Number[]} signingKey - signing key for encyrption
205
+ * @param {Date} time - time stamp for verification
206
+ * @param {Number[]} iv - IV Array
207
+ * @param {String} cipherText - the cipher text
208
+ * @return {String} an undigested byte string
209
+ */
210
+ function createHmac(signingKey, time, iv, cipherText) {
211
+ let hmacWords = enc_hex_1.default.parse(defaults.versionHex);
212
+ for (let c of [time, iv, cipherText]) {
213
+ hmacWords = hmacWords.concat(c);
214
+ }
215
+ return (0, hmac_sha256_1.default)(hmacWords, signingKey);
216
+ }
217
+ /**
218
+ * Instance of a Secret to be used for the token encryption
219
+ */
220
+ class Secret {
221
+ /**
222
+ * Creates a Secret to be used for the token encryption
223
+ * @param {String} secret64 - base64 encoded secret string
224
+ */
225
+ constructor(secret64) {
226
+ const secret = decode64toHex(secret64);
227
+ if (secret.length !== hexBits(256)) {
228
+ throw new Error("Secret must be 32 url-safe base64-encoded bytes.");
229
+ }
230
+ this.signingKeyHex = secret.slice(0, hexBits(128));
231
+ this.signingKey = enc_hex_1.default.parse(this.signingKeyHex);
232
+ this.encryptionKeyHex = secret.slice(hexBits(128));
233
+ this.encryptionKey = enc_hex_1.default.parse(this.encryptionKeyHex);
234
+ }
235
+ }
236
+ exports.Secret = Secret;
237
+ /**
238
+ * Options for token object to perform encryption
239
+ * @typedef TokenOptions
240
+ * @property {Number} [ttl=60] - time to live in seconds
241
+ * @property {Secret} secret - Secret object to use for encryption/decryption
242
+ * @property {String} message - message to encrypt
243
+ * @property {String} cipherText - cipher text to decrypt
244
+ * @property {String} token - a token string
245
+ * @property {String} [version='80'] - version of the token
246
+ * @property {Number[]} iv - IV Array
247
+ */
248
+ /**
249
+ * Token object to perform encryption/decryption
250
+ */
251
+ class Token {
252
+ /**
253
+ * Token object to perform encryption/decryption
254
+ * @param {TokenOptions} opts - options for token initialization
255
+ */
256
+ constructor(opts) {
257
+ var _a, _b;
258
+ this.maxClockSkew = 60;
259
+ opts = opts || {};
260
+ this.secret = opts.secret || defaults.secret;
261
+ this.ttl = opts.ttl || defaults.ttl;
262
+ if (opts.ttl === 0)
263
+ this.ttl = 0;
264
+ this.message = (_a = opts.message) !== null && _a !== void 0 ? _a : "";
265
+ this.cipherText = opts.cipherText;
266
+ this.token = (_b = opts.token) !== null && _b !== void 0 ? _b : "";
267
+ this.version = opts.version || parseHex(defaults.versionHex);
268
+ this.optsIV = opts.iv;
269
+ // @ts-ignore
270
+ this.time = opts.time ? timeBytes(Date.parse(opts.time)) : timeBytes();
271
+ this.encoded = false;
272
+ }
273
+ /**
274
+ * converts token to string
275
+ * @return {String} to stringified token
276
+ */
277
+ toString() {
278
+ return this.encoded ? this.token : this.message;
279
+ }
280
+ /**
281
+ * Encrypts a message
282
+ * @param {String} message - message to encrypt
283
+ * @return {String} encoded token string
284
+ */
285
+ encode(message) {
286
+ if (!this.secret)
287
+ throw new Error("Secret not set");
288
+ this.encoded = true;
289
+ this.ivHex = setIV(this.optsIV);
290
+ this.iv = enc_hex_1.default.parse(this.ivHex); //if null will always be a fresh IV
291
+ this.message = message || this.message;
292
+ this.cipherText = encryptMessage(this.message, this.secret.encryptionKey, this.iv);
293
+ this.token = createToken(this.secret.signingKey, this.time, this.iv, this.cipherText);
294
+ return this.token;
295
+ }
296
+ /**
297
+ * Decrypts a token
298
+ * @param {String} token - token to decrypt
299
+ * @return {String} decoded message
300
+ */
301
+ decode(token) {
302
+ if (!this.secret)
303
+ throw new Error("Secret not set");
304
+ this.encoded = false;
305
+ this.token = token || this.token;
306
+ const tokenString = decode64toHex(this.token);
307
+ const versionOffset = hexBits(8);
308
+ const timeOffset = versionOffset + hexBits(64);
309
+ const ivOffset = timeOffset + hexBits(128);
310
+ const hmacOffset = tokenString.length - hexBits(256);
311
+ const timeInt = parseHex(tokenString.slice(versionOffset, timeOffset));
312
+ this.version = parseHex(tokenString.slice(0, versionOffset));
313
+ if (this.version != 128) {
314
+ throw new Error("Invalid version");
315
+ }
316
+ this.time = new Date(timeInt * 1000);
317
+ const currentTime = new Date();
318
+ // @ts-ignore
319
+ const timeDiff = (currentTime - this.time) / 1000;
320
+ if (this.ttl > 0 && timeDiff > this.ttl) {
321
+ throw new Error("Invalid Token: TTL");
322
+ }
323
+ // @ts-ignore
324
+ if (currentTime / 1000 + this.maxClockSkew < timeInt) {
325
+ throw new Error("far-future timestamp");
326
+ }
327
+ this.ivHex = tokenString.slice(timeOffset, ivOffset);
328
+ this.iv = enc_hex_1.default.parse(this.ivHex);
329
+ this.cipherTextHex = tokenString.slice(ivOffset, hmacOffset);
330
+ this.cipherText = enc_hex_1.default.parse(this.cipherTextHex);
331
+ this.hmacHex = tokenString.slice(hmacOffset);
332
+ const decodedHmac = createHmac(this.secret.signingKey, timeBytes(this.time), this.iv, this.cipherText);
333
+ const decodedHmacHex = decodedHmac.toString(enc_hex_1.default);
334
+ let accum = 0;
335
+ for (let i = 0; i < 64; i++) {
336
+ accum += decodedHmacHex.charCodeAt(i) ^ this.hmacHex.charCodeAt(i);
337
+ }
338
+ if (accum != 0)
339
+ throw new Error("Invalid Token: HMAC");
340
+ this.message = decryptMessage(this.cipherText, this.secret.encryptionKey, this.iv);
341
+ return this.message;
342
+ }
343
+ }
344
+ exports.Token = Token;
@@ -0,0 +1,5 @@
1
+ export declare const lg: {
2
+ info: (...args: any[]) => void;
3
+ warn: (...args: any[]) => void;
4
+ error: (...args: any[]) => void;
5
+ };
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.lg = void 0;
4
+ /* eslint-disable @typescript-eslint/no-explicit-any */
5
+ const winston_1 = require("winston");
6
+ const utils_1 = require("./utils");
7
+ const winstonInfoLogger = (0, winston_1.createLogger)({
8
+ transports: [
9
+ new winston_1.transports.Console({
10
+ level: "info",
11
+ format: winston_1.format.simple(),
12
+ handleExceptions: true,
13
+ }),
14
+ new winston_1.transports.Console({
15
+ level: "error",
16
+ format: winston_1.format.combine(winston_1.format.prettyPrint(), winston_1.format.colorize({ all: true })),
17
+ handleExceptions: true,
18
+ }),
19
+ ],
20
+ exitOnError: false,
21
+ });
22
+ const winstonErrorLogger = (0, winston_1.createLogger)({
23
+ transports: [
24
+ new winston_1.transports.Console({
25
+ level: "error",
26
+ format: winston_1.format.combine(winston_1.format.prettyPrint(), winston_1.format.colorize({ all: true })),
27
+ handleExceptions: true,
28
+ }),
29
+ ],
30
+ exitOnError: false,
31
+ });
32
+ const info = (...args) => {
33
+ winstonInfoLogger.info(args.filter(utils_1.isDefined));
34
+ };
35
+ const warn = (...args) => {
36
+ winstonInfoLogger.warn(args.filter(utils_1.isDefined));
37
+ };
38
+ const error = (...args) => {
39
+ winstonErrorLogger.error(args.filter(utils_1.isDefined));
40
+ };
41
+ exports.lg = {
42
+ info,
43
+ warn,
44
+ error,
45
+ };
@@ -0,0 +1,28 @@
1
+ import type { z, ZodTypeAny } from "zod";
2
+ export interface RequestOptions<T extends ZodTypeAny, K extends ZodTypeAny> {
3
+ url: string;
4
+ headers?: RequestInit["headers"];
5
+ method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
6
+ responseValidationModel: T | false;
7
+ inputValidationModel: K | false;
8
+ body?: z.TypeOf<K>;
9
+ credentials?: RequestInit["credentials"];
10
+ isFormData?: boolean;
11
+ }
12
+ export interface RequestResponse {
13
+ status: number;
14
+ requestUrl: string;
15
+ statusText: string;
16
+ responseHeaders: Headers;
17
+ response: Record<string, unknown> | string;
18
+ }
19
+ export declare class RequestError extends Error {
20
+ status: number;
21
+ requestUrl: string;
22
+ statusText: string;
23
+ responseHeaders: Headers;
24
+ response: Record<string, unknown> | string;
25
+ constructor({ status, requestUrl, statusText, responseHeaders, response, }: RequestResponse);
26
+ toJSON(): Record<string, unknown>;
27
+ }
28
+ export declare function request<T extends ZodTypeAny, K extends ZodTypeAny>(options: RequestOptions<T, K>): Promise<z.infer<T>>;
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __rest = (this && this.__rest) || function (s, e) {
12
+ var t = {};
13
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
14
+ t[p] = s[p];
15
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
16
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
17
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
18
+ t[p[i]] = s[p[i]];
19
+ }
20
+ return t;
21
+ };
22
+ Object.defineProperty(exports, "__esModule", { value: true });
23
+ exports.RequestError = void 0;
24
+ exports.request = request;
25
+ const zodUtils_1 = require("./zodUtils");
26
+ const logger_1 = require("./logger");
27
+ class RequestError extends Error {
28
+ constructor({ status, requestUrl, statusText, responseHeaders, response, }) {
29
+ super(`Request failed with status ${status}: ${statusText} for URL ${requestUrl}`); // Call the parent `Error` constructor with the message
30
+ // Manually set the prototype to ensure `instanceof` works
31
+ Object.setPrototypeOf(this, RequestError.prototype);
32
+ this.name = "RequestError"; // Set the error name explicitly
33
+ this.status = status;
34
+ this.requestUrl = requestUrl;
35
+ this.statusText = statusText;
36
+ this.responseHeaders = responseHeaders; // Serialize headers as raw object
37
+ this.response = response;
38
+ // Attach the preserved stack trace if it was passed
39
+ Error.captureStackTrace(this, RequestError);
40
+ }
41
+ toJSON() {
42
+ return {
43
+ name: this.name,
44
+ message: this.message,
45
+ status: this.status,
46
+ requestUrl: this.requestUrl,
47
+ statusText: this.statusText,
48
+ responseHeaders: this.responseHeaders,
49
+ response: this.response,
50
+ responseJson: JSON.stringify(this.response, null, 2),
51
+ };
52
+ }
53
+ }
54
+ exports.RequestError = RequestError;
55
+ function request(options) {
56
+ return __awaiter(this, void 0, void 0, function* () {
57
+ var _a, _b;
58
+ const _c = options || {}, { body, url, inputValidationModel, responseValidationModel } = _c, requestOptions = __rest(_c, ["body", "url", "inputValidationModel", "responseValidationModel"]);
59
+ // Validate the request body
60
+ const validatedBody = inputValidationModel
61
+ ? (0, zodUtils_1.validateData)(body, inputValidationModel)
62
+ : body;
63
+ const contentType = options.headers &&
64
+ "Content-Type" in options.headers &&
65
+ ((_a = options.headers) === null || _a === void 0 ? void 0 : _a["Content-Type"])
66
+ ? (_b = options.headers) === null || _b === void 0 ? void 0 : _b["Content-Type"]
67
+ : undefined;
68
+ const stringifiedBody = options.method !== "GET" &&
69
+ contentType !== "text/xml" &&
70
+ contentType !== "application/xml" &&
71
+ contentType !== "application/x-www-form-urlencoded"
72
+ ? JSON.stringify(validatedBody)
73
+ : undefined;
74
+ const convertedOptions = Object.assign(Object.assign({}, requestOptions), (options.method !== "GET"
75
+ ? { body: stringifiedBody !== null && stringifiedBody !== void 0 ? stringifiedBody : validatedBody }
76
+ : {}));
77
+ // Log requests
78
+ logger_1.lg.info(`${options.method}${url}`);
79
+ // Wrap fetch
80
+ const response = yield fetch(url, convertedOptions);
81
+ const { status, url: requestUrl, statusText, ok } = response;
82
+ const textResult = yield response.text();
83
+ let finalResult;
84
+ try {
85
+ finalResult = JSON.parse(textResult);
86
+ }
87
+ catch (err) {
88
+ finalResult = textResult;
89
+ }
90
+ // Reject promise when status is expected or unexpected error. Allows use of try/catch to catch http errors
91
+ if (!ok) {
92
+ logger_1.lg.error(`Error in request to ${requestUrl} with status text ${statusText}`, JSON.stringify(finalResult, null, 2));
93
+ throw new RequestError({
94
+ status,
95
+ requestUrl,
96
+ statusText,
97
+ responseHeaders: response === null || response === void 0 ? void 0 : response.headers,
98
+ response: finalResult,
99
+ });
100
+ }
101
+ // Validate the response body
102
+ const validatedResult = responseValidationModel
103
+ ? (0, zodUtils_1.validateData)(finalResult, responseValidationModel)
104
+ : finalResult;
105
+ return validatedResult;
106
+ });
107
+ }
@@ -0,0 +1,4 @@
1
+ export declare function addDays(date: string | number | Date, days: number): Date;
2
+ export declare function addMinutes(date: Date, minutes: number): Date;
3
+ export declare function timeout(ms: number): Promise<void>;
4
+ export declare function isDefined<T>(value: T | null | undefined): value is T;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addDays = addDays;
4
+ exports.addMinutes = addMinutes;
5
+ exports.timeout = timeout;
6
+ exports.isDefined = isDefined;
7
+ function addDays(date, days) {
8
+ const result = new Date(date);
9
+ result.setUTCDate(result.getUTCDate() + days);
10
+ return result;
11
+ }
12
+ function addMinutes(date, minutes) {
13
+ const result = new Date(date);
14
+ result.setUTCMinutes(result.getUTCMinutes() + minutes);
15
+ return result;
16
+ }
17
+ // async await version of setTimeout
18
+ function timeout(ms) {
19
+ return new Promise(resolve => setTimeout(resolve, ms));
20
+ }
21
+ function isDefined(value) {
22
+ return value !== null && value !== undefined;
23
+ }
@@ -0,0 +1,2 @@
1
+ import { z } from "zod";
2
+ export declare function validateData<T extends z.ZodTypeAny>(data: unknown, ValidationModel: T): z.infer<T>;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateData = validateData;
4
+ const constants_1 = require("../constants");
5
+ function validateData(data, ValidationModel) {
6
+ if (!ValidationModel.description)
7
+ throw new Error("ValidationModel.description is required");
8
+ const validationResult = ValidationModel.safeParse(data);
9
+ if (!validationResult.success) {
10
+ throw {
11
+ message: `Validation error in model ${ValidationModel.description}`,
12
+ error: validationResult.error,
13
+ input: constants_1.constants.DEBUG_MODE === "true" ? data : "set DEBUG_MODE to true to see the input",
14
+ };
15
+ }
16
+ return validationResult.data;
17
+ }
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "erpnext-queue-client",
3
+ "description": "Client to setup jobs in the temporal queue for processing ERPNext requests called \"erpnext-queue\"",
4
+ "files": [
5
+ "dist"
6
+ ],
7
+ "main": "dist/index.js",
8
+ "scripts": {
9
+ "test": "dotenv -c test vitest",
10
+ "build": "tsc",
11
+ "release": "npm run build && npm publish"
12
+ },
13
+ "author": "Jonathan Laukenmann",
14
+ "license": "ISC",
15
+ "dependencies": {
16
+ "@temporalio/client": "^1.11.3",
17
+ "crypto-js": "^4.2.0",
18
+ "dotenv": "^16.4.7",
19
+ "dotenv-cli": "^8.0.0",
20
+ "lodash": "^4.17.21",
21
+ "nanoid": "^3.3.7",
22
+ "randombytes": "^2.1.0",
23
+ "winston": "^3.15.0",
24
+ "zod": "^3.23.8"
25
+ },
26
+ "version": "1.0.2",
27
+ "devDependencies": {
28
+ "@types/crypto-js": "^4.2.2",
29
+ "typescript": "^5.6.3",
30
+ "vitest": "^2.1.8"
31
+ }
32
+ }