forked from microsoft/typed-rest-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.ts
52 lines (43 loc) · 1.8 KB
/
rest.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
import * as rm from 'typed-rest-client/RestClient';
import * as fs from 'fs';
import * as path from 'path';
import * as cm from './common';
let baseUrl: string = 'https://httpbin.org';
let restc: rm.RestClient = new rm.RestClient('rest-samples',
baseUrl);
export async function run() {
try {
cm.banner('Rest Samples');
//
// Get Resource: strong typing of resource(s) via generics.
// In this case httpbin.org has a response structure
// response.result carries the resource(s)
//
cm.heading('get rest obj');
let restRes: rm.IRestResponse<cm.HttpBinData> = await restc.get<cm.HttpBinData>('get');
console.log(restRes.statusCode, restRes.result['url']);
//
// Create and Update Resource(s)
// Generics <T,R> are the type sent and the type returned in the body. Ideally the same in REST service
//
interface HelloObj {
message: string;
}
let hello: HelloObj = <HelloObj>{ message: "Hello World!" };
let options: rm.IRequestOptions = cm.httpBinOptions();
cm.heading('create rest obj');
let hres: rm.IRestResponse<HelloObj> = await restc.create<HelloObj>('/post', hello, options);
console.log(hres.result);
cm.heading('update rest obj');
hello.message += '!';
// you can also specify a full url (not relative) per request
hres = await restc.update<HelloObj>('https://httpbin.org/patch', hello, options);
console.log(hres.result);
cm.heading('options rest call');
let ores: rm.IRestResponse<void> = await restc.options<void>('', options);
console.log(ores.statusCode);
}
catch (err) {
console.error('Failed: ' + err.message);
}
}