@chainfuse/helpers 3.4.9 → 3.5.1

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/db.d.mts CHANGED
@@ -34,8 +34,8 @@ export declare class SQLCache<C extends CacheStorageLike> extends DrizzleCache {
34
34
  cacheTTL: z.ZodDefault<z.ZodInt>;
35
35
  cachePurge: z.ZodDefault<z.ZodUnion<readonly [z.ZodBoolean, z.ZodDate]>>;
36
36
  strategy: z.ZodDefault<z.ZodEnum<{
37
- explicit: "explicit";
38
37
  all: "all";
38
+ explicit: "explicit";
39
39
  }>>;
40
40
  }, z.core.$strip>;
41
41
  /**
@@ -56,7 +56,7 @@ export declare class SQLCache<C extends CacheStorageLike> extends DrizzleCache {
56
56
  * - `all`: All queries are cached globally.
57
57
  * @default 'explicit'
58
58
  */
59
- strategy(): "explicit" | "all";
59
+ strategy(): "all" | "explicit";
60
60
  /**
61
61
  * Generates a cache key as a `Request` object based on the provided tag or key.
62
62
  *
package/dist/net.d.mts CHANGED
@@ -18,27 +18,143 @@ export declare enum Methods {
18
18
  'PATCH' = "PATCH"
19
19
  }
20
20
  export declare class NetHelpers {
21
- static cfApiLogging(): Promise<z3.ZodDefault<z3.ZodObject<{
22
- level: z3.ZodDefault<z3.ZodNumber>;
23
- error: z3.ZodDefault<z3.ZodNumber>;
24
- color: z3.ZodDefault<z3.ZodBoolean>;
25
- custom: z3.ZodOptional<z3.ZodFunction<z3.ZodTuple<[], z3.ZodUnknown>, z3.ZodUnion<[z3.ZodVoid, z3.ZodPromise<z3.ZodVoid>]>>>;
21
+ static cfApiConfig(): Promise<z3.ZodDefault<z3.ZodObject<{
22
+ logging: z3.ZodDefault<z3.ZodObject<{
23
+ level: z3.ZodDefault<z3.ZodNumber>;
24
+ error: z3.ZodDefault<z3.ZodNumber>;
25
+ color: z3.ZodDefault<z3.ZodBoolean>;
26
+ custom: z3.ZodOptional<z3.ZodFunction<z3.ZodTuple<[], z3.ZodUnknown>, z3.ZodUnion<[z3.ZodVoid, z3.ZodPromise<z3.ZodVoid>]>>>;
27
+ }, "strip", z3.ZodTypeAny, {
28
+ error: number;
29
+ level: number;
30
+ color: boolean;
31
+ custom?: ((...args: unknown[]) => void | Promise<void>) | undefined;
32
+ }, {
33
+ error?: number | undefined;
34
+ custom?: ((...args: unknown[]) => void | Promise<void>) | undefined;
35
+ level?: number | undefined;
36
+ color?: boolean | undefined;
37
+ }>>;
38
+ cf: z3.ZodDefault<z3.ZodObject<{
39
+ /**
40
+ * Whether Cloudflare Apps should be enabled for this request
41
+ * @link https://www.cloudflare.com/apps/
42
+ * @default true
43
+ */
44
+ apps: z3.ZodDefault<z3.ZodBoolean>;
45
+ /**
46
+ * Treats all content as static and caches all file types beyond the Cloudflare default cached content. Respects cache headers from the origin web server. This is equivalent to setting the Page Rule Cache Level (to Cache Everything).
47
+ * @default false
48
+ * @note This option applies to GET and HEAD request methods only
49
+ */
50
+ cacheEverything: z3.ZodDefault<z3.ZodBoolean>;
51
+ /**
52
+ * A request’s cache key is what determines if two requests are the same for caching purposes. If a request has the same cache key as some previous request, then Cloudflare can serve the same cached response for both.
53
+ */
54
+ cacheKey: z3.ZodOptional<z3.ZodString>;
55
+ /**
56
+ * This option appends additional Cache-Tag headers to the response from the origin server. This allows for purges of cached content based on tags provided by the Worker, without modifications to the origin server. This is performed using the Purge by Tag feature.
57
+ */
58
+ cacheTags: z3.ZodOptional<z3.ZodArray<z3.ZodString, "atleastone">>;
59
+ /**
60
+ * This option forces Cloudflare to cache the response for this request, regardless of what headers are seen on the response. This is equivalent to setting two Page Rules: Edge Cache TTL and Cache Level (to Cache Everything). The value must be zero or a positive number. A value of 0 indicates that the cache asset expires immediately.
61
+ * @note This option applies to GET and HEAD request methods only.
62
+ */
63
+ cacheTtl: z3.ZodOptional<z3.ZodNumber>;
64
+ /**
65
+ * This option is a version of the cacheTtl feature which chooses a TTL based on the response’s status code. If the response to this request has a status code that matches, Cloudflare will cache for the instructed time and override cache instructives sent by the origin. For example: { "200-299": 86400, "404": 1, "500-599": 0 }. The value can be any integer, including zero and negative integers. A value of 0 indicates that the cache asset expires immediately. Any negative value instructs Cloudflare not to cache at all.
66
+ * @note This option applies to GET and HEAD request methods only.
67
+ */
68
+ cacheTtlByStatus: z3.ZodOptional<z3.ZodRecord<z3.ZodString, z3.ZodNumber>>;
69
+ /**
70
+ * Whether Mirage should be enabled for this request, if otherwise configured for this zone.
71
+ * @link https://www.cloudflare.com/website-optimization/mirage/
72
+ * @default true
73
+ */
74
+ mirage: z3.ZodDefault<z3.ZodBoolean>;
75
+ /**
76
+ * Sets Polish mode. The possible values are lossy, lossless or off.
77
+ * @link https://blog.cloudflare.com/introducing-polish-automatic-image-optimizati/
78
+ */
79
+ polish: z3.ZodOptional<z3.ZodEnum<["lossy", "lossless", "off"]>>;
80
+ /**
81
+ * Whether ScrapeShield should be enabled for this request, if otherwise configured for this zone.
82
+ * @link https://blog.cloudflare.com/introducing-scrapeshield-discover-defend-dete/
83
+ * @default true
84
+ */
85
+ scrapeShield: z3.ZodDefault<z3.ZodBoolean>;
86
+ /**
87
+ * Enables or disables WebP image format in Polish.
88
+ * @link https://blog.cloudflare.com/a-very-webp-new-year-from-cloudflare/
89
+ */
90
+ webp: z3.ZodOptional<z3.ZodBoolean>;
91
+ }, "strip", z3.ZodTypeAny, {
92
+ cacheEverything: boolean;
93
+ scrapeShield: boolean;
94
+ apps: boolean;
95
+ mirage: boolean;
96
+ cacheKey?: string | undefined;
97
+ cacheTags?: [string, ...string[]] | undefined;
98
+ cacheTtl?: number | undefined;
99
+ cacheTtlByStatus?: Record<string, number> | undefined;
100
+ polish?: "off" | "lossy" | "lossless" | undefined;
101
+ webp?: boolean | undefined;
102
+ }, {
103
+ cacheEverything?: boolean | undefined;
104
+ cacheKey?: string | undefined;
105
+ cacheTags?: [string, ...string[]] | undefined;
106
+ cacheTtl?: number | undefined;
107
+ cacheTtlByStatus?: Record<string, number> | undefined;
108
+ scrapeShield?: boolean | undefined;
109
+ apps?: boolean | undefined;
110
+ mirage?: boolean | undefined;
111
+ polish?: "off" | "lossy" | "lossless" | undefined;
112
+ webp?: boolean | undefined;
113
+ }>>;
26
114
  }, "strip", z3.ZodTypeAny, {
27
- error: number;
28
- level: number;
29
- color: boolean;
30
- custom?: ((...args: unknown[]) => void | Promise<void>) | undefined;
115
+ logging: {
116
+ error: number;
117
+ level: number;
118
+ color: boolean;
119
+ custom?: ((...args: unknown[]) => void | Promise<void>) | undefined;
120
+ };
121
+ cf: {
122
+ cacheEverything: boolean;
123
+ scrapeShield: boolean;
124
+ apps: boolean;
125
+ mirage: boolean;
126
+ cacheKey?: string | undefined;
127
+ cacheTags?: [string, ...string[]] | undefined;
128
+ cacheTtl?: number | undefined;
129
+ cacheTtlByStatus?: Record<string, number> | undefined;
130
+ polish?: "off" | "lossy" | "lossless" | undefined;
131
+ webp?: boolean | undefined;
132
+ };
31
133
  }, {
32
- error?: number | undefined;
33
- custom?: ((...args: unknown[]) => void | Promise<void>) | undefined;
34
- level?: number | undefined;
35
- color?: boolean | undefined;
134
+ logging?: {
135
+ error?: number | undefined;
136
+ custom?: ((...args: unknown[]) => void | Promise<void>) | undefined;
137
+ level?: number | undefined;
138
+ color?: boolean | undefined;
139
+ } | undefined;
140
+ cf?: {
141
+ cacheEverything?: boolean | undefined;
142
+ cacheKey?: string | undefined;
143
+ cacheTags?: [string, ...string[]] | undefined;
144
+ cacheTtl?: number | undefined;
145
+ cacheTtlByStatus?: Record<string, number> | undefined;
146
+ scrapeShield?: boolean | undefined;
147
+ apps?: boolean | undefined;
148
+ mirage?: boolean | undefined;
149
+ polish?: "off" | "lossy" | "lossless" | undefined;
150
+ webp?: boolean | undefined;
151
+ } | undefined;
36
152
  }>>>;
