mapycz-api
Version:
mapy.cz fastrpc and REST interfaces
34 lines • 1.11 kB
JavaScript
import { Cache, FileCache } from './cache.js';
import { parse, serializeCall } from './fastrpc.js';
const OPTIONS = {
timeout: 0,
hints: {},
url: '/RPC2',
hashId: '',
headers: [],
type: 'frpc',
withCredentials: true
};
export async function xfrpc(method, args = [], options = {}, cacher = new FileCache()) {
const _options = { ...OPTIONS, ...options };
const headers = {
Accept: 'application/x-frpc',
'Content-Type': 'application/x-frpc'
};
for (let index = 0; index < _options.headers.length; index++) {
const { name, value } = _options.headers[index];
headers[name] = value;
}
const key = cacher.key({ ..._options, ...{ method, args } });
if (cacher.is(key))
return cacher.get(key);
const response = await fetch(_options.url, {
body: new Uint8Array(serializeCall(method, args, _options.hints)),
method: 'POST',
headers
});
const result = parse(new Uint8Array(await response.arrayBuffer()));
cacher.set(key, result);
return result;
}
//# sourceMappingURL=request.js.map