Skip to content

Commit

Permalink
bug/issue 1329 pass custom runtime to API routes for Vercel adapter (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 authored Jan 13, 2025
1 parent 0f4d656 commit 37a3b90
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/plugin-adapter-vercel/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ async function vercelAdapter(compilation, options) {
const outputRoot = new URL(`.${basePath}/api/${id}.func/`, adapterOutputUrl);
const { assets = [] } = value;

await setupFunctionBuildFolder(id, outputType, outputRoot);
await setupFunctionBuildFolder(id, outputType, outputRoot, runtime);

await fs.cp(
new URL(outputHref),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* index.js
*/
import chai from 'chai';
import fs from 'fs/promises';
import fs from 'fs';
import glob from 'glob-promise';
import { JSDOM } from 'jsdom';
import path from 'path';
Expand Down Expand Up @@ -65,31 +65,35 @@ describe('Build Greenwood With: ', function() {
let functionFolders;

before(async function() {
configFile = await fs.readFile(new URL('./config.json', vercelOutputFolder), 'utf-8');
configFile = await fs.promises.readFile(new URL('./config.json', vercelOutputFolder), 'utf-8');
functionFolders = await glob.promise(path.join(normalizePathnameForWindows(vercelFunctionsOutputUrl), '**/*.func'));
});

it('should output the expected number of serverless function output folders', function() {
expect(functionFolders.length).to.be.equal(1);
expect(functionFolders.length).to.be.equal(2);
});

it('should output the expected configuration file for the build output', function() {
expect(configFile).to.be.equal('{"version":3}');
});

it('should output the expected package.json for each serverless function', function() {
functionFolders.forEach(async (folder) => {
const packageJson = await fs.readFile(new URL('./package.json', `file://${folder}/`), 'utf-8');

expect(packageJson).to.be.equal('{"type":"module"}');
functionFolders.forEach((folder) => {
fs.readFile(path.join(folder, 'package.json'), 'utf-8', (err, contents) => {
if (!err) {
expect(contents).to.equal('{"type":"module"}');
}
});
});
});

it('should output the expected .vc-config.json for each serverless function with runtime option honored', function() {
functionFolders.forEach(async (folder) => {
const packageJson = await fs.readFile(new URL('./vc-config.json', `file://${folder}/`), 'utf-8');

expect(packageJson).to.be.equal('{"runtime":"nodejs22.x","handler":"index.js","launcherType":"Nodejs","shouldAddHelpers":true}');
functionFolders.forEach((folder) => {
fs.readFile(path.join(folder, '.vc-config.json'), 'utf-8', (err, contents) => {
if (!err) {
expect(contents).to.equal('{"runtime":"nodejs22.x","handler":"index.js","launcherType":"Nodejs","shouldAddHelpers":true}');
}
});
});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export async function handler(request) {
const params = new URLSearchParams(request.url.slice(request.url.indexOf('?')));
const name = params.has('name') ? params.get('name') : 'World';
const body = { message: `Hello ${name}!` };

return new Response(JSON.stringify(body), {
headers: new Headers({
'Content-Type': 'application/json'
})
});
}

0 comments on commit 37a3b90

Please sign in to comment.