Add basic project and change LICENCE to AGPL

Signed-off-by: Lucas Fernandes de Oliveira's avatarLucas Fernandes de Oliveira <lfoliveira@inf.ufpr.br>
parents
Pipeline #19206 passed with stage
in 43 seconds
*.log
*.out
*.pid
*.map
*.swp
*.env
*.swo
/.trash
/pids
/logs
/node_modules
/doc
/coverage
/build
/dist
image: node:6.12.2
cache:
paths:
- node_modules
stages:
- test
run_test:
stage: test
script:
- yarn install --frozen-lockfile --silent --non-interactive
- ln -s config.env.example config/test.env
- yarn test
- yarn run lint
tags:
- node
This diff is collapsed.
# Form Creator API
[![Build Status](https://gitlab.c3sl.ufpr.br/simmctic/form-creator/form-creator-api/badges/master/pipeline.svg)](https://gitlab.c3sl.ufpr.br/simmctic/form-creator/form-creator-api/commits/master)
[![Coverage Report](https://gitlab.c3sl.ufpr.br/simmctic/form-creator/form-creator-api/badges/master/coverage.svg)](https://gitlab.c3sl.ufpr.br/simmctic/form-creator/form-creator-api/commits/master)
RESTful API used to manage and answer forms.
## Objective
The main objective of this project is create a *Web Service* which allows users
to create and update its own forms offering the following features.
* Versionable
* The form can be updated, and answers of several versions are stored.
* Validable
* The forms can contain constraints and only valid answers are stored.
# This is a example file used as configuration of the environment vars used in
# this project. The pattern used is VAR=VALUE.
# Lines with # will be ignored
# WARNING: This is a versionable file, and is used in versioning control
# This file will be sent to git repo, any sentive information may be visible
# to undesired eyes.
# You should not modify this file. You should copy it and edit and use the copy.
PORT=3000
#!/usr/bin/env node
/*
* form-creator-api. RESTful API to manage and answer forms.
* Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
*
* This file is part of form-creator-api.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
require('ts-node/register');
require('./src/main.ts');
{
"name": "ts-api-start-up",
"version": "0.0.1",
"description": "Typescript (API) base project",
"main": "index.js",
"scripts": {
"start": "scripts/start.sh config/config.env"
, "test": "scripts/test.sh config/test.env"
, "lint": "tslint -s node_modules/tslint-stylish -t stylish src/**/*.ts test/**/*.ts"
, "show-coverage": "xdg-open coverage/lcov-report/index.html"
, "doc-code": "typedoc --mode 'file' --module 'commonjs' --target 'ES6' --ignoreCompilerErrors --exclude '**/*.spec.ts' --out 'doc/code' 'src'"
},
"repository": {
"type": "git",
"url": "git@gitlab.c3sl.ufpr.br:lfoliveira/ts-api-start-up.git"
},
"author": "",
"license": "ISC",
"dependencies": {
"@types/async": "^2.0.50",
"@types/express": "^4.16.0",
"async": "^2.6.1",
"express": "^4.16.4",
"ts-node": "^7.0.1",
"typescript": "^3.2.2"
},
"devDependencies": {
"@types/chai": "^4.1.7",
"@types/mocha": "^5.2.5",
"@types/supertest": "^2.0.7",
"chai": "^4.2.0",
"istanbul": "1.1.0-alpha.1",
"mocha": "^5.2.0",
"supertest": "^3.3.0",
"tslint": "^5.12.1",
"tslint-stylish": "^2.1.0",
"typedoc": "^0.14.1"
}
}
#! /bin/bash
# form-creator-api. RESTful API to manage and answer forms.
# Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre
# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
#
# This file is part of form-creator-api.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
confFile=${1:-config/config.env}
if [[ ! (-r $confFile) ]]; then
echo "The config $confFile does not exist."
echo "You should create it."
echo "Use the example files in the config dir as example!"
exit 1
fi
env $(cat config/config.env | grep -v '#') node index
#! /bin/bash
# form-creator-api. RESTful API to manage and answer forms.
# Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre
# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
#
# This file is part of form-creator-api.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
confFile=${1:-config/test.env}
if [[ ! (-r $confFile) ]]; then
echo "The config $confFile does not exist."
echo "You should create it."
echo "Use the example files in the config dir as example!"
exit 1
fi
env $(cat config/test.env | grep -v '#') ts-node node_modules/istanbul/lib/cli.js cover -x \"**/*.spec.ts\" -e .ts _mocha
/*
* form-creator-api. RESTful API to manage and answer forms.
* Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
*
* This file is part of form-creator-api.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import * as express from "express";
import { Collection } from "../core/collection";
/**
* Extension of Express requests that suports the addition
* of an engine and an adapter.
* This extension is required because some Middlewares
* add some objetcs, in this case metrics and dimensions,
* to the Request object. To typescript compiler do not
* return a error the extension must be made.
*/
export interface Request extends express.Request {
/** A collection object. Simulate a database. */
collection: Collection;
}
/**
* Extension Middleware function of ExpressJS Module.
* Uses the custom Request object and is used to define
* the middlewares.
*/
export type Middleware = (req: Request, res: express.Response, next: express.NextFunction) => void;
/*
* form-creator-api. RESTful API to manage and answer forms.
* Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
*
* This file is part of form-creator-api.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import * as request from "supertest";
import { expect } from "chai";
import * as server from "../../main";
describe("API data controller", () => {
it("should respond 200 when posting valid item", (done) => {
request(server)
.post("/item")
.send({qtd: 30, name: "Arrow"})
.expect(200)
.expect((res: any) => {
const message = "Item added. Id on key 'id'";
expect(res.body).to.be.an("object");
expect(res.body).to.have.property("id");
expect(res.body).to.have.property("status");
expect(res.body.id).to.be.eql(0);
expect(res.body.status).to.be.eql(message);
})
.end(done);
});
it("should respond 500 when posting invalid item", (done) => {
request(server)
.post("/item")
.send({name: "Arrow"})
.expect(500)
.expect((res: any) => {
const message = "Invalid body. Check out request body keys";
expect(res.body).to.be.an("object");
expect(res.body).to.have.property("error");
expect(res.body.error).to.be.eql(message);
})
.end(done);
});
it("should respond 200 when putting valid item", (done) => {
request(server)
.put("/item/0")
.send({qtd: 29, name: "Arrow"})
.expect(200)
.expect((res: any) => {
const message = "Item updated. Id on key 'id'";
expect(res.body).to.be.an("object");
expect(res.body).to.have.property("id");
expect(res.body).to.have.property("status");
expect(res.body.id).to.be.eql("0");
expect(res.body.status).to.be.eql(message);
})
.end(done);
});
it("should respond 500 when putting invalid item", (done) => {
request(server)
.put("/item/0")
.send({name: "Arrow"})
.expect(500)
.expect((res: any) => {
const message = "Invalid body. Check out request body keys";
expect(res.body).to.be.an("object");
expect(res.body).to.have.property("error");
expect(res.body.error).to.be.eql(message);
})
.end(done);
});
it("should respond 500 when putting inexistent item", (done) => {
request(server)
.put("/item/1")
.send({name: "Arrow", qtd: 30})
.expect(500)
.expect((res: any) => {
const message = "Item with id: '1' not found";
expect(res.body).to.be.an("object");
expect(res.body).to.have.property("error");
expect(res.body.error).to.be.eql(message);
})
.end(done);
});
it("should respond 200 when getting valid item", (done) => {
request(server)
.get("/item/0")
.expect(200)
.expect((res: any) => {
expect(res.body).to.be.an("object");
expect(res.body).to.have.property("qtd");
expect(res.body).to.have.property("name");
expect(res.body.qtd).to.be.eql(29);
expect(res.body.name).to.be.eql("Arrow");
})
.end(done);
});
it("should respond 500 when getting inexistent item", (done) => {
request(server)
.get("/item/1")
.expect(500)
.expect((res: any) => {
const message = "Item with id: '1' not found";
expect(res.body).to.be.an("object");
expect(res.body).to.have.property("error");
expect(res.body.error).to.be.eql(message);
})
.end(done);
});
it("should respond 200 when deleting valid item", (done) => {
request(server)
.delete("/item/0")
.expect(200)
.expect((res: any) => {
const message = "Item deleted. Id on key 'id'";
expect(res.body).to.be.an("object");
expect(res.body).to.have.property("id");
expect(res.body).to.have.property("status");
expect(res.body.id).to.be.eql("0");
expect(res.body.status).to.be.eql(message);
})
.end(done);
});
it("should respond 500 when deleting inexistent item", (done) => {
request(server)
.delete("/item/0")
.expect(500)
.expect((res: any) => {
const message = "Item with id: '0' not found";
expect(res.body).to.be.an("object");
expect(res.body).to.have.property("error");
expect(res.body.error).to.be.eql(message);
})
.end(done);
});
});
/*
* form-creator-api. RESTful API to manage and answer forms.
* Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
*
* This file is part of form-creator-api.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { Item } from "../../core/item";
import { Response, NextFunction } from "express";
import { Request } from "../apiTypes";
export class ItemCtrl {
public static read(req: Request, res: Response, next: NextFunction) {
const item = req.collection.getItem(req.params.id);
if (item) {
res.json(item);
return;
}
else {
res.status(500).json({
error: "Item with id: '" + req.params.id + "' not found"
});
return;
}
}
public static write(req: Request, res: Response, next: NextFunction) {
if (!Item.validator(req.body)) {
res.status(500).json({
error: "Invalid body. Check out request body keys"
});
return;
}
const item: Item = new Item(req.body.qtd, req.body.name);
const id = req.collection.addItem(item);
res.json({
id: id
, status: "Item added. Id on key 'id'"
});
}
public static update(req: Request, res: Response, next: NextFunction) {
if (!Item.validator(req.body)) {
res.status(500).json({
error: "Invalid body. Check out request body keys"
});
return;
}
const item: Item = new Item(req.body.qtd, req.body.name);
const updated = req.collection.updateItem(req.params.id, item);
if (updated) {
res.json({
id: req.params.id
, status: "Item updated. Id on key 'id'"
});
}
else {
res.status(500).json({
error: "Item with id: '" + req.params.id + "' not found"
});
}
}
public static del(req: Request, res: Response, next: NextFunction) {
const i = req.collection.deleteItem(req.params.id);
if (i) {
res.json({
id: req.params.id
, status: "Item deleted. Id on key 'id'"
});
}
else {
res.status(500).json({
error: "Item with id: '" + req.params.id + "' not found"
});
}
}
}
/*
* form-creator-api. RESTful API to manage and answer forms.
* Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
*
* This file is part of form-creator-api.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { Collection } from "../../core/collection";
import { Middleware } from "../apiTypes";
/**
* Creates a engine and middleware that
* inserts the collection into the request objects.
*/
export function CollectionMw (): Middleware {
const collection: Collection = new Collection();
return function collectionMiddleware(req, res, next) {
req.collection = collection;
next();
};
}
/*
* form-creator-api. RESTful API to manage and answer forms.
* Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
*
* This file is part of form-creator-api.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { expect } from "chai";
import { Collection } from "./collection";
import { Item } from "./item";
describe("collection class", () => {
const collection = new Collection ();
it("should add 3 items to the collection", () => {
const items: Item[] = [
new Item(1, "Bow")
, new Item(30, "Arrow")
, new Item(1, "Sword")
];
for (let i = 0; i < items.length; ++i) {
const id = collection.addItem(items[i]);
expect(id).to.be.equal(i);
}
});
it("should update the second item", () => {
const item: Item = new Item(29, "Arrow");
const updated = collection.updateItem(1, item);
expect(updated).to.be.true;
});
it("should NOT update a inexistent item", () => {
const item: Item = new Item(29, "Arrow");
const updated = collection.updateItem(3, item);
expect(updated).to.be.false;
});
it("should delete the third item", () => {
const deleted = collection.deleteItem(2);
expect(deleted).to.be.true;
});
it("should NOT delete a inexistent item", () => {
const deleted = collection.deleteItem(2);
expect(deleted).to.be.false;
});
it("should get the first item", () => {
const item = collection.getItem(0);
expect(item).to.be.an("object");
expect(item).to.have.property("qtd");
expect(item).to.have.property("name");
expect(item.qtd).to.be.equal(1);
expect(item.name).to.be.equal("Bow");
});
it("should NOT get a inexistent item", () => {
const item = collection.getItem(2);
expect(item).to.be.null;
});
});
/*
* form-creator-api. RESTful API to manage and answer forms.
* Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
*
* This file is part of form-creator-api.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*