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

Parsing Figma text nodes and sending to the mocked API #2

Open
wants to merge 37 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
16d2fe6
(chore): add env file
Samesma Sep 25, 2024
7a7cbbe
(chore): add ignore files
Samesma Sep 25, 2024
b5f1c60
(chore): add config files
Samesma Sep 25, 2024
0135848
(chore): update ReadMe file
Samesma Sep 25, 2024
3d8d199
(chore): update package.json files
Samesma Sep 25, 2024
62b29e2
(chore): add tsconfig file
Samesma Sep 25, 2024
b3045ab
(feat): add index file
Samesma Sep 25, 2024
f5130cb
(feat): add inversify file
Samesma Sep 25, 2024
0799897
(feat): add server file
Samesma Sep 25, 2024
9c97399
(feat): add index file
Samesma Sep 25, 2024
3aad638
(feat): add figma.controler file
Samesma Sep 25, 2024
689345b
(feat): add file.helper file
Samesma Sep 25, 2024
4ac9e08
(feat): add nock-setup file
Samesma Sep 25, 2024
c6ff1b5
(feat): add figmanodetype file
Samesma Sep 25, 2024
ed1408c
(feat): add TextNode file
Samesma Sep 25, 2024
97035a2
(feat): add route file
Samesma Sep 25, 2024
a1aa3d9
(feat): add route file
Samesma Sep 25, 2024
42712dc
(feat): add ICsvWriter file
Samesma Sep 25, 2024
1662eba
(feat): add FastCsvWriterService file
Samesma Sep 25, 2024
6d02f2f
(feat): add FigmaService file
Samesma Sep 25, 2024
3b2dac5
(feat): add ILogger file
Samesma Sep 25, 2024
cacd3e5
(feat): add PinoLoggerService file
Samesma Sep 25, 2024
9244709
(feat): add PinoLoggerService file
Samesma Sep 25, 2024
42bd7f0
(feat): add setUpTests file
Samesma Sep 25, 2024
e3b766c
(test): add e2e test file
Samesma Sep 25, 2024
a4dcdd9
(test): add integration test file
Samesma Sep 25, 2024
e367181
(test): add unit test file
Samesma Sep 25, 2024
e2fd8b9
(chore):delete the file
Samesma Sep 25, 2024
d5054e7
(chore):update the file
Samesma Sep 25, 2024
c98b2d6
(chore):update ReadMe file
Samesma Sep 25, 2024
30f182b
(feat):update server file
Samesma Sep 25, 2024
554e764
(feat):update route files
Samesma Sep 25, 2024
9440347
(test):update test file
Samesma Sep 25, 2024
5349188
(fix):update the file
Samesma Sep 25, 2024
6d5a80c
(chore):update ReadMe file
Samesma Sep 25, 2024
4999a9c
(chore):update package.json file
Samesma Sep 25, 2024
45de4fc
(chore):update ReadMe file
Samesma Sep 25, 2024
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
14 changes: 14 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
NODE_ENV=development
PORT=3000
LOG_LEVEL=info # Log level (info, debug, error, etc.)
FIGMA_API_TOKEN=add_your_figma_token
FIGMA_FILE_ID=FLca5Z5BpSca7dtQFmJva9
FIGMA_API_URL=https://api.figma.com/v1 # Figma API Base URL
CSV_FILE_PATH=./csv-files
CSV_FILE_NAME=operation_statistics.csv
LOG_FILE_PATH=./logs
LOG_FILE_NAME=application.log
JSON_FILE_PATH=./json-files
JSON_FILE_NAME=figma_response.json


3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist
.eslintrc.js
.prettierrc.js
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ node_modules
dist
*.local

.env