37
153
  /**
38
154
  * Creates an instance of the Cloudflare API client with enhanced logging capabilities.
39
155
  *
40
156
  * @param apiKey - The API token used to authenticate with the Cloudflare API.
41
- * @param logging - The logging configuration object, parsed using the `cfApiLogging` parser.
157
+ * @param config - The logging configuration object, parsed using the `cfApiLogging` parser.
42
158
  *
43
159
  * @returns A promise that resolves to an instance of the Cloudflare API client.
44
160
  *
@@ -52,7 +168,7 @@ export declare class NetHelpers {
52
168
  * - Formatting and coloring log output for better readability.
53
169
  * - Stripping redundant parts of URLs and wrapping unique IDs in brackets with color coding.
54
170
  */
55
- static cfApi(apiKey: string, logging?: z3.input<Awaited<ReturnType<typeof NetHelpers.cfApiLogging>>>): Promise<import("cloudflare").Cloudflare>;
171
+ static cfApi(apiKey: string, config?: z3.input<Awaited<ReturnType<typeof NetHelpers.cfApiConfig>>>): Promise<import("cloudflare").Cloudflare>;
56
172
  static loggingFetchInit(): Promise<z3.ZodObject<{
57
173
  logging: z3.ZodDefault<z3.ZodObject<{
58
174
  level: z3.ZodDefault<z3.ZodNumber>;
package/dist/net.mjs CHANGED
@@ -16,17 +16,84 @@ export var Methods;
16
16
  Methods["PATCH"] = "PATCH";
17
17
  })(Methods || (Methods = {}));
