Skip to content

Instantly share code, notes, and snippets.

@urbanisierung
Created December 9, 2020 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save urbanisierung/fbc3303426dd070c4c2c91470fe91a7a to your computer and use it in GitHub Desktop.
Save urbanisierung/fbc3303426dd070c4c2c91470fe91a7a to your computer and use it in GitHub Desktop.
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