172 lines
7.4 KiB
JavaScript
172 lines
7.4 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.DevApiHttpClient = exports.AadApiHttpClient = exports.ApiClientError = void 0;
|
|
var tslib_1 = require("tslib");
|
|
var sp_http_1 = require("@microsoft/sp-http");
|
|
var ApiClientError = /** @class */ (function (_super) {
|
|
tslib_1.__extends(ApiClientError, _super);
|
|
function ApiClientError(status, messages) {
|
|
var _this = _super.call(this, messages.join('; ') || "HTTP ".concat(status)) || this;
|
|
_this.status = status;
|
|
_this.messages = messages;
|
|
return _this;
|
|
}
|
|
return ApiClientError;
|
|
}(Error));
|
|
exports.ApiClientError = ApiClientError;
|
|
function buildUrl(baseUrl, path, query) {
|
|
var normalizedBase = baseUrl.endsWith('/') ? baseUrl : "".concat(baseUrl, "/");
|
|
var url = new URL(path.replace(/^\//, ''), normalizedBase);
|
|
if (query) {
|
|
Object.keys(query).forEach(function (key) {
|
|
var value = query[key];
|
|
if (value !== undefined && value !== null && value !== '') {
|
|
url.searchParams.set(key, String(value));
|
|
}
|
|
});
|
|
}
|
|
return url.toString();
|
|
}
|
|
function parseResponse(response) {
|
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
var text, json, messages;
|
|
var _a, _b;
|
|
return tslib_1.__generator(this, function (_c) {
|
|
switch (_c.label) {
|
|
case 0:
|
|
if (response.status === 204) {
|
|
return [2 /*return*/, undefined];
|
|
}
|
|
return [4 /*yield*/, response.text()];
|
|
case 1:
|
|
text = _c.sent();
|
|
try {
|
|
json = text ? JSON.parse(text) : undefined;
|
|
}
|
|
catch (_d) {
|
|
// Ответ пришёл не в конверте ApiResponse/ApiError (например, "сырая" страница
|
|
// необработанного исключения ASP.NET Core или ошибка от прокси перед API).
|
|
if (!response.ok) {
|
|
throw new ApiClientError(response.status, [text || "HTTP ".concat(response.status)]);
|
|
}
|
|
throw new ApiClientError(response.status, ['Сервер вернул не-JSON ответ.']);
|
|
}
|
|
if (!response.ok) {
|
|
messages = (_a = json === null || json === void 0 ? void 0 : json.messages) !== null && _a !== void 0 ? _a : ["HTTP ".concat(response.status)];
|
|
throw new ApiClientError(response.status, messages);
|
|
}
|
|
return [2 /*return*/, (_b = json === null || json === void 0 ? void 0 : json.data) !== null && _b !== void 0 ? _b : undefined];
|
|
}
|
|
});
|
|
});
|
|
}
|
|
/**
|
|
* Прод/staging: вызовы через AAD-защищённое SPFx-подключение (AadHttpClient).
|
|
* Требует webApiPermissionRequests в package-solution.json и админского
|
|
* согласия в SharePoint admin center → Advanced → API access.
|
|
*/
|
|
var AadApiHttpClient = /** @class */ (function () {
|
|
function AadApiHttpClient(client, baseUrl) {
|
|
this.client = client;
|
|
this.baseUrl = baseUrl;
|
|
}
|
|
AadApiHttpClient.prototype.get = function (path, query) {
|
|
var url = buildUrl(this.baseUrl, path, query);
|
|
return this.client
|
|
.get(url, sp_http_1.AadHttpClient.configurations.v1)
|
|
.then(function (r) { return parseResponse(r); });
|
|
};
|
|
AadApiHttpClient.prototype.post = function (path, body) {
|
|
return this.send('POST', path, body);
|
|
};
|
|
AadApiHttpClient.prototype.put = function (path, body) {
|
|
return this.send('PUT', path, body);
|
|
};
|
|
AadApiHttpClient.prototype.patch = function (path, body) {
|
|
return this.send('PATCH', path, body);
|
|
};
|
|
AadApiHttpClient.prototype.delete = function (path) {
|
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
return tslib_1.__generator(this, function (_a) {
|
|
switch (_a.label) {
|
|
case 0: return [4 /*yield*/, this.send('DELETE', path)];
|
|
case 1:
|
|
_a.sent();
|
|
return [2 /*return*/];
|
|
}
|
|
});
|
|
});
|
|
};
|
|
AadApiHttpClient.prototype.send = function (method, path, body) {
|
|
var url = buildUrl(this.baseUrl, path);
|
|
return this.client
|
|
.fetch(url, sp_http_1.AadHttpClient.configurations.v1, {
|
|
method: method,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: body !== undefined ? JSON.stringify(body) : undefined
|
|
})
|
|
.then(function (r) { return parseResponse(r); });
|
|
};
|
|
return AadApiHttpClient;
|
|
}());
|
|
exports.AadApiHttpClient = AadApiHttpClient;
|
|
/**
|
|
* Локальная отладка: обычный fetch с заголовком X-Dev-User-Email.
|
|
* Работает только если бэкенд поднят с пустыми Entra:TenantId/Entra:Audience
|
|
* (см. backend/.../Auth/AuthSetup.cs — тогда включается DevAuthHandler).
|
|
*/
|
|
var DevApiHttpClient = /** @class */ (function () {
|
|
function DevApiHttpClient(baseUrl, devUserEmail) {
|
|
this.baseUrl = baseUrl;
|
|
this.devUserEmail = devUserEmail;
|
|
}
|
|
DevApiHttpClient.prototype.get = function (path, query) {
|
|
return this.send('GET', path, undefined, query);
|
|
};
|
|
DevApiHttpClient.prototype.post = function (path, body) {
|
|
return this.send('POST', path, body);
|
|
};
|
|
DevApiHttpClient.prototype.put = function (path, body) {
|
|
return this.send('PUT', path, body);
|
|
};
|
|
DevApiHttpClient.prototype.patch = function (path, body) {
|
|
return this.send('PATCH', path, body);
|
|
};
|
|
DevApiHttpClient.prototype.delete = function (path) {
|
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
return tslib_1.__generator(this, function (_a) {
|
|
switch (_a.label) {
|
|
case 0: return [4 /*yield*/, this.send('DELETE', path)];
|
|
case 1:
|
|
_a.sent();
|
|
return [2 /*return*/];
|
|
}
|
|
});
|
|
});
|
|
};
|
|
DevApiHttpClient.prototype.send = function (method, path, body, query) {
|
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
var url, response;
|
|
return tslib_1.__generator(this, function (_a) {
|
|
switch (_a.label) {
|
|
case 0:
|
|
url = buildUrl(this.baseUrl, path, query);
|
|
return [4 /*yield*/, fetch(url, {
|
|
method: method,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-Dev-User-Email': this.devUserEmail
|
|
},
|
|
body: body !== undefined ? JSON.stringify(body) : undefined
|
|
})];
|
|
case 1:
|
|
response = _a.sent();
|
|
return [2 /*return*/, parseResponse(response)];
|
|
}
|
|
});
|
|
});
|
|
};
|
|
return DevApiHttpClient;
|
|
}());
|
|
exports.DevApiHttpClient = DevApiHttpClient;
|
|
//# sourceMappingURL=httpClient.js.map
|