UNPKG

@tragedy-labs/sprite

Version:

A TypeScript driver for ArcadeDB

65 lines 3.14 kB
import { HttpClient } from './HttpClient.js'; import { JsonResponse } from './JsonResponse.js'; import { SpriteHeaders } from './SpriteHeaders.js'; import { SpriteBody } from './SpriteBody.js'; var Method; (function (Method) { Method["POST"] = "POST"; Method["GET"] = "GET"; })(Method || (Method = {})); /** * Static methods for making RESTful API calls. */ class Rest { /** * Make a {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET `GET`} request. * @param route The route (key) to target in the request. * @param session The `Session` to target in the request. * @returns A promise that resolves to a {@link https://developer.mozilla.org/en-US/docs/Web/API/Response `Response`} object. */ static async get(route, session) { return HttpClient.request(session.endpoints[route], { method: Method.GET, headers: SpriteHeaders.compose(session), keepalive: true }); } /** * Make a {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET `GET`} request in which a JSON response is expected. * @param route The route (key) to target in the request. * @param session The `Session` to target in the request. * @returns The json response from the ArcadeDB Server. */ static async getJson(route, session) { return JsonResponse.parse(await this.get(route, session)); } /** * Make a {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST `POST`} request. * @param session The `Session` to target in the request. * @param endpoint The relative endpoint of request. * @param body The {@link https://developer.mozilla.org/en-US/docs/Web/API/Request/body body} to include in the request. * @param headers The {@link https://developer.mozilla.org/en-US/docs/Web/API/Request/headers headers} to include in the request. * @returns A promise that resolves to a {@link https://developer.mozilla.org/en-US/docs/Web/API/Response `Response`} object. */ static async post(route, body, session, transaction) { return HttpClient.request(session.endpoints[route], { method: Method.POST, headers: SpriteHeaders.compose(session, transaction), body: body ? SpriteBody.compose(body) : null, keepalive: true }); } /** * Make a {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST `POST`} request in which a JSON response is expected. * @param route The route (key) to target in the request. * @param body The {@link https://developer.mozilla.org/en-US/docs/Web/API/Request/body body} to include in the request. * @param session The `DatabaseSession` to target in the request. * @param transaction The `SpriteTransaction` to target in the request. * @returns The json response from the ArcadeDB Server. */ static async postJson(route, body, session, transaction) { return JsonResponse.parse(await this.post(route, body, session, transaction)); } } export { Rest }; //# sourceMappingURL=Rest.js.map