Skip to content

Commit d47d916

Browse files
author
Lukas Holzer
authored
fix: make Netlify.env working by injecting it in the entry file (netlify/zip-it-and-ship-it#1618)
* fix: make Netlify.env working by injecting it in the entry file Fixes https://github.com/netlify/pod-dev-foundations/issues/602 * chore: fix make it available in global context * chore: add testcase for environment variables
1 parent f5f7233 commit d47d916

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

packages/zip-it-and-ship-it/src/runtimes/node/utils/entry_file.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const getEntryFileContents = (
3737
// See https://esbuild.github.io/content-types/#default-interop.
3838
'const funcModule = typeof func.default === "function" ? func : func.default',
3939

40+
`global.Netlify = bootstrap.getNetlifyGlobal()`,
4041
`export const handler = bootstrap.getLambdaHandler(funcModule)`,
4142
].join(';')
4243
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default async () => {
2+
Netlify.env.set('bar', 'bar!')
3+
4+
const text = `Foo ${Netlify.env.get('bar')}`
5+
6+
Netlify.env.delete('bar')
7+
8+
return Response.json({
9+
text,
10+
foo: Netlify.env.has('foo'),
11+
bar: Netlify.env.get('bar'),
12+
env: Netlify.env.toObject(),
13+
})
14+
}

packages/zip-it-and-ship-it/tests/v2api.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,4 +467,37 @@ describe.runIf(semver.gte(nodeVersion, '18.13.0'))('V2 functions API', () => {
467467
expect(customErrorInfo.location.runtime).toBe('js')
468468
}
469469
})
470+
471+
testMany(
472+
'Retrieves the process environment through the Netlify.env helper',
473+
['bundler_default', 'bundler_esbuild', 'bundler_esbuild_zisi', 'bundler_default_nft', 'bundler_nft'],
474+
async (options) => {
475+
const { files, tmpDir } = await zipFixture('v2-api-environment-variables', {
476+
fixtureDir: FIXTURES_ESM_DIR,
477+
opts: merge(options, {
478+
archiveFormat: ARCHIVE_FORMAT.NONE,
479+
}),
480+
})
481+
482+
vi.stubEnv('foo', 'foo!')
483+
484+
const [{ name: archive, entryFilename }] = files
485+
const func = await importFunctionFile(`${tmpDir}/${archive}/${entryFilename}`)
486+
const { body: bodyStream, statusCode } = await invokeLambda(func)
487+
const body = await readAsBuffer(bodyStream)
488+
489+
const parsed = JSON.parse(body)
490+
491+
expect(parsed).toMatchObject({
492+
text: 'Foo bar!',
493+
foo: true,
494+
env: expect.objectContaining({
495+
foo: 'foo!',
496+
}),
497+
})
498+
// bar got set and deleted again
499+
expect(parsed).not.toHaveProperty('bar')
500+
expect(statusCode).toBe(200)
501+
},
502+
)
470503
})

0 commit comments

Comments
 (0)