simple-api-client-ng2
v11.2.9
Published
Angular simple api client service that work with simple-api-express
Downloads
5
Maintainers
Readme
simple-api-client-ng2 is an Angular api service, which work with simple-api-express, an ExpressJS api handler.
simple-api-client-ng2 uses Angular CLI starting 8.2.0. New repository github.com/J-Siu/ng2-simple-api-lib contains both library and server example.
Version < 8.2.0 are in old repository github.com/J-Siu/simple-api-client-ng2.
Install
npm install simple-api-client-ng2
Usage
simple-api-client-ng2
is implemented as Angular 2 injectable service name SimpleApiClient.
Module
Add SimpleApiClient
into module providers:
import { SimpleApiClient } from 'simple-api-client-ng2';
@NgModule({
providers: [SimpleApiClient]
})
Component
import {SimpleApiClient, SimpleApiObj} from 'simple-api-client-ng2';
export class ChildComponent implement OnInit {
apiObject: SimpleApiObj;
constructor(private api: SimpleApiClient) { }
ngOnInit() {
this.apiObject = this.api.get('/demo');
let reply = '';
this.apiObject.call(
'echo',
'This is a test',
r => this.reply = r);
console.log(this.reply);
}
}
API
SimpleApiClient.get
SimpleApiClient.get(baseUrl: string = '/'): SimpleApiObj
will return a SimpleApiObj configure with baseUrl
.
Previous created SimpleApiObj will be returned if the same baseUrl is used.
this.apiObject = this.api.get('/demo');
SimpleApiClient.list
SimpleApiClient.list(): string[]
will return a string array containing the baseUrl of all SimpleApiObj created.
SimpleApiObj.call
SimpleApiObj.call(method, params, callback, errorHandler)
- method: string Name of api
- params: any Argument of api, can be basic type like string, number, or object
- callback: (result: any) => void Callback function for handling api result
- errorHandler: (error: any) => void = this.errorHandler Optional error handler to handle api call error
SimpleApiObj.setErrorHandler
SimpleApiObj.setErrorHandler(handler: (any) => void)
replace SimpleApiObj default error handler with the specified one.
Error Handling
For detail example on error handling, please refer to error.component.ts contain in full example below.
Example
├── dist/
│ ├── ng2-simple-api-lib/ // Compiled angular application
│ └── simple-api-client-ng2/ // Compiled library
├── projects/
│ └── simple-api-client-ng2/ // Library source
├── server/ // Example server.
└── src/
└── app/ // Example app source
The example server is not an angular application. It is a nodejs application.
You will need Angular CLI to build the library and run the example.
- Angular CLI: 8.2.2
- Node : v10.10.0
git clone https://github.com/J-Siu/ng2-simple-api-lib.git
cd ng2-simple-api-lib
npm i
Build the library:
ng build simple-api-client-ng2
Build the Angular app:
ng build
Run the server:
cd server
node server.js
Connect your browser to http://localhost:4000 .
Repository
Contributors
Changelog
- 1.2.0
- Publish to NPM.
- 1.2.1
- Fix Readme.md typo
- 1.2.2
- Update package.json
- Update Readme.md
- 8.2.0
- Support Angular 8.2.0
- Switch to Angular Cli for faster update.
- Include example
- 8.2.1
- README.md clean up
- 9.1.0
- Support Angular 9.1.0
- 11.2.9
- Support Angular 11.2.9
License
The MIT License
Copyright (c) 2021
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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.