@mathrunet/masamune
Version:
Manages packages for the server portion (NodeJS) of the Masamune framework.
155 lines • 4.49 kB
JavaScript
;
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 async get(url, options) {
const res = await (0, node_fetch_1.default)(url, {
method: "GET",
headers: options?.headers,
timeout: 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 async post(url, options) {
const data = 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 = await (0, node_fetch_1.default)(url, {
method: "POST",
headers: options?.headers,
body: body,
timeout: 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 async put(url, options) {
const data = 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 = await (0, node_fetch_1.default)(url, {
method: "PUT",
headers: options?.headers,
body: body,
timeout: 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 async delete(url, options) {
const res = await (0, node_fetch_1.default)(url, {
method: "DELETE",
headers: options?.headers,
timeout: options?.timeout,
});
return res;
}
}
exports.Api = Api;
//# sourceMappingURL=api.js.map