UNPKG

paypal-spotlight

Version:

A smart launcher for PayPal.

55 lines (49 loc) 1.36 kB
const { request } = require('./requests') const { SERVICE_BASE_URL } = require('../app/constants') const SLACK_USER_API = '/api/slack/users' const SLACK_CHANNEL_API = '/api/slack/channels' const TEAM_ID = 'T0G9AL7B8' const requestOptions = { rejectUnauthorized: false } let latestQuery const mapSlackItemsWithType = type => ({ display_name: displayName, full_name: fullName, id, }) => ({ title: displayName, subtitle: fullName, arg: `slack://${type}?team=${TEAM_ID}&id=${id}`, icon: { path: './icon.png', }, }) const querySlackDataHandler = (prefix, apiURL, type) => q => { return new Promise(resolve => { let items = [] if (!q.replace(prefix, '') || !q.startsWith(prefix)) { resolve({ items }) } else { const query = q.replace(prefix, '') // Set the flag for the latestQuery latestQuery = query request( `${SERVICE_BASE_URL}/${apiURL}?q=${query}`, requestOptions, (err, resp, body) => { if (!err && resp.statusCode === 200) { items = JSON.parse(body).result.map(mapSlackItemsWithType(type)) console.log('>> items', items) } if (latestQuery === query) { resolve({ items }) } }, ) } }) } module.exports = { SLACK_CHANNEL_API, SLACK_USER_API, querySlackDataHandler, }