UNPKG

@mathrunet/masamune

Version:

Manages packages for the server portion (NodeJS) of the Masamune framework.

172 lines 6.12 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Api = exports.ApiResponse = void 0; const node_fetch_1 = __importDefault(require("node-fetch")); const form_data_1 = __importDefault(require("form-data")); var node_fetch_2 = require("node-fetch"); Object.defineProperty(exports, "ApiResponse", { enumerable: true, get: function () { return node_fetch_2.Response; } }); /** * API for Rest can be called. * * Rest用のAPIを呼び出すことができます。 */ class Api { /** * Call the API with the `GET` parameter. * * `GET`パラメーターでAPIを呼び出します。 * * @param {string} url * The URL of the API to call. * * 呼び出すAPIのURL。 * * @param {GetRequestOptions | undefined} options * Options for `GET` requests. * * `GET`リクエスト用のオプション。 * * @returns {Promise<ApiResponse>} * Response from API. * * APIからのレスポンス。 */ static get(url, options) { return __awaiter(this, void 0, void 0, function* () { const res = yield (0, node_fetch_1.default)(url, { method: "GET", headers: options === null || options === void 0 ? void 0 : options.headers, timeout: options === null || options === void 0 ? void 0 : options.timeout, }); return res; }); } /** * Call the API with the `POST` parameter. * * `POST`パラメーターでAPIを呼び出します。 * * @param {string} url * The URL of the API to call. * * 呼び出すAPIのURL。 * * @param {PostRequestOptions | undefined} options * Options for `POST` requests. * * `POST`リクエスト用のオプション。 * * @returns {Promise<ApiResponse>} * Response from API. * * APIからのレスポンス。 */ static post(url, options) { return __awaiter(this, void 0, void 0, function* () { const data = options === null || options === void 0 ? void 0 : options.data; let body = undefined; if (data && typeof data === "string") { body = data; } else if (data && typeof data === "object") { body = new form_data_1.default(); for (const key in data) { body.append(key, data[key]); } } const res = yield (0, node_fetch_1.default)(url, { method: "POST", headers: options === null || options === void 0 ? void 0 : options.headers, body: body, timeout: options === null || options === void 0 ? void 0 : options.timeout, }); return res; }); } /** * Call the API with the `PUT` parameter. * * `PUT`パラメーターでAPIを呼び出します。 * * @param {string} url * The URL of the API to call. * * 呼び出すAPIのURL。 * * @param {PutRequestOptions | undefined} options * Options for `PUT` requests. * * `PUT`リクエスト用のオプション。 * * @returns {Promise<ApiResponse>} * Response from API. * * APIからのレスポンス。 */ static put(url, options) { return __awaiter(this, void 0, void 0, function* () { const data = options === null || options === void 0 ? void 0 : options.data; let body = undefined; if (data && typeof data === "string") { body = data; } else if (data && typeof data === "object") { body = new form_data_1.default(); for (const key in data) { body.append(key, data[key]); } } const res = yield (0, node_fetch_1.default)(url, { method: "PUT", headers: options === null || options === void 0 ? void 0 : options.headers, body: body, timeout: options === null || options === void 0 ? void 0 : options.timeout, }); return res; }); } /** * Call the API with the `DELETE` parameter. * * `DELETE`パラメーターでAPIを呼び出します。 * * @param {string} url * The URL of the API to call. * * 呼び出すAPIのURL。 * * @param {DeleteRequestOptions | undefined} options * Options for `DELETE` requests. * * `DELETE`リクエスト用のオプション。 * * @returns {Promise<ApiResponse>} * Response from API. * * APIからのレスポンス。 */ static delete(url, options) { return __awaiter(this, void 0, void 0, function* () { const res = yield (0, node_fetch_1.default)(url, { method: "DELETE", headers: options === null || options === void 0 ? void 0 : options.headers, timeout: options === null || options === void 0 ? void 0 : options.timeout, }); return res; }); } } exports.Api = Api; //# sourceMappingURL=api.js.map