typeshi 2.1.1 → 2.1.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.
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/object.d.ts +39 -0
- package/dist/utils/object.js +70 -0
- package/package.json +1 -1
package/dist/utils/index.d.ts
CHANGED
package/dist/utils/index.js
CHANGED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/utils/object.ts
|
|
3
|
+
* @TODO just use zod instead
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @returns `boolean`
|
|
7
|
+
* - `true` if all keys in obj are also in validKeys
|
|
8
|
+
* - `false` if there exists a key in obj that is not in validKeys
|
|
9
|
+
*/
|
|
10
|
+
export declare function hasValidKeysOnly<T extends object>(obj: object, validKeys: readonly (keyof T)[]): obj is T;
|
|
11
|
+
/**
|
|
12
|
+
* @returns a new object containing only the specified keys.
|
|
13
|
+
*/
|
|
14
|
+
export declare function picked<T extends object, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>;
|
|
15
|
+
export declare class Restrict {
|
|
16
|
+
/**
|
|
17
|
+
* `hasValidKeysOnly`
|
|
18
|
+
* @returns `boolean` - `true` if `obj` contains ONLY keys found in `validKeys`
|
|
19
|
+
*/
|
|
20
|
+
static keys: typeof hasValidKeysOnly;
|
|
21
|
+
/**
|
|
22
|
+
* @returns a new object containing only the specified keys.
|
|
23
|
+
*/
|
|
24
|
+
static toPicked: typeof picked;
|
|
25
|
+
}
|
|
26
|
+
export type TransformationSchema<T> = {
|
|
27
|
+
[K in keyof T]?: (val: any) => T[K];
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* @param obj `any` - source object (e.g., Request Body)
|
|
31
|
+
* @param schema {@link TransformationSchema}`<T>` - map of keys to transformation functions
|
|
32
|
+
* @param passThroughKeys `(keyof T)[]` - keys to move over without transformation (Identity mapping)
|
|
33
|
+
* @returns **`data`** `T` - new object with keys/values from generated from `schema` & `passThroughKeys`
|
|
34
|
+
*/
|
|
35
|
+
export declare function sanitizeAndMap<T extends object>(obj: any, schema: TransformationSchema<T>, passThroughKeys?: (keyof T)[]): T;
|
|
36
|
+
/**
|
|
37
|
+
* @returns `Object.prototype.hasOwnProperty.call(obj, key) && obj[key] !== undefined;`
|
|
38
|
+
*/
|
|
39
|
+
export declare function hasDefinedEntry<T extends object>(obj: any, key: keyof T): boolean;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @file src/utils/object.ts
|
|
4
|
+
* @TODO just use zod instead
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.Restrict = void 0;
|
|
8
|
+
exports.hasValidKeysOnly = hasValidKeysOnly;
|
|
9
|
+
exports.picked = picked;
|
|
10
|
+
exports.sanitizeAndMap = sanitizeAndMap;
|
|
11
|
+
exports.hasDefinedEntry = hasDefinedEntry;
|
|
12
|
+
/**
|
|
13
|
+
* @returns `boolean`
|
|
14
|
+
* - `true` if all keys in obj are also in validKeys
|
|
15
|
+
* - `false` if there exists a key in obj that is not in validKeys
|
|
16
|
+
*/
|
|
17
|
+
function hasValidKeysOnly(obj, validKeys) {
|
|
18
|
+
return Object.keys(obj).every(key => validKeys.includes(key));
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* @returns a new object containing only the specified keys.
|
|
22
|
+
*/
|
|
23
|
+
function picked(obj, keys) {
|
|
24
|
+
const result = {};
|
|
25
|
+
for (const key of keys) {
|
|
26
|
+
if (key in obj && obj[key] !== undefined)
|
|
27
|
+
result[key] = obj[key];
|
|
28
|
+
}
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
class Restrict {
|
|
32
|
+
}
|
|
33
|
+
exports.Restrict = Restrict;
|
|
34
|
+
/**
|
|
35
|
+
* `hasValidKeysOnly`
|
|
36
|
+
* @returns `boolean` - `true` if `obj` contains ONLY keys found in `validKeys`
|
|
37
|
+
*/
|
|
38
|
+
Restrict.keys = hasValidKeysOnly;
|
|
39
|
+
/**
|
|
40
|
+
* @returns a new object containing only the specified keys.
|
|
41
|
+
*/
|
|
42
|
+
Restrict.toPicked = picked;
|
|
43
|
+
/**
|
|
44
|
+
* @param obj `any` - source object (e.g., Request Body)
|
|
45
|
+
* @param schema {@link TransformationSchema}`<T>` - map of keys to transformation functions
|
|
46
|
+
* @param passThroughKeys `(keyof T)[]` - keys to move over without transformation (Identity mapping)
|
|
47
|
+
* @returns **`data`** `T` - new object with keys/values from generated from `schema` & `passThroughKeys`
|
|
48
|
+
*/
|
|
49
|
+
function sanitizeAndMap(obj, schema, passThroughKeys = []) {
|
|
50
|
+
const data = {};
|
|
51
|
+
// 1. Handle explicit transformations
|
|
52
|
+
for (const key in schema) {
|
|
53
|
+
if (schema[key] && hasDefinedEntry(obj, key)) {
|
|
54
|
+
data[key] = schema[key](obj[key]);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
// 2. Handle simple pass-throughs (Identity mapping)
|
|
58
|
+
for (const key of passThroughKeys) {
|
|
59
|
+
if (!data[key] && hasDefinedEntry(obj, key)) {
|
|
60
|
+
data[key] = obj[key];
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return data;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* @returns `Object.prototype.hasOwnProperty.call(obj, key) && obj[key] !== undefined;`
|
|
67
|
+
*/
|
|
68
|
+
function hasDefinedEntry(obj, key) {
|
|
69
|
+
return Object.prototype.hasOwnProperty.call(obj, key) && obj[key] !== undefined;
|
|
70
|
+
}
|