medusa-plugin-ip-lookup-maxmind-geoip2
Version:
A Medusa plugin to lookup a user's country based on IP using MaxMind
24 lines (20 loc) • 681 B
text/typescript
import { AddressNotFoundError } from "@maxmind/geoip2-node/dist/src/errors";
import IpLookupService from "../../src/services/ip-lookup";
const service = new IpLookupService(
{},
{
maxmind_db_path: "data/dbip-country-lite-2022-06.mmdb",
}
);
describe("lookupIp", () => {
test("finds correct country", async () => {
const data = await service.lookupIp("45.55.27.15");
expect(data.country.isoCode).toBe("US");
});
test("throws exception on unknown IP", async () => {
await expect(service.lookupIp("::1")).rejects.toThrow(AddressNotFoundError);
await expect(service.lookupIp("127.0.0.1")).rejects.toThrow(
AddressNotFoundError
);
});
});