UNPKG

line-api

Version:

Simple LINE api client for Node.js written in TypeScript

116 lines 5.01 kB
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()); }); }; import qs from 'querystring'; import fetch, { Headers } from 'node-fetch'; import { Readable } from 'stream'; import { FormData } from 'formdata-node'; import { fileFromPath } from 'formdata-node/file-from-path'; import { FormDataEncoder } from 'form-data-encoder'; import { ENDPOINT, STICKER } from './consts'; export class NotifyError extends Error { constructor(param) { super(param.message); this.name = 'NotifyError'; this.status = param.status; } } export class Notify { constructor({ token }) { this.accessToken = token; } req(type, path, param) { return __awaiter(this, void 0, void 0, function* () { const url = ENDPOINT.notify[type] + path; const headers = new Headers(param === null || param === void 0 ? void 0 : param.headers); headers.set('Authorization', `Bearer ${this.accessToken}`); return fetch(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 ? '?' + qs.stringify(query) : ''; return this.req(type, path + q, { method: 'get' }); } post(type, path, formData) { const encoder = new FormDataEncoder(formData); return this.req(type, path, { method: 'post', headers: encoder.headers, body: 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(); formData.append('message', message); if (image) { if (typeof image === 'string') { formData.append('imageFile', yield fileFromPath(image)); } else { formData.append('imageFullsize', image.fullsize); formData.append('imageThumbnail', image.thumbnail); } } if (sticker) { const { packageId, id } = typeof sticker === 'string' ? 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(); 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); } } //# sourceMappingURL=notify.js.map