Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [] - Re-organize handler logic #11

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading