|
1 | 1 | import { constants } from 'node:fs'; |
2 | 2 | import { access, mkdir, readFile, stat, writeFile } from 'node:fs/promises'; |
3 | 3 | 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'; |
19 | 9 |
|
20 | 10 | export class SvelteKitBuilder extends BaseBuilder { |
21 | 11 | constructor(config?: Partial<SvelteKitConfig>) { |
@@ -93,9 +83,9 @@ export class SvelteKitBuilder extends BaseBuilder { |
93 | 83 | // Replace the default export with SvelteKit-compatible handler |
94 | 84 | stepsRouteContent = stepsRouteContent.replace( |
95 | 85 | /export\s*\{\s*stepEntrypoint\s+as\s+POST\s*\}\s*;?$/m, |
96 | | - `${SVELTEKIT_REQUEST_CONVERTER} |
| 86 | + `${NORMALIZE_REQUEST_CODE} |
97 | 87 | export const POST = async ({request}) => { |
98 | | - const normalRequest = await convertSvelteKitRequest(request); |
| 88 | + const normalRequest = await normalizeRequest(request); |
99 | 89 | return stepEntrypoint(normalRequest); |
100 | 90 | }` |
101 | 91 | ); |
@@ -134,9 +124,9 @@ export const POST = async ({request}) => { |
134 | 124 | // Replace the default export with SvelteKit-compatible handler |
135 | 125 | workflowsRouteContent = workflowsRouteContent.replace( |
136 | 126 | /export const POST = workflowEntrypoint\(workflowCode\);?$/m, |
137 | | - `${SVELTEKIT_REQUEST_CONVERTER} |
| 127 | + `${NORMALIZE_REQUEST_CODE} |
138 | 128 | export const POST = async ({request}) => { |
139 | | - const normalRequest = await convertSvelteKitRequest(request); |
| 129 | + const normalRequest = await normalizeRequest(request); |
140 | 130 | return workflowEntrypoint(workflowCode)(normalRequest); |
141 | 131 | }` |
142 | 132 | ); |
@@ -177,9 +167,9 @@ export const POST = async ({request}) => { |
177 | 167 | // Replace all HTTP method exports with SvelteKit-compatible handlers |
178 | 168 | webhookRouteContent = webhookRouteContent.replace( |
179 | 169 | /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} |
181 | 171 | const createSvelteKitHandler = (method) => async ({ request, params, platform }) => { |
182 | | - const normalRequest = await convertSvelteKitRequest(request); |
| 172 | + const normalRequest = await normalizeRequest(request); |
183 | 173 | const response = await handler(normalRequest, params.token); |
184 | 174 | return response; |
185 | 175 | }; |
|
0 commit comments