KoAJAX

KoAJAX

HTTP Client based on Koa-like middlewares

NPM Dependency CI & CD

NPM

Automatic Serialized types:

  1. Pure text: string
  2. Form encoding: URLSearchParams, FormData
  3. DOM object: Node
  4. JSON object: Object
  5. Binary data: Blob, ArrayBuffer, TypedArray, DataView
  6. Stream object: ReadableStream

Automatic Parsed type:

  1. HTML/XML: Document
  2. JSON: Object
  3. Binary data: ArrayBuffer
npm install koajax
<head>
<script src="https://polyfill.web-cell.dev/feature/Regenerator.js"></script>
<script src="https://polyfill.web-cell.dev/feature/ECMAScript.js"></script>
<script src="https://polyfill.web-cell.dev/feature/TextEncoder.js"></script>
<script src="https://polyfill.web-cell.dev/feature/AbortController.js"></script>
<script src="https://polyfill.web-cell.dev/feature/Stream.js"></script>
</head>
npm install koajax jsdom
import { HTTPClient } from 'koajax';
import { polyfill } from 'koajax/source/polyfill'

const origin = 'https://your-target-origin.com';

polyfill(origin).then(() => {
const client = new HTTPClient({
baseURI: `${origin}/api`,
responseType: 'json'
});
const { body } = await client.get('test/interface');

console.log(body);
});
npx tsx index.ts
  1. https://github.com/idea2app/KoAJAX-Taro-adapter
import { HTTPClient } from 'koajax';

var token = '';

export const client = new HTTPClient().use(
async ({ request: { method, path, headers }, response }, next) => {
if (token) headers['Authorization'] = 'token ' + token;

await next();

if (method === 'POST' && path.startsWith('/session'))
token = response.headers.Token;
}
);

client.get('/path/to/your/API').then(console.log);

(based on Async Generator)

import { request } from 'koajax';

document.querySelector('input[type="file"]').onchange = async ({
target: { files }
}) => {
for (const file of files) {
const { upload, download, response } = request({
method: 'POST',
path: '/files',
body: file,
responseType: 'json'
});

for await (const { loaded } of upload)
console.log(`Upload ${file.name} : ${(loaded / file.size) * 100}%`);

const { body } = await response;

console.log(`Upload ${file.name} : ${body.url}`);
}
};
npm i native-file-system-adapter  # Web standard API polyfill
import { showSaveFilePicker } from 'native-file-system-adapter';
import { HTTPClient } from 'koajax';

const bufferClient = new HTTPClient({ responseType: 'arraybuffer' });

document.querySelector('#download').onclick = async () => {
const fileURL = 'https://your.server/with/Range/header/supported/file.zip';
const suggestedName = new URL(fileURL).pathname.split('/').pop();

const fileHandle = await showSaveFilePicker({ suggestedName });
const writer = await fileHandle.createWritable(),
stream = bufferClient.download(fileURL);

try {
for await (const { total, loaded, percent, buffer } of stream) {
await writer.write(buffer);

console.table({ total, loaded, percent });
}
window.alert(`File ${fileHandle.name} downloaded successfully!`);
} finally {
await writer.close();
}
};
npm install browser-unhandled-rejection  # Web standard API polyfill
import { auto } from 'browser-unhandled-rejection';
import { HTTPError } from 'koajax';

auto();

window.addEventListener('unhandledrejection', ({ reason }) => {
if (!(reason instanceof HTTPError)) return;

const { message } = reason.response.body;

if (message) window.alert(message);
});

(based on Async Generator)

import { readAs } from 'koajax';

document.querySelector('input[type="file"]').onchange = async ({
target: { files }
}) => {
for (const file of files) {
const { progress, result } = readAs(file, 'dataURL');

for await (const { loaded } of progress)
console.log(
`Loading ${file.name} : ${(loaded / file.size) * 100}%`
);

const URI = await result;

console.log(`Loaded ${file.name} : ${URI}`);
}
};
MMNEPVFCICPMFPCPTTAAATR