axe-api 0.19.0 → 0.19.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/package.json +17 -17
- package/readme.md +89 -12
- package/src/handlers/store.js +6 -1
- package/src/resolvers/setExpressRoutes.js +13 -6
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "axe-api",
|
|
3
|
-
"version": "0.19.
|
|
3
|
+
"version": "0.19.1",
|
|
4
4
|
"description": "AXE API is a simple tool which has been created based on Express and Knex.js to create Rest APIs quickly.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -20,37 +20,37 @@
|
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
22
|
"change-case": "^4.1.2",
|
|
23
|
-
"dotenv": "^
|
|
24
|
-
"express": "^4.17.
|
|
25
|
-
"knex": "^0.
|
|
23
|
+
"dotenv": "^14.2.0",
|
|
24
|
+
"express": "^4.17.2",
|
|
25
|
+
"knex": "^1.0.1",
|
|
26
26
|
"knex-paginate": "^3.0.0",
|
|
27
|
-
"knex-schema-inspector": "^1.
|
|
27
|
+
"knex-schema-inspector": "^1.7.1",
|
|
28
28
|
"pluralize": "^8.0.0",
|
|
29
29
|
"validatorjs": "^3.22.1"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@babel/cli": "^7.
|
|
33
|
-
"@babel/core": "^7.
|
|
34
|
-
"@babel/node": "^7.
|
|
35
|
-
"@babel/preset-env": "^7.
|
|
36
|
-
"@babel/runtime": "^7.
|
|
37
|
-
"babel-jest": "^27.
|
|
38
|
-
"babel-loader": "^8.2.
|
|
32
|
+
"@babel/cli": "^7.16.8",
|
|
33
|
+
"@babel/core": "^7.16.10",
|
|
34
|
+
"@babel/node": "^7.16.8",
|
|
35
|
+
"@babel/preset-env": "^7.16.11",
|
|
36
|
+
"@babel/runtime": "^7.16.7",
|
|
37
|
+
"babel-jest": "^27.4.6",
|
|
38
|
+
"babel-loader": "^8.2.3",
|
|
39
39
|
"babel-plugin-module-resolver": "^4.1.0",
|
|
40
40
|
"babel-preset-minify": "^0.5.1",
|
|
41
41
|
"eslint": "^7.31.0",
|
|
42
42
|
"eslint-config-standard": "^16.0.3",
|
|
43
|
-
"eslint-plugin-import": "^2.
|
|
43
|
+
"eslint-plugin-import": "^2.25.4",
|
|
44
44
|
"eslint-plugin-node": "^11.1.0",
|
|
45
45
|
"eslint-plugin-promise": "^5.1.0",
|
|
46
46
|
"eslint-plugin-standard": "^5.0.0",
|
|
47
47
|
"eslint-plugin-unicorn": "^33.0.1",
|
|
48
48
|
"eslint-watch": "^7.0.0",
|
|
49
|
-
"jest": "^27.
|
|
49
|
+
"jest": "^27.4.7",
|
|
50
50
|
"mysql": "^2.18.1",
|
|
51
|
-
"nodemon": "^2.0.
|
|
52
|
-
"pg": "^8.
|
|
53
|
-
"set-value": ">=4.
|
|
51
|
+
"nodemon": "^2.0.15",
|
|
52
|
+
"pg": "^8.7.1",
|
|
53
|
+
"set-value": ">=4.1.0",
|
|
54
54
|
"sqlite3": "^5.0.2"
|
|
55
55
|
}
|
|
56
56
|
}
|
package/readme.md
CHANGED
|
@@ -23,21 +23,98 @@
|
|
|
23
23
|
</a>
|
|
24
24
|
</h1>
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
The fastest way to create Rest API, by defining database models and relations.
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
> Axe API has great documentation. Please [check it out in here](https://axe-api.github.io/).
|
|
29
29
|
|
|
30
|
-
##
|
|
30
|
+
## What Is Axe API?
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
-
|
|
32
|
+
**Axe API** is the _fastest_ way to create **Rest API** by defining only database models and relationships between them. It is built on [Knex.js](http://knexjs.org), and its awesome active records pattern. On the other hand, you have another familiar thing, [Express](https://expressjs.com/).
|
|
33
|
+
|
|
34
|
+
You are going to be able to develop an API **10 times faster** with **Axe API**!
|
|
35
|
+
|
|
36
|
+
## How It Works?
|
|
37
|
+
|
|
38
|
+
[Express](https://expressjs.com/) and [Knex.js](http://knexjs.org) are great tools to create [Node.js](https://nodejs.org) based applications. But usually, we code too much the same things to design an API. We aim to reduce code duplication and give you speed by using Axe API.
|
|
39
|
+
|
|
40
|
+
Axe API provides you the ability to separate your common tasks to build an API from your business logic. **Axe API** expects model definitions to analyze your routing structure. After you created your models and their relations between them, Axe API can handle all _well-known_ API requests. Creating an API with 5 tables takes almost 15 minutes.
|
|
41
|
+
|
|
42
|
+
Shortly, **Axe API** performs three basic functions;
|
|
43
|
+
|
|
44
|
+
- **Analyzes** your models and their relationships to create routes.
|
|
45
|
+
- **Handles** all HTTP requests.
|
|
46
|
+
- **Separate** your business logic from API best practices.
|
|
47
|
+
|
|
48
|
+
Let's assume that you have a model like this;
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
import { Model } from "axe-api";
|
|
52
|
+
|
|
53
|
+
class User extends Model {}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
With this model, you will have all of the basic API routes for **User** resources. **Axe API** will create **CRUD** routes for you in the _booting_ process and these routes would be completely ready to be handled and processed by Axe API. The following routes will be handled automatically;
|
|
57
|
+
|
|
58
|
+
- `POST api/users`
|
|
59
|
+
- `GET api/users`
|
|
60
|
+
- `GET api/users/:id`
|
|
61
|
+
- `PUT api/users/:id`
|
|
62
|
+
- `DELETE api/users/:id`
|
|
63
|
+
|
|
64
|
+
This is the magic of **Axe API**!
|
|
65
|
+
|
|
66
|
+
## Installation
|
|
67
|
+
|
|
68
|
+
Using **Axe API** in an application is very easy. We've created a CLI tool for you; [axe-magic](https://github.com/axe-api/axe-magic).
|
|
69
|
+
|
|
70
|
+
You can create a new Axe API project by using [axe-magic](https://github.com/axe-api/axe-magic). But first, you can install it in your development environment. When you installed it, you can be able to access **axe-magic** command via CLI. You can use the following command to install **axe-magic** to your machine;
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
$ npm i -g axe-magic
|
|
74
|
+
$ axe-magic --version
|
|
75
|
+
1.0.0
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
After that, creating a new project is very easy. Just you can execute the following command;
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
$ axe-magic new my-api
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
This command will pull [axe-api-template](https://github.com/axe-api/axe-api-template) project to your current directory with a new name, **my-api**.
|
|
85
|
+
|
|
86
|
+
To install your project's depencies, you can execute the following commands in the root directory;
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
$ cd my-api
|
|
90
|
+
$ npm install
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
To serve this application, you can execute the following command;
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
$ npm run start:dev
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
> `start:dev` command use [nodemon](https://www.npmjs.com/package/nodemon). If you haven't installed it yet, we suggest you install it first.
|
|
100
|
+
|
|
101
|
+
After that, your first **Axe API** application will be running in `localhost:3000`.
|
|
102
|
+
|
|
103
|
+
You will see the following API response if you visit [localhost:3000](http://localhost:3000).
|
|
104
|
+
|
|
105
|
+
```json
|
|
106
|
+
{
|
|
107
|
+
"name": "AXE API",
|
|
108
|
+
"description": "The best API creation tool in the world.",
|
|
109
|
+
"aim": "To kill them all!"
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
If you can see that response, it means that your project is running properly.
|
|
114
|
+
|
|
115
|
+
## Documentation
|
|
116
|
+
|
|
117
|
+
Axe API has great documentation. Please [check it out in here](https://axe-api.github.io/).
|
|
41
118
|
|
|
42
119
|
## How To Run Integration Tests
|
|
43
120
|
|
package/src/handlers/store.js
CHANGED
|
@@ -43,10 +43,15 @@ export default async (context) => {
|
|
|
43
43
|
formData,
|
|
44
44
|
});
|
|
45
45
|
|
|
46
|
-
|
|
46
|
+
const [returningResult] = await trx(model.instance.table)
|
|
47
47
|
.insert(formData)
|
|
48
48
|
.returning(model.instance.primaryKey);
|
|
49
49
|
|
|
50
|
+
let insertedPrimaryKeyValue =
|
|
51
|
+
typeof returningResult === "number"
|
|
52
|
+
? returningResult
|
|
53
|
+
: returningResult[model.instance.primaryKey];
|
|
54
|
+
|
|
50
55
|
// If the user use a special primary key value, we should use that value
|
|
51
56
|
if (insertedPrimaryKeyValue === 0) {
|
|
52
57
|
insertedPrimaryKeyValue = formData[model.instance.primaryKey];
|
|
@@ -164,12 +164,19 @@ const getModelMiddlewares = (model, handler) => {
|
|
|
164
164
|
return middlewares;
|
|
165
165
|
};
|
|
166
166
|
|
|
167
|
-
const getRootPrefix = () => {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
167
|
+
export const getRootPrefix = async () => {
|
|
168
|
+
const config = await IoC.use("Config");
|
|
169
|
+
let prefix = config?.Application?.prefix || "api";
|
|
170
|
+
|
|
171
|
+
if (prefix.substr(0, 1) === "/") {
|
|
172
|
+
prefix = prefix.substr(1);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (prefix.substr(prefix.length - 1) === "/") {
|
|
176
|
+
prefix = prefix.substr(0, prefix.length - 1);
|
|
171
177
|
}
|
|
172
|
-
|
|
178
|
+
|
|
179
|
+
return prefix;
|
|
173
180
|
};
|
|
174
181
|
|
|
175
182
|
const createRouteByModel = async (
|
|
@@ -209,7 +216,7 @@ const createRouteByModel = async (
|
|
|
209
216
|
|
|
210
217
|
const routeTemplate = API_ROUTE_TEMPLATES[handler];
|
|
211
218
|
const url = routeTemplate.url(
|
|
212
|
-
getRootPrefix(),
|
|
219
|
+
await getRootPrefix(),
|
|
213
220
|
urlPrefix,
|
|
214
221
|
resource,
|
|
215
222
|
model.instance.primaryKey
|