Skip to content

Commit 1586efd

Browse files
committed
8.2.0 commit
1 parent 6437770 commit 1586efd

34 files changed

+2092
-127
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,5 @@ testem.log
4444
# System Files
4545
.DS_Store
4646
Thumbs.db
47+
48+
package-lock.json

LICENSE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright (c) 2019
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 317 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,330 @@
1-
# Ng2SimpleApiLib
1+
# simple-api-express
22

3-
This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 8.2.2.
3+
[simple-api-express](https://github.com/J-Siu/simple-api-express) is an ExpressJS api handler (NOT middleware) that work with
4+
[simple-api-client-ng2](https://github.com/J-Siu/ng2-simple-api-lib), an Angular api service.
45

5-
## Development server
6+
> To enable faster update, simple-api-client-ng2 switched to Angular CLI starting 8.2.0 and use new repository https://github.com/J-Siu/ng2-simple-api-lib/
7+
>
8+
> This new repository contains both library and server example.
9+
>
10+
> All version < 8.2.0 are in old repository https://github.com/J-Siu/simple-api-client-ng2/
611
7-
Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files.
12+
## Index
813

9-
## Code scaffolding
14+
- [Install](#install)
15+
- [Usage Flow](#usage-flow)
16+
- [API](#api)
17+
- [constructor](#constructor)
18+
- [debug](#debug)
19+
- [list](#list)
20+
- [register](#register)
21+
- [registerObject](#registerobject)
22+
- [response](#response)
23+
- [handler](#handler)
24+
- [Error Handling](#error-handling)
25+
- [404 Not Found](#404-not-found)
26+
- [Callback throw](#callback-throw)
27+
- [Example](#example)
28+
- [Contributors](#contributors)
29+
- [Changelog](#changelog)
30+
- [License](#license)
1031

11-
Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module`.
32+
## Install
1233

13-
## Build
34+
```sh
35+
npm install simple-api-express
36+
```
1437

15-
Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `--prod` flag for a production build.
38+
## Usage Flow
1639

17-
## Running unit tests
40+
`simple-api-express` depends on ExpressJS middleware bodyParser for json body decode.
1841

19-
Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io).
42+
```javascript
43+
const express = require('express');
44+
var app = express();
45+
var bodyParser = require('body-parser');
46+
app.use(bodyParser.json());
47+
app.listen(8080);
48+
```
2049

21-
## Running end-to-end tests
50+
Import `simple-api-express`:
2251

23-
Run `ng e2e` to execute the end-to-end tests via [Protractor](http://www.protractortest.org/).
52+
```javascript
53+
const SimpleApi = require('simple-api-express').SimpleApi;
54+
```
2455

25-
## Further help
56+
Create api object with base url:
2657

27-
To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md).
58+
```javascript
59+
const apiDemoUrl = '/demo';
60+
var apiDemo = new SimpleApi(apiDemoUrl, true); // enable debug
61+
```
62+
63+
Register a function as api callback:
64+
65+
```javascript
66+
apiDemo.register('echo2',param => 'echo2:' + param);
67+
```
68+
69+
You can also register all functions of an object as api callbacks:
70+
71+
```javascript
72+
apiDemo.registerObject(require('./api-object').DemoObj);
73+
```
74+
75+
Use express post and `SimpleApi.response()` to handle incoming api request:
76+
77+
```javascript
78+
// Post request + API response
79+
app.post(path.join(apiDemoUrl, '*'), (req, res) => apiDemo.response(req, res))
80+
```
81+
82+
`SimpleApi.handler()` can be use if additional action(eg: customizing response header or error page)
83+
is required before reply:
84+
85+
```javascript
86+
// Post request + API handler
87+
app.post(path.join(apiDemoUrl, '*'), (req, res) => {
88+
// Log request body before process
89+
console.log(req.body);
90+
try {
91+
// Manual handler used, server code responsible to send result and handle error
92+
// Use manual handler if custom header or custom 404 error are needed
93+
let result = apiDemo.handler(req);
94+
95+
// Result must be return in json format
96+
res.json(result);
97+
}
98+
catch (e) {
99+
// Catch api not found error
100+
res.status(e.status).end(e.error);
101+
}
102+
})
103+
```
104+
105+
### API
106+
107+
#### constructor
108+
109+
`SimpleApi(baseUrl:string, debug:boolean)`
110+
111+
- `baseUrl` will prefix all api url registered to this SimpleApi instance.
112+
- `debug` will enable/disable debug log. Default to false.
113+
114+
```javascript
115+
const SimpleApi = require('simple-api-express').SimpleApi;
116+
const apiDemoUrl = '/demo';
117+
var apiDemo = new SimpleApi(apiDemoUrl, true); // enable debug
118+
```
119+
120+
#### debug
121+
122+
`debug(enable: boolean)` can enable/disable debug log.
123+
124+
```javascript
125+
apiDemo.debug(false);
126+
```
127+
128+
#### list
129+
130+
`list()` return a `string[]` containing all registered api url.
131+
132+
```javascript
133+
console.log(apiDemo.list());
134+
```
135+
136+
Output:
137+
138+
```js
139+
[ '/demo/echo', '/demo/echo2' ]
140+
```
141+
142+
#### register
143+
144+
`register(url:string,callback)` register a callback function to `url`
145+
146+
- `url` : Api url path after baseUrl. The resulting url for the api is baseUrl/url.
147+
- `callback` : a function that take a single argument as api parameter, and return a result.
148+
149+
```javascript
150+
apiDemo.register('echo2',param => 'echo2:' + param);
151+
```
152+
153+
#### registerObject
154+
155+
`registerObject(object)` register all functions of an object as api callbacks.
156+
157+
All functions of the object should take a single argument as api parameter, and return a result.
158+
159+
The function name will be used api url.
160+
161+
```javascript
162+
var DemoObj = {
163+
echo(r) {
164+
return r;
165+
}
166+
}
167+
168+
apiDemo.registerObject(DemoObj);
169+
```
170+
171+
#### response
172+
173+
`SimpleApi.response(req, res)` is a handle function for incoming api post request.
174+
Api parameter will be passed to corresponding callback.
175+
Callback result will be passed back to api client.
176+
177+
`req, res` are request and response object pass in from ExpressJS post.
178+
179+
```javascript
180+
// Post request + API response
181+
app.post(path.join(apiDemoUrl, '*'), (req, res) => apiDemo.response(req, res))
182+
```
183+
184+
#### handler
185+
186+
`SimpleApi.handler(req)` is an api handler function.
187+
It will invoke the corresponding callback base on the request url, and return the result.
188+
189+
IT WILL NOT send out the result.
190+
191+
IT IS NOT a ExpressJS post handler function. It needed to be called INSIDE the post handler function.
192+
193+
Api handler can be use if additional action is required (eg: customizing response header or error page):
194+
195+
```javascript
196+
// Post request + API handler
197+
app.post(path.join(apiDemoUrl, '*'), (req, res) => {
198+
// Log request body before process
199+
console.log(req.body);
200+
try {
201+
// Manual handler used, server code responsible to send result and handle error
202+
// Use manual handler if custom header or custom 404 error are needed
203+
let result = apiDemo.handler(req);
204+
205+
// Result must be return in json format
206+
res.json(result);
207+
}
208+
catch (e) {
209+
// Catch api not found error
210+
res.status(e.status).end(e.error);
211+
}
212+
})
213+
```
214+
215+
### Error Handling
216+
217+
There are two types of error.
218+
219+
#### 404 Not Found
220+
221+
When `response()` is called with an non-exist api url,
222+
it will response with a HTTP 404 Not Found.
223+
224+
When `handle()` is called with an non-exist api url,
225+
it will throw an error,
226+
which can be caught in the post handle function.
227+
228+
#### Callback throw
229+
230+
When `response()` is called,
231+
and the invoked api callback throw an error,
232+
which will be passed to remote client.
233+
The remote client, using [simple-api-client-ng2](https://github.com/J-Siu/simple-api-client-ng2),
234+
will throw an exception with the error.
235+
236+
When `handle()` is called,
237+
and the invoked api callback throw an error,
238+
the error can be inspected from the result object.
239+
240+
```javascript
241+
let result = apiDemo.handler(req);
242+
243+
// If api callback return
244+
if(result.error) {
245+
console.log(result.error);
246+
}
247+
248+
// Result must be return in json format
249+
res.json(result);
250+
```
251+
252+
The remote client, using [simple-api-client-ng2](https://github.com/J-Siu/simple-api-client-ng2),
253+
will throw an exception with the error.
254+
255+
## Example
256+
257+
```sh
258+
├── dist
259+
│ ├── ng2-simple-api-lib // Compiled angular application
260+
│ └── simple-api-client-ng2 // Compiled library
261+
├── projects
262+
│ └── simple-api-client-ng2 // Library source
263+
├── server // Example server.
264+
└── src
265+
└── app // Example app source
266+
```
267+
268+
> The example server is not an angular application. It is a nodejs application.
269+
270+
You will need Angular CLI to build the library and run the example.
271+
272+
- Angular CLI: 8.2.2
273+
- Node : v10.10.0
274+
275+
```sh
276+
git clone https://github.com/J-Siu/ng2-simple-api-lib.git
277+
cd ng2-simple-api-lib
278+
npm i
279+
```
280+
281+
Build the library:
282+
283+
```sh
284+
ng build simple-api-client-ng2
285+
```
286+
287+
Build the Angular app:
288+
289+
```sh
290+
ng build
291+
```
292+
293+
Run the server:
294+
295+
```sh
296+
cd server
297+
node server.js
298+
```
299+
300+
Connect your browser to http://localhost:4000 .
301+
302+
## Contributors
303+
304+
- [John Sing Dao Siu](https://github.com/J-Siu)
305+
306+
## Changelog
307+
308+
- 1.2.0
309+
- Publish to NPM.
310+
- 1.2.1
311+
- Fix Readme.md typo
312+
- 1.2.2
313+
- Update package.json
314+
- Update Readme.md
315+
- 8.2.0
316+
- Support Angular 8.2.0
317+
- Switch to Angular Cli for faster future update.
318+
- Include example in project
319+
320+
## License
321+
322+
The MIT License
323+
324+
Copyright (c) 2019
325+
326+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
327+
328+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
329+
330+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0 commit comments

Comments
 (0)