API Gateway Designer
REST API Patterns
Resource-Oriented Endpoints
router.get('/api/users', requireAuth, async (req, res) => {
const users = await userService.list(req.query);
res.json({ data: users, meta: { total: users.length } });
});
router.post('/api/users', requireAuth, async (req, res) => {
const user = await userService.create(req.body);
res.status(201).json({ data: user });
});
router.get('/api/users/:id', requireAuth, async (req, res) => {
const user = await userService.getById(req.params.id);
if (!user) throw new NotFoundError('User not found');
res.json({ data: user });
});
Query Builder with Pagination
class QueryBuilder<T> {
build() {
let q = db.select().from(this.table);
if (this.query.filter) {
q = q.where(and(...conditions));
}
q = q.limit(this.query.limit).offset((this.query.page - 1) * this.query.limit);
return q;
}
}
GraphQL Schema
builder.prismaObject('User', {
fields: (t) => ({
id: t.exposeID('id'),
email: t.exposeString('email'),
posts: t.relation('posts'),
})
});
builder.queryType({
fields: (t) => ({
user: t.prismaField({
type: 'User',
args: { id: t.arg.id({ required: true }) },
resolve: (query, _, args) => db.user.findUnique({ ...query, where: { id: args.id } })
})
})
});
WebSocket Handler
export class WebSocketManager {
broadcast(channel: string, payload: unknown) {
this.clients.forEach(client => {
if (client.subscriptions.has(channel)) {
client.send(JSON.stringify({ channel, payload }));
}
});
}
}
Middleware
export const rateLimitMiddleware = async (req, res, next) => {
try {
await rateLimiter.consume(req.user?.id || req.ip);
next();
} catch {
res.status(429).json({ error: 'Too many requests' });
}
};
Protocol
- Assess — Understand the specific requirement and context
- Plan — Determine the approach based on the guidance above
- Execute — Apply the api gateway designer methodology
- Verify — Validate the output against expected standards
- Report — Document results and any issues encountered