Adds ip lookup middleware for cart creation
This commit is contained in:
1 parent
79cc7e6639
commit
eeca05117a
11 files changed
+154
-23
No files matched your search
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"plugins": [
|
||||
"@babel/plugin-proposal-class-properties",
|
||||
"@babel/plugin-transform-classes",
|
||||
"@babel/plugin-transform-instanceof"
|
||||
],
|
||||
"presets": ["@babel/preset-env"],
|
||||
"env": {
|
||||
"test": {
|
||||
"plugins": ["@babel/plugin-transform-runtime"]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
dist/
|
||||
node_modules/
|
||||
.DS_store
|
||||
.env*
|
||||
/*.js
|
||||
!index.js
|
||||
yarn.lock
|
||||
yarn-error.log
|
||||
|
||||
/api
|
||||
/services
|
||||
/models
|
||||
/subscribers
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"endOfLine": "lf",
|
||||
"semi": false,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5"
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"name": "medusa-plugin-ip-lookup",
|
||||
"version": "1.0.0-alpha.3",
|
||||
"description": "IP lookup middleware for Medusa core",
|
||||
"main": "dist/index.js",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/medusajs/medusa",
|
||||
"directory": "packages/medusa-plugin-ip-lookup"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "babel src --out-dir . --ignore **/__tests__",
|
||||
"prepare": "cross-env NODE_ENV=production npm run build",
|
||||
"watch": "babel -w src --out-dir . --ignore **/__tests__",
|
||||
"test": "jest"
|
||||
},
|
||||
"author": "Oliver Juhl",
|
||||
"license": "AGPL-3.0-or-later",
|
||||
"devDependencies": {
|
||||
"@babel/cli": "^7.7.5",
|
||||
"@babel/core": "^7.7.5",
|
||||
"@babel/plugin-proposal-class-properties": "^7.7.4",
|
||||
"@babel/plugin-transform-classes": "^7.9.5",
|
||||
"@babel/plugin-transform-instanceof": "^7.8.3",
|
||||
"@babel/plugin-transform-runtime": "^7.7.6",
|
||||
"@babel/preset-env": "^7.7.5",
|
||||
"@babel/runtime": "^7.7.6",
|
||||
"cross-env": "^5.2.1",
|
||||
"jest": "^25.5.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^0.20.0",
|
||||
"mongoose": "^5.8.0"
|
||||
},
|
||||
"gitHead": "3cc7cbe5124cbcbb75f6e1435db4dcfaa2a60408"
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
export default {
|
||||
preCartCreation: async function (req, res, next) {
|
||||
try {
|
||||
if (req.body.region_id) {
|
||||
next()
|
||||
return
|
||||
}
|
||||
|
||||
const ipLookupService = req.scope.resolve("ipLookupService")
|
||||
const regionService = req.scope.resolve("regionService")
|
||||
|
||||
const ip = req.headers["x-forwarded-for"] || req.connection.remoteAddress
|
||||
|
||||
const { data } = await ipLookupService.lookupIp(ip)
|
||||
|
||||
if (!data.country_code) {
|
||||
next()
|
||||
return
|
||||
}
|
||||
|
||||
// Find region using the country code from ip lookup
|
||||
const regions = await regionService.list({
|
||||
countries: data.country_code,
|
||||
})
|
||||
|
||||
// If this region exists, add it to the body of the cart creation request
|
||||
if (regions[0]) {
|
||||
req.body.region_id = regions[0]._id.toString()
|
||||
}
|
||||
|
||||
next()
|
||||
} catch (error) {
|
||||
next()
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { BaseService } from "medusa-interfaces"
|
||||
import axios from "axios"
|
||||
|
||||
class IpLookupService extends BaseService {
|
||||
constructor({}, options) {
|
||||
super()
|
||||
|
||||
this.options_ = options
|
||||
}
|
||||
|
||||
async lookupIp(ipAddress) {
|
||||
return axios.get(
|
||||
`http://api.ipstack.com/${ipAddress}?access_key=${this.options_.access_token}`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export default IpLookupService
|
||||
Reference in new issue
Block a user