Skip to content

Commit 19c271c

Browse files
authored
refactor: move request converters to @workflow/builders (#503)
sveltekit and astro use the exact same code for normalizing requests so it's been refactored to `request-converter.ts` inside `@workflow/builders`
1 parent 205b395 commit 19c271c

File tree

5 files changed

+40
-41
lines changed

5 files changed

+40
-41
lines changed

.changeset/deep-falcons-pick.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@workflow/sveltekit": patch
3+
"@workflow/builders": patch
4+
"@workflow/astro": patch
5+
---
6+
7+
Refactor request converter code in SvelteKit and Astro builder to @workflow/builders

packages/astro/src/builder.ts

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,9 @@ import {
55
VercelBuildOutputAPIBuilder,
66
createBaseBuilderConfig,
77
type AstroConfig,
8+
NORMALIZE_REQUEST_CODE,
89
} from '@workflow/builders';
910

10-
// NOTE: This is the same as SvelteKit request converter, should merge
11-
const NORMALIZE_REQUEST_CONVERTER = `
12-
async function normalizeRequestConverter(request) {
13-
const options = {
14-
method: request.method,
15-
headers: new Headers(request.headers)
16-
};
17-
if (!['GET', 'HEAD', 'OPTIONS', 'TRACE', 'CONNECT'].includes(request.method)) {
18-
options.body = await request.arrayBuffer();
19-
}
20-
return new Request(request.url, options);
21-
}
22-
`;
23-
2411
const WORKFLOW_ROUTES = [
2512
{
2613
src: '^/\\.well-known/workflow/v1/flow/?$',
@@ -105,9 +92,9 @@ export class LocalBuilder extends BaseBuilder {
10592
// Normalize request, needed for preserving request through astro
10693
stepsRouteContent = stepsRouteContent.replace(
10794
/export\s*\{\s*stepEntrypoint\s+as\s+POST\s*\}\s*;?$/m,
108-
`${NORMALIZE_REQUEST_CONVERTER}
95+
`${NORMALIZE_REQUEST_CODE}
10996
export const POST = async ({request}) => {
110-
const normalRequest = await normalizeRequestConverter(request);
97+
const normalRequest = await normalizeRequest(request);
11198
return stepEntrypoint(normalRequest);
11299
}
113100
@@ -143,9 +130,9 @@ export const prerender = false;`
143130
// Normalize request, needed for preserving request through astro
144131
workflowsRouteContent = workflowsRouteContent.replace(
145132
/export const POST = workflowEntrypoint\(workflowCode\);?$/m,
146-
`${NORMALIZE_REQUEST_CONVERTER}
133+
`${NORMALIZE_REQUEST_CODE}
147134
export const POST = async ({request}) => {
148-
const normalRequest = await normalizeRequestConverter(request);
135+
const normalRequest = await normalizeRequest(request);
149136
return workflowEntrypoint(workflowCode)(normalRequest);
150137
}
151138
@@ -185,9 +172,9 @@ export const prerender = false;`
185172
// Normalize request, needed for preserving request through astro
186173
webhookRouteContent = webhookRouteContent.replace(
187174
/export const GET = handler;\nexport const POST = handler;\nexport const PUT = handler;\nexport const PATCH = handler;\nexport const DELETE = handler;\nexport const HEAD = handler;\nexport const OPTIONS = handler;/,
188-
`${NORMALIZE_REQUEST_CONVERTER}
175+
`${NORMALIZE_REQUEST_CODE}
189176
const createHandler = (method) => async ({ request, params, platform }) => {
190-
const normalRequest = await normalizeRequestConverter(request);
177+
const normalRequest = await normalizeRequest(request);
191178
const response = await handler(normalRequest, params.token);
192179
return response;
193180
};

packages/builders/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ export type {
1919
export { isValidBuildTarget, validBuildTargets } from './types.js';
2020
export { VercelBuildOutputAPIBuilder } from './vercel-build-output-api.js';
2121
export { createBuildQueue } from './build-queue.js';
22+
export { NORMALIZE_REQUEST_CODE } from './request-converter.js';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const NORMALIZE_REQUEST_CODE = `
2+
async function normalizeRequest(request) {
3+
const options = {
4+
method: request.method,
5+
headers: new Headers(request.headers)
6+
};
7+
if (!['GET', 'HEAD', 'OPTIONS', 'TRACE', 'CONNECT'].includes(request.method)) {
8+
options.body = await request.arrayBuffer();
9+
}
10+
return new Request(request.url, options);
11+
}
12+
`;
13+
14+
export { NORMALIZE_REQUEST_CODE };

packages/sveltekit/src/builder.ts

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
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 SvelteKitConfig } from '@workflow/builders';
5-
6-
// Helper function code for converting SvelteKit requests to standard Request objects
7-
const SVELTEKIT_REQUEST_CONVERTER = `
8-
async function convertSvelteKitRequest(request) {
9-
const options = {
10-
method: request.method,
11-
headers: new Headers(request.headers)
12-
};
13-
if (!['GET', 'HEAD', 'OPTIONS', 'TRACE', 'CONNECT'].includes(request.method)) {
14-
options.body = await request.arrayBuffer();
15-
}
16-
return new Request(request.url, options);
17-
}
18-
`;
4+
import {
5+
BaseBuilder,
6+
type SvelteKitConfig,
7+
NORMALIZE_REQUEST_CODE,
8+
} from '@workflow/builders';
199

2010
export class SvelteKitBuilder extends BaseBuilder {
2111
constructor(config?: Partial<SvelteKitConfig>) {
@@ -93,9 +83,9 @@ export class SvelteKitBuilder extends BaseBuilder {
9383
// Replace the default export with SvelteKit-compatible handler
9484
stepsRouteContent = stepsRouteContent.replace(
9585
/export\s*\{\s*stepEntrypoint\s+as\s+POST\s*\}\s*;?$/m,
96-
`${SVELTEKIT_REQUEST_CONVERTER}
86+
`${NORMALIZE_REQUEST_CODE}
9787
export const POST = async ({request}) => {
98-
const normalRequest = await convertSvelteKitRequest(request);
88+
const normalRequest = await normalizeRequest(request);
9989
return stepEntrypoint(normalRequest);
10090
}`
10191
);
@@ -134,9 +124,9 @@ export const POST = async ({request}) => {
134124
// Replace the default export with SvelteKit-compatible handler
135125
workflowsRouteContent = workflowsRouteContent.replace(
136126
/export const POST = workflowEntrypoint\(workflowCode\);?$/m,
137-
`${SVELTEKIT_REQUEST_CONVERTER}
127+
`${NORMALIZE_REQUEST_CODE}
138128
export const POST = async ({request}) => {
139-
const normalRequest = await convertSvelteKitRequest(request);
129+
const normalRequest = await normalizeRequest(request);
140130
return workflowEntrypoint(workflowCode)(normalRequest);
141131
}`
142132
);
@@ -177,9 +167,9 @@ export const POST = async ({request}) => {
177167
// Replace all HTTP method exports with SvelteKit-compatible handlers
178168
webhookRouteContent = webhookRouteContent.replace(
179169
/export const GET = handler;\nexport const POST = handler;\nexport const PUT = handler;\nexport const PATCH = handler;\nexport const DELETE = handler;\nexport const HEAD = handler;\nexport const OPTIONS = handler;/,
180-
`${SVELTEKIT_REQUEST_CONVERTER}
170+
`${NORMALIZE_REQUEST_CODE}
181171
const createSvelteKitHandler = (method) => async ({ request, params, platform }) => {
182-
const normalRequest = await convertSvelteKitRequest(request);
172+
const normalRequest = await normalizeRequest(request);
183173
const response = await handler(normalRequest, params.token);
184174
return response;
185175
};

0 commit comments

Comments
 (0)