UNPKG

mclogs

Version:

TypeScript library for the mclo.gs API

95 lines (94 loc) 3.51 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MCLogs = void 0; const constants_1 = require("./constants"); /** * The main class for interacting with the mclo.gs API */ class MCLogs { /** * Create a new paste on mclo.gs * @param content The content of the log file * @returns {Promise<CreatePasteResponse>} * @example const data = await mclogs.create('Hello, world!'); * console.log(data.url); */ create(content) { return __awaiter(this, void 0, void 0, function* () { return yield this.request('/log', { method: 'POST', body: `content=${content}`, contentType: 'application/x-www-form-urlencoded', }); }); } /** * Get the raw content of a paste * @param id The ID of the paste * @returns {Promise<string>} * @example const raw = await mclogs.getRaw('qLHAQBz'); * console.log(raw); */ getRaw(id) { return __awaiter(this, void 0, void 0, function* () { return yield this.request(`/raw/${id}`, { contentType: 'text/plain', }); }); } /** * Get insights for a paste * @param id The ID of the paste * @returns {Promise<InsightsResponse>} * @example const insights = await mclogs.getInsights('bs47Bij'); * console.log(insights.analysis); */ getInsights(id) { return __awaiter(this, void 0, void 0, function* () { return yield this.request(`/insights/${id}`); }); } /** * Get storage limits for mclo.gs * @returns {Promise<StorageLimitsResponse>} * @example const limits = await mclogs.getStorageLimits(); * console.log(limits); */ getStorageLimits() { return __awaiter(this, void 0, void 0, function* () { return yield this.request('/limits'); }); } request(endpoint, options = {}) { return __awaiter(this, void 0, void 0, function* () { const headers = { 'Content-Type': options.contentType || 'application/json', }; const body = options.contentType === 'application/x-www-form-urlencoded' ? options.body : JSON.stringify(options.body); return yield fetch(`${constants_1.MCLOGS_API_BASE}${endpoint}`, { method: options.method || 'GET', headers, body: options.body ? body : undefined, }).then(res => { if (options.contentType === 'text/plain') { return res.text(); } else { return res.json(); } }); }); } } exports.MCLogs = MCLogs;