typeshi 2.1.5 → 2.1.6
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/utils/object.d.ts +5 -5
- package/dist/utils/object.js +4 -2
- package/package.json +1 -1
package/dist/utils/object.d.ts
CHANGED
|
@@ -37,10 +37,10 @@ export declare class Restrict {
|
|
|
37
37
|
* - `defaultValue (T[K], optional)` - assign this value to `K` **only when** `transform` is `undefined`
|
|
38
38
|
* and `source[sourceKey]` is `undefined`
|
|
39
39
|
*/
|
|
40
|
-
export type TransformationSchema<T extends object, S extends
|
|
40
|
+
export type TransformationSchema<T extends object, S extends object = any> = {
|
|
41
41
|
[K in keyof T]?: ((val: S[keyof S]) => T[K]) | TransformOptions<T, K, S>;
|
|
42
42
|
};
|
|
43
|
-
export type TransformOptions<T extends object, K extends keyof T, S extends
|
|
43
|
+
export type TransformOptions<T extends object, K extends keyof T, S extends object = any> = {
|
|
44
44
|
transform?: (val: S[keyof S], ...args: any[]) => T[K];
|
|
45
45
|
args?: any[];
|
|
46
46
|
defaultValue?: T[K];
|
|
@@ -49,11 +49,11 @@ export type TransformOptions<T extends object, K extends keyof T, S extends Reco
|
|
|
49
49
|
/**
|
|
50
50
|
* @param obj `S` - source object (e.g., Request Body)
|
|
51
51
|
* @param schema {@link TransformationSchema}`<T, S>` - map of keys to transformation functions.
|
|
52
|
-
* @param passThroughKeys `(keyof T)[] (optional)` - keys to move over without transformation (Identity mapping)
|
|
52
|
+
* @param passThroughKeys `(keyof T & keyof S)[] (optional)` - keys to move over without transformation (Identity mapping)
|
|
53
53
|
* @returns **`data`** `T` - new object with keys/values from generated from `schema` & `passThroughKeys`
|
|
54
54
|
*/
|
|
55
|
-
export declare function sanitizeAndMap<T extends object, S extends
|
|
55
|
+
export declare function sanitizeAndMap<T extends object, S extends object = any>(obj: S, schema: TransformationSchema<T, S>, passThroughKeys?: (keyof T & keyof S)[]): T;
|
|
56
56
|
/**
|
|
57
57
|
* @returns `Object.prototype.hasOwnProperty.call(obj, key) && obj[key] !== undefined;`
|
|
58
58
|
*/
|
|
59
|
-
export declare function hasDefinedEntry<T extends object>(obj: any, key: keyof T): boolean;
|
|
59
|
+
export declare function hasDefinedEntry<T extends object>(obj: any, key: keyof T | keyof any): boolean;
|
package/dist/utils/object.js
CHANGED
|
@@ -44,7 +44,7 @@ Restrict.toPicked = picked;
|
|
|
44
44
|
/**
|
|
45
45
|
* @param obj `S` - source object (e.g., Request Body)
|
|
46
46
|
* @param schema {@link TransformationSchema}`<T, S>` - map of keys to transformation functions.
|
|
47
|
-
* @param passThroughKeys `(keyof T)[] (optional)` - keys to move over without transformation (Identity mapping)
|
|
47
|
+
* @param passThroughKeys `(keyof T & keyof S)[] (optional)` - keys to move over without transformation (Identity mapping)
|
|
48
48
|
* @returns **`data`** `T` - new object with keys/values from generated from `schema` & `passThroughKeys`
|
|
49
49
|
*/
|
|
50
50
|
function sanitizeAndMap(obj, schema, passThroughKeys = []) {
|
|
@@ -54,7 +54,9 @@ function sanitizeAndMap(obj, schema, passThroughKeys = []) {
|
|
|
54
54
|
const schemaValue = schema[key];
|
|
55
55
|
if (!schemaValue)
|
|
56
56
|
continue;
|
|
57
|
-
const sourceKey = 'sourceKey' in schemaValue && schemaValue.sourceKey
|
|
57
|
+
const sourceKey = ('sourceKey' in schemaValue && schemaValue.sourceKey
|
|
58
|
+
? schemaValue.sourceKey
|
|
59
|
+
: key);
|
|
58
60
|
if (hasDefinedEntry(obj, sourceKey)) {
|
|
59
61
|
if ((0, typeValidation_1.isFunction)(schemaValue)) { // schemaValue is ((val: S[keyof S]) => T[K])
|
|
60
62
|
data[key] = schemaValue(obj[sourceKey]);
|