Skip to content

Commit

Permalink
Add initial pages router implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Gordon committed Apr 26, 2024
1 parent 284e312 commit 27fdf73
Show file tree
Hide file tree
Showing 6 changed files with 361 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/pages-router/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './pages-router';
46 changes: 46 additions & 0 deletions lib/pages-router/handlers/pages-router.spec.ts
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);
});
});
30 changes: 30 additions & 0 deletions lib/pages-router/handlers/pages-router.ts
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();
}
1 change: 1 addition & 0 deletions lib/pages-router/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './handlers/pages-router';
Loading

0 comments on commit 27fdf73

Please sign in to comment.