Skip to content

Commit aad86e5

Browse files
committed
docs: add TS and preload example
1 parent 73cf53c commit aad86e5

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

README.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@ See [Legal Warning](#legal-warning) section for more info on licensing and limit
2121

2222
## Usage
2323

24+
### Installing
25+
26+
`npm install geolite2-redist`
27+
2428
### Using the geoip data
2529

2630
Example geoip lookup in a Node environment, using the `GeoLite2-City` database with `node-maxmind` as a db reader:
2731

32+
#### Javascript
33+
2834
```javascript
2935
const maxmind = require('maxmind');
3036

@@ -38,14 +44,44 @@ import('geolite2-redist').then((geolite2) => {
3844
}).then((reader) => {
3945
const lookup = reader.get('185.194.81.29')
4046

41-
console.log(lookup.country.iso_code) // FR 🥖🇫
47+
console.log(lookup.country.iso_code) // FR 🥖🇫🇷
4248

4349
// Calling close() here shuts everything down nicely and clears up Node's event loop.
4450
reader.close()
4551
})
4652
```
4753

48-
TODO: Typescript example
54+
#### Typescript
55+
56+
```typescript
57+
import geolite2, { GeoIpDbName } from 'geolite2-redist';
58+
import maxmind, { CountryResponse } from 'maxmind';
59+
60+
(async () => {
61+
const reader = await geolite2.open(
62+
GeoIpDbName.Country, // Use the enum instead of a string!
63+
(path) => maxmind.open<CountryResponse>(path)
64+
)
65+
66+
const lookup = reader.get('185.194.81.29')
67+
68+
console.log(lookup.country.iso_code) // FR 🥖🇫🇷
69+
70+
reader.close()
71+
})();
72+
```
73+
74+
### Preloading databases
75+
76+
You can add this to your `package.json` to preload the databases after running `npm install`, instead of downloading them the first time `open` is called:
77+
78+
```json
79+
{
80+
"scripts": {
81+
"preload": "node -e \"import('geolite2-redist').then(geolite => geolite.downloadDbs())\""
82+
}
83+
}
84+
```
4985

5086
## API
5187

0 commit comments

Comments
 (0)