Skip to content

Commit 2b4a857

Browse files
Add sample for user-agent parsing
1 parent f1b48c7 commit 2b4a857

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

samples/parse.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2019 Ipregistry (https://ipregistry.co).
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {ApiError, ClientError, IpregistryClient} from '../src';
18+
19+
async function main() {
20+
const client = new IpregistryClient('tryout');
21+
22+
try {
23+
// Input one or more user-agent header values to parse
24+
const response =
25+
await client.parse(
26+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',
27+
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36'
28+
);
29+
30+
// As a result you get an array
31+
console.log(response.data.length);
32+
console.log(response.data[0].name);
33+
console.log(response.data[0].os.name);
34+
console.log(response.data[0].type);
35+
console.log(response.data[0].version);
36+
} catch (error) {
37+
if (error instanceof ApiError) {
38+
// Handle API error here (e.g. Invalid API key)
39+
console.error('API error', error);
40+
} else if (error instanceof ClientError) {
41+
// Handle client error here (e.g. request timeout)
42+
console.error('Client error', error);
43+
} else {
44+
// Handle unexpected error here
45+
console.error('Unexpected error', error);
46+
}
47+
}
48+
}
49+
50+
main().then(() => 0).catch(() => 1);

0 commit comments

Comments
 (0)