Skip to content

Commit

Permalink
feat: [] - Re-organize handler logic (#11)
Browse files Browse the repository at this point in the history
* Add initial pages router implementation
  • Loading branch information
Matthew Gordon authored Apr 26, 2024
1 parent 284e312 commit ba56624
Show file tree
Hide file tree
Showing 5 changed files with 85 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';
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"type": "module",
"files": [
"dist",
"app-router"
"app-router",
"pages-router"
],
"exports": {
".": {
Expand All @@ -23,6 +24,11 @@
"import": "./dist/app-router/index.js",
"require": "./dist/app-router/index.js",
"types": "./dist/app-router/index.d.ts"
},
"./pages-router": {
"import": "./dist/pages-router/index.js",
"require": "./dist/pages-router/index.js",
"types": "./dist/pages-router/index.d.ts"
}
},
"repository": {
Expand Down

0 comments on commit ba56624

Please sign in to comment.