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
|
||||||
@@ -3,12 +3,19 @@ import middlewares from "../../../middlewares"
|
|||||||
|
|
||||||
const route = Router()
|
const route = Router()
|
||||||
|
|
||||||
export default app => {
|
export default (app, container) => {
|
||||||
|
const middlewareService = container.resolve("middlewareService")
|
||||||
|
|
||||||
app.use("/carts", route)
|
app.use("/carts", route)
|
||||||
|
|
||||||
route.get("/:id", middlewares.wrap(require("./get-cart").default))
|
route.get("/:id", middlewares.wrap(require("./get-cart").default))
|
||||||
|
|
||||||
route.post("/", middlewares.wrap(require("./create-cart").default))
|
route.post(
|
||||||
|
"/",
|
||||||
|
middlewareService.usePreCartCreation(),
|
||||||
|
middlewares.wrap(require("./create-cart").default)
|
||||||
|
)
|
||||||
|
|
||||||
route.post("/:id", middlewares.wrap(require("./update-cart").default))
|
route.post("/:id", middlewares.wrap(require("./update-cart").default))
|
||||||
|
|
||||||
// Line items
|
// Line items
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ export default (app, container, config) => {
|
|||||||
customerRoutes(route, container)
|
customerRoutes(route, container)
|
||||||
productRoutes(route)
|
productRoutes(route)
|
||||||
orderRoutes(route)
|
orderRoutes(route)
|
||||||
cartRoutes(route)
|
cartRoutes(route, container)
|
||||||
shippingOptionRoutes(route)
|
shippingOptionRoutes(route)
|
||||||
regionRoutes(route)
|
regionRoutes(route)
|
||||||
|
|
||||||
|
|||||||
@@ -109,6 +109,10 @@ function registerMedusaMiddleware(pluginDetails, container) {
|
|||||||
pluginDetails.options
|
pluginDetails.options
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (module.preCartCreation) {
|
||||||
|
middlewareService.addPreCartCreation(module.preCartCreation)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function registerCoreRouters(pluginDetails, container) {
|
function registerCoreRouters(pluginDetails, container) {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ class MiddlewareService {
|
|||||||
constructor(container) {
|
constructor(container) {
|
||||||
this.postAuthentication_ = []
|
this.postAuthentication_ = []
|
||||||
this.preAuthentication_ = []
|
this.preAuthentication_ = []
|
||||||
|
this.preCartCreation_ = []
|
||||||
this.routers = {}
|
this.routers = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,6 +66,17 @@ class MiddlewareService {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a middleware function to be called before cart creation
|
||||||
|
* @param {function} middleware - the middleware function. Should return a
|
||||||
|
* middleware function.
|
||||||
|
* @return {void}
|
||||||
|
*/
|
||||||
|
addPreCartCreation(middleware) {
|
||||||
|
this.validateMiddleware_(middleware)
|
||||||
|
this.preCartCreation_.push(middleware)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds post authentication middleware to an express app.
|
* Adds post authentication middleware to an express app.
|
||||||
* @param {ExpressApp} app - the express app to add the middleware to
|
* @param {ExpressApp} app - the express app to add the middleware to
|
||||||
@@ -86,6 +98,10 @@ class MiddlewareService {
|
|||||||
app.use(object.middleware(object.options))
|
app.use(object.middleware(object.options))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
usePreCartCreation() {
|
||||||
|
return this.preCartCreation_
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default MiddlewareService
|
export default MiddlewareService
|
||||||
@@ -1965,13 +1965,6 @@ cli-width@^2.0.0:
|
|||||||
resolved "https://registry.yarnpkg.com/cli-width/-/cli-width-2.2.0.tgz#ff19ede8a9a5e579324147b0c11f0fbcbabed639"
|
resolved "https://registry.yarnpkg.com/cli-width/-/cli-width-2.2.0.tgz#ff19ede8a9a5e579324147b0c11f0fbcbabed639"
|
||||||
integrity sha1-/xnt6Kml5XkyQUewwR8PvLq+1jk=
|
integrity sha1-/xnt6Kml5XkyQUewwR8PvLq+1jk=
|
||||||
|
|
||||||
client-sessions@^0.8.0:
|
|
||||||
version "0.8.0"
|
|
||||||
resolved "https://registry.yarnpkg.com/client-sessions/-/client-sessions-0.8.0.tgz#a7d8c5558ad5d56f2a199f3533eb654b5df893fd"
|
|
||||||
integrity sha1-p9jFVYrV1W8qGZ81M+tlS134k/0=
|
|
||||||
dependencies:
|
|
||||||
cookies "^0.7.0"
|
|
||||||
|
|
||||||
cliui@^6.0.0:
|
cliui@^6.0.0:
|
||||||
version "6.0.0"
|
version "6.0.0"
|
||||||
resolved "https://registry.yarnpkg.com/cliui/-/cliui-6.0.0.tgz#511d702c0c4e41ca156d7d0e96021f23e13225b1"
|
resolved "https://registry.yarnpkg.com/cliui/-/cliui-6.0.0.tgz#511d702c0c4e41ca156d7d0e96021f23e13225b1"
|
||||||
@@ -2178,14 +2171,6 @@ cookiejar@^2.1.0:
|
|||||||
resolved "https://registry.yarnpkg.com/cookiejar/-/cookiejar-2.1.2.tgz#dd8a235530752f988f9a0844f3fc589e3111125c"
|
resolved "https://registry.yarnpkg.com/cookiejar/-/cookiejar-2.1.2.tgz#dd8a235530752f988f9a0844f3fc589e3111125c"
|
||||||
integrity sha512-Mw+adcfzPxcPeI+0WlvRrr/3lGVO0bD75SxX6811cxSh1Wbxx7xZBGK1eVtDf6si8rg2lhnUjsVLMFMfbRIuwA==
|
integrity sha512-Mw+adcfzPxcPeI+0WlvRrr/3lGVO0bD75SxX6811cxSh1Wbxx7xZBGK1eVtDf6si8rg2lhnUjsVLMFMfbRIuwA==
|
||||||
|
|
||||||
cookies@^0.7.0:
|
|
||||||
version "0.7.3"
|
|
||||||
resolved "https://registry.yarnpkg.com/cookies/-/cookies-0.7.3.tgz#7912ce21fbf2e8c2da70cf1c3f351aecf59dadfa"
|
|
||||||
integrity sha512-+gixgxYSgQLTaTIilDHAdlNPZDENDQernEMiIcZpYYP14zgHsCt4Ce1FEjFtcp6GefhozebB6orvhAAWx/IS0A==
|
|
||||||
dependencies:
|
|
||||||
depd "~1.1.2"
|
|
||||||
keygrip "~1.0.3"
|
|
||||||
|
|
||||||
copy-descriptor@^0.1.0:
|
copy-descriptor@^0.1.0:
|
||||||
version "0.1.1"
|
version "0.1.1"
|
||||||
resolved "https://registry.yarnpkg.com/copy-descriptor/-/copy-descriptor-0.1.1.tgz#676f6eb3c39997c2ee1ac3a924fd6124748f578d"
|
resolved "https://registry.yarnpkg.com/copy-descriptor/-/copy-descriptor-0.1.1.tgz#676f6eb3c39997c2ee1ac3a924fd6124748f578d"
|
||||||
@@ -4363,11 +4348,6 @@ kareem@2.3.1:
|
|||||||
resolved "https://registry.yarnpkg.com/kareem/-/kareem-2.3.1.tgz#def12d9c941017fabfb00f873af95e9c99e1be87"
|
resolved "https://registry.yarnpkg.com/kareem/-/kareem-2.3.1.tgz#def12d9c941017fabfb00f873af95e9c99e1be87"
|
||||||
integrity sha512-l3hLhffs9zqoDe8zjmb/mAN4B8VT3L56EUvKNqLFVs9YlFA+zx7ke1DO8STAdDyYNkeSo1nKmjuvQeI12So8Xw==
|
integrity sha512-l3hLhffs9zqoDe8zjmb/mAN4B8VT3L56EUvKNqLFVs9YlFA+zx7ke1DO8STAdDyYNkeSo1nKmjuvQeI12So8Xw==
|
||||||
|
|
||||||
keygrip@~1.0.3:
|
|
||||||
version "1.0.3"
|
|
||||||
resolved "https://registry.yarnpkg.com/keygrip/-/keygrip-1.0.3.tgz#399d709f0aed2bab0a059e0cdd3a5023a053e1dc"
|
|
||||||
integrity sha512-/PpesirAIfaklxUzp4Yb7xBper9MwP6hNRA6BGGUFCgbJ+BM5CKBtsoxinNXkLHAr+GXS1/lSlF2rP7cv5Fl+g==
|
|
||||||
|
|
||||||
kind-of@^3.0.2, kind-of@^3.0.3, kind-of@^3.2.0:
|
kind-of@^3.0.2, kind-of@^3.0.3, kind-of@^3.2.0:
|
||||||
version "3.2.2"
|
version "3.2.2"
|
||||||
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.2.2.tgz#31ea21a734bab9bbb0f32466d893aea51e4a3c64"
|
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.2.2.tgz#31ea21a734bab9bbb0f32466d893aea51e4a3c64"
|
||||||
|
|||||||
Reference in new issue
Block a user