forked from microsoft/typed-rest-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.ts
62 lines (53 loc) · 1.62 KB
/
common.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import http = require("http");
import * as restm from 'typed-rest-client/RestClient';
// using httpbin.org.
export interface HttpBinData {
url: string;
data: any;
}
export function getEnv(name: string): string {
let val = process.env[name];
return val;
}
export function banner(title: string): void {
console.log();
console.log('=======================================');
console.log('\t' + title);
console.log('=======================================');
}
export function heading(title: string): void {
console.log();
console.log('> ' + title);
}
//
// Utility functions
//
export async function outputHttpBinResponse(body: string, message?: http.IncomingMessage) {
if (message) {
if (message.statusCode) {
console.log('status', message.statusCode);
}
if (message.rawHeaders) {
console.log('headers:' + JSON.stringify(message.rawHeaders));
}
}
if (body) {
let obj = JSON.parse(body.toString());
console.log('response from ' + obj.url);
if (obj.data) {
console.log('data:', obj.data);
}
}
}
//
// This is often not needed. In this case, using httpbin.org which echos the object
// in the data property of the json. It's an artifact of sample service used.
// But it's useful to note that we do offer a processing function which is invoked on the returned json.
//
export function httpBinOptions(): restm.IRequestOptions {
let options: restm.IRequestOptions = <restm.IRequestOptions>{};
options.responseProcessor = (obj: any) => {
return obj['data'];
}
return options;
}