@tak-ps/node-tak
Version:
Lightweight JavaScript library for communicating with TAK Server
26 lines • 935 B
JavaScript
import Err from '@openaddresses/batch-error';
import { TypeCompiler } from "@sinclair/typebox/compiler";
import { fetch, Response } from 'undici';
export class TypedResponse extends Response {
constructor(response) {
super(response.body, {
status: response.status,
statusText: response.statusText,
headers: response.headers,
});
}
async typed(type) {
const body = await this.json();
const typeChecker = TypeCompiler.Compile(type);
const result = typeChecker.Check(body);
if (result)
return body;
const errors = typeChecker.Errors(body);
const firstError = errors.First();
throw new Err(500, null, `Internal Validation Error: ${JSON.stringify(firstError)}`);
}
}
export default async function (input, init) {
return new TypedResponse(await fetch(input, init));
}
//# sourceMappingURL=fetch.js.map