https://github.com/wolfchamane/amjs-ajax-service
Handles AJAX requests
https://github.com/wolfchamane/amjs-ajax-service
Last synced: about 1 year ago
JSON representation
Handles AJAX requests
- Host: GitHub
- URL: https://github.com/wolfchamane/amjs-ajax-service
- Owner: Wolfchamane
- License: mit
- Created: 2019-07-09T10:29:41.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T02:01:09.000Z (over 3 years ago)
- Last Synced: 2025-01-22T17:14:45.839Z (over 1 year ago)
- Language: JavaScript
- Size: 854 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# @amjs/ajax-service 0.1.7
   
> Handles AJAX requests
## Installation
```bash
$ npm i @amjs/ajax-service
```
## Usage
Install following dependencies:
```bash
$ npm i --save @amjs/data-types @amjs/ajax-adapter
```
- Create your API Adapter:
```javascript
const { AmjsAjaxAdapterJSON } = require('@amjs/ajax-adapter');
class SWApiAdapter extends AmjsAjaxAdapterJSON
{
serialize(request, service)
{
// ie.: add authorization header to request configuration headers (if your API requires auth. token)
// request.headers = request.headers || {};
// request.headers['Authorization'] = `Bearer ${this.constructor.token}`;
// ...
super.serialize(request, service);
}
async unserialize(response, service)
{
// ie.: recover authorization response header value from response object
// this.constructor.token = response.headers.get('Authorization');
// ...
return await super.unserialize(response, service);
}
}
// Register your API adapter for an specific domain
AmjsAjaxAdapterJSON.register('Adapter::https://swapi.dev', SWApiAdapter);
```
- Create your model:
```javascript
//Pre-register required data types:
require('@amjs/data-types/src/String');
const { AmjsDataTypesObject } = require('@amjs/data-types');
class SWApiPeople extends AmjsDataTypesObject
{
constructor(values)
{
super();
// Declare this model properties
this.name = null;
this.height = null;
this.mass = null;
this['hair_color'] = null;
this['skin_color'] = null;
this['eye_color'] = null;
this['birth_year'] = null;
this.gender = null;
this.homeworld = null;
this.films = null;
this.species = null;
this.vehicles = null;
this.starships = null;
this.created = null;
this.edited = null;
this.url = null;
/**
* @override
*/
this.$propertyTypes = {
name: 'String',
height: 'String',
mass: 'String',
'hair_color': 'String,
'skin_color': 'String,
'eye_color': 'String,
'birth_year': 'String,
gender: 'String',
homeworld: 'String',
films: '*',
species: '*',
vehicles: '*',
starships: '*',
created: 'String',
edited: 'String',
url: 'String'
}
this._setProperties(values);
}
}
// Register the class name, would be use later within your service
AmjsDataTypesObject.register('SWApiPeople', SWApiPeople);
```
- Create your service
```javascript
require('./sw-api-people'); // Pre-register model
const AmjsAjaxService = require('@amjs/ajax-service');
class SWApiPeopleService extends AmjsAjaxService
{
/**
* @inheritDoc
*/
constructor()
{
super({
host : 'https://swapi.dev', // must equals adapter registration
path : '/api/people/{id}'
});
this.$allowedMethods = ['GET'];
}
/**
* @override
*/
_getModel(method = 'GET')
{
let model;
switch (method)
{
case 'GET':
default:
model = 'SWApiPeople'; // this is the name you pre-register your model
break;
}
return model;
}
}
const srv = new SWApiPeopleService();
srv.params = {
id : 1
};
const response = await srv.doRequest();
console.log(response.toJSON());
/**
name : 'Luke Skywalker',
height : '172',
mass : '77',
'hair_color' : 'blond',
'skin_color' : 'fair',
'eye_color' : 'blue',
'birth_year' : '19BBY',
gender : 'male',
homeworld : 'https://swapi.dev/api/planets/1/',
films : [
'https://swapi.dev/api/films/2/',
'https://swapi.dev/api/films/6/',
'https://swapi.dev/api/films/3/',
'https://swapi.dev/api/films/1/',
'https://swapi.dev/api/films/7/'
],
species : [
'https://swapi.dev/api/species/1/'
],
vehicles : [
'https://swapi.dev/api/vehicles/14/',
'https://swapi.dev/api/vehicles/30/'
],
starships : [
'https://swapi.dev/api/starships/12/',
'https://swapi.dev/api/starships/22/'
],
created : '2014-12-09T13:50:51.644000Z',
edited : '2014-12-20T21:17:56.891000Z',
url : 'https://swapi.dev/api/people/1/'
*/
```