cfw-utils 0.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026-present Lyntor Paul Figueroa (https://github.com/h4ckedneko)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # Cloudflare Workers Utilities
2
+
3
+ A set of utility functions for Cloudflare Workers.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install cfw-utils
9
+ ```
10
+
11
+ ```bash
12
+ pnpm add cfw-utils
13
+ ```
14
+
15
+ ```bash
16
+ yarn add cfw-utils
17
+ ```
18
+
19
+ Alternatively, you can also just copy and paste the utilities in [src/utils](src/utils) to your project.
20
+
21
+ ## Usage
22
+
23
+ ### configureStep()
24
+
25
+ A reusable step runner for Cloudflare Workflows. Simplifies running workflow steps with shared default configuration and centralized error handling.
26
+
27
+ #### Features
28
+
29
+ - **Default config** - Apply consistent timeout, retries, or other settings across all steps
30
+ - **Error handling** - Automatically execute cleanup logic when any step fails
31
+ - **Familiar API** - Mirrors Cloudflare's `step.do` signature
32
+
33
+ #### Example
34
+
35
+ ```typescript
36
+ export class Workflow extends WorkflowEntrypoint<Env, Params> {
37
+ async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
38
+ const { runStep } = configureStep(step, {
39
+ // Default config applied to all steps (can be overridden per step).
40
+ defaultConfig: {
41
+ retries: {
42
+ limit: 10,
43
+ delay: 1000,
44
+ backoff: "exponential",
45
+ },
46
+ timeout: "5 minutes",
47
+ },
48
+ // Cleanup logic executed when any step fails.
49
+ onError: async (error) => {
50
+ await setAsFailed();
51
+ console.error(error);
52
+ },
53
+ });
54
+
55
+ // Run a step with default configuration.
56
+ const result = await runStep("fetch data", async () => {
57
+ return await fetch("https://example.com").then(r => r.json());
58
+ });
59
+
60
+ // Run a step with overridden timeout (other defaults still apply).
61
+ await runStep("process data", { timeout: "30 minutes" }, async () => {
62
+ await saveToDb(result);
63
+ });
64
+ }
65
+ }
66
+ ```
67
+
68
+ ## License
69
+
70
+ MIT © [Lyntor Paul Figueroa](https://github.com/h4ckedneko)
package/dist/index.cjs ADDED
@@ -0,0 +1,43 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region src/utils/workflow.ts
3
+ /**
4
+ * Configures a reusable step runner for a Cloudflare Workflow.
5
+ *
6
+ * @param step - The workflow step instance
7
+ * @param options - See {@link ConfigureStepOptions}
8
+ * @returns An object containing the configured `runStep` function
9
+ *
10
+ * @example
11
+ * const { runStep } = configureStep(step, {
12
+ * defaultConfig: { timeout: "1 minute" },
13
+ * onError: async () => {
14
+ * await db.job.update({
15
+ * where: { id },
16
+ * data: { status: "FAILED" },
17
+ * });
18
+ * },
19
+ * });
20
+ *
21
+ * const result = await runStep("fetch data", async () => {
22
+ * return await fetch("https://example.com").then(r => r.json());
23
+ * });
24
+ */
25
+ function configureStep(step, options = {}) {
26
+ async function runStep(name, configOrCallback, maybeCallback) {
27
+ const config = typeof configOrCallback === "function" ? {} : configOrCallback;
28
+ const callback = typeof configOrCallback === "function" ? configOrCallback : maybeCallback;
29
+ const run = () => step.do(name, {
30
+ ...options.defaultConfig,
31
+ ...config
32
+ }, callback);
33
+ if (options.onError) try {
34
+ return await run();
35
+ } catch (error) {
36
+ await step.do(`handle error for: ${name}`, () => options.onError(error));
37
+ }
38
+ return await run();
39
+ }
40
+ return { runStep };
41
+ }
42
+ //#endregion
43
+ exports.configureStep = configureStep;
@@ -0,0 +1,37 @@
1
+ import { WorkflowStep, WorkflowStepConfig, WorkflowStepContext } from "cloudflare:workers";
2
+
3
+ //#region src/utils/workflow.d.ts
4
+ type ConfigureStepOptions = {
5
+ defaultConfig?: WorkflowStepConfig;
6
+ onError?: (error: unknown) => Promise<void>;
7
+ };
8
+ /**
9
+ * Configures a reusable step runner for a Cloudflare Workflow.
10
+ *
11
+ * @param step - The workflow step instance
12
+ * @param options - See {@link ConfigureStepOptions}
13
+ * @returns An object containing the configured `runStep` function
14
+ *
15
+ * @example
16
+ * const { runStep } = configureStep(step, {
17
+ * defaultConfig: { timeout: "1 minute" },
18
+ * onError: async () => {
19
+ * await db.job.update({
20
+ * where: { id },
21
+ * data: { status: "FAILED" },
22
+ * });
23
+ * },
24
+ * });
25
+ *
26
+ * const result = await runStep("fetch data", async () => {
27
+ * return await fetch("https://example.com").then(r => r.json());
28
+ * });
29
+ */
30
+ declare function configureStep(step: WorkflowStep, options?: ConfigureStepOptions): {
31
+ runStep: {
32
+ <T extends Rpc.Serializable<T>>(name: string, callback: (ctx: WorkflowStepContext) => Promise<T>): Promise<T>;
33
+ <T extends Rpc.Serializable<T>>(name: string, config: WorkflowStepConfig, callback: (ctx: WorkflowStepContext) => Promise<T>): Promise<T>;
34
+ };
35
+ };
36
+ //#endregion
37
+ export { ConfigureStepOptions, configureStep };
@@ -0,0 +1,37 @@
1
+ import { WorkflowStep, WorkflowStepConfig, WorkflowStepContext } from "cloudflare:workers";
2
+
3
+ //#region src/utils/workflow.d.ts
4
+ type ConfigureStepOptions = {
5
+ defaultConfig?: WorkflowStepConfig;
6
+ onError?: (error: unknown) => Promise<void>;
7
+ };
8
+ /**
9
+ * Configures a reusable step runner for a Cloudflare Workflow.
10
+ *
11
+ * @param step - The workflow step instance
12
+ * @param options - See {@link ConfigureStepOptions}
13
+ * @returns An object containing the configured `runStep` function
14
+ *
15
+ * @example
16
+ * const { runStep } = configureStep(step, {
17
+ * defaultConfig: { timeout: "1 minute" },
18
+ * onError: async () => {
19
+ * await db.job.update({
20
+ * where: { id },
21
+ * data: { status: "FAILED" },
22
+ * });
23
+ * },
24
+ * });
25
+ *
26
+ * const result = await runStep("fetch data", async () => {
27
+ * return await fetch("https://example.com").then(r => r.json());
28
+ * });
29
+ */
30
+ declare function configureStep(step: WorkflowStep, options?: ConfigureStepOptions): {
31
+ runStep: {
32
+ <T extends Rpc.Serializable<T>>(name: string, callback: (ctx: WorkflowStepContext) => Promise<T>): Promise<T>;
33
+ <T extends Rpc.Serializable<T>>(name: string, config: WorkflowStepConfig, callback: (ctx: WorkflowStepContext) => Promise<T>): Promise<T>;
34
+ };
35
+ };
36
+ //#endregion
37
+ export { ConfigureStepOptions, configureStep };
package/dist/index.mjs ADDED
@@ -0,0 +1,42 @@
1
+ //#region src/utils/workflow.ts
2
+ /**
3
+ * Configures a reusable step runner for a Cloudflare Workflow.
4
+ *
5
+ * @param step - The workflow step instance
6
+ * @param options - See {@link ConfigureStepOptions}
7
+ * @returns An object containing the configured `runStep` function
8
+ *
9
+ * @example
10
+ * const { runStep } = configureStep(step, {
11
+ * defaultConfig: { timeout: "1 minute" },
12
+ * onError: async () => {
13
+ * await db.job.update({
14
+ * where: { id },
15
+ * data: { status: "FAILED" },
16
+ * });
17
+ * },
18
+ * });
19
+ *
20
+ * const result = await runStep("fetch data", async () => {
21
+ * return await fetch("https://example.com").then(r => r.json());
22
+ * });
23
+ */
24
+ function configureStep(step, options = {}) {
25
+ async function runStep(name, configOrCallback, maybeCallback) {
26
+ const config = typeof configOrCallback === "function" ? {} : configOrCallback;
27
+ const callback = typeof configOrCallback === "function" ? configOrCallback : maybeCallback;
28
+ const run = () => step.do(name, {
29
+ ...options.defaultConfig,
30
+ ...config
31
+ }, callback);
32
+ if (options.onError) try {
33
+ return await run();
34
+ } catch (error) {
35
+ await step.do(`handle error for: ${name}`, () => options.onError(error));
36
+ }
37
+ return await run();
38
+ }
39
+ return { runStep };
40
+ }
41
+ //#endregion
42
+ export { configureStep };
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "cfw-utils",
3
+ "version": "0.0.1",
4
+ "description": "A set of utility functions for Cloudflare Workers.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/h4ckedneko/cfwutils.git"
9
+ },
10
+ "type": "module",
11
+ "main": "./dist/index.cjs",
12
+ "module": "./dist/index.mjs",
13
+ "types": "./dist/index.d.cts",
14
+ "exports": {
15
+ ".": {
16
+ "import": "./dist/index.mjs",
17
+ "require": "./dist/index.cjs"
18
+ },
19
+ "./package.json": "./package.json"
20
+ },
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "scripts": {
25
+ "build": "tsdown",
26
+ "check": "tsc",
27
+ "format": "dprint fmt",
28
+ "prepare": "lefthook install",
29
+ "prepublishOnly": "npm run build"
30
+ },
31
+ "devDependencies": {
32
+ "@arethetypeswrong/core": "^0.18.2",
33
+ "@cloudflare/workers-types": "^4.20260317.1",
34
+ "dprint": "^0.53.1",
35
+ "lefthook": "^2.1.4",
36
+ "publint": "^0.3.18",
37
+ "tsdown": "^0.21.6",
38
+ "typescript": "^6.0.2"
39
+ }
40
+ }