@zimic/fetch 1.2.7-canary.5 → 1.2.7

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.
Files changed (2) hide show
  1. package/dist/index.d.ts +62 -2
  2. package/package.json +1 -1
package/dist/index.d.ts CHANGED
@@ -6,15 +6,75 @@ type JSON = {
6
6
  declare namespace JSON {
7
7
  type Loose = Record<string, any> | Loose[] | string | number | boolean | null | undefined;
8
8
  }
9
+ /**
10
+ * Represents or validates a type that is compatible with JSON.
11
+ *
12
+ * **IMPORTANT**: the input of `JSONValue` and all of its internal types must be declared inline or as a type aliases
13
+ * (`type`). They cannot be interfaces.
14
+ *
15
+ * @example
16
+ * import { type JSONValue } from '@zimic/http';
17
+ *
18
+ * // Can be used as a standalone type:
19
+ * const value: JSONValue = {
20
+ * name: 'example',
21
+ * tags: ['one', 'two'],
22
+ * };
23
+ *
24
+ * @example
25
+ * import { type JSONValue } from '@zimic/http';
26
+ *
27
+ * // Can be used with a type argument to validate a JSON value:
28
+ * type ValidJSON = JSONValue<{
29
+ * id: string;
30
+ * email: string;
31
+ * createdAt: string;
32
+ * }>;
33
+ *
34
+ * // This results in a type error:
35
+ * type InvalidJSON = JSONValue<{
36
+ * id: string;
37
+ * email: string;
38
+ * createdAt: Date; // `Date` is not a valid JSON value.
39
+ * save: () => Promise<void>; // Functions are not valid JSON values.
40
+ * }>;
41
+ */
42
+ type JSONValue<Type extends JSON = JSON> = Type;
43
+ declare namespace JSONValue {
44
+ /** A loose version of the JSON value type. JSON objects are not strictly typed. */
45
+ type Loose<Type extends JSON.Loose = JSON.Loose> = Type;
46
+ }
47
+ /**
48
+ * Recursively converts a type to its JSON-serialized version. Dates are converted to strings and keys with non-JSON
49
+ * values are excluded.
50
+ *
51
+ * @example
52
+ * import { type JSONSerialized } from '@zimic/http';
53
+ *
54
+ * type SerializedUser = JSONSerialized<{
55
+ * id: string;
56
+ * email: string;
57
+ * createdAt: Date;
58
+ * save: () => Promise<void>;
59
+ * }>;
60
+ * // {
61
+ * // id: string;
62
+ * // email: string;
63
+ * // createdAt: string;
64
+ * // }
65
+ */
66
+ type JSONSerialized<Type> = Type extends JSONValue ? Type : Type extends Date ? string : Type extends (...parameters: never[]) => unknown ? never : Type extends symbol ? never : Type extends Map<infer _Key, infer _Value> ? Record<string, never> : Type extends Set<infer _Value> ? Record<string, never> : Type extends (infer ArrayItem)[] ? JSONSerialized<ArrayItem>[] : Type extends object ? {
67
+ [Key in keyof Type as [JSONSerialized<Type[Key]>] extends [never] ? never : Key]: JSONSerialized<Type[Key]>;
68
+ } : never;
9
69
  declare global {
10
70
  interface JSON {
11
71
  readonly value: unique symbol;
12
72
  stringify<Value>(value: Value, replacer?: ((this: any, key: string, value: Value) => any) | (number | string)[] | null, space?: string | number): JSONStringified<Value>;
13
- parse<Value>(text: JSONStringified<Value>, reviver?: (this: any, key: string, value: any) => any): Value;
73
+ parse<Value>(text: JSONStringified<Value>, reviver?: (this: any, key: string, value: any) => any): JSONSerialized<Value>;
14
74
  }
15
75
  }
16
76
  type JSONStringified<Value> = string & {
17
- [JSON.value]: Value;
77
+ [JSON.value]: JSONSerialized<Value>;
18
78
  };
19
79
 
20
80
  type Default<Type, IfEmpty = never> = [undefined | void] extends [Type] ? IfEmpty : Exclude<Type, undefined | void>;
package/package.json CHANGED
@@ -13,7 +13,7 @@
13
13
  "api",
14
14
  "static"
15
15
  ],
16
- "version": "1.2.7-canary.5",
16
+ "version": "1.2.7",
17
17
  "homepage": "https://zimic.dev/docs/fetch",
18
18
  "repository": {
19
19
  "type": "git",