-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose locals to render api and from requests in dev mode (#7385)
Co-authored-by: Emanuele Stoppa <[email protected]> Co-authored-by: wrapperup <[email protected]>
- Loading branch information
Showing
6 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "@test/locals", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"astro": "workspace:*", | ||
"@astrojs/node": "workspace:*" | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/adapters/node/test/fixtures/locals/src/pages/api.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
export async function post({ locals }) { | ||
let out = { ...locals }; | ||
|
||
return new Response(JSON.stringify(out), { | ||
headers: { | ||
'Content-Type': 'application/json' | ||
} | ||
}); | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/adapters/node/test/fixtures/locals/src/pages/foo.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
const { foo } = Astro.locals; | ||
--- | ||
<h1>{foo}</h1> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import nodejs from '../dist/index.js'; | ||
import { loadFixture, createRequestAndResponse } from './test-utils.js'; | ||
import { expect } from 'chai'; | ||
|
||
describe('API routes', () => { | ||
/** @type {import('./test-utils').Fixture} */ | ||
let fixture; | ||
|
||
before(async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/locals/', | ||
output: 'server', | ||
adapter: nodejs({ mode: 'middleware' }), | ||
}); | ||
await fixture.build(); | ||
}); | ||
|
||
it('Can render locals in page', async () => { | ||
const { handler } = await import('./fixtures/locals/dist/server/entry.mjs'); | ||
let { req, res, text } = createRequestAndResponse({ | ||
method: 'POST', | ||
url: '/foo', | ||
}); | ||
|
||
let locals = { foo: 'bar' }; | ||
|
||
handler(req, res, () => {}, locals); | ||
req.send(); | ||
|
||
let html = await text(); | ||
|
||
expect(html).to.contain('<h1>bar</h1>'); | ||
}); | ||
|
||
it('Can access locals in API', async () => { | ||
const { handler } = await import('./fixtures/locals/dist/server/entry.mjs'); | ||
let { req, res, done } = createRequestAndResponse({ | ||
method: 'POST', | ||
url: '/api', | ||
}); | ||
|
||
let locals = { foo: 'bar' }; | ||
|
||
handler(req, res, () => {}, locals); | ||
req.send(); | ||
|
||
let [buffer] = await done; | ||
|
||
let json = JSON.parse(buffer.toString('utf-8')); | ||
|
||
expect(json.foo).to.equal('bar'); | ||
}); | ||
}); |