Skip to content

Commit 744d82f

Browse files
pranaygpclaudeTooTallNate
authored
feat: add type safety for builder configurations with discriminated unions (#80)
* feat: add type safety for builder configurations with discriminated unions Improves type safety by creating discriminated union types for builder-specific configurations, making it clear which configuration options are required for each builder type. Changes: - Created BaseWorkflowConfig interface with common options - Created StandaloneConfig, VercelBuildOutputConfig, and NextConfig types - Made WorkflowConfig a discriminated union based on buildTarget - Added documentation for each configuration type - Exported new types from @workflow/builders This enables better IntelliSense and type checking when constructing builder configurations, preventing invalid configuration combinations at compile time. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * . Signed-off-by: Nathan Rajlich <n@n8.io> --------- Signed-off-by: Nathan Rajlich <n@n8.io> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Nathan Rajlich <n@n8.io>
1 parent bf54a7b commit 744d82f

File tree

4 files changed

+70
-8
lines changed

4 files changed

+70
-8
lines changed

.changeset/add-type-safety.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@workflow/builders": patch
3+
---
4+
5+
Add type safety for builder configurations with discriminated unions

packages/builders/src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
export { BaseBuilder } from './base-builder.js';
22
export { StandaloneBuilder } from './standalone.js';
33
export { VercelBuildOutputAPIBuilder } from './vercel-build-output-api.js';
4-
export type { WorkflowConfig, BuildTarget } from './types.js';
4+
export type {
5+
WorkflowConfig,
6+
BuildTarget,
7+
StandaloneConfig,
8+
VercelBuildOutputConfig,
9+
NextConfig,
10+
SvelteKitConfig,
11+
} from './types.js';
512
export { validBuildTargets, isValidBuildTarget } from './types.js';
613
export type { WorkflowManifest } from './apply-swc-transform.js';
714
export { applySwcTransform } from './apply-swc-transform.js';

packages/builders/src/types.ts

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ export const validBuildTargets = [
66
] as const;
77
export type BuildTarget = (typeof validBuildTargets)[number];
88

9-
export interface WorkflowConfig {
9+
/**
10+
* Common configuration options shared across all builder types.
11+
*/
12+
interface BaseWorkflowConfig {
1013
watch?: boolean;
1114
dirs: string[];
1215
workingDir: string;
13-
buildTarget: BuildTarget;
14-
stepsBundlePath: string;
15-
workflowsBundlePath: string;
16-
webhookBundlePath: string;
1716

1817
// Optionally generate a client library for workflow execution. The preferred
1918
// method of using workflow is to use a loader within a framework (like
@@ -25,6 +24,57 @@ export interface WorkflowConfig {
2524
workflowManifestPath?: string;
2625
}
2726

27+
/**
28+
* Configuration for standalone (CLI-based) builds.
29+
*/
30+
export interface StandaloneConfig extends BaseWorkflowConfig {
31+
buildTarget: 'standalone';
32+
stepsBundlePath: string;
33+
workflowsBundlePath: string;
34+
webhookBundlePath: string;
35+
}
36+
37+
/**
38+
* Configuration for Vercel Build Output API builds.
39+
*/
40+
export interface VercelBuildOutputConfig extends BaseWorkflowConfig {
41+
buildTarget: 'vercel-build-output-api';
42+
stepsBundlePath: string;
43+
workflowsBundlePath: string;
44+
webhookBundlePath: string;
45+
}
46+
47+
/**
48+
* Configuration for Next.js builds.
49+
*/
50+
export interface NextConfig extends BaseWorkflowConfig {
51+
buildTarget: 'next';
52+
// Next.js builder computes paths dynamically, so these are not used
53+
stepsBundlePath: string;
54+
workflowsBundlePath: string;
55+
webhookBundlePath: string;
56+
}
57+
58+
/**
59+
* Configuration for SvelteKit builds.
60+
*/
61+
export interface SvelteKitConfig extends BaseWorkflowConfig {
62+
buildTarget: 'sveltekit';
63+
// SvelteKit builder computes paths dynamically, so these are not used
64+
stepsBundlePath: string;
65+
workflowsBundlePath: string;
66+
webhookBundlePath: string;
67+
}
68+
69+
/**
70+
* Discriminated union of all builder configuration types.
71+
*/
72+
export type WorkflowConfig =
73+
| StandaloneConfig
74+
| VercelBuildOutputConfig
75+
| NextConfig
76+
| SvelteKitConfig;
77+
2878
export function isValidBuildTarget(
2979
target: string | undefined
3080
): target is BuildTarget {

packages/sveltekit/src/builder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { constants } from 'node:fs';
22
import { access, mkdir, readFile, stat, writeFile } from 'node:fs/promises';
33
import { join, resolve } from 'node:path';
4-
import { BaseBuilder, type WorkflowConfig } from '@workflow/builders';
4+
import { BaseBuilder, type SvelteKitConfig } from '@workflow/builders';
55

66
// Helper function code for converting SvelteKit requests to standard Request objects
77
const SVELTEKIT_REQUEST_CONVERTER = `
@@ -18,7 +18,7 @@ async function convertSvelteKitRequest(request) {
1818
`;
1919

2020
export class SvelteKitBuilder extends BaseBuilder {
21-
constructor(config?: Partial<WorkflowConfig>) {
21+
constructor(config?: Partial<SvelteKitConfig>) {
2222
super({
2323
...config,
2424
dirs: ['workflows'],

0 commit comments

Comments
 (0)