UNPKG

line-api

Version:

Simple LINE api client for Node.js written in TypeScript

147 lines 6.56 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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.Notify = exports.NotifyError = void 0; const querystring_1 = __importDefault(require("querystring")); const node_fetch_1 = __importStar(require("node-fetch")); const stream_1 = require("stream"); const formdata_node_1 = require("formdata-node"); const file_from_path_1 = require("formdata-node/file-from-path"); const form_data_encoder_1 = require("form-data-encoder"); const consts_1 = require("./consts"); class NotifyError extends Error { constructor(param) { super(param.message); this.name = 'NotifyError'; this.status = param.status; } } exports.NotifyError = NotifyError; class Notify { constructor({ token }) { this.accessToken = token; } req(type, path, param) { return __awaiter(this, void 0, void 0, function* () { const url = consts_1.ENDPOINT.notify[type] + path; const headers = new node_fetch_1.Headers(param === null || param === void 0 ? void 0 : param.headers); headers.set('Authorization', `Bearer ${this.accessToken}`); return (0, node_fetch_1.default)(url, Object.assign(Object.assign({}, param), { headers })) .then((r) => { const limit = r.headers.get('x-ratelimit-limit'); const remaining = r.headers.get('x-ratelimit-remaining'); const imageLimit = r.headers.get('x-ratelimit-imagelimit'); const imageRemaining = r.headers.get('x-ratelimit-imageremaining'); const reset = r.headers.get('x-ratelimit-reset'); if (limit && remaining && imageLimit && imageRemaining && reset) { this.ratelimit = { request: { limit: parseInt(limit), remaining: parseInt(remaining), }, image: { limit: parseInt(imageLimit), remaining: parseInt(imageRemaining), }, reset: new Date(parseInt(reset) * 1000), }; } return r.json(); }) .then((r) => { if (r.status !== 200) { throw new NotifyError(r); } return r; }); }); } get(type, path, query) { const q = query ? '?' + querystring_1.default.stringify(query) : ''; return this.req(type, path + q, { method: 'get' }); } post(type, path, formData) { const encoder = new form_data_encoder_1.FormDataEncoder(formData); return this.req(type, path, { method: 'post', headers: encoder.headers, body: stream_1.Readable.from(encoder) }); } status() { return this.get('api', '/status'); } revoke() { return this.get('api', '/revoke'); } send({ message, image, sticker, notificationDisabled }) { return __awaiter(this, void 0, void 0, function* () { const formData = new formdata_node_1.FormData(); formData.append('message', message); if (image) { if (typeof image === 'string') { formData.append('imageFile', yield (0, file_from_path_1.fileFromPath)(image)); } else { formData.append('imageFullsize', image.fullsize); formData.append('imageThumbnail', image.thumbnail); } } if (sticker) { const { packageId, id } = typeof sticker === 'string' ? consts_1.STICKER[sticker] || {} : sticker; if (packageId && id) { formData.append('stickerPackageId', packageId); formData.append('stickerId', id); } } if (notificationDisabled !== void 0) { formData.append('notificationDisabled', notificationDisabled); } return this.post('api', '/notify', formData); }); } authorize(opt) { return this.get('oauth', '/authorize', opt); } token(opt) { const formData = new formdata_node_1.FormData(); formData.append('grant_type', opt.grant_type); formData.append('code', opt.code); formData.append('redirect_uri', opt.redirect_uri); formData.append('client_id', opt.client_id); formData.append('client_secret', opt.client_secret); return this.post('oauth', '/token', formData); } } exports.Notify = Notify; //# sourceMappingURL=notify.js.map