@jeniex/utils 1.1.3 → 1.2.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/browser/index.cjs CHANGED
@@ -54,6 +54,11 @@ function asserted(object) {
54
54
  }
55
55
  return object;
56
56
  }
57
+ function stringOrThrow(value) {
58
+ if (!isString(value)) {
59
+ throw new Error('Expected a string.');
60
+ }
61
+ }
57
62
  function isTruthy(object) {
58
63
  return Boolean(object) === true;
59
64
  }
@@ -468,6 +473,114 @@ function customizeConsole(debug = false) {
468
473
  };
469
474
  }
470
475
 
476
+ function createRequest(transport, url, options) {
477
+ const getResponse = async () => {
478
+ return transport(url, options);
479
+ };
480
+ return {
481
+ response: async () => {
482
+ return getResponse();
483
+ },
484
+ blob: async () => {
485
+ const response = await getResponse();
486
+ return response.blob();
487
+ },
488
+ buffer: async () => {
489
+ const response = await getResponse();
490
+ return response.arrayBuffer();
491
+ },
492
+ json: async () => {
493
+ const response = await getResponse();
494
+ return response.json();
495
+ },
496
+ text: async () => {
497
+ const response = await getResponse();
498
+ return response.text();
499
+ },
500
+ document: async () => {
501
+ const response = await getResponse();
502
+ const responseText = await response.text();
503
+ const parser = new DOMParser();
504
+ return parser.parseFromString(responseText, 'text/html');
505
+ },
506
+ multi: async (types) => {
507
+ const response = await getResponse();
508
+ const responses = { response };
509
+ if (types.includes('blob')) {
510
+ responses.blob = await response.clone().blob();
511
+ }
512
+ if (types.includes('buffer')) {
513
+ responses.buffer = await response.clone().arrayBuffer();
514
+ }
515
+ if (types.includes('json')) {
516
+ responses.json = (await response.clone().json());
517
+ }
518
+ if (types.includes('text')) {
519
+ responses.text = await response.clone().text();
520
+ }
521
+ if (types.includes('document')) {
522
+ const responseText = await response.clone().text();
523
+ const parser = new DOMParser();
524
+ responses.document = parser.parseFromString(responseText, 'text/html');
525
+ }
526
+ return responses;
527
+ },
528
+ };
529
+ }
530
+
531
+ function isFileURL(url) {
532
+ return url.startsWith('file:///');
533
+ }
534
+
535
+ async function fetchTransport(url, options) {
536
+ const response = await fetch(url, options);
537
+ if (!response.ok && !isFileURL(response.url)) {
538
+ throw new Error(`Request to ${response.url} ended with ${response.status} status.`);
539
+ }
540
+ return response;
541
+ }
542
+
543
+ function rq(url, options) {
544
+ return createRequest(fetchTransport, url, options);
545
+ }
546
+
547
+ async function gmTransport(url, options) {
548
+ const { method, anonymous, headers, body, timeOut, onProgress } = options ?? {};
549
+ return new Promise((resolve, reject) => {
550
+ GM.xmlHttpRequest({
551
+ url,
552
+ method: method ?? 'GET',
553
+ headers,
554
+ /** Note: To manually set the 'cookie' header, you have to set 'anonymous' to true. */
555
+ anonymous,
556
+ data: body,
557
+ responseType: 'blob',
558
+ timeout: timeOut,
559
+ onprogress: onProgress,
560
+ onload({ response, statusText, status, finalUrl }) {
561
+ const ok = status >= 200 && status < 300;
562
+ if (!ok && !isFileURL(finalUrl)) {
563
+ reject(new Error(`Request to ${url} ended with ${status} status.`));
564
+ return;
565
+ }
566
+ const properResponse = new Response(response, {
567
+ statusText,
568
+ status: isFileURL(finalUrl) ? 200 : status,
569
+ });
570
+ Object.defineProperty(properResponse, 'url', { value: finalUrl });
571
+ resolve(properResponse);
572
+ },
573
+ onerror({ status }) {
574
+ reject(new Error(`Request to ${url} ended with ${status} status.`));
575
+ },
576
+ });
577
+ });
578
+ }
579
+
580
+ function rx(url, options) {
581
+ return createRequest(gmTransport, url, options);
582
+ }
583
+
471
584
  exports.$ = $;
