ltijs
Version:
Easily turn your web application into a LTI 1.3 Learning Tool.
87 lines • 3.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FetchRequestHandler = void 0;
const errors_1 = require("#services/request-handler/errors");
const http_handler_types_1 = require("#services/http-handler/http-handler.types");
class FetchRequestHandler {
AUTHORIZATION_HEADER = 'Authorization';
ACCEPT_HEADER = 'Accept';
CONTENT_TYPE_HEADER = 'Content-Type';
JSON_CONTENT_TYPE = 'application/json';
permanentHeaders = {};
async get(url, options) {
return await this.request(http_handler_types_1.HttpMethod.Get, url, undefined, options);
}
async post(url, body, options) {
return await this.request(http_handler_types_1.HttpMethod.Post, url, body, options);
}
async put(url, body, options) {
return await this.request(http_handler_types_1.HttpMethod.Put, url, body, options);
}
async delete(url, options) {
return await this.request(http_handler_types_1.HttpMethod.Delete, url, undefined, options);
}
setPermanentHeader(name, value) {
this.permanentHeaders[name] = value;
}
async request(method, url, body, options) {
const response = await fetch(this.buildUrl(url, options?.query), {
method,
headers: this.buildHeaders(body, options?.headers),
body: this.buildBody(body),
});
const data = await this.parseBody(response);
if (!response.ok) {
throw new errors_1.HttpError({
message: `HTTP request failed with status ${response.status}: ${response.statusText}`,
status: response.status,
statusText: response.statusText,
url: response.url,
response: data,
});
}
return { data, headers: Object.fromEntries(response.headers.entries()) };
}
buildUrl(url, query) {
if (query === undefined || query.toString() === '')
return url;
const separator = url.includes('?') ? '&' : '?';
return `${url}${separator}${query.toString()}`;
}
buildHeaders(body, headers) {
const mapped = { ...this.permanentHeaders };
if (headers?.authorization !== undefined)
mapped[this.AUTHORIZATION_HEADER] = headers.authorization;
if (headers?.accept !== undefined)
mapped[this.ACCEPT_HEADER] = headers.accept;
if (headers?.contentType !== undefined)
mapped[this.CONTENT_TYPE_HEADER] = headers.contentType;
if (body !== undefined && !(body instanceof URLSearchParams) && !(this.CONTENT_TYPE_HEADER in mapped)) {
mapped[this.CONTENT_TYPE_HEADER] = this.JSON_CONTENT_TYPE;
}
return mapped;
}
// URLSearchParams bodies (e.g. the OAuth2 client-credentials grant) must be form-encoded, not JSON --
// passed straight through, fetch serializes it and sets the correct content-type itself.
buildBody(body) {
if (body === undefined)
return undefined;
if (body instanceof URLSearchParams)
return body;
return JSON.stringify(body);
}
async parseBody(response) {
const text = await response.text();
if (text === '')
return undefined;
try {
return JSON.parse(text);
}
catch {
return text;
}
}
}
exports.FetchRequestHandler = FetchRequestHandler;
exports.default = FetchRequestHandler;
//# sourceMappingURL=fetch-request-handler.service.js.map