ApiCoffee is a backend API server built with TypeScript, Express, Firebase, and GraphQL. It provides CRUD (Create, Read, Update, Delete) operations for managing coffee data, leveraging Firebase Firestore for data storage and GraphQL for flexible querying.
- Technologies
- Folder Structure
- Installation
- Configuration
- Usage
- API Endpoints
- Interfaces
- Error Handling
- Contributing
- License
- TypeScript: Superset of JavaScript for type-safe code.
- Express: Web framework for Node.js.
- Firebase Firestore: NoSQL database for data storage.
- GraphQL: Query language for APIs.
- Axios: Promise-based HTTP client.
- Jest: Testing framework.
- dotenv: Environment variable management.
- Sequelize: ORM for SQL databases (included as a dependency).
- Other Tools: Nodemon, ts-node, etc.
apicoffee/
├── app/
│ ├── base.ts
│ ├── .firebase/
│ │ └── firebase-setup.ts
│ ├── graphql/
│ │ ├── graphql-setup.ts
│ │ └── schema.graphql
│ ├── interfaces/
│ │ └── coffee-interfaces.ts
│ ├── models/
│ │ └── coffee.ts
│ ├── repositories/
│ │ └── coffee-dao.ts
│ ├── services/
│ │ └── coffee-management.ts
│ ├── routers/
│ │ └── router-manager.ts
│ ├── utils/
│ │ └── custom-error-feedback.ts
│ └── index.ts
├── dist/
│ └── app/
│ └── graphql/
│ └── schema.graphql
├── tests/
│ └── ... (test files)
├── .gitignore
├── package.json
├── tsconfig.json
└── README.md
- app/: Contains the core application code.
- .firebase/: Firebase configuration and initialization.
- graphql/: GraphQL setup, including schema definitions and resolver configurations.
- interfaces/: TypeScript interfaces.
- models/: Data models representing business entities.
- repositories/: Data Access Objects (DAOs) for interacting with databases.
- services/: Business logic and service layer.
- routers/: Express router management.
- utils/: Utility functions and custom error handling.
- base.ts: Centralized handler for GraphQL events.
- index.ts: Entry point of the application.
- dist/: Compiled JavaScript files.
- tests/: Contains test suites for the application.
- package.json: Project metadata and dependencies.
- tsconfig.json: TypeScript configuration.
- README.md: Project documentation.
-
Clone the repository
https://github.com/antfconeto/apiCoffeeBack.git cd apiCoffeeBack
-
Install dependencies
npm install
-
Build the project
npm run build
-
Start the server
npm start
-
Environment Variables
Create a
.env
file in the root directory and populate it with the following variables:FIREBASE_TYPE=service_account FIREBASE_PROJECT_ID=your_project_id FIREBASE_PRIVATE_KEY_ID=your_private_key_id FIREBASE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\nYOUR_PRIVATE_KEY\n-----END PRIVATE KEY-----\n" FIREBASE_CLIENT_EMAIL=your_client_email FIREBASE_CLIENT_ID=your_client_id FIREBASE_AUTH_URI=https://accounts.google.com/o/oauth2/auth FIREBASE_TOKEN_URI=https://oauth2.googleapis.com/token FIREBASE_AUTH_PROVIDER_X509_CERT_URL=https://www.googleapis.com/oauth2/v1/certs FIREBASE_CLIENT_X509_CERT_URL=https://www.googleapis.com/robot/v1/metadata/x509/your_client_email PORT=5000
Note: Ensure that sensitive information like
FIREBASE_PRIVATE_KEY
is kept secure and never committed to version control.
Once the server is running, you can interact with the API using GraphQL queries and mutations at the /graphql
endpoint or via the root route (/
) which performs a sample GraphQL query using Axios.
-
List All Coffees
query { listAllCoffees { id name description price createdAt updatedAt } }
-
Get Coffee by ID
query { getCoffeeById(coffeeId: "coffee_id_here") { id name description price createdAt updatedAt } }
-
Create a New Coffee
mutation { createCoffee(coffee: { name: "Espresso", description: "Strong and bold coffee", price: 2.99 }) { id name description price createdAt updatedAt } }
-
Update an Existing Coffee
mutation { updateCoffee(coffee: { id: "coffee_id_here", name: "Latte", description: "Coffee with milk", price: 3.99 }) { id name description price createdAt updatedAt } }
-
Delete a Coffee
mutation { deleteCoffee(coffeeId: "coffee_id_here") }
-
GraphQL Endpoint
- Path:
/graphql
- Description: Primary endpoint for all GraphQL queries and mutations.
- Access: Accessible via GraphiQL interface for testing and development.
- Path:
-
Root Endpoint
- Path:
/
- Method:
GET
- Description: Executes a sample GraphQL query to list all coffees using Axios and returns the response.
- Response: JSON data containing the list of coffees or an error message.
- Path:
Defines the structure of a Coffee object.
export interface Coffee {
id: string;
name: string;
description: string;
createdAt: string;
updatedAt: string;
price: number;
}
Defines the data access methods for Coffee.
export interface ICoffeeDAO {
createCoffee(coffee: CoffeeModel): Promise<CoffeeModel>;
updateCoffee(coffee: CoffeeModel): Promise<CoffeeModel>;
getCoffeeById(id: string): Promise<CoffeeModel | undefined>;
deleteCoffee(id: string): Promise<boolean>;
listAllCoffees(): Promise<CoffeeModel[]>;
}
Defines the business logic methods for Coffee management.
export interface ICoffeeManagement {
createCoffee(coffee: CoffeeModel): Promise<CoffeeModel>;
updateCoffee(coffee: CoffeeModel): Promise<CoffeeModel>;
getCoffeeById(id: string): Promise<CoffeeModel | undefined>;
deleteCoffee(id: string): Promise<boolean>;
listAllCoffees(): Promise<CoffeeModel[]>;
}
Custom error handling is implemented using the CustomError
class, which extends the native Error
class and includes an HTTP status code.
export class CustomError extends Error {
statusCode: number;
constructor(statusCode: number, message: string) {
super(message);
this.statusCode = statusCode;
Object.setPrototypeOf(this, CustomError.prototype);
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
}
}
- Usage: Throw
CustomError
with appropriate status codes (e.g., 400 for Bad Request, 500 for Internal Server Error) to handle different error scenarios gracefully.
Contributions are welcome! Please follow these steps:
-
Fork the repository
-
Create a new branch
git checkout -b feature/your-feature-name
-
Commit your changes
git commit -m "Add your message"
-
Push to the branch
git push origin feature/your-feature-name
-
Create a Pull Request
This project is licensed under the ISC License.
Note: Ensure that the .env
file is properly configured and that sensitive information is secured. Avoid committing .env
files to version control.