472
585
  exports.$$ = $$;
473
586
  exports.addStyle = addStyle;
@@ -501,9 +614,13 @@ exports.pageLoad = pageLoad;
501
614
  exports.prompt = prompt;
502
615
  exports.remove = remove;
503
616
  exports.removeSearchParams = removeSearchParams;
617
+ exports.rq = rq;
618
+ exports.rqc = createRequest;
619
+ exports.rx = rx;
504
620
  exports.saveFile = saveFile;
505
621
  exports.scriptName = scriptName;
506
622
  exports.setSearchParam = setSearchParam;
507
623
  exports.sleep = sleep;
624
+ exports.stringOrThrow = stringOrThrow;
508
625
  exports.tabURL = tabURL;
509
626
  exports.toString = toString;
@@ -17,6 +17,7 @@ declare function noop(): void;
17
17
  declare function toString(object: unknown): string;
18
18
 
19
19
  declare function asserted<T>(object: T | null | undefined): T;
20
+ declare function stringOrThrow(value: unknown): asserts value is string;
20
21
  declare function isTruthy<T>(object: T): object is Exclude<T, '' | 0 | false | null | undefined>;
21
22
  declare function isFalsy(object: unknown): object is '' | 0 | false | null | undefined;
22
23
  declare function isNullish(object: unknown): object is null | undefined;
@@ -59,9 +60,9 @@ declare function alert(message?: string): void;
59
60
  declare function confirm(message: string): boolean;
60
61
  declare function prompt(message: string, _default?: string): string | null;
61
62
 
62
- type Method = 'CONNECT' | 'DELETE' | 'GET' | 'HEAD' | 'OPTIONS' | 'PATCH' | 'POST' | 'PUT' | 'TRACE';
63
+ type Method$1 = 'CONNECT' | 'DELETE' | 'GET' | 'HEAD' | 'OPTIONS' | 'PATCH' | 'POST' | 'PUT' | 'TRACE';
63
64
  interface FishXOptions {
64
- method?: Method;
65
+ method?: Method$1;
65
66
  anonymous?: boolean;
66
67
  headers?: Record<string, string>;
67
68
  body?: Blob | FormData | string;
@@ -167,4 +168,39 @@ interface CustomizedConsole {
167
168
 
168
169
  declare function customizeConsole(debug?: boolean): CustomizedConsole;
169
170
 
170
- export { $, $$, Flag, Fn, Nullable, Prettify, addStyle, alert, asserted, capitalize, checkVPN, checkVPNRandomly, confirm, customizeConsole, fish, fishResponse, fishX, fishXResponse, getOptions, getSearchParam, imageLoad, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, noop, padZeros, pageLoad, prompt, remove, removeSearchParams, saveFile, scriptName, setSearchParam, sleep, tabURL, toString };
171
+ type Method = 'CONNECT' | 'DELETE' | 'GET' | 'HEAD' | 'OPTIONS' | 'PATCH' | 'POST' | 'PUT' | 'TRACE';
172
+ interface RxOptions {
173
+ method?: Method;
174
+ anonymous?: boolean;
175
+ headers?: Record<string, string>;
176
+ body?: Blob | FormData | string;
177
+ timeOut?: number;
178
+ onProgress?: (response: VMScriptResponseObject<Blob>) => Promise<void> | void;
179
+ }
180
+ interface RqRequest {
181
+ response: () => Promise<Response>;
182
+ blob: () => Promise<Blob>;
183
+ buffer: () => Promise<ArrayBuffer>;
184
+ json: <T = unknown>() => Promise<T>;
185
+ text: () => Promise<string>;
186
+ document: () => Promise<Document>;
187
+ multi: <T = unknown>(types: ResponseType[]) => Promise<Responses<T>>;
188
+ }
189
+ type Transport<TOptions = unknown> = (url: string, options?: TOptions) => Promise<Response>;
190
+ type ResponseType = 'blob' | 'buffer' | 'document' | 'json' | 'text';
191
+ interface Responses<T> {
192
+ response?: Response;
193
+ blob?: Blob;
194
+ buffer?: ArrayBuffer;
195
+ json?: T;
196
+ text?: string;
197
+ document?: Document;
198
+ }
199
+
200
+ declare function rq(url: string, options?: RequestInit): RqRequest;
201
+
202
+ declare function rx(url: string, options?: RxOptions): RqRequest;
203
+
204
+ declare function createRequest<TOptions>(transport: Transport<TOptions>, url: string, options?: TOptions): RqRequest;
205
+
206
+ export { $, $$, Flag, Fn, Nullable, Prettify, addStyle, alert, asserted, capitalize, checkVPN, checkVPNRandomly, confirm, customizeConsole, fish, fishResponse, fishX, fishXResponse, getOptions, getSearchParam, imageLoad, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, noop, padZeros, pageLoad, prompt, remove, removeSearchParams, rq, createRequest as rqc, rx, saveFile, scriptName, setSearchParam, sleep, stringOrThrow, tabURL, toString };
package/browser/index.js CHANGED
@@ -52,6 +52,11 @@ function asserted(object) {
52
52
  }
53
53
  return object;
54
54
  }
55
+ function stringOrThrow(value) {
56
+ if (!isString(value)) {
57
+ throw new Error('Expected a string.');
58
+ }
59
+ }
55
60
  function isTruthy(object) {
56
61
  return Boolean(object) === true;
57
62
  }
@@ -466,4 +471,112 @@ function customizeConsole(debug = false) {
466
471
  };
467
472
  }
