tscコンパイル前に、promiseも変換できるようにtsconfig.json内に 「es2015.promise」を追記します。
"lib" : ["es2015", "es2015.iterable", "dom", "es2015.promise"]
その後、tscを実行して下さい。
class slack { | |
private path = "https://slack.com/api/"; | |
private request(url:string):any { | |
return new Promise(resolve => { | |
let xhr = new XMLHttpRequest(); | |
xhr.onload = ()=>{ | |
resolve(JSON.parse(xhr.response)); | |
}; | |
xhr.open("GET", url); | |
xhr.send(); | |
}); | |
} | |
private urlParametor(params:{[key:string]:string}):string { | |
let queryArray: Array<string> = new Array(); | |
for(var key in params){ | |
console.log(key + '=' + encodeURIComponent(params[key])); | |
queryArray.push(key + '=' + encodeURIComponent(params[key])); | |
} | |
const query = queryArray.join('&'); | |
return query; | |
} | |
public async postMessage(params:{[key:string]:string}){ | |
const method = "chat.postMessage"; | |
const query = this.urlParametor(params); | |
const url = this.path + method + "?" + query; | |
let res:any = await this.request(url); | |
return res; | |
} | |
} | |
const s = new slack(); | |
const params:{[key:string]:string} = { | |
"token": "xoxp-xxxxxxx", | |
"text": "test!!!!", | |
"channel": "general" | |
}; | |
const res = s.postMessage(params); |