@naturalcycles/js-lib 15.40.2 → 15.41.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.
@@ -101,9 +101,10 @@ export declare class JsonSchemaStringBuilder<T extends string = string, Opt exte
101
101
  languageTag: () => this;
102
102
  countryCode: () => this;
103
103
  currency: () => this;
104
- trim: (trim?: boolean) => this;
105
- toLowerCase: (toLowerCase?: boolean) => this;
106
- toUpperCase: (toUpperCase?: boolean) => this;
104
+ trim(trim?: boolean): this;
105
+ toLowerCase(toLowerCase?: boolean): this;
106
+ toUpperCase(toUpperCase?: boolean): this;
107
+ truncate(toLength: number): this;
107
108
  branded<B extends string>(): JsonSchemaStringBuilder<B>;
108
109
  /**
109
110
  * Accepts only the `YYYY-MM-DD` shape from all ISO 8601 variants.
@@ -115,11 +116,10 @@ export declare class JsonSchemaStringBuilder<T extends string = string, Opt exte
115
116
  */
116
117
  isoDateTime(): JsonSchemaStringBuilder<IsoDateTime>;
117
118
  jwt(): this;
118
- private transformModify;
119
119
  }
120
120
  export declare class JsonSchemaObjectBuilder<T extends AnyObject, Opt extends boolean = false> extends JsonSchemaAnyBuilder<T, JsonSchemaObject<T>, Opt> {
121
121
  constructor();
122
- addProperties(props: {
122
+ addProperties(props?: {
123
123
  [k in keyof T]: JsonSchemaBuilder<T[k]>;
124
124
  }): this;
125
125
  /**
@@ -144,14 +144,14 @@ export declare class JsonSchemaArrayBuilder<ITEM, Opt extends boolean = false> e
144
144
  export declare class JsonSchemaTupleBuilder<T extends any[]> extends JsonSchemaAnyBuilder<T, JsonSchemaTuple<T>> {
145
145
  constructor(items: JsonSchemaBuilder[]);
146
146
  }
147
- declare function object<P extends Record<string, JsonSchemaAnyBuilder<any, any, any>>>(props: P): JsonSchemaObjectBuilder<{
147
+ declare function object<P extends Record<string, JsonSchemaAnyBuilder<any, any, any>>>(props?: P): JsonSchemaObjectBuilder<{
148
148
  [K in keyof P as P[K] extends JsonSchemaAnyBuilder<any, any, infer Opt> ? Opt extends true ? never : K : never]: P[K] extends JsonSchemaAnyBuilder<infer U, any, any> ? U : never;
149
149
  } & {
150
150
  [K in keyof P as P[K] extends JsonSchemaAnyBuilder<any, any, infer Opt> ? Opt extends true ? K : never : never]?: P[K] extends JsonSchemaAnyBuilder<infer U, any, any> ? U : never;
151
151
  } extends infer O ? {
152
152
  [K in keyof O]: O[K];
153
153
  } : never>;
154
- declare function object<T extends AnyObject>(props: {
154
+ declare function object<T extends AnyObject>(props?: {
155
155
  [K in keyof T]: JsonSchemaAnyBuilder<T[K]>;
156
156
  }): JsonSchemaObjectBuilder<T>;
157
157
  export {};
@@ -54,9 +54,7 @@ export const j = {
54
54
  });
55
55
  },
56
56
  buffer() {
57
- return new JsonSchemaAnyBuilder({
58
- instanceof: 'Buffer',
59
- });
57
+ return new JsonSchemaAnyBuilder({ instanceof: 'Buffer' });
60
58
  },
61
59
  // number types
62
60
  number() {
@@ -144,7 +142,7 @@ export class JsonSchemaAnyBuilder {
144
142
  return this;
145
143
  }
146
144
  instanceof(of) {
147
- this.schema.instanceof = of;
145
+ Object.assign(this.schema, { type: 'object', instanceof: of });
148
146
  return this;
149
147
  }
150
148
  optional(optional = true) {
@@ -280,9 +278,22 @@ export class JsonSchemaStringBuilder extends JsonSchemaAnyBuilder {
280
278
  languageTag = () => this.format('languageTag');
281
279
  countryCode = () => this.format('countryCode');
282
280
  currency = () => this.format('currency');
283
- trim = (trim = true) => this.transformModify('trim', trim);
284
- toLowerCase = (toLowerCase = true) => this.transformModify('toLowerCase', toLowerCase);
285
- toUpperCase = (toUpperCase = true) => this.transformModify('toUpperCase', toUpperCase);
281
+ trim(trim = true) {
282
+ Object.assign(this.schema, { transform: { ...this.schema.transform, trim } });
283
+ return this;
284
+ }
285
+ toLowerCase(toLowerCase = true) {
286
+ Object.assign(this.schema, { transform: { ...this.schema.transform, toLowerCase } });
287
+ return this;
288
+ }
289
+ toUpperCase(toUpperCase = true) {
290
+ Object.assign(this.schema, { transform: { ...this.schema.transform, toUpperCase } });
291
+ return this;
292
+ }
293
+ truncate(toLength) {
294
+ Object.assign(this.schema, { transform: { ...this.schema.transform, truncate: toLength } });
295
+ return this;
296
+ }
286
297
  branded() {
287
298
  return this;
288
299
  }
@@ -302,15 +313,6 @@ export class JsonSchemaStringBuilder extends JsonSchemaAnyBuilder {
302
313
  jwt() {
303
314
  return this.regex(JWT_REGEX);
304
315
  }
305
- transformModify(t, add) {
306
- if (add) {
307
- this.schema.transform = _uniq([...(this.schema.transform || []), t]);
308
- }
309
- else {
310
- this.schema.transform = this.schema.transform?.filter(s => s !== t);
311
- }
312
- return this;
313
- }
314
316
  }
315
317
  export class JsonSchemaObjectBuilder extends JsonSchemaAnyBuilder {
316
318
  constructor() {
@@ -322,6 +324,8 @@ export class JsonSchemaObjectBuilder extends JsonSchemaAnyBuilder {
322
324
  });
323
325
  }
324
326
  addProperties(props) {
327
+ if (!props)
328
+ return this;
325
329
  Object.entries(props).forEach(([k, builder]) => {
326
330
  const schema = builder.build();
327
331
  if (!schema.optionalField) {
@@ -44,6 +44,7 @@ export async function pTimeout(fn, opt) {
44
44
  _typeCast(err);
45
45
  // keep original stack
46
46
  err.stack = fakeError.stack.replace('Error: TimeoutError', err.name + ': ' + err.message);
47
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
47
48
  reject(_errorDataAppend(err, opt.errorData));
48
49
  }
49
50
  return;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
3
  "type": "module",
4
- "version": "15.40.2",
4
+ "version": "15.41.0",
5
5
  "dependencies": {
6
6
  "tslib": "^2",
7
7
  "undici": "^7",
@@ -101,9 +101,7 @@ export const j = {
101
101
  })
102
102
  },
103
103
  buffer() {
104
- return new JsonSchemaAnyBuilder<Buffer, JsonSchemaAny<Buffer>>({
105
- instanceof: 'Buffer',
106
- })
104
+ return new JsonSchemaAnyBuilder<Buffer, JsonSchemaAny<Buffer>>({ instanceof: 'Buffer' })
107
105
  },
108
106
 
109
107
  // number types
@@ -215,7 +213,7 @@ export class JsonSchemaAnyBuilder<
215
213
  }
216
214
 
217
215
  instanceof(of: string): this {
218
- this.schema.instanceof = of
216
+ Object.assign(this.schema, { type: 'object', instanceof: of })
219
217
  return this
220
218
  }
221
219
 
@@ -395,9 +393,25 @@ export class JsonSchemaStringBuilder<
395
393
  countryCode = (): this => this.format('countryCode')
396
394
  currency = (): this => this.format('currency')
397
395
 
398
- trim = (trim = true): this => this.transformModify('trim', trim)
399
- toLowerCase = (toLowerCase = true): this => this.transformModify('toLowerCase', toLowerCase)
400
- toUpperCase = (toUpperCase = true): this => this.transformModify('toUpperCase', toUpperCase)
396
+ trim(trim = true): this {
397
+ Object.assign(this.schema, { transform: { ...this.schema.transform, trim } })
398
+ return this
399
+ }
400
+
401
+ toLowerCase(toLowerCase = true): this {
402
+ Object.assign(this.schema, { transform: { ...this.schema.transform, toLowerCase } })
403
+ return this
404
+ }
405
+
406
+ toUpperCase(toUpperCase = true): this {
407
+ Object.assign(this.schema, { transform: { ...this.schema.transform, toUpperCase } })
408
+ return this
409
+ }
410
+
411
+ truncate(toLength: number): this {
412
+ Object.assign(this.schema, { transform: { ...this.schema.transform, truncate: toLength } })
413
+ return this
414
+ }
401
415
 
402
416
  branded<B extends string>(): JsonSchemaStringBuilder<B> {
403
417
  return this as unknown as JsonSchemaStringBuilder<B>
@@ -422,15 +436,6 @@ export class JsonSchemaStringBuilder<
422
436
  return this.regex(JWT_REGEX)
423
437
  }
424
438
 
425
- private transformModify(t: 'trim' | 'toLowerCase' | 'toUpperCase', add: boolean): this {
426
- if (add) {
427
- this.schema.transform = _uniq([...(this.schema.transform || []), t])
428
- } else {
429
- this.schema.transform = this.schema.transform?.filter(s => s !== t)
430
- }
431
- return this
432
- }
433
-
434
439
  // contentMediaType?: string
435
440
  // contentEncoding?: string
436
441
  }
@@ -448,7 +453,9 @@ export class JsonSchemaObjectBuilder<
448
453
  })
449
454
  }
450
455
 
451
- addProperties(props: { [k in keyof T]: JsonSchemaBuilder<T[k]> }): this {
456
+ addProperties(props?: { [k in keyof T]: JsonSchemaBuilder<T[k]> }): this {
457
+ if (!props) return this
458
+
452
459
  Object.entries(props).forEach(([k, builder]: [keyof T, JsonSchemaBuilder]) => {
453
460
  const schema = builder.build()
454
461
  if (!schema.optionalField) {
@@ -555,7 +562,7 @@ export class JsonSchemaTupleBuilder<T extends any[]> extends JsonSchemaAnyBuilde
555
562
  }
556
563
 
557
564
  function object<P extends Record<string, JsonSchemaAnyBuilder<any, any, any>>>(
558
- props: P,
565
+ props?: P,
559
566
  ): JsonSchemaObjectBuilder<
560
567
  {
561
568
  [K in keyof P as P[K] extends JsonSchemaAnyBuilder<any, any, infer Opt>
@@ -573,10 +580,10 @@ function object<P extends Record<string, JsonSchemaAnyBuilder<any, any, any>>>(
573
580
  ? { [K in keyof O]: O[K] }
574
581
  : never
575
582
  >
576
- function object<T extends AnyObject>(props: {
583
+ function object<T extends AnyObject>(props?: {
577
584
  [K in keyof T]: JsonSchemaAnyBuilder<T[K]>
578
585
  }): JsonSchemaObjectBuilder<T>
579
586
 
580
- function object(props: any): any {
587
+ function object(props?: any): any {
581
588
  return new JsonSchemaObjectBuilder<any>().addProperties(props)
582
589
  }
@@ -88,6 +88,7 @@ export async function pTimeout<T>(fn: AnyAsyncFunction<T>, opt: PTimeoutOptions)
88
88
  _typeCast<Error>(err)
89
89
  // keep original stack
90
90
  err.stack = fakeError.stack!.replace('Error: TimeoutError', err.name + ': ' + err.message)
91
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
91
92
  reject(_errorDataAppend(err, opt.errorData))
92
93
  }
93
94
  return