468
473
 
469
- export { $, $$, addStyle, alert, asserted, capitalize, checkVPN, checkVPNRandomly, confirm, customizeConsole, fish, fishResponse, fishX, fishXResponse, getOptions, getSearchParam, imageLoad, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, noop, padZeros, pageLoad, prompt, remove, removeSearchParams, saveFile, scriptName, setSearchParam, sleep, tabURL, toString };
474
+ function createRequest(transport, url, options) {
475
+ const getResponse = async () => {
476
+ return transport(url, options);
477
+ };
478
+ return {
479
+ response: async () => {
480
+ return getResponse();
481
+ },
482
+ blob: async () => {
483
+ const response = await getResponse();
484
+ return response.blob();
485
+ },
486
+ buffer: async () => {
487
+ const response = await getResponse();
488
+ return response.arrayBuffer();
489
+ },
490
+ json: async () => {
491
+ const response = await getResponse();
492
+ return response.json();
493
+ },
494
+ text: async () => {
495
+ const response = await getResponse();
496
+ return response.text();
497
+ },
498
+ document: async () => {
499
+ const response = await getResponse();
500
+ const responseText = await response.text();
501
+ const parser = new DOMParser();
502
+ return parser.parseFromString(responseText, 'text/html');
503
+ },
504
+ multi: async (types) => {
505
+ const response = await getResponse();
506
+ const responses = { response };
507
+ if (types.includes('blob')) {
508
+ responses.blob = await response.clone().blob();
509
+ }
510
+ if (types.includes('buffer')) {
511
+ responses.buffer = await response.clone().arrayBuffer();
512
+ }
513
+ if (types.includes('json')) {
514
+ responses.json = (await response.clone().json());
515
+ }
516
+ if (types.includes('text')) {
517
+ responses.text = await response.clone().text();
518
+ }
519
+ if (types.includes('document')) {
520
+ const responseText = await response.clone().text();
521
+ const parser = new DOMParser();
522
+ responses.document = parser.parseFromString(responseText, 'text/html');
523
+ }
524
+ return responses;
525
+ },
526
+ };
527
+ }
528
+
529
+ function isFileURL(url) {
530
+ return url.startsWith('file:///');
531
+ }
532
+
533
+ async function fetchTransport(url, options) {
534
+ const response = await fetch(url, options);
535
+ if (!response.ok && !isFileURL(response.url)) {
536
+ throw new Error(`Request to ${response.url} ended with ${response.status} status.`);
537
+ }
538
+ return response;
539
+ }
540
+
541
+ function rq(url, options) {
542
+ return createRequest(fetchTransport, url, options);
543
+ }
544
+
545
+ async function gmTransport(url, options) {
546
+ const { method, anonymous, headers, body, timeOut, onProgress } = options ?? {};
547
+ return new Promise((resolve, reject) => {
548
+ GM.xmlHttpRequest({
549
+ url,
550
+ method: method ?? 'GET',
551
+ headers,
552
+ /** Note: To manually set the 'cookie' header, you have to set 'anonymous' to true. */
553
+ anonymous,
554
+ data: body,
555
+ responseType: 'blob',
556
+ timeout: timeOut,
557
+ onprogress: onProgress,
558
+ onload({ response, statusText, status, finalUrl }) {
559
+ const ok = status >= 200 && status < 300;
560
+ if (!ok && !isFileURL(finalUrl)) {
561
+ reject(new Error(`Request to ${url} ended with ${status} status.`));
562
+ return;
563
+ }
564
+ const properResponse = new Response(response, {
565
+ statusText,
566
+ status: isFileURL(finalUrl) ? 200 : status,
567
+ });
568
+ Object.defineProperty(properResponse, 'url', { value: finalUrl });
569
+ resolve(properResponse);
570
+ },
571
+ onerror({ status }) {
572
+ reject(new Error(`Request to ${url} ended with ${status} status.`));
573
+ },
574
+ });
575
+ });
576
+ }
577
+
578
+ function rx(url, options) {
579
+ return createRequest(gmTransport, url, options);
580
+ }
581
+
582
+ export { $, $$, addStyle, alert, asserted, capitalize, checkVPN, checkVPNRandomly, confirm, customizeConsole, fish, fishResponse, fishX, fishXResponse, getOptions, getSearchParam, imageLoad, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, noop, padZeros, pageLoad, prompt, remove, removeSearchParams, rq, createRequest as rqc, rx, saveFile, scriptName, setSearchParam, sleep, stringOrThrow, tabURL, toString };
package/node/index.cjs CHANGED
@@ -58,6 +58,11 @@ function asserted(object) {
58
58
  }
