Skip to content

Commit

Permalink
add apiVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
Lan Vu committed Apr 27, 2022
1 parent 357be28 commit b01b3ae
Show file tree
Hide file tree
Showing 7 changed files with 672 additions and 1,035 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ node_modules/

# dotenv environment variables file
.env

.idea
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ const ShopifyConfigTypes = {
afterAuth: PropTypes.func.isRequired,
shopStore: PropTypes.object,
accessMode: PropTypes.oneOf(['offline', 'online']),
apiVersion: PropTypes.string
};

const defaults = {
shopStore: new MemoryStrategy(),
accessMode: 'offline'
accessMode: 'offline',
apiVersion: '2022-04'
};

module.exports = function shopify(shopifyConfig) {
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@topmonks/shopify-express-x",
"version": "1.0.3",
"version": "1.0.4",
"author": "Minh Phan",
"description": "Alternative for @shopify/express - Get up and running quickly with Express.js and the Shopify API.",
"keywords": [
Expand Down Expand Up @@ -32,18 +32,18 @@
"cookie-parser": "^1.4.5",
"express": "^4.17.1",
"express-http-proxy": "^1.6.2",
"express-session": "^1.17.1",
"node-fetch": "^2.6.0",
"express-session": "^1.17.2",
"node-fetch": "^2.6.1",
"prop-types": "^15.7.2",
"raw-body": "^2.4.1",
"url": "^0.11.0"
},
"devDependencies": {
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-prettier": "^3.4.0",
"find-free-port": "^2.0.0",
"jest": "^26.4.2",
"nock": "^13.0.4",
"prettier": "^2.1.0",
"jest": "^26.6.3",
"nock": "^13.1.1",
"prettier": "^2.3.2",
"prettier-check": "^2.0.0",
"supertest": "^4.0.2"
}
Expand Down
6 changes: 3 additions & 3 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ module.exports = function createRouter(shopifyConfig) {
'/api',
rawParser,
verifyApiCall,
shopifyApiProxy,
shopifyApiProxy(shopifyConfig),
);
router.use(shopifyGraphQLProxy());
router.use(shopifyGraphQLProxy(shopifyConfig));

return router;
};
Expand All @@ -33,4 +33,4 @@ function verifyApiCall(request, response, next) {

response.status(401).send();
return;
}
}
80 changes: 41 additions & 39 deletions routes/shopifyApiProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,53 +12,55 @@ const DISALLOWED_URLS = [
'/webhooks',
'/oauth',
];
const SHOPIFY_API_VERSION = process.env.SHOPIFY_API_VERSION || '2020-07';

module.exports = async function shopifyApiProxy(incomingRequest, response, next) {
const { query, method, path: pathname, body, session } = incomingRequest;
module.exports = function shopifyApiProxy(config) {
const {apiVersion} = config
return async function (incomingRequest, response, next) {
const { query, method, path: pathname, body, session } = incomingRequest;

if (session == null) {
console.error('A session middleware must be installed to use ApiProxy.')
response.status(401).send(new Error('Unauthorized'));
return;
}
if (session == null) {
console.error('A session middleware must be installed to use ApiProxy.')
response.status(401).send(new Error('Unauthorized'));
return;
}

const { shop, accessToken } = session;
const { shop, accessToken } = session;

if (shop == null || accessToken == null) {
response.status(401).send(new Error('Unauthorized'));
return;
}
if (shop == null || accessToken == null) {
response.status(401).send(new Error('Unauthorized'));
return;
}

if (!validRequest(pathname)) {
response.status(403).send('Endpoint not in whitelist');
return;
}
if (!validRequest(pathname)) {
response.status(403).send('Endpoint not in whitelist');
return;
}

try {
const searchParams = querystring.stringify(query);
const searchString = searchParams.length > 0
? `?${searchParams}`
: '';
try {
const searchParams = querystring.stringify(query);
const searchString = searchParams.length > 0
? `?${searchParams}`
: '';

const url = `https://${shop}/admin/api/${SHOPIFY_API_VERSION}${pathname}${searchString}`;
const options = {
method,
headers: {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': accessToken,
},
}
if (method !== 'GET' && method !== 'HEAD') {
options.body = body
}
const result = await fetch(url, options);
const url = `https://${shop}/admin/api/${apiVersion}${pathname}${searchString}`;
const options = {
method,
headers: {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': accessToken,
},
}
if (method !== 'GET' && method !== 'HEAD') {
options.body = body
}
const result = await fetch(url, options);

const data = await result.json();
response.status(result.status).json(data);
} catch (error) {
console.log(error);
response.status(500).json(error);
const data = await result.json();
response.status(result.status).json(data);
} catch (error) {
console.log(error);
response.status(500).json(error);
}
}
};

Expand Down
10 changes: 5 additions & 5 deletions routes/shopifyGraphQLProxy.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const proxy = require('express-http-proxy');

const PROXY_BASE_PATH = '/graphql';
const GRAPHQL_PATH = '/admin/api/graphql.json';
// const GRAPHQL_PATH = '/admin/api/graphql.json';

module.exports = function shopifyGraphQLProxy() {
module.exports = function shopifyGraphQLProxy(configs) {
const {apiVersion} = configs
return function shopifyGraphQLProxyMiddleware(req, res, next) {
const { session: { shop, accessToken } } = req;

Expand All @@ -15,6 +16,8 @@ module.exports = function shopifyGraphQLProxy() {
return res.status(403).send('Unauthorized');
}

const GRAPHQL_PATH = `https://${shop}/admin/api/${apiVersion}/graphql.json`;

proxy(shop, {
https: true,
parseReqBody: false,
Expand All @@ -29,6 +32,3 @@ module.exports = function shopifyGraphQLProxy() {
})(req, res, next);
}
};

module.exports.PROXY_BASE_PATH = PROXY_BASE_PATH;
module.exports.GRAPHQL_PATH = GRAPHQL_PATH;
Loading

0 comments on commit b01b3ae

Please sign in to comment.