-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
148 lines (127 loc) · 4.92 KB
/
index.js
File metadata and controls
148 lines (127 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const express = require('express')
const bodyParser = require('body-parser')
const path = require('path')
const request = require('request-promise-native')
const app = express()
const port = process.env.PORT || 3000
const authURL = 'https://account.demandware.com/dw/oauth2/access_token'
const baseURL = (host) => `https://${host}/s/-/dw/meta/v1/rest`
const defaultClientID = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
const defaultClientSecret = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
var authToken = undefined
const defaultRequestOptions = url => {
return {
url: url,
method: 'GET',
headers: {
'Authorization': authToken
},
json: true
}
}
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')
app.use('/assets', express.static('assets'))
app.use(bodyParser.json());
app.get('/', (req, res) => res.render('index', {
googleAnalyticsTagID: process.env.GA_TAG_ID
}))
app.get('/manifest.json', (req, res) => res.sendFile(path.join(__dirname, '/src/manifest.json')))
app.get('/service-worker.js', (req, res) => res.sendFile(path.join(__dirname, '/src/service-worker.js')))
app.post('/apis', (req, res) => {
fetchEndpoints(
req.body.host,
req.body.client_id,
req.body.client_secret,
req.body.api,
req.body.apiVersion
).then(paths => res.send(JSON.stringify(paths))).catch(e => {
res.send(JSON.stringify({
success: false,
error: JSON.stringify(e)
}))
})
})
app.listen(port, () => console.log(`App started & now listening`))
function fetchEndpoints(host, clientId, clientSecret, apiName, apiVersion) {
const client_id = clientId || defaultClientID
const client_secret = clientSecret || defaultClientSecret
return new Promise((resolve, reject) => {
var promise = Promise.resolve()
promise = promise.then(() => authenticate(client_id, client_secret))
promise = promise.then(() => getAPI(host, apiName))
promise = promise.then(apiURL => getVersion(apiURL, apiVersion))
promise = promise.then(apiURL => getPaths(apiURL))
promise = promise.then(response => {
if (!response.paths) {
reject(`Failed to find paths in the ${apiName} API. Please ensure you allowed at least one endpoint for the ${client_id} client ID on the ${host} instance.`)
return
}
transformedPaths = {}
Object.keys(response.paths).forEach(pathKey => {
newPathKey = pathKey.replace(/{\w+}/gi, '*')
transformedPaths[newPathKey] = response.paths[pathKey]
})
response.success = true
response.paths = transformedPaths
resolve(response)
})
promise = promise.catch(err => {
console.log(err);
reject(err);
});
})
}
function authenticate(clientID, clientSecret) {
return new Promise((resolve, reject) => {
performRequest({
url: authURL,
method: 'POST',
headers: {
'Content-type': 'application/x-www-form-urlencoded',
Authorization: `Basic ${Buffer.from(clientID + ':' + clientSecret).toString('base64')}`
},
body: 'grant_type=client_credentials',
json: true
}).then(response => {
if (response.access_token) {
authToken = `${response.token_type} ${response.access_token}`
resolve(authToken)
return
}
reject(`Cannot authenticate with the ${defaultClientID} client ID and ${defaultClientSecret} client secret.`)
}).catch(e => reject(e))
})
}
function getAPI(host, apiName) {
return new Promise((resolve, reject) => {
performRequest(defaultRequestOptions(baseURL(host))).then(response => {
const api = response.apis.find(api => api.name === apiName)
if (api) {
resolve(api.link)
return
}
reject(`Cannot find the api ${apiName} on the ${host} instance. Please ensure you authorized it in the OCAPI Settings.`)
}).catch(e => reject(e))
})
}
function getVersion(apiURL, apiVersion) {
return new Promise((resolve, reject) => {
performRequest(defaultRequestOptions(apiURL)).then(response => {
const version = apiVersion
? response.versions.find(version => version.name === apiVersion || version.status === apiVersion)
: response.versions.find(version => version.status === 'current')
if (version) {
resolve(version.link)
return
}
reject(`Cannot find the version ${apiVersion} in the API ${apiURL}.`)
}).catch(e => reject(e))
})
}
function getPaths(apiURL) {
return performRequest(defaultRequestOptions(apiURL))
}
function performRequest(options) {
return request(options)
}