Giter Site home page Giter Site logo

node's Introduction

IPData JavaScript Library

JavaScript library that can be used in a web browser or Node.js application to gather information for an IP address using https://ipdata.co.

Table of Contents

Install

$ npm install ipdata

Use

Import library

Import the library.

import IPData from 'ipdata';

Note: If you are using require() then you will need to use the default value exported from the library.

const IPData = require('ipdata').default;

Create an Instance

Create an instance of the IPData class and pass your api key for IPData as the first parameter.

const ipdata = new IPData('<apiKey>');

The library will cache 4096 ip addresses responses for 24 hours using a LRU cache by default. You can configure the cache by passing an object as the second paramenter.

const cacheConfig = {
  max: 1000, // max size
  maxAge: 10 * 60 * 1000, // max age in ms (i.e. 10 minutes)
};
const ipdata = new IPData('<apiKey>', cacheConfig);

Note: To disable the cache pass -1 as the maxAge.

const cacheConfig = {
  maxAge: -1, // disable the cache
};
const ipdata = new IPData('<apiKey>', cacheConfig);

Lookup

The library will lookup the ip address of the host computer if no ip address is provided.

ipdata.lookup()
  .then(function(info) {
    // info.ip === '<hostcomputerip>'
    // ...
  });

You can pass an ip address as the first parameter to the lookup() method to lookup information about the ip address using IPData.

const ip = '1.1.1.1';
ipdata.lookup(ip)
  .then(function(info) {
    // info.ip === 1.1.1.1
    // ...
  });

You can specify only a select field to be returned when looking up an ip address by passing a field as the second parameter to the lookup() method.

const ip = '1.1.1.1';
const selectField = 'ip';
ipdata.lookup(ip, selectField)
  .then(function(info) {
    // info.select_field === 1.1.1.1
    // ...
  });

You can specify only certain fields to be returned when looking up an ip address by passing an array of fields as the third parameter to the lookup() method.

const ip = '1.1.1.1';
const fields = ['ip', 'city'];
ipdata.lookup(ip, null, fields)
  .then(function(info) {
    // ...
  });

Bulk Lookup

You can lookup multiple ip addresses with one API call using the bulkLookup() method.

const ips = ['1.1.1.1', '1.0.0.1'];
ipdata.bulkLookup(ips)
  .then(function(info) {
    // info[0].ip === 1.1.1.1
    // ...
  });

You can specify only certain fields to be returned when looking up multiple ip addresses by passing an array of fields as the second parameter to the bulkLookup() method.

const ips = ['1.1.1.1', '1.0.0.1'];
const fields = ['ip', 'city'];
ipdata.bulkLookup(ips, fields)
  .then(function(info) {
    // ...
  });

node's People

Contributors

dependabot[bot] avatar thomasconner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

node's Issues

Add callback for getting complete response

The api should provided an option to provided a callback for retrieving the complete response.

function onResponse(response) {
   // ...
}

lookup({ onResponse })
  .then(function(info) {
    // info.ip === '<hostcomputerip>'
    // ...
  });

IPData is not a constructor

Hi! I'm trying to create the IPData instance but returns "IPData is not a constructor".
I'm following all the documentation instructions...

[Enhancement] Make lookup() method accept named params

Currently, the main lookup method is configured to accept a series of params:

async lookup(ip?: string, selectField?: string, fields?: string[])

The issue is if you are wanting to set only one of these, you need to pass undefined to the preceding params.

A common pattern to help solve this scenario would be:

export interface LookupParams {
  ip?: string
  selectField?: string
  fields?: string[]
}

// ... main class
{
  async lookup({ ip, selectField, fields }: LookupParams = {}) {
   // ... lookup logic
  }
}

Naturally, changing the shape of the method signature is a tricky undertaking, so potentially you could run a type check on the first param and keep the existing ones to allow backward compatibility?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.