# Editor directories and files
.vscode/*
Expand Down
197 changes: 195 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,209 @@ Write a script which aggregates this data and makes a request to a mocked API at
1. Pull this repository, make changes, and create a Pull Request
2. Include documentation on how to run the script, and how to observe its outputs

## Summary of Features

* Fetches text node data from the Figma API and extracts key information (text content, font details, etc.).
* Uses Inversify for dependency injection, making the app modular and easy to extend or test.
* Generates CSV reports for logging operation statistics such as number of nodes processed, request status, and processing time.
* Logs real-time information using the Pino logger, with log levels configurable via environment variables.
* Saves extracted data to a JSON file for persistent storage.
* REST API built on Fastify with extensible routes for future endpoints.
* Unit and integration tests using Jest and Supertest for reliability.
* Scalable and easily deployable with modular architecture and cloud-ready features.
* Integrating Swagger into the app, ensuring that the API is both well-documented and easy to test, facilitating better collaboration and faster development.
* API versioning to ensure backward compatibility with older clients if you need to introduce breaking changes.
* Configured CORS (Cross-Origin Resource Sharing) to prevent unauthorized external domains from accessing the API
* Follow the Separation of Concerns (SoC) principle by making sure that each module/class focuses on one responsibility.
* Utilize Fastify features to allow the application to stay performant, scalable, and easy to maintain while being modular and optimized for handling HTTP requests, logging, and error handling.

## Setup

Install Dependencies
```sh
npm install
```

## Build
## Build Instructions

### Set Up Figma API Token

Ensure you have updated the FIGMA_API_TOKEN in the .env file, located in the root directory, with the personal access token from your Figma account.


You can obtain the Figma API Token by following the instructions on the [Figma API documentation](https://help.figma.com/hc/en-us/articles/8085703771159-Manage-personal-access-tokens).

#### .env File

```sh
PORT=3000
FIGMA_API_TOKEN=your_figma_api_token_here # Replace with your actual Figma API token
FIGMA_FILE_ID=default_file_id
FIGMA_API_URL=https://api.figma.com/v1 # Figma API Base URL
CSV_FILE_PATH=./csv-files
CSV_FILE_NAME=operation_statistics.csv
LOG_FILE_PATH=./logs
LOG_FILE_NAME=application.log
JSON_FILE_PATH=./json-files
JSON_FILE_NAME=figma_response.json
```

You can modify the PORT, LOG_LEVEL, JSON_FILE_NAME, and paths for LOG_FILE_PATH and CSV_FILE_PATH as per your specific project requirements.

#### Building the Application

To build the application, run the following command:

Run scripting
```sh
npm run build
```

## Starting the Server

Once everything is configured, you can start the Fastify server with the following command:

```sh
npm run start
```

For development with hot-reloading, use this command:


```sh
npm run dev
```

## Making Requests and Observing Output

### Make requests (e.g., using curl, Postman, Web Browser)

1. Fetch Data from Default Figma File

To retrieve data from the default Figma file ID specified in the .env file (which can be updated based on your access), execute the following command in your terminal:

```sh
curl http://localhost:3000/v1/figma/file/default-file
```

Alternatively, you can access the same endpoint through a web browser or tools like Postman by using the URL:

```sh
http://localhost:3000/v1/figma/file/default-file
```

The response will be returned in JSON format, similar to the following example:

```js
[
{
"content": "Settings",
"fontSize": 32,
"fontFamily": "Toyota Type W02",
"fontWeight": 600,
"lineHeight": 39,
"width": 128,
"height": 39
},
{
"content": "Display",
"fontSize": 32,
"fontFamily": "Toyota Type W02",
"fontWeight": 400,
"lineHeight": 39,
"width": 113,
"height": 39
}
]
```

Additionally, after each successful API request, a JSON file is generated and saved to the location specified in your .env file **(./json-files/figma_response.json)**.

This file can be shared with the translation team for further use.

2. Fetch Data Using a Specific Figma File ID

You can also retrieve data using a specific Figma file ID by calling the following endpoint (make sure you have access to the file):

```sh
curl http://localhost:3000/v1/figma/file/{fileId}
```

Alternatively, use Postman or a browser to send a request to this endpoint. The response format will be the same as with the default file ID.

## Run tests

The application includes **unit, integration, and end-to-end tests**. To run the complete test suite, use the following command:

```sh
npm run test
```

This will execute all the tests and provide feedback on the correctness and performance of the application.

## Explanation of Package Roles

### Core App

- inversify: For dependency injection (DI) in services and controllers.
- axios: For making HTTP requests (Figma API).
- fastify: For handling API requests and responses.
- dotenv: For managing environment variables.
- pino: For structured logging in the app.
- reflect-metadata: Required for Inversify's DI system to work.
- swagger plugin: Provides OpenAPI specification generation.
- swaggerUI plugin: Provides an interactive Swagger UI accessible at /swagger/documentation.
- fastify/cors: prevent unauthorized external domains from accessing the API

### Testing & Development

- jest: For unit and integration testing.
- ts-jest: Jest plugin for TypeScript support.
- supertest: HTTP testing library for route testing.
- nock: For mocking HTTP requests during testing.
- typescript: TypeScript compiler.
- ts-node-dev: Hot-reloading during development.
- ts-node: To run TypeScript files without compiling.

This setup ensures that the app can interact with the Figma API, generate logs, manage dependencies, and perform testing efficiently.

## Statistics of the operation in CVS file

In this application, the CSV file serves as a report for logging the statistics of the operation

### CSV File Overview

The CSV file is meant to store **metrics** and **statistics** about each operation performed by the app, such as fetching data from the Figma API and processing the text nodes.

### Data Saved in the CSV

* Timestamp: When the operation occurred.
* Number of Text Nodes Processed: How many nodes were extracted from the Figma file.
* Request and Response Information: Status codes, response times, payload sizes, etc.
* Processing Time: How long it took to process the text nodes.

### CSV File Path

The CSV_FILE_PATH environment variable in the .env file defines where the CSV file is saved. The default path is **./cvs-files**, and the file is named **operation_statistics.csv**, and in the prod environments the cloud resources like AWS S3 buckets can be used for the saving path.

## Logging Mechanism in the App

The logging mechanism in this application is built using the Pino logger, which is known for its high performance.

### Log Levels

info: General information about the application's operation, such as successful API requests or the number of nodes processed.

error: Errors or issues that occur during runtime, such as failures in fetching data from the Figma API.
You can control the verbosity of logs by setting the LOG_LEVEL environment variable. For instance, setting LOG_LEVEL=debug would allow more detailed logs to appear.

### Log Output

The logs can be output to the console (using Pino's pino-pretty for human-readable formatting) or, in production, they can be redirected to files or external logging systems (such as CloudWatch, Loggly, etc.).

## Swagger in the App

- Auto-generated API documentation: Swagger generates OpenAPI-compliant documentation for all Fastify routes.
- Interactive Swagger UI: Users can explore the API and test endpoints directly from the browser at /swagger/documentation.
- Detailed endpoint information: Each route includes descriptions, response formats, and status codes.
- Real-time API testing: Users can send live requests to API endpoints directly from Swagger UI.
- Easy configuration: Routes are documented by adding a schema to - the Fastify route definition, allowing for quick updates and easy maintenance.
40 changes: 40 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module.exports = {
env: {
browser: true,
es2021: true,
node: true,
},
parser: '@typescript-eslint/parser', // Specifies the ESLint parser for TypeScript
extends: [
'eslint:recommended', // Use recommended ESLint rules
'plugin:@typescript-eslint/recommended', // Recommended TypeScript rules
'plugin:prettier/recommended', // Enables prettier as an ESLint rule
],
parserOptions: {
ecmaVersion: 12, // Supports modern ECMAScript features
sourceType: 'module', // Enables ECMAScript modules
},
plugins: ['@typescript-eslint', 'import', 'prettier'],
rules: {
'prettier/prettier': 'error', // Treat prettier errors as ESLint errors
'@typescript-eslint/no-unused-vars': ['error'], // Disallow unused variables
'no-console': 'warn', // Warn on console.log() usage
'import/order': [
'error',
{
'newlines-between': 'always',
groups: [['builtin', 'external', 'internal']],
},
],
'no-var': 'error', // Disallow var
'prefer-const': 'error', // Prefer const over let when possible
},
ignorePatterns: [
'node_modules/', // Ignore dependencies folder
'dist/', // Ignore build output folder
'.env', // Ignore environment configuration file
'package-lock.json', // Ignore lock files
'yarn.lock', // Ignore yarn lock files
'tsconfig.json', // Ignore TypeScript config file
],
};
8 changes: 8 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
moduleFileExtensions: ['ts', 'js', 'json'],
testPathIgnorePatterns: ['/node_modules/'], // Exclude node_modules from tests
testMatch: ['**/?(*.)+(spec|test).[tj]s?(x)'], // Look for .spec.ts or .test.ts files
setupFilesAfterEnv: ['<rootDir>/src/tests/setupTests.ts'], // Add this for global setup
};
Loading