@chainfuse/helpers 2.4.2 → 3.0.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.
package/dist/net.d.mts CHANGED
@@ -1,17 +1,173 @@
1
- import type { CustomLoging } from '@chainfuse/types';
1
+ import type { z } from 'zod';
2
+ export type LoggingFetchInitType<RI extends RequestInit = RequestInit> = RI & z.input<Awaited<ReturnType<typeof NetHelpers.loggingFetchInit>>>;
3
+ /**
4
+ * Enum representing HTTP request methods.
5
+ *
6
+ * Each member of the enum corresponds to a standard HTTP method, which can be used to specify the desired action to be performed on a given resource.
7
+ */
8
+ export declare enum Methods {
9
+ 'GET' = "GET",
10
+ 'HEAD' = "HEAD",
11
+ 'POST' = "POST",
12
+ 'PUT' = "PUT",
13
+ 'DELETE' = "DELETE",
14
+ 'CONNECT' = "CONNECT",
15
+ 'OPTIONS' = "OPTIONS",
16
+ 'TRACE' = "TRACE",
17
+ 'PATCH' = "PATCH"
18
+ }
2
19
  export declare class NetHelpers {
20
+ static cfApiLogging(): Promise<z.ZodDefault<z.ZodUnion<[z.ZodObject<{
21
+ level: z.ZodLiteral<0>;
22
+ }, "strip", z.ZodTypeAny, {
23
+ level: 0;
24
+ }, {
25
+ level: 0;
26
+ }>, z.ZodObject<{
27
+ level: z.ZodNumber;
28
+ color: z.ZodDefault<z.ZodBoolean>;
29
+ custom: z.ZodOptional<z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnion<[z.ZodVoid, z.ZodPromise<z.ZodVoid>]>>>;
30
+ }, "strip", z.ZodTypeAny, {
31
+ level: number;
32
+ color: boolean;
33
+ custom?: ((...args: unknown[]) => void | Promise<void>) | undefined;
34
+ }, {
35
+ level: number;
36
+ custom?: ((...args: unknown[]) => void | Promise<void>) | undefined;
37
+ color?: boolean | undefined;
38
+ }>]>>>;
3
39
  /**
4
- * Removes the `body` property from a RequestInit object to reduce verbosity when logging.
40
+ * Creates an instance of the Cloudflare API client with enhanced logging capabilities.
41
+ *
42
+ * @param apiKey - The API token used to authenticate with the Cloudflare API.
43
+ * @param logging - The logging configuration object, parsed using the `cfApiLogging` parser.
44
+ *
45
+ * @returns A promise that resolves to an instance of the Cloudflare API client.
46
+ *
47
+ * The logging configuration supports the following properties:
48
+ * - `level`: The logging level. If set to `1`, it is internally treated as `2` to include headers for the `cf-ray` ID, but reset back to 1 for the returned logs.
49
+ * - `color`: Optional. If `true`, enables colored output using the `chalk` library.
50
+ * - `custom`: Optional. A custom logging function that receives detailed request and response information.
51
+ *
52
+ * The function also enhances logging by:
53
+ * - Extracting and displaying the `cf-ray` ID from response headers.
54
+ * - Formatting and coloring log output for better readability.
55
+ * - Stripping redundant parts of URLs and wrapping unique IDs in brackets with color coding.
56
+ */
57
+ static cfApi(apiKey: string, logging?: z.input<Awaited<ReturnType<typeof NetHelpers.cfApiLogging>>>): Promise<import("cloudflare").Cloudflare>;
58
+ static loggingFetchInit(): Promise<z.ZodObject<{
59
+ logging: z.ZodDefault<z.ZodUnion<[z.ZodObject<{
60
+ level: z.ZodLiteral<0>;
61
+ }, "strip", z.ZodTypeAny, {
62
+ level: 0;
63
+ }, {
64
+ level: 0;
65
+ }>, z.ZodObject<{
66
+ level: z.ZodNumber;
67
+ color: z.ZodDefault<z.ZodBoolean>;
68
+ custom: z.ZodOptional<z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnion<[z.ZodVoid, z.ZodPromise<z.ZodVoid>]>>>;
69
+ }, "strip", z.ZodTypeAny, {
70
+ level: number;
71
+ color: boolean;
72
+ custom?: ((...args: unknown[]) => void | Promise<void>) | undefined;
73
+ }, {
74
+ level: number;
75
+ custom?: ((...args: unknown[]) => void | Promise<void>) | undefined;
76
+ color?: boolean | undefined;
77
+ }>]>>;
78
+ }, "strip", z.ZodTypeAny, {
79
+ logging: {
80
+ level: 0;
81
+ } | {
82
+ level: number;
83
+ color: boolean;
84
+ custom?: ((...args: unknown[]) => void | Promise<void>) | undefined;
85
+ };
86
+ }, {
87
+ logging?: {
88
+ level: 0;
89
+ } | {
90
+ level: number;
91
+ custom?: ((...args: unknown[]) => void | Promise<void>) | undefined;
92
+ color?: boolean | undefined;
93
+ } | undefined;
94
+ }>>;
95
+ static loggingFetchInitLogging(): Promise<z.ZodDefault<z.ZodUnion<[z.ZodObject<{
96
+ level: z.ZodLiteral<0>;
97
+ }, "strip", z.ZodTypeAny, {
98
+ level: 0;
99
+ }, {
100
+ level: 0;
101
+ }>, z.ZodObject<{
102
+ level: z.ZodNumber;
103
+ color: z.ZodDefault<z.ZodBoolean>;
104
+ custom: z.ZodOptional<z.ZodFunction<z.ZodTuple<[], z.ZodUnknown>, z.ZodUnion<[z.ZodVoid, z.ZodPromise<z.ZodVoid>]>>>;
105
+ }, "strip", z.ZodTypeAny, {
106
+ level: number;
107
+ color: boolean;
108
+ custom?: ((...args: unknown[]) => void | Promise<void>) | undefined;
109
+ }, {
110
+ level: number;
111
+ custom?: ((...args: unknown[]) => void | Promise<void>) | undefined;
112
+ color?: boolean | undefined;
113
+ }>]>>>;
114
+ /**
115
+ * A utility function that wraps the native `fetch` API with enhanced capabilities.
116
+ * This function allows for customizable logging of request and response details, including headers, body, and status, with support for colorized output and custom logging handlers.
117
+ *
118
+ * @template RI - The type of the `RequestInit` object, defaulting to `RequestInit`. Intended for cloudflare's `RequestInit` variation.
119
+ *
120
+ * @param info - The input to the `fetch` function, which can be a `Request` object or a URL string.
121
+ * @param init - An optional configuration object extending `RequestInit` with additional options.
122
+ *
123
+ * @returns A promise that resolves to the `Response` object returned by the `fetch` call.
124
+ *
125
+ * ### Logging Levels:
126
+ * - `level >= 1`: Logs basic request details (timestamp, unique ID, method, and URL).
127
+ * - `level >= 2`: Logs request headers (with sensitive headers stripped).
128
+ * - `level >= 3`: Logs request body (if available) and response body (if available).
5
129
  *
6
- * @param {RequestInit} [init={}] - The RequestInit object from which to remove the 'body' property. If not provided, an empty object will be used.
130
+ * ### Logging Options:
131
+ * - `logging.level`: The verbosity level of logging (1, 2, or 3).
132
+ * - `logging.color`: A boolean indicating whether to use colorized output.
133
+ * - `logging.custom`: An optional custom logging function to handle the log output.
7
134
  *
8
- * @returns {RequestInit} The updated RequestInit object without the 'body' property.
135
+ * ### Features:
136
+ * - Automatically generates a unique ID for each request.
137
+ * - Strips sensitive headers from logs to ensure security.
138
+ * - Supports JSON and stream-based request/response bodies.
139
+ * - Allows for colorized output using the `chalk` library.
140
+ * - Provides hooks for custom logging implementations.
141
+ */
142
+ static loggingFetch<RI extends RequestInit = RequestInit>(info: Parameters<typeof fetch>[0], init?: LoggingFetchInitType<RI>): Promise<Response>;
143
+ /**
144
+ * Removes sensitive headers from the provided `Headers` object. Specifically, it deletes the `Set-Cookie` and `Authorization` headers.
145
+ *
146
+ * @param originalHeaders - The original `Headers` object to sanitize. Defaults to an empty `Headers` object if not provided.
147
+ * @returns A new `Headers` object with the sensitive headers removed.
9
148
  */
10
- static initBodyTrimmer(init?: RequestInit): RequestInit;
11
149
  static stripSensitiveHeaders(originalHeaders?: Headers): Headers;
12
- static cfApi(apiKey: string, logger?: CustomLoging): Promise<import("cloudflare").Cloudflare>;
150
+ /**
151
+ * Determines if the given object is a `Request`-like object.
152
+ *
153
+ * This function checks if the provided object has a `url` property of type `string`,
154
+ * which is a characteristic of the `Request` object used in the Fetch API.
155
+ *
156
+ * @param obj - The object to check, typically the first parameter of the `fetch` function.
157
+ * @returns A boolean indicating whether the object is a `Request` instance.
158
+ */
13
159
  static isRequestLike(obj: Parameters<typeof fetch>[0]): obj is Request;
14
- static loggingFetch(info: Parameters<typeof fetch>[0], init?: Parameters<typeof fetch>[1], body?: boolean, logger?: CustomLoging): Promise<Response>;
160
+ /**
161
+ * Returns a promise that resolves to a `chalk` instance with a color corresponding to the provided HTTP method.
162
+ *
163
+ * The colors are based on the Swagger UI method colors:
164
+ * @link https://github.com/swagger-api/swagger-ui/blob/master/src/style/_variables.scss#L48-L55
165
+ *
166
+ * @param method - The HTTP method for which to retrieve the color.
167
+ * @returns A promise that resolves to a `chalk` instance with the color corresponding to the provided HTTP method.
168
+ * @throws An error if the provided method is unsupported.
169
+ */
170
+ static methodColors(method: Methods): Promise<import("chalk").ChalkInstance>;
15
171
  /**
16
172
  * Parses the Server-Timing header and returns an object with the metrics.
17
173
  * The object keys are the metric names (with optional descriptions), and the values are the duration of each metric or null if no duration is found.