Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'
import * as functions from 'firebase-functions'
import { v4 } from 'uuid'
import { Document } from '../types/Document.type'
import { StorageController } from './storage.controller'
const BASEURL = 'https://api.trello.com/1'
export enum TRELLO {
KEY = 'key',
TOKEN = 'token',
ID_LIST = 'idList',
NAME = 'name',
}
export enum ROUTE {
CARDS = 'cards',
}
export class TrelloController {
private trelloKey: string
private trelloToken: string
constructor(private store: StorageController) {
this.trelloKey = functions.config().trello.key
this.trelloToken = functions.config().trello.token
}
public async storeWebhookPayload(payload: any) {
const uuid: string = v4()
await this.store.set(Document.TRELLO_WEBHOOK_PAYLOAD, uuid, payload)
}
public async addCard(idList: string, name: string): Promise<string> {
const queryParams: URLSearchParams = new URLSearchParams()
queryParams.append(TRELLO.ID_LIST, idList)
queryParams.append(TRELLO.NAME, name)
const result = await this.request('POST', ROUTE.CARDS, queryParams)
return result ? result.id : undefined
}
private async request(
method: 'GET' | 'POST' | 'PATCH' | 'DELETE',
route: string,
queryParams: URLSearchParams,
) {
const params = queryParams
params.append(TRELLO.KEY, this.trelloKey)
params.append(TRELLO.TOKEN, this.trelloToken)
const config: AxiosRequestConfig = {
method,
url: `${BASEURL}/${route}`,
params,
}
try {
const result: AxiosResponse = await axios(config)
return result ? result.data : undefined
} catch (error) {
console.error(error)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment