Skip to content
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
19 changes: 19 additions & 0 deletions apps/api/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module",
"project": "./tsconfig.json"
},
"plugins": ["@typescript-eslint"],
"rules": {
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }]
},
"ignorePatterns": ["dist", "node_modules", "test", "**/*.d.ts", "**/*.js"]
}

21 changes: 21 additions & 0 deletions apps/api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Build output
dist
*.tsbuildinfo

# Dependencies
node_modules

# Environment
.env
.env.local

# Database
*.db
*.db-journal

# Test coverage
coverage

# Logs
*.log

Binary file added apps/api/:memory:?cache=shared
Binary file not shown.
19 changes: 19 additions & 0 deletions apps/api/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
roots: ['<rootDir>/test'],
testMatch: ['**/*.test.ts'],
collectCoverageFrom: [
'src/**/*.ts',
'!src/**/*.d.ts',
],
coverageThreshold: {
global: {
branches: 80,
functions: 80,
lines: 80,
statements: 80,
},
},
};

24 changes: 19 additions & 5 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,26 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"dev": "echo 'API dev server not yet implemented'",
"build": "echo 'API build not yet implemented'",
"test": "echo 'API tests not yet implemented'",
"dev": "tsx watch src/index.ts",
"build": "tsc",
"test": "jest",
"lint": "eslint src"
},
"dependencies": {},
"devDependencies": {}
"dependencies": {
"@togglekit/flags-core-ts": "workspace:*",
"fastify": "^4.25.2",
"@fastify/cors": "^8.5.0",
"better-sqlite3": "^9.2.2",
"zod": "^3.22.4"
},
"devDependencies": {
"@types/better-sqlite3": "^7.6.8",
"@types/node": "^20.11.0",
"tsx": "^4.7.0",
"typescript": "^5.3.3",
"jest": "^29.7.0",
"@types/jest": "^29.5.11",
"ts-jest": "^29.1.1"
}
}

79 changes: 79 additions & 0 deletions apps/api/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Fastify application factory
* Creates and configures the Fastify server instance
*/

import Fastify, { FastifyInstance } from 'fastify';
import cors from '@fastify/cors';
import { configRoutes } from './routes/config';
import { flagRoutes } from './routes/flags';
import { evaluateRoutes } from './routes/evaluate';

export interface AppOptions {
logger?: boolean;
cors?: {
origin: string | string[] | boolean;
};
}

/**
* Create and configure Fastify application
*/
export async function createApp(options: AppOptions = {}): Promise<FastifyInstance> {
const app = Fastify({
logger: options.logger ?? true,
disableRequestLogging: false,
requestIdHeader: 'x-request-id',
});

// Register CORS
await app.register(cors, {
origin: options.cors?.origin ?? true,
credentials: true,
});

// Health check endpoint
app.get('/health', async () => {
return { status: 'ok', timestamp: new Date().toISOString() };
});

// Register routes
await app.register(configRoutes);
await app.register(flagRoutes);
await app.register(evaluateRoutes);

// Global error handler
app.setErrorHandler((error, request, reply) => {
request.log.error(error);

// Handle validation errors
if (error.validation) {
return reply.status(400).send({
error: 'Bad Request',
message: 'Validation failed',
statusCode: 400,
details: error.validation,
});
}

// Default error response
const statusCode = error.statusCode ?? 500;
return reply.status(statusCode).send({
error: error.name || 'Internal Server Error',
message: error.message || 'An unexpected error occurred',
statusCode,
});
});

// 404 handler
app.setNotFoundHandler((request, reply) => {
return reply.status(404).send({
error: 'Not Found',
message: `Route ${request.method} ${request.url} not found`,
statusCode: 404,
});
});

return app;
}

Loading
Loading