ipregistry / ipregistry-javascript

Official Javascript Client for Ipregistry: a Non-Intrusive Solution for IP Geolocation and Threat Data.
https://ipregistry.co
Apache License 2.0
17 stars 3 forks source link

import issues with Svelte / Sapper Project #2

Closed evdama closed 5 years ago

evdama commented 5 years ago

I'm using Sapper which does server side rendering. There I have code calling the IPRegistry API using the javascript library of yours:

import { IpregistryClient, DefaultCache, IpregistryOptions } from '@ipregistry/client'

export default async ( ipAddress, filters ) => {
  try {
    const IPRClient = new IpregistryClient( `ROLLUP_IPREGISTRY_KEY`, new DefaultCache(16384) )

        return await IPRClient.lookup( ipAddress, IpregistryOptions.hostname(true), IpregistryOptions.filter(filters) )

  } catch ( error ) {
    console.log('error from call-to-ipregistry-api.js')
  }
}

Because with sapper you do SSR, then ship the page down to the client, den hydrate it you've to basically take care of server and client when importing. Other ES6 imports work fine, but using /@ipregistry/client I get this error in the console

src/node_modules/utils/ipregistry/call-to-ipregistry-api.js changed. rebuilding...
✗ client
'IpregistryClient' is not exported by node_modules/@ipregistry/client/dist/browser/index.js
1: import { IpregistryClient, DefaultCache, IpregistryOptions } from '@ipregistry/client'
            ^
2:
3: export default async ( ipAddress, filters ) => {
✗ server
'DefaultCache' is not exported by node_modules/@ipregistry/client/dist/esm/index.js
1: import { IpregistryClient, DefaultCache, IpregistryOptions } from '@ipregistry/client'
                              ^
2:
3: export default async ( ipAddress, filters ) => {
✔ service worker (29ms)

Note that a native axios version works just fine. This is what I had before:

import axios from 'axios'

export default async ( ipAddress, filters ) => {
  try {
    const ipRegistryResponse = await axios.get( `ROLLUP_IPREGISTRY_URL${ ipAddress }?key=ROLLUP_IPREGISTRY_KEY&hostname=true&fields=${ filters }` )
    return ipRegistryResponse.data
  } catch ( error ) {
    console.log('error from call-to-ipregistry-api.js')
  }
}
lpellegr commented 5 years ago

Thanks for raising this issue. I don't have the time to investigate right now but I will do it at the beginning of the next week.

lpellegr commented 5 years ago

I was able to reproduce and fix the issue.

The default Ipregistry bundle loaded by sapper on browsers does export Ipregistry classes, nor a default export. Only the ESM bundle does.

I have published a new version of the Ipregistry client library (1.2.1) and written a small working example with sapper:

https://github.com/ipregistry/ipregistry-sapper

The file that you are interested in is src/routes/ipregistry.svelte:

https://github.com/ipregistry/ipregistry-sapper/blob/109535c12111ac8f65843c661a99fb286e041f4f/src/routes/ipregistry.svelte#L3-L17

evdama commented 5 years ago

brilliant, I'll test this tonight and report back here! Also, the fact that you addressed and fixed this issue in time assures me even more that I've landed on the right IP service with you guys... good stuff! :)

I posted your sapper example to https://discordapp.com/channels/457912077277855764/473466028106579978 for others to test and toy around with...

lpellegr commented 5 years ago

Your support and trust are really appreciated. Thanks!

evdama commented 5 years ago

so I've settled with this code which works just fine

import ipregistry from '@ipregistry/client'

export default async ( ipAddress, filters ) => {
  try {
    const client = new ipregistry.IpregistryClient('ROLLUP_IPREGISTRY_KEY', new ipregistry.DefaultCache(16384))
    return await client.lookup(ipAddress,
      ipregistry.IpregistryOptions.hostname(true),
      ipregistry.IpregistryOptions.filter(filters))
  } catch ( error ) {
    console.log( 'IPRegistry error code: ' + error.code )
    console.log( 'IPRegistry error message: ' + error.message )
    console.log( 'IPRegistry error resolution: ' + error.resolution )
  }
}

two more things

I was thinking about using https://www.npmjs.com/package/express-useragent in order to get the user-agent information, then put it inside an express session variable because that's the place where I make the call to IPRegistry as well (every session/visitor gets his IP evaluation). Makes sense?

lpellegr commented 5 years ago

Thanks for your feedback.

I don't know where you call the functions above but be careful. As you know, with Sapper, the same code may be called on server-side and client-side depending on where you put it. This means that you might need to check if the window variable is defined to detect the environment and adapt how the user-agent is retrieved:

req.headers['user-agent']

on server-side with expressjs, or

navigator.userAgent

on client-side.

Please create a separate issue if you encounter any problems.