Simple REST API for transaction handling.
Express
TypeScript
Jest
node
http client
for ex.postman
,httpie
git clone https://github.com/wojciechszmelczerczyk/ts-transaction-api.git
cd /ts-transaction-api
Create .env
file in root directory.
CSV_FILENAME=transactions.csv
npm i --force
npm run dev
You can import provided postman json and play with api.
Library used to develop app architecture routing-controllers. REST API using Controller
, Service
and Repository
approach.
User send request with date and status body then express rest api intercept request data, validate date with moment library, status with isStatusCorrect function, modify date with modifyDate function and create random uuid
. Then with csv-writer library data is save in transactions.csv file. Server respond with modified date.
User send request with page
and limit
query params then express rest api validate those parameters with validateParams function. With convert-csv-to-json library, csv
is being converted to json
. By using paginatejson library, pagination is made on json data. In the end paginated json data is being converted back to csv
format json2csv and send back to client.
Method | Endpoint |
---|---|
POST | /api/transaction |
GET | /api/transaction |
Simple function which takes as an input Date
and transaction status Boolean
value and returns date in future:
- when boolean is set to
false
, returned date should be 5 days in future. - when boolean is set to
true
, returned date should be month in future.
npm run test:function
ex.
{ "date": "2024-01-31T08:12:59Z", "status": true }
{ "date": "2024-02-29T08:12:59Z" }
npm run test:api
when date correct and status true, should return date month in future
test("when date correct and status true, should return date month in future", async () => {
const res = await request(app)
.post("/api/transaction")
.send({ date: "2012-02-02", status: "true" });
expect(new Date(res.body.modifiedDate)).toStrictEqual(new Date("2012-03-02"));
});
when date correct and status false, should return date 5 days in future
test("when date correct and status false, should return date 5 days in future", async () => {
const res = await request(app)
.post("/api/transaction")
.send({ date: "2012-02-02", status: "false" });
expect(new Date(res.body.modifiedDate)).toStrictEqual(new Date("2012-02-07"));
});
when date incorrect, should return error message
test("when date incorrect, should return error message", async () => {
const res = await request(app)
.post("/api/transaction")
.send({ date: 2, status: "true" });
expect(res.body).toStrictEqual({
err: "Bad date format. String has to be date format",
});
});
when status incorrect, should return error message
test("when status incorrect, should return error message", async () => {
const res = await request(app)
.post("/api/transaction")
.send({ date: "2012-02-02", status: "x" });
expect(res.body).toStrictEqual({
err: "Bad status type. Status has to be either 'true' or 'false'",
});
});
when page and limit query params correct, should return records specific for params
test("when page and limit query params correct, should return records specific for params", async () => {
const res = await request(app)
.get("/api/transaction")
.query({ page: "1", limit: "2" });
expect(res).toBeTruthy();
});
when query params exceed number of transactions, should return error message
test("when query params exceed number of transactions, should return error message", async () => {
const res = await request(app)
.get("/api/transaction")
.query({ page: "1000", limit: "2000" });
expect(res.body.err).toBe("No data available for this parameters");
});
when no query params provided, should return first 5 records
test("when no query params provided, should return first 5 records", async () => {
const res = await request(app).get("/api/transaction");
expect(res).toBeTruthy();
});
when one of provided parameters incorrect, should return error message
test("when one of provided parameters incorrect, should return error message", async () => {
const res = await request(app)
.get("/api/transaction")
.query({ page: "x", limit: "2" });
expect(res.body.err).toBe(
"Page and limit have to be positive numeric values"
);
});