59
59
  return object;
60
60
  }
61
+ function stringOrThrow(value) {
62
+ if (!isString(value)) {
63
+ throw new Error('Expected a string.');
64
+ }
65
+ }
61
66
  function isTruthy(object) {
62
67
  return Boolean(object) === true;
63
68
  }
@@ -239,6 +244,77 @@ async function readJSON(filePath, withComments = false) {
239
244
  return JSON.parse(stringJSON);
240
245
  }
241
246
 
247
+ function createRequest(transport, url, options) {
248
+ const getResponse = async () => {
249
+ return transport(url, options);
250
+ };
251
+ return {
252
+ response: async () => {
253
+ return getResponse();
254
+ },
255
+ blob: async () => {
256
+ const response = await getResponse();
257
+ return response.blob();
258
+ },
259
+ buffer: async () => {
260
+ const response = await getResponse();
261
+ return response.arrayBuffer();
262
+ },
263
+ json: async () => {
264
+ const response = await getResponse();
265
+ return response.json();
266
+ },
267
+ text: async () => {
268
+ const response = await getResponse();
269
+ return response.text();
270
+ },
271
+ document: async () => {
272
+ const response = await getResponse();
273
+ const responseText = await response.text();
274
+ const parser = new DOMParser();
275
+ return parser.parseFromString(responseText, 'text/html');
276
+ },
277
+ multi: async (types) => {
278
+ const response = await getResponse();
279
+ const responses = { response };
280
+ if (types.includes('blob')) {
281
+ responses.blob = await response.clone().blob();
282
+ }
283
+ if (types.includes('buffer')) {
284
+ responses.buffer = await response.clone().arrayBuffer();
285
+ }
286
+ if (types.includes('json')) {
287
+ responses.json = (await response.clone().json());
288
+ }
289
+ if (types.includes('text')) {
290
+ responses.text = await response.clone().text();
291
+ }
292
+ if (types.includes('document')) {
293
+ const responseText = await response.clone().text();
294
+ const parser = new DOMParser();
295
+ responses.document = parser.parseFromString(responseText, 'text/html');
296
+ }
297
+ return responses;
298
+ },
299
+ };
300
+ }
301
+
302
+ function isFileURL(url) {
303
+ return url.startsWith('file:///');
304
+ }
305
+
306
+ async function fetchTransport(url, options) {
307
+ const response = await fetch(url, options);
308
+ if (!response.ok && !isFileURL(response.url)) {
309
+ throw new Error(`Request to ${response.url} ended with ${response.status} status.`);
310
+ }
311
+ return response;
312
+ }
313
+
314
+ function rq(url, options) {
315
+ return createRequest(fetchTransport, url, options);
316
+ }
317
+
242
318
  exports.asserted = asserted;
