-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial pages router implementation
- Loading branch information
Matt Gordon
committed
Apr 26, 2024
1 parent
52b75b2
commit c66ddf2
Showing
6 changed files
with
361 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './pages-router'; |
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,46 @@ | ||
import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import { enableDraftHandler as handler } from './pages-router'; | ||
|
||
describe('handler', () => { | ||
const bypassToken = 'bypass-token'; | ||
|
||
// const url = `https://foo.vercel.app/api/enable-draft?x-vercel-protection-bypass=${bypassToken}&path=%2Fblogs%2Fmy-cat`; | ||
|
||
beforeEach(() => { | ||
vi.stubEnv('VERCEL_AUTOMATION_BYPASS_SECRET', bypassToken); | ||
}); | ||
|
||
it('redirects safely to the provided path, passing through the token and bypass cookie query params', async () => { | ||
const mockReq = { | ||
query: { | ||
path: '/blogs/test-slug', | ||
bypass: bypassToken, | ||
}, | ||
} as unknown as NextApiRequest; | ||
|
||
const mockRes = { | ||
status: vi.fn().mockReturnThis(), | ||
json: vi.fn(), | ||
setDraftMode: vi.fn(), | ||
setHeader: vi.fn(), | ||
end: vi.fn(), | ||
} as unknown as NextApiResponse; | ||
|
||
const result = await handler(mockReq, mockRes); | ||
|
||
expect(result).to.be.undefined; | ||
expect(mockRes.status).toHaveBeenCalled(); | ||
expect(mockRes.status).toHaveBeenCalledWith(307); | ||
expect(mockRes.setDraftMode).toHaveBeenCalled(); | ||
expect(mockRes.setDraftMode).toHaveBeenCalledTimes(1); | ||
expect(mockRes.setDraftMode).toHaveBeenCalledWith({ enable: true }); | ||
expect(mockRes.setHeader).toHaveBeenCalled(); | ||
expect(mockRes.setHeader).toHaveBeenCalledTimes(1); | ||
expect(mockRes.setHeader).toHaveBeenCalledWith( | ||
'Location', | ||
'/blogs/test-slug?x-vercel-bypass-token=bypass-token&x-vercel-set-bypass-cookie=samesitenone', | ||
); | ||
expect(mockRes.end).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
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,30 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
export function enableDraftHandler(req: NextApiRequest, res: NextApiResponse) { | ||
const { path, bypass } = req.query; | ||
|
||
if (bypass !== process.env.VERCEL_AUTOMATION_BYPASS_SECRET) { | ||
return res.status(401).json({ | ||
message: 'Invalid bypass token', | ||
}); | ||
} | ||
|
||
if (!path) { | ||
return res.status(400).json({ | ||
message: 'Missing required parameter: `path`', | ||
}); | ||
} | ||
|
||
console.log('HERE!'); | ||
const decodedPath = decodeURIComponent(path as string); | ||
|
||
// TODO: validate path | ||
|
||
res.setDraftMode({ enable: true }); | ||
res.setHeader( | ||
'Location', | ||
`${decodedPath}?x-vercel-bypass-token=${bypass}&x-vercel-set-bypass-cookie=samesitenone`, | ||
); | ||
|
||
return res.status(307).end(); | ||
} |
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 @@ | ||
export * from './handlers/pages-router'; |
Oops, something went wrong.