-
Notifications
You must be signed in to change notification settings - Fork 21
/
City.ts
48 lines (44 loc) · 1.62 KB
/
City.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
import camelcaseKeys = require('camelcase-keys');
import * as records from '../records';
import { CityResponse, Json } from '../types';
import Country from './Country';
/** Class representing the model of a "City" response **/
export default class City extends Country {
/**
* The city for the requested IP address.
*/
public readonly city?: records.CityRecord;
/**
* The location for the requested IP address.
*/
public readonly location?: records.LocationRecord;
/**
* The postal object for the requested IP address.
*/
public readonly postal?: records.PostalRecord;
/**
* An array of SubdivisionsRecord objects representing the country subdivisions for
* the requested IP address. The number and type of subdivisions varies by
* country, but a subdivision is typically a state, province, county, etc.
* Subdivisions are ordered from most general (largest) to most specific
* (smallest). If the response did not contain any subdivisions, this method
* returns an empty array.
*/
public readonly subdivisions?: records.SubdivisionsRecord[];
/**
* Instantiates a "City" using fields from the response
*
* @param response The GeoIP2 response
*/
public constructor(response: CityResponse) {
super(response);
const camelcaseResponse = camelcaseKeys(response as Json, {
deep: true,
exclude: [/\-/],
}) as unknown as City;
this.city = camelcaseResponse.city || undefined;
this.location = camelcaseResponse.location || undefined;
this.postal = camelcaseResponse.postal || undefined;
this.subdivisions = camelcaseResponse.subdivisions || undefined;
}
}