serverless-spy 0.0.61 → 0.0.63
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/.jsii +3 -2
- package/_config.yml +15 -3
- package/_includes/head/custom.html +6 -0
- package/assets/images/site-logo.png +0 -0
- package/dist/releasetag.txt +1 -1
- package/extension/interceptor.ts +5 -1
- package/lib/extension/dist/layer/nodejs/node_modules/interceptor.js +147 -2
- package/lib/extension/dist/layer/nodejs/node_modules/interceptor.js.map +4 -4
- package/lib/src/ServerlessSpy.js +1 -1
- package/logo/full_logo.png +0 -0
- package/node_modules/serialize-error/error-constructors.d.ts +8 -0
- package/node_modules/serialize-error/error-constructors.js +26 -0
- package/node_modules/serialize-error/index.d.ts +171 -0
- package/node_modules/serialize-error/index.js +205 -0
- package/node_modules/serialize-error/license +9 -0
- package/node_modules/serialize-error/node_modules/type-fest/index.d.ts +95 -0
- package/node_modules/serialize-error/node_modules/type-fest/package.json +52 -0
- package/node_modules/serialize-error/node_modules/type-fest/readme.md +905 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/async-return-type.d.ts +25 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/asyncify.d.ts +33 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/basic.d.ts +45 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/camel-case.d.ts +73 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/camel-cased-properties-deep.d.ts +54 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/camel-cased-properties.d.ts +36 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/conditional-except.d.ts +45 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/conditional-keys.d.ts +47 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/conditional-pick.d.ts +44 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/delimiter-case.d.ts +93 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/delimiter-cased-properties-deep.d.ts +60 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/delimiter-cased-properties.d.ts +37 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/entries.d.ts +62 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/entry.d.ts +65 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/exact.d.ts +73 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/except.d.ts +57 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/fixed-length-array.d.ts +43 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/get.d.ts +184 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/has-optional-keys.d.ts +21 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/has-required-keys.d.ts +59 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/includes.d.ts +22 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/internal.d.ts +59 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/invariant-of.d.ts +76 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/iterable-element.d.ts +54 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/join.d.ts +30 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/jsonify.d.ts +90 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/kebab-case.d.ts +38 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/kebab-cased-properties-deep.d.ts +47 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/kebab-cased-properties.d.ts +30 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/last-array-element.d.ts +28 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/literal-to-primitive.d.ts +36 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/literal-union.d.ts +35 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/merge-exclusive.d.ts +41 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/merge.d.ts +27 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/multidimensional-array.d.ts +43 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/multidimensional-readonly-array.d.ts +47 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/mutable.d.ts +5 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/numeric.d.ts +170 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/observable-like.d.ts +62 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/opaque.d.ts +107 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/optional-keys-of.d.ts +38 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/package-json.d.ts +663 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/partial-deep.d.ts +113 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/partial-on-undefined-deep.d.ts +70 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/pascal-case.d.ts +38 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/pascal-cased-properties-deep.d.ts +54 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/pascal-cased-properties.d.ts +34 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/primitive.d.ts +13 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/promisable.d.ts +25 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/promise-value.d.ts +29 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/readonly-deep.d.ts +85 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/readonly-tuple.d.ts +41 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/remove-index-signature.d.ts +104 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/replace.d.ts +67 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/require-all-or-none.d.ts +36 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/require-at-least-one.d.ts +35 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/require-exactly-one.d.ts +34 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/required-keys-of.d.ts +29 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/schema.d.ts +72 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/screaming-snake-case.d.ts +33 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/set-non-nullable.d.ts +35 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/set-optional.d.ts +35 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/set-required.d.ts +35 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/set-return-type.d.ts +31 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/simplify.d.ts +83 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/snake-case.d.ts +38 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/snake-cased-properties-deep.d.ts +47 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/snake-cased-properties.d.ts +30 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/split.d.ts +29 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/spread.d.ts +85 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/string-key-of.d.ts +25 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/stringified.d.ts +23 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/trim.d.ts +25 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/tsconfig-json.d.ts +1172 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/typed-array.d.ts +17 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/union-to-intersection.d.ts +60 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/value-of.d.ts +42 -0
- package/node_modules/serialize-error/node_modules/type-fest/source/writable.d.ts +40 -0
- package/node_modules/serialize-error/package.json +46 -0
- package/node_modules/serialize-error/readme.md +198 -0
- package/package.json +6 -4
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Create a type that requires all of the given keys or none of the given keys. The remaining keys are kept as is.
|
|
3
|
+
|
|
4
|
+
Use-cases:
|
|
5
|
+
- Creating interfaces for components with mutually-inclusive keys.
|
|
6
|
+
|
|
7
|
+
The caveat with `RequireAllOrNone` is that TypeScript doesn't always know at compile time every key that will exist at runtime. Therefore `RequireAllOrNone` can't do anything to prevent extra keys it doesn't know about.
|
|
8
|
+
|
|
9
|
+
@example
|
|
10
|
+
```
|
|
11
|
+
import type {RequireAllOrNone} from 'type-fest';
|
|
12
|
+
|
|
13
|
+
type Responder = {
|
|
14
|
+
text?: () => string;
|
|
15
|
+
json?: () => string;
|
|
16
|
+
secure: boolean;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const responder1: RequireAllOrNone<Responder, 'text' | 'json'> = {
|
|
20
|
+
secure: true
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const responder2: RequireAllOrNone<Responder, 'text' | 'json'> = {
|
|
24
|
+
text: () => '{"message": "hi"}',
|
|
25
|
+
json: () => '{"message": "ok"}',
|
|
26
|
+
secure: true
|
|
27
|
+
};
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
@category Object
|
|
31
|
+
*/
|
|
32
|
+
export type RequireAllOrNone<ObjectType, KeysType extends keyof ObjectType = never> = (
|
|
33
|
+
| Required<Pick<ObjectType, KeysType>> // Require all of the given keys.
|
|
34
|
+
| Partial<Record<KeysType, never>> // Require none of the given keys.
|
|
35
|
+
) &
|
|
36
|
+
Omit<ObjectType, KeysType>; // The rest of the keys.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type {Except} from './except';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Create a type that requires at least one of the given keys. The remaining keys are kept as is.
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```
|
|
8
|
+
import type {RequireAtLeastOne} from 'type-fest';
|
|
9
|
+
|
|
10
|
+
type Responder = {
|
|
11
|
+
text?: () => string;
|
|
12
|
+
json?: () => string;
|
|
13
|
+
|
|
14
|
+
secure?: boolean;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const responder: RequireAtLeastOne<Responder, 'text' | 'json'> = {
|
|
18
|
+
json: () => '{"message": "ok"}',
|
|
19
|
+
secure: true
|
|
20
|
+
};
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
@category Object
|
|
24
|
+
*/
|
|
25
|
+
export type RequireAtLeastOne<
|
|
26
|
+
ObjectType,
|
|
27
|
+
KeysType extends keyof ObjectType = keyof ObjectType,
|
|
28
|
+
> = {
|
|
29
|
+
// For each `Key` in `KeysType` make a mapped type:
|
|
30
|
+
[Key in KeysType]-?: Required<Pick<ObjectType, Key>> & // 1. Make `Key`'s type required
|
|
31
|
+
// 2. Make all other keys in `KeysType` optional
|
|
32
|
+
Partial<Pick<ObjectType, Exclude<KeysType, Key>>>;
|
|
33
|
+
}[KeysType] &
|
|
34
|
+
// 3. Add the remaining keys not in `KeysType`
|
|
35
|
+
Except<ObjectType, KeysType>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Create a type that requires exactly one of the given keys and disallows more. The remaining keys are kept as is.
|
|
3
|
+
|
|
4
|
+
Use-cases:
|
|
5
|
+
- Creating interfaces for components that only need one of the keys to display properly.
|
|
6
|
+
- Declaring generic keys in a single place for a single use-case that gets narrowed down via `RequireExactlyOne`.
|
|
7
|
+
|
|
8
|
+
The caveat with `RequireExactlyOne` is that TypeScript doesn't always know at compile time every key that will exist at runtime. Therefore `RequireExactlyOne` can't do anything to prevent extra keys it doesn't know about.
|
|
9
|
+
|
|
10
|
+
@example
|
|
11
|
+
```
|
|
12
|
+
import type {RequireExactlyOne} from 'type-fest';
|
|
13
|
+
|
|
14
|
+
type Responder = {
|
|
15
|
+
text: () => string;
|
|
16
|
+
json: () => string;
|
|
17
|
+
secure: boolean;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const responder: RequireExactlyOne<Responder, 'text' | 'json'> = {
|
|
21
|
+
// Adding a `text` key here would cause a compile error.
|
|
22
|
+
|
|
23
|
+
json: () => '{"message": "ok"}',
|
|
24
|
+
secure: true
|
|
25
|
+
};
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
@category Object
|
|
29
|
+
*/
|
|
30
|
+
export type RequireExactlyOne<ObjectType, KeysType extends keyof ObjectType = keyof ObjectType> =
|
|
31
|
+
{[Key in KeysType]: (
|
|
32
|
+
Required<Pick<ObjectType, Key>> &
|
|
33
|
+
Partial<Record<Exclude<KeysType, Key>, never>>
|
|
34
|
+
)}[KeysType] & Omit<ObjectType, KeysType>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Extract all required keys from the given type.
|
|
3
|
+
|
|
4
|
+
This is useful when you want to create a new type that contains different type values for the required keys only or use the list of keys for validation purposes, etc...
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```
|
|
8
|
+
import type {RequiredKeysOf} from 'type-fest';
|
|
9
|
+
|
|
10
|
+
declare function createValidation<Entity extends object, Key extends RequiredKeysOf<Entity> = RequiredKeysOf<Entity>>(field: Key, validator: (value: Entity[Key]) => boolean): ValidatorFn;
|
|
11
|
+
|
|
12
|
+
interface User {
|
|
13
|
+
name: string;
|
|
14
|
+
surname: string;
|
|
15
|
+
|
|
16
|
+
luckyNumber?: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const validator1 = createValidation<User>('name', value => value.length < 25);
|
|
20
|
+
const validator2 = createValidation<User>('surname', value => value.length < 25);
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
@category Utilities
|
|
24
|
+
*/
|
|
25
|
+
export type RequiredKeysOf<BaseType extends object> = Exclude<{
|
|
26
|
+
[Key in keyof BaseType]: BaseType extends Record<Key, BaseType[Key]>
|
|
27
|
+
? Key
|
|
28
|
+
: never
|
|
29
|
+
}[keyof BaseType], undefined>;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Create a deep version of another object type where property values are recursively replaced into a given value type.
|
|
3
|
+
|
|
4
|
+
Use-cases:
|
|
5
|
+
- Form validation: Define how each field should be validated.
|
|
6
|
+
- Form settings: Define configuration for input fields.
|
|
7
|
+
- Parsing: Define types that specify special behavior for specific fields.
|
|
8
|
+
|
|
9
|
+
@example
|
|
10
|
+
```
|
|
11
|
+
import type {Schema} from 'type-fest';
|
|
12
|
+
|
|
13
|
+
interface User {
|
|
14
|
+
id: string;
|
|
15
|
+
name: {
|
|
16
|
+
firstname: string;
|
|
17
|
+
lastname: string;
|
|
18
|
+
};
|
|
19
|
+
created: Date;
|
|
20
|
+
active: boolean;
|
|
21
|
+
passwordHash: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
type UserMask = Schema<User, 'mask' | 'hide' | 'show'>;
|
|
25
|
+
|
|
26
|
+
const userMaskSettings: UserMask = {
|
|
27
|
+
id: 'show',
|
|
28
|
+
name: {
|
|
29
|
+
firstname: 'show',
|
|
30
|
+
lastname: 'mask',
|
|
31
|
+
},
|
|
32
|
+
phoneNumbers: 'mask',
|
|
33
|
+
created: 'show',
|
|
34
|
+
active: 'show',
|
|
35
|
+
passwordHash: 'hide',
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
@category Object
|
|
40
|
+
*/
|
|
41
|
+
export type Schema<ObjectType, ValueType> = ObjectType extends string
|
|
42
|
+
? ValueType
|
|
43
|
+
: ObjectType extends Map<unknown, unknown>
|
|
44
|
+
? ValueType
|
|
45
|
+
: ObjectType extends Set<unknown>
|
|
46
|
+
? ValueType
|
|
47
|
+
: ObjectType extends ReadonlyMap<unknown, unknown>
|
|
48
|
+
? ValueType
|
|
49
|
+
: ObjectType extends ReadonlySet<unknown>
|
|
50
|
+
? ValueType
|
|
51
|
+
: ObjectType extends readonly unknown[]
|
|
52
|
+
? ValueType
|
|
53
|
+
: ObjectType extends unknown[]
|
|
54
|
+
? ValueType
|
|
55
|
+
: ObjectType extends (...arguments: unknown[]) => unknown
|
|
56
|
+
? ValueType
|
|
57
|
+
: ObjectType extends Date
|
|
58
|
+
? ValueType
|
|
59
|
+
: ObjectType extends Function
|
|
60
|
+
? ValueType
|
|
61
|
+
: ObjectType extends RegExp
|
|
62
|
+
? ValueType
|
|
63
|
+
: ObjectType extends object
|
|
64
|
+
? SchemaObject<ObjectType, ValueType>
|
|
65
|
+
: ValueType;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
Same as `Schema`, but accepts only `object`s as inputs. Internal helper for `Schema`.
|
|
69
|
+
*/
|
|
70
|
+
type SchemaObject<ObjectType extends object, K> = {
|
|
71
|
+
[KeyType in keyof ObjectType]: Schema<ObjectType[KeyType], K> | K;
|
|
72
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type {SplitIncludingDelimiters} from './delimiter-case';
|
|
2
|
+
import type {SnakeCase} from './snake-case';
|
|
3
|
+
import type {Includes} from './includes';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
Returns a boolean for whether the string is screaming snake case.
|
|
7
|
+
*/
|
|
8
|
+
type IsScreamingSnakeCase<Value extends string> = Value extends Uppercase<Value>
|
|
9
|
+
? Includes<SplitIncludingDelimiters<Lowercase<Value>, '_'>, '_'> extends true
|
|
10
|
+
? true
|
|
11
|
+
: false
|
|
12
|
+
: false;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
Convert a string literal to screaming-snake-case.
|
|
16
|
+
|
|
17
|
+
This can be useful when, for example, converting a camel-cased object property to a screaming-snake-cased SQL column name.
|
|
18
|
+
|
|
19
|
+
@example
|
|
20
|
+
```
|
|
21
|
+
import type {ScreamingSnakeCase} from 'type-fest';
|
|
22
|
+
|
|
23
|
+
const someVariable: ScreamingSnakeCase<'fooBar'> = 'FOO_BAR';
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
@category Change case
|
|
27
|
+
@category Template literal
|
|
28
|
+
*/
|
|
29
|
+
export type ScreamingSnakeCase<Value> = Value extends string
|
|
30
|
+
? IsScreamingSnakeCase<Value> extends true
|
|
31
|
+
? Value
|
|
32
|
+
: Uppercase<SnakeCase<Value>>
|
|
33
|
+
: Value;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type {Except} from './except';
|
|
2
|
+
import type {Simplify} from './simplify';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
Create a type that makes the given keys non-nullable. The remaining keys are kept as is.
|
|
6
|
+
|
|
7
|
+
Use-case: You want to define a single model where the only thing that changes is whether or not some of the keys are non-nullable.
|
|
8
|
+
|
|
9
|
+
@example
|
|
10
|
+
```
|
|
11
|
+
import type {SetNonNullable} from 'type-fest';
|
|
12
|
+
|
|
13
|
+
type Foo = {
|
|
14
|
+
a: number;
|
|
15
|
+
b: string | undefined;
|
|
16
|
+
c?: boolean | null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type SomeNonNullable = SetNonNullable<Foo, 'b' | 'c'>;
|
|
20
|
+
// type SomeNonNullable = {
|
|
21
|
+
// a: number;
|
|
22
|
+
// b: string; // Can no longer be undefined.
|
|
23
|
+
// c?: boolean; // Can no longer be null, but is still optional.
|
|
24
|
+
// }
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
@category Object
|
|
28
|
+
*/
|
|
29
|
+
export type SetNonNullable<BaseType, Keys extends keyof BaseType> =
|
|
30
|
+
Simplify<
|
|
31
|
+
// Pick just the keys that are readonly from the base type.
|
|
32
|
+
Except<BaseType, Keys> &
|
|
33
|
+
// Pick the keys that should be non-nullable from the base type and make them non-nullable.
|
|
34
|
+
{[Key in Keys]: NonNullable<BaseType[Key]>}
|
|
35
|
+
>;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type {Except} from './except';
|
|
2
|
+
import type {Simplify} from './simplify';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
Create a type that makes the given keys optional. The remaining keys are kept as is. The sister of the `SetRequired` type.
|
|
6
|
+
|
|
7
|
+
Use-case: You want to define a single model where the only thing that changes is whether or not some of the keys are optional.
|
|
8
|
+
|
|
9
|
+
@example
|
|
10
|
+
```
|
|
11
|
+
import type {SetOptional} from 'type-fest';
|
|
12
|
+
|
|
13
|
+
type Foo = {
|
|
14
|
+
a: number;
|
|
15
|
+
b?: string;
|
|
16
|
+
c: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type SomeOptional = SetOptional<Foo, 'b' | 'c'>;
|
|
20
|
+
// type SomeOptional = {
|
|
21
|
+
// a: number;
|
|
22
|
+
// b?: string; // Was already optional and still is.
|
|
23
|
+
// c?: boolean; // Is now optional.
|
|
24
|
+
// }
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
@category Object
|
|
28
|
+
*/
|
|
29
|
+
export type SetOptional<BaseType, Keys extends keyof BaseType> =
|
|
30
|
+
Simplify<
|
|
31
|
+
// Pick just the keys that are readonly from the base type.
|
|
32
|
+
Except<BaseType, Keys> &
|
|
33
|
+
// Pick the keys that should be mutable from the base type and make them mutable.
|
|
34
|
+
Partial<Pick<BaseType, Keys>>
|
|
35
|
+
>;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type {Except} from './except';
|
|
2
|
+
import type {Simplify} from './simplify';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
Create a type that makes the given keys required. The remaining keys are kept as is. The sister of the `SetOptional` type.
|
|
6
|
+
|
|
7
|
+
Use-case: You want to define a single model where the only thing that changes is whether or not some of the keys are required.
|
|
8
|
+
|
|
9
|
+
@example
|
|
10
|
+
```
|
|
11
|
+
import type {SetRequired} from 'type-fest';
|
|
12
|
+
|
|
13
|
+
type Foo = {
|
|
14
|
+
a?: number;
|
|
15
|
+
b: string;
|
|
16
|
+
c?: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type SomeRequired = SetRequired<Foo, 'b' | 'c'>;
|
|
20
|
+
// type SomeRequired = {
|
|
21
|
+
// a?: number;
|
|
22
|
+
// b: string; // Was already required and still is.
|
|
23
|
+
// c: boolean; // Is now required.
|
|
24
|
+
// }
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
@category Object
|
|
28
|
+
*/
|
|
29
|
+
export type SetRequired<BaseType, Keys extends keyof BaseType> =
|
|
30
|
+
Simplify<
|
|
31
|
+
// Pick just the keys that are optional from the base type.
|
|
32
|
+
Except<BaseType, Keys> &
|
|
33
|
+
// Pick the keys that should be required from the base type and make them required.
|
|
34
|
+
Required<Pick<BaseType, Keys>>
|
|
35
|
+
>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
type IsAny<T> = 0 extends (1 & T) ? true : false; // https://stackoverflow.com/a/49928360/3406963
|
|
2
|
+
type IsNever<T> = [T] extends [never] ? true : false;
|
|
3
|
+
type IsUnknown<T> = IsNever<T> extends false ? T extends unknown ? unknown extends T ? IsAny<T> extends false ? true : false : false : false : false;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
Create a function type with a return type of your choice and the same parameters as the given function type.
|
|
7
|
+
|
|
8
|
+
Use-case: You want to define a wrapped function that returns something different while receiving the same parameters. For example, you might want to wrap a function that can throw an error into one that will return `undefined` instead.
|
|
9
|
+
|
|
10
|
+
@example
|
|
11
|
+
```
|
|
12
|
+
import type {SetReturnType} from 'type-fest';
|
|
13
|
+
|
|
14
|
+
type MyFunctionThatCanThrow = (foo: SomeType, bar: unknown) => SomeOtherType;
|
|
15
|
+
|
|
16
|
+
type MyWrappedFunction = SetReturnType<MyFunctionThatCanThrow, SomeOtherType | undefined>;
|
|
17
|
+
//=> type MyWrappedFunction = (foo: SomeType, bar: unknown) => SomeOtherType | undefined;
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
@category Function
|
|
21
|
+
*/
|
|
22
|
+
export type SetReturnType<Fn extends (...args: any[]) => any, TypeToReturn> =
|
|
23
|
+
// Just using `Parameters<Fn>` isn't ideal because it doesn't handle the `this` fake parameter.
|
|
24
|
+
Fn extends (this: infer ThisArg, ...args: infer Arguments) => any ? (
|
|
25
|
+
// If a function did not specify the `this` fake parameter, it will be inferred to `unknown`.
|
|
26
|
+
// We want to detect this situation just to display a friendlier type upon hovering on an IntelliSense-powered IDE.
|
|
27
|
+
IsUnknown<ThisArg> extends true ? (...args: Arguments) => TypeToReturn : (this: ThisArg, ...args: Arguments) => TypeToReturn
|
|
28
|
+
) : (
|
|
29
|
+
// This part should be unreachable, but we make it meaningful just in case…
|
|
30
|
+
(...args: Parameters<Fn>) => TypeToReturn
|
|
31
|
+
);
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
@see Simplify
|
|
3
|
+
*/
|
|
4
|
+
export interface SimplifyOptions {
|
|
5
|
+
/**
|
|
6
|
+
Do the simplification recursively.
|
|
7
|
+
|
|
8
|
+
@default false
|
|
9
|
+
*/
|
|
10
|
+
deep?: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Flatten a type without worrying about the result.
|
|
14
|
+
type Flatten<
|
|
15
|
+
AnyType,
|
|
16
|
+
Options extends SimplifyOptions = {},
|
|
17
|
+
> = Options['deep'] extends true
|
|
18
|
+
? {[KeyType in keyof AnyType]: Simplify<AnyType[KeyType], Options>}
|
|
19
|
+
: {[KeyType in keyof AnyType]: AnyType[KeyType]};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.
|
|
23
|
+
|
|
24
|
+
@example
|
|
25
|
+
```
|
|
26
|
+
import type {Simplify} from 'type-fest';
|
|
27
|
+
|
|
28
|
+
type PositionProps = {
|
|
29
|
+
top: number;
|
|
30
|
+
left: number;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
type SizeProps = {
|
|
34
|
+
width: number;
|
|
35
|
+
height: number;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// In your editor, hovering over `Props` will show a flattened object with all the properties.
|
|
39
|
+
type Props = Simplify<PositionProps & SizeProps>;
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Sometimes it is desired to pass a value as a function argument that has a different type. At first inspection it may seem assignable, and then you discover it is not because the `value`'s type definition was defined as an interface. In the following example, `fn` requires an argument of type `Record<string, unknown>`. If the value is defined as a literal, then it is assignable. And if the `value` is defined as type using the `Simplify` utility the value is assignable. But if the `value` is defined as an interface, it is not assignable because the interface is not sealed and elsewhere a non-string property could be added to the interface.
|
|
43
|
+
|
|
44
|
+
If the type definition must be an interface (perhaps it was defined in a third-party npm package), then the `value` can be defined as `const value: Simplify<SomeInterface> = ...`. Then `value` will be assignable to the `fn` argument. Or the `value` can be cast as `Simplify<SomeInterface>` if you can't re-declare the `value`.
|
|
45
|
+
|
|
46
|
+
@example
|
|
47
|
+
```
|
|
48
|
+
import type {Simplify} from 'type-fest';
|
|
49
|
+
|
|
50
|
+
interface SomeInterface {
|
|
51
|
+
foo: number;
|
|
52
|
+
bar?: string;
|
|
53
|
+
baz: number | undefined;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
type SomeType = {
|
|
57
|
+
foo: number;
|
|
58
|
+
bar?: string;
|
|
59
|
+
baz: number | undefined;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const literal = {foo: 123, bar: 'hello', baz: 456};
|
|
63
|
+
const someType: SomeType = literal;
|
|
64
|
+
const someInterface: SomeInterface = literal;
|
|
65
|
+
|
|
66
|
+
function fn(object: Record<string, unknown>): void {}
|
|
67
|
+
|
|
68
|
+
fn(literal); // Good: literal object type is sealed
|
|
69
|
+
fn(someType); // Good: type is sealed
|
|
70
|
+
fn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-opened
|
|
71
|
+
fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type`
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
@link https://github.com/microsoft/TypeScript/issues/15300
|
|
75
|
+
|
|
76
|
+
@category Object
|
|
77
|
+
*/
|
|
78
|
+
export type Simplify<
|
|
79
|
+
AnyType,
|
|
80
|
+
Options extends SimplifyOptions = {},
|
|
81
|
+
> = Flatten<AnyType> extends AnyType
|
|
82
|
+
? Flatten<AnyType, Options>
|
|
83
|
+
: AnyType;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type {DelimiterCase} from './delimiter-case';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Convert a string literal to snake-case.
|
|
5
|
+
|
|
6
|
+
This can be useful when, for example, converting a camel-cased object property to a snake-cased SQL column name.
|
|
7
|
+
|
|
8
|
+
@example
|
|
9
|
+
```
|
|
10
|
+
import type {SnakeCase} from 'type-fest';
|
|
11
|
+
|
|
12
|
+
// Simple
|
|
13
|
+
|
|
14
|
+
const someVariable: SnakeCase<'fooBar'> = 'foo_bar';
|
|
15
|
+
|
|
16
|
+
// Advanced
|
|
17
|
+
|
|
18
|
+
type SnakeCasedProperties<T> = {
|
|
19
|
+
[K in keyof T as SnakeCase<K>]: T[K]
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
interface ModelProps {
|
|
23
|
+
isHappy: boolean;
|
|
24
|
+
fullFamilyName: string;
|
|
25
|
+
foo: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const dbResult: SnakeCasedProperties<ModelProps> = {
|
|
29
|
+
'is_happy': true,
|
|
30
|
+
'full_family_name': 'Carla Smith',
|
|
31
|
+
foo: 123
|
|
32
|
+
};
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
@category Change case
|
|
36
|
+
@category Template literal
|
|
37
|
+
*/
|
|
38
|
+
export type SnakeCase<Value> = DelimiterCase<Value, '_'>;
|
package/node_modules/serialize-error/node_modules/type-fest/source/snake-cased-properties-deep.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type {DelimiterCasedPropertiesDeep} from './delimiter-cased-properties-deep';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Convert object properties to snake case recursively.
|
|
5
|
+
|
|
6
|
+
This can be useful when, for example, converting some API types from a different style.
|
|
7
|
+
|
|
8
|
+
@see SnakeCase
|
|
9
|
+
@see SnakeCasedProperties
|
|
10
|
+
|
|
11
|
+
@example
|
|
12
|
+
```
|
|
13
|
+
import type {SnakeCasedPropertiesDeep} from 'type-fest';
|
|
14
|
+
|
|
15
|
+
interface User {
|
|
16
|
+
userId: number;
|
|
17
|
+
userName: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface UserWithFriends {
|
|
21
|
+
userInfo: User;
|
|
22
|
+
userFriends: User[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const result: SnakeCasedPropertiesDeep<UserWithFriends> = {
|
|
26
|
+
user_info: {
|
|
27
|
+
user_id: 1,
|
|
28
|
+
user_name: 'Tom',
|
|
29
|
+
},
|
|
30
|
+
user_friends: [
|
|
31
|
+
{
|
|
32
|
+
user_id: 2,
|
|
33
|
+
user_name: 'Jerry',
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
user_id: 3,
|
|
37
|
+
user_name: 'Spike',
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
};
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
@category Change case
|
|
44
|
+
@category Template literal
|
|
45
|
+
@category Object
|
|
46
|
+
*/
|
|
47
|
+
export type SnakeCasedPropertiesDeep<Value> = DelimiterCasedPropertiesDeep<Value, '_'>;
|
package/node_modules/serialize-error/node_modules/type-fest/source/snake-cased-properties.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type {DelimiterCasedProperties} from './delimiter-cased-properties';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Convert object properties to snake case but not recursively.
|
|
5
|
+
|
|
6
|
+
This can be useful when, for example, converting some API types from a different style.
|
|
7
|
+
|
|
8
|
+
@see SnakeCase
|
|
9
|
+
@see SnakeCasedPropertiesDeep
|
|
10
|
+
|
|
11
|
+
@example
|
|
12
|
+
```
|
|
13
|
+
import type {SnakeCasedProperties} from 'type-fest';
|
|
14
|
+
|
|
15
|
+
interface User {
|
|
16
|
+
userId: number;
|
|
17
|
+
userName: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const result: SnakeCasedProperties<User> = {
|
|
21
|
+
user_id: 1,
|
|
22
|
+
user_name: 'Tom',
|
|
23
|
+
};
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
@category Change case
|
|
27
|
+
@category Template literal
|
|
28
|
+
@category Object
|
|
29
|
+
*/
|
|
30
|
+
export type SnakeCasedProperties<Value> = DelimiterCasedProperties<Value, '_'>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Represents an array of strings split using a given character or character set.
|
|
3
|
+
|
|
4
|
+
Use-case: Defining the return type of a method like `String.prototype.split`.
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```
|
|
8
|
+
import type {Split} from 'type-fest';
|
|
9
|
+
|
|
10
|
+
declare function split<S extends string, D extends string>(string: S, separator: D): Split<S, D>;
|
|
11
|
+
|
|
12
|
+
type Item = 'foo' | 'bar' | 'baz' | 'waldo';
|
|
13
|
+
const items = 'foo,bar,baz,waldo';
|
|
14
|
+
let array: Item[];
|
|
15
|
+
|
|
16
|
+
array = split(items, ',');
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
@category String
|
|
20
|
+
@category Template literal
|
|
21
|
+
*/
|
|
22
|
+
export type Split<
|
|
23
|
+
S extends string,
|
|
24
|
+
Delimiter extends string,
|
|
25
|
+
> = S extends `${infer Head}${Delimiter}${infer Tail}`
|
|
26
|
+
? [Head, ...Split<Tail, Delimiter>]
|
|
27
|
+
: S extends Delimiter
|
|
28
|
+
? []
|
|
29
|
+
: [S];
|