18
18
  export class NetHelpers {
19
- static cfApiLogging() {
19
+ static cfApiConfig() {
20
20
  return import('zod/v3').then(({ z: z3 }) => z3
21
21
  .object({
22
- level: z3.coerce.number().int().min(0).max(3).default(0),
23
- error: z3.coerce.number().int().min(0).max(3).default(1),
24
- color: z3.boolean().default(true),
25
- custom: z3
26
- .function()
27
- .args()
28
- .returns(z3.union([z3.void(), z3.promise(z3.void())]))
29
- .optional(),
22
+ logging: z3
23
+ .object({
24
+ level: z3.coerce.number().int().min(0).max(3).default(0),
25
+ error: z3.coerce.number().int().min(0).max(3).default(1),
26
+ color: z3.boolean().default(true),
27
+ custom: z3
28
+ .function()
29
+ .args()
30
+ .returns(z3.union([z3.void(), z3.promise(z3.void())]))
31
+ .optional(),
32
+ })
33
+ .default({}),
34
+ cf: z3
35
+ .object({
36
+ /**
37
+ * Whether Cloudflare Apps should be enabled for this request
38
+ * @link https://www.cloudflare.com/apps/
39
+ * @default true
40
+ */
41
+ apps: z3.boolean().default(true),
42
+ /**
43
+ * Treats all content as static and caches all file types beyond the Cloudflare default cached content. Respects cache headers from the origin web server. This is equivalent to setting the Page Rule Cache Level (to Cache Everything).
44
+ * @default false
45
+ * @note This option applies to GET and HEAD request methods only
46
+ */
47
+ cacheEverything: z3.boolean().default(false),
48
+ /**
49
+ * A request’s cache key is what determines if two requests are the same for caching purposes. If a request has the same cache key as some previous request, then Cloudflare can serve the same cached response for both.
50
+ */
51
+ cacheKey: z3.string().nonempty().optional(),
52
+ /**
53
+ * This option appends additional Cache-Tag headers to the response from the origin server. This allows for purges of cached content based on tags provided by the Worker, without modifications to the origin server. This is performed using the Purge by Tag feature.
54
+ */
55
+ cacheTags: z3.array(z3.string().nonempty()).nonempty().optional(),
56
+ /**
57
+ * This option forces Cloudflare to cache the response for this request, regardless of what headers are seen on the response. This is equivalent to setting two Page Rules: Edge Cache TTL and Cache Level (to Cache Everything). The value must be zero or a positive number. A value of 0 indicates that the cache asset expires immediately.
58
+ * @note This option applies to GET and HEAD request methods only.
59
+ */
60
+ cacheTtl: z3.coerce.number().int().nonnegative().optional(),
61
+ /**
62
+ * This option is a version of the cacheTtl feature which chooses a TTL based on the response’s status code. If the response to this request has a status code that matches, Cloudflare will cache for the instructed time and override cache instructives sent by the origin. For example: { "200-299": 86400, "404": 1, "500-599": 0 }. The value can be any integer, including zero and negative integers. A value of 0 indicates that the cache asset expires immediately. Any negative value instructs Cloudflare not to cache at all.
63
+ * @note This option applies to GET and HEAD request methods only.
64
+ */
65
+ cacheTtlByStatus: z3
66
+ .record(z3
67
+ .string()
68
+ .nonempty()
69
+ .regex(/^(1|2|3|4|5)\d{2}(-(1|2|3|4|5)\d{2})?$/), z3.coerce.number().int())
70
+ .optional(),
71
+ // image:
72
+ /**
73
+ * Whether Mirage should be enabled for this request, if otherwise configured for this zone.
74
+ * @link https://www.cloudflare.com/website-optimization/mirage/
75
+ * @default true
76
+ */
77
+ mirage: z3.boolean().default(true),
78
+ /**
79
+ * Sets Polish mode. The possible values are lossy, lossless or off.
80
+ * @link https://blog.cloudflare.com/introducing-polish-automatic-image-optimizati/
81
+ */
82
+ polish: z3.enum(['lossy', 'lossless', 'off']).optional(),
83
+ // resolveOverride:
84
+ /**
85
+ * Whether ScrapeShield should be enabled for this request, if otherwise configured for this zone.
86
+ * @link https://blog.cloudflare.com/introducing-scrapeshield-discover-defend-dete/
87
+ * @default true
88
+ */
89
+ scrapeShield: z3.boolean().default(true),
90
+ /**
91
+ * Enables or disables WebP image format in Polish.
92
+ * @link https://blog.cloudflare.com/a-very-webp-new-year-from-cloudflare/
93
+ */
94
+ webp: z3.boolean().optional(),
95
+ })
96
+ .default({}),
30
97
  })
31
98
  .default({}));
32
99
  }
@@ -34,7 +101,7 @@ export class NetHelpers {
34
101
  * Creates an instance of the Cloudflare API client with enhanced logging capabilities.
35
102
  *
36
103
  * @param apiKey - The API token used to authenticate with the Cloudflare API.
37
- * @param logging - The logging configuration object, parsed using the `cfApiLogging` parser.
104
+ * @param config - The logging configuration object, parsed using the `cfApiLogging` parser.
38
105
  *
39
106
  * @returns A promise that resolves to an instance of the Cloudflare API client.
40
107
  *
@@ -48,28 +115,32 @@ export class NetHelpers {
48
115
  * - Formatting and coloring log output for better readability.
49
116
  * - Stripping redundant parts of URLs and wrapping unique IDs in brackets with color coding.
50
117
  */
51
- static cfApi(apiKey, logging) {
118
+ static cfApi(apiKey, config) {
52
119
  return Promise.all([
53
120
  //
54
121
  import('cloudflare'),
55
- NetHelpers.cfApiLogging().then((parser) => parser.parseAsync(logging)),
56
- ]).then(([{ Cloudflare }, logging]) => new Cloudflare({
122
+ NetHelpers.cfApiConfig().then((parser) => parser.parseAsync(config)),
123
+ ]).then(([{ Cloudflare }, config]) => new Cloudflare({
57
124
  apiToken: apiKey,
58
125
  fetch: (info, init) => this.loggingFetch(info, {
59
126
  ...init,
127
+ cf: {
128
+ ...(init && 'cf' in init && init.cf),
129
+ ...config.cf,
130
+ },
60
131
  logging: {
61
132
  // Fake level 1 as 2 to get headers for ray-id
62
- level: logging.level === 1 ? 2 : logging.level,
63
- error: logging.error === 1 ? 2 : logging.error,
64
- ...('color' in logging && { color: logging.color }),
65
- ...((logging.level > 0 || logging.error > 0) && {
133
+ level: config.logging.level === 1 ? 2 : config.logging.level,
134
+ error: config.logging.error === 1 ? 2 : config.logging.error,
135
+ ...('color' in config.logging && { color: config.logging.color }),
136
+ ...((config.logging.level > 0 || config.logging.error > 0) && {
66
137
  custom: async (...args) => {
67
138
  const [, id, , url, headers] = args;
68
139
  const customUrl = new URL(url);
69
140
  const customHeaders = new Headers(headers);
70
141
  if (customHeaders.has('cf-ray')) {
71
142
  args.splice(3, 0, customHeaders.get('cf-ray'));
72
- if ('color' in logging && logging.color) {
143
+ if ('color' in config && config.logging.color) {
73
144
  await import('chalk')
74
145
  .then(({ Chalk }) => new Chalk({ level: 2 }))
75
146
  // eslint-disable-next-line @typescript-eslint/no-unsafe-return
@@ -78,21 +149,21 @@ export class NetHelpers {
78
149
  .catch(() => { });
79
150
  }
80
151
  }
81
- if ('custom' in logging && logging.custom) {
152
+ if ('custom' in config && config.logging.custom) {
82
153
  // We faked level 1 as 2 to get headers for ray-id
83
- if (logging.level === 1 || logging.error === 1) {
154
+ if (config.logging.level === 1 || config.logging.error === 1) {
84
155
  // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
85
- return logging.custom(...args.slice(0, -1));
156
+ return config.logging.custom(...args.slice(0, -1));
86
157
  }
87
158
  else {
88
159
  // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
89
- return logging.custom(...args);
160
+ return config.logging.custom(...args);
90
161
  }
91
162
  }
92
163
  else {
93
164
  await Promise.all([import('strip-ansi'), import('chalk').then(({ Chalk }) => new Chalk({ level: 2 })), import("./index.mjs")]).then(([{ default: stripAnsi }, chalk, { Helpers }]) => {
94
165
  // We faked level 1 as 2 to get headers for ray-id
95
- if (logging.level === 1) {
166
+ if (config.logging.level === 1) {
96
167
  console.info('CF Rest',
97
168
  // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
98
169
  ...args
@@ -104,7 +175,7 @@ export class NetHelpers {
104
175
  .map((value) => {
105
176
  if (value === id) {
106
177
  const wrappedString = `[${stripAnsi(id)}]`;
107
- if (logging.color) {
178
+ if (config.logging.color) {
108
179
  return chalk.rgb(...Helpers.uniqueIdColor(stripAnsi(id)))(wrappedString);
109
180
  }
110
181
  else {
@@ -120,7 +191,7 @@ export class NetHelpers {
120
191
  // eslint-disable-next-line @typescript-eslint/no-unsafe-return
121
192
  .map((value) => (value === url ? `${customUrl.pathname}${customUrl.search}${customUrl.hash}` : value)));
122
193
  }
123
- else if (logging.error === 1) {
194
+ else if (config.logging.error === 1) {
124
195
  console.error('CF Rest',
125
196
  // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
126
197
  ...args
@@ -132,7 +203,7 @@ export class NetHelpers {
132
203
  .map((value) => {
133
204
  if (value === id) {
134
205
  const wrappedString = `[${stripAnsi(id)}]`;
135
- if (logging.color) {
206
+ if (config.logging.color) {
136
207
  return chalk.rgb(...Helpers.uniqueIdColor(stripAnsi(id)))(wrappedString);
137
208
  }
138
209
  else {
@@ -148,7 +219,7 @@ export class NetHelpers {
148
219
  // eslint-disable-next-line @typescript-eslint/no-unsafe-return
149
220
  .map((value) => (value === url ? `${customUrl.pathname}${customUrl.search}${customUrl.hash}` : value)));
150
221
  }
151
- else if (logging.level > 0) {
222
+ else if (config.logging.level > 0) {
152
223
  console.info('CF Rest',
153
224
  // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
154
225
  ...args
@@ -159,7 +230,7 @@ export class NetHelpers {
159
230
  .map((value) => {
160
231
  if (value === id) {
161
232
  const wrappedString = `[${stripAnsi(id)}]`;
162
- if (logging.color) {
233
+ if (config.logging.color) {
163
234
  return chalk.rgb(...Helpers.uniqueIdColor(stripAnsi(id)))(wrappedString);
164
235
  }
165
236
  else {
@@ -175,7 +246,7 @@ export class NetHelpers {
175
246
  // eslint-disable-next-line @typescript-eslint/no-unsafe-return
176
247
  .map((value) => (value === url ? `${customUrl.pathname}${customUrl.search}${customUrl.hash}` : value)));
177
248
  }
178
- else if (logging.error > 0) {
249
+ else if (config.logging.error > 0) {
179
250
  console.error('CF Rest',
180
251
  // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
181
252
  ...args
@@ -186,7 +257,7 @@ export class NetHelpers {
186
257
  .map((value) => {
187
258
  if (value === id) {
188
259
  const wrappedString = `[${stripAnsi(id)}]`;
189
- if (logging.color) {
260
+ if (config.logging.color) {
190
261
  return chalk.rgb(...Helpers.uniqueIdColor(stripAnsi(id)))(wrappedString);
191
262
  }
192
263
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chainfuse/helpers",
3
- "version": "3.4.9",
3
+ "version": "3.5.1",
4
4
  "description": "",
5
5
  "author": "ChainFuse",
6
6
  "homepage": "https://github.com/ChainFuse/packages/tree/main/packages/helpers#readme",
@@ -76,18 +76,18 @@
76
76
  },
77
77
  "prettier": "@demosjarco/prettier-config",
78
78
  "dependencies": {
79
- "@chainfuse/types": "^2.11.0",
79
+ "@chainfuse/types": "^2.11.4",
80
80
  "@discordjs/rest": "^2.5.1",
81
- "chalk": "^5.5.0",
81
+ "chalk": "^5.6.0",
82
82
  "cloudflare": "^4.5.0",
83
83
  "drizzle-orm": "^0.44.4",
84
84
  "strip-ansi": "^7.1.0",
85
85
  "uuid": "^11.1.0",
86
- "zod": "^4.0.15"
86
+ "zod": "^4.0.17"
87
87
  },
88
88
  "devDependencies": {
89
- "@cloudflare/workers-types": "^4.20250807.0",
90
- "@types/node": "^22.17.0"
89
+ "@cloudflare/workers-types": "^4.20250819.0",
90
+ "@types/node": "^22.17.2"
91
91
  },
92
- "gitHead": "2da7381e517cfdbd6b2750e2fc1a59f3b36edc4d"
92
+ "gitHead": "34690e8aa0cd8c205ab9e5340eeef97daefc76cd"
93
93
  }