Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wolfchamane/amjs-ajax-adapter
Set up of AJAX request adapters
https://github.com/wolfchamane/amjs-ajax-adapter
adapter ajax api commonjs nodejs
Last synced: about 1 month ago
JSON representation
Set up of AJAX request adapters
- Host: GitHub
- URL: https://github.com/wolfchamane/amjs-ajax-adapter
- Owner: Wolfchamane
- License: mit
- Created: 2019-07-08T09:29:32.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T02:01:10.000Z (almost 2 years ago)
- Last Synced: 2024-02-29T21:06:52.958Z (10 months ago)
- Topics: adapter, ajax, api, commonjs, nodejs
- Language: JavaScript
- Size: 616 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @amjs/ajax-adapter 0.1.3
![Statements](https://img.shields.io/badge/Statements-100%25-brightgreen.svg) ![Branches](https://img.shields.io/badge/Branches-100%25-brightgreen.svg) ![Functions](https://img.shields.io/badge/Functions-100%25-brightgreen.svg) ![Lines](https://img.shields.io/badge/Lines-100%25-brightgreen.svg)
> Set up of AJAX request adapters
## Installation
```bash
$ npm i @amjs/ajax-adapter
```
## Usage- Register your URL adapter:
```javascript
const { AmjsAjaxAdpaterJSON } = require('@amjs/ajax-adapter');class MyURLAdpater extends AmjsAjaxAdpaterJSON
{
/**
* @override
*/
async serialize(config = {}, service = {})
{
// apply into 'config' any API required changeawait super.serialize(config, service);
}/**
* @override
*/
async unserialize(response, service)
{
// do whatever API requires with response objectreturn await super.unserialize(response, service);
}
}AmjsAjaxAdpaterJSON.register('Adapter::http://my-url', MyURLAdpater);
module.exports = MyURLAdpater;
```- Use your URL adapter before/after fetching request:
```javascript
async function doFetch()
{
const AmjsFactory = require('@amjs/factory');
const URL = 'https://my-url';
const adapter = AmjsFactory.create(`Adapter::${URL}`);
const request = {};
const service = {};
await adpater.serialize(request, service);
let response = await fetch(URL, request);
response = await adapter.unserialize(response.clone(), service);
console.log(response); // { data : { ... }} --> OK: 200 - 399
// { errors: [ ... ]} --> ERROR: 400 - 599
}
```I suggest to use `AmjsAjaxService` as long as previous operations are performed internally and has being tested out.