feat: base shortcut code
This commit is contained in:
65
app/api/shortcut/route.test.ts
Normal file
65
app/api/shortcut/route.test.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { POST } from './route';
|
||||
|
||||
describe('Shortcuts API Route', () => {
|
||||
const originalEnv = process.env;
|
||||
|
||||
beforeEach(() => {
|
||||
process.env = { ...originalEnv };
|
||||
process.env.USER_KEY = 'test-key-123';
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
process.env = originalEnv;
|
||||
});
|
||||
|
||||
it('should handle valid ping request', async () => {
|
||||
const request = new Request('http://localhost:3000/api/shortcuts', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
command: 'ping',
|
||||
apiKey: 'test-key-123'
|
||||
})
|
||||
});
|
||||
|
||||
const response = await POST(request);
|
||||
const data = await response.json();
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(data.success).toBe(true);
|
||||
expect(data.message).toContain('operational');
|
||||
});
|
||||
|
||||
it('should reject invalid API key', async () => {
|
||||
const request = new Request('http://localhost:3000/api/shortcuts', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
command: 'ping',
|
||||
apiKey: 'wrong-key'
|
||||
})
|
||||
});
|
||||
|
||||
const response = await POST(request);
|
||||
expect(response.status).toBe(401);
|
||||
});
|
||||
|
||||
it('should handle invalid request format', async () => {
|
||||
const request = new Request('http://localhost:3000/api/shortcuts', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
command: 'ping'
|
||||
})
|
||||
});
|
||||
|
||||
const response = await POST(request);
|
||||
expect(response.status).toBe(400);
|
||||
});
|
||||
});
|
||||
50
app/api/shortcut/route.ts
Normal file
50
app/api/shortcut/route.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { ShortcutsHandler } from '@utils/handler';
|
||||
import { RequestSchema } from '@utils/types';
|
||||
|
||||
export async function POST(req: Request) {
|
||||
try {
|
||||
const body = await req.json();
|
||||
|
||||
const result = RequestSchema.safeParse(body);
|
||||
if (!result.success) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: 'Invalid request format.',
|
||||
errors: result.error.errors
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const shortcutsHandler = new ShortcutsHandler();
|
||||
const isValid = shortcutsHandler.validateRequest(result.data);
|
||||
|
||||
if (!isValid) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: 'Unauthorized.'
|
||||
},
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
const response = await shortcutsHandler.processCommand(
|
||||
result.data.command,
|
||||
result.data.parameters
|
||||
);
|
||||
|
||||
return NextResponse.json(response);
|
||||
} catch (error) {
|
||||
console.error('Error processing shortcuts request:', error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: 'An error occurred while processing your request.'
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user