243
319
  exports.capitalize = capitalize;
244
320
  exports.isBoolean = isBoolean;
@@ -259,5 +335,8 @@ exports.padZeros = padZeros;
259
335
  exports.readJSON = readJSON;
260
336
  exports.remove = remove;
261
337
  exports.removeFiles = removeFiles;
338
+ exports.rq = rq;
339
+ exports.rqc = createRequest;
262
340
  exports.sleep = sleep;
341
+ exports.stringOrThrow = stringOrThrow;
263
342
  exports.toString = toString;
package/node/index.d.ts CHANGED
@@ -17,6 +17,7 @@ declare function noop(): void;
17
17
  declare function toString(object: unknown): string;
18
18
 
19
19
  declare function asserted<T>(object: T | null | undefined): T;
20
+ declare function stringOrThrow(value: unknown): asserts value is string;
20
21
  declare function isTruthy<T>(object: T): object is Exclude<T, '' | 0 | false | null | undefined>;
21
22
  declare function isFalsy(object: unknown): object is '' | 0 | false | null | undefined;
22
23
  declare function isNullish(object: unknown): object is null | undefined;
@@ -109,4 +110,28 @@ declare function removeFiles({ folderPath, filter }: RemoveFilesOpt): Promise<vo
109
110
 
110
111
  declare function readJSON<T = object>(filePath: string, withComments?: boolean): Promise<T>;
111
112
 
112
- export { Fn, Nullable, Prettify, asserted, capitalize, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, listFiles, listFolders, listFoldersSync, noop, padZeros, readJSON, remove, removeFiles, sleep, toString };
113
+ interface RqRequest {
114
+ response: () => Promise<Response>;
115
+ blob: () => Promise<Blob>;
116
+ buffer: () => Promise<ArrayBuffer>;
117
+ json: <T = unknown>() => Promise<T>;
118
+ text: () => Promise<string>;
119
+ document: () => Promise<Document>;
120
+ multi: <T = unknown>(types: ResponseType[]) => Promise<Responses<T>>;
121
+ }
122
+ type Transport<TOptions = unknown> = (url: string, options?: TOptions) => Promise<Response>;
123
+ type ResponseType = 'blob' | 'buffer' | 'document' | 'json' | 'text';
124
+ interface Responses<T> {
125
+ response?: Response;
126
+ blob?: Blob;
127
+ buffer?: ArrayBuffer;
128
+ json?: T;
129
+ text?: string;
130
+ document?: Document;
131
+ }
132
+
133
+ declare function rq(url: string, options?: RequestInit): RqRequest;
134
+
135
+ declare function createRequest<TOptions>(transport: Transport<TOptions>, url: string, options?: TOptions): RqRequest;
136
+
137
+ export { Fn, Nullable, Prettify, asserted, capitalize, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, listFiles, listFolders, listFoldersSync, noop, padZeros, readJSON, remove, removeFiles, rq, createRequest as rqc, sleep, stringOrThrow, toString };
package/node/index.js CHANGED
@@ -56,6 +56,11 @@ function asserted(object) {
56
56
  }
57
57
  return object;
58
58
  }
59
+ function stringOrThrow(value) {
60
+ if (!isString(value)) {
61
+ throw new Error('Expected a string.');
62
+ }
63
+ }
59
64
  function isTruthy(object) {
60
65
  return Boolean(object) === true;
61
66
  }
@@ -237,4 +242,75 @@ async function readJSON(filePath, withComments = false) {
237
242
  return JSON.parse(stringJSON);
238
243
  }
239
244
 
240
- export { asserted, capitalize, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, listFiles, listFolders, listFoldersSync, noop, padZeros, readJSON, remove, removeFiles, sleep, toString };
245
+ function createRequest(transport, url, options) {
246
+ const getResponse = async () => {
247
+ return transport(url, options);
248
+ };
249
+ return {
250
+ response: async () => {
251
+ return getResponse();
252
+ },
253
+ blob: async () => {
254
+ const response = await getResponse();
255
+ return response.blob();
256
+ },
257
+ buffer: async () => {
258
+ const response = await getResponse();
259
+ return response.arrayBuffer();
260
+ },
261
+ json: async () => {
262
+ const response = await getResponse();
263
+ return response.json();
264
+ },
265
+ text: async () => {
266
+ const response = await getResponse();
267
+ return response.text();
268
+ },
269
+ document: async () => {
270
+ const response = await getResponse();
271
+ const responseText = await response.text();
272
+ const parser = new DOMParser();
273
+ return parser.parseFromString(responseText, 'text/html');
274
+ },
275
+ multi: async (types) => {
276
+ const response = await getResponse();
277
+ const responses = { response };
278
+ if (types.includes('blob')) {
279
+ responses.blob = await response.clone().blob();
280
+ }
281
+ if (types.includes('buffer')) {
282
+ responses.buffer = await response.clone().arrayBuffer();
283
+ }
284
+ if (types.includes('json')) {
285
+ responses.json = (await response.clone().json());
286
+ }
287
+ if (types.includes('text')) {
288
+ responses.text = await response.clone().text();
289
+ }
290
+ if (types.includes('document')) {
291
+ const responseText = await response.clone().text();
292
+ const parser = new DOMParser();
293
+ responses.document = parser.parseFromString(responseText, 'text/html');
294
+ }
295
+ return responses;
296
+ },
297
+ };
298
+ }
299
+
300
+ function isFileURL(url) {
301
+ return url.startsWith('file:///');
302
+ }
303
+
304
+ async function fetchTransport(url, options) {
305
+ const response = await fetch(url, options);
306
+ if (!response.ok && !isFileURL(response.url)) {
307
+ throw new Error(`Request to ${response.url} ended with ${response.status} status.`);
308
+ }
309
+ return response;
310
+ }
311
+
312
+ function rq(url, options) {
313
+ return createRequest(fetchTransport, url, options);
314
+ }
315
+
316
+ export { asserted, capitalize, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, listFiles, listFolders, listFoldersSync, noop, padZeros, readJSON, remove, removeFiles, rq, createRequest as rqc, sleep, stringOrThrow, toString };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jeniex/utils",
3
- "version": "1.1.3",
3
+ "version": "1.2.0",
4
4
  "author": "JenieX",
5
5
  "homepage": "https://github.com/JenieX/utils",
6
6
  "type": "module",