Skip to content

Instantly share code, notes, and snippets.

@astrotars
Created February 21, 2018 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astrotars/138a60d3215a5bf32b881f777eb408bf to your computer and use it in GitHub Desktop.
Save astrotars/138a60d3215a5bf32b881f777eb408bf to your computer and use it in GitHub Desktop.
// import npm modules
import axios from 'axios';
import jwt from 'jsonwebtoken';
// import custom utilities
import config from '../../config';
const personalization = data => {
// setup promise
return new Promise((resolve, reject) => {
// build jwt for signing the API call
const token = jwt.sign(
{
action: '*',
feed_id: '*',
resource: '*',
user_id: '*',
},
config.stream.apiSecret,
{ algorithm: 'HS256', noTimestamp: true },
);
// initiate call via axios (http module)
return axios({
baseURL: config.stream.baseUrl,
headers: {
Authorization: token,
'Content-Type': 'application/json',
'Stream-Auth-Type': 'jwt',
},
method: 'GET',
params: {
api_key: config.stream.apiKey,
user_id: data.userId,
},
url: data.endpoint,
})
.then(res => {
// map over results and deserialize
const data = res.data.results.map(result => {
return result.foreign_id.split(':')[1];
});
// successfully resolve call and return deserialized data
resolve(data);
})
.catch(err => {
// catch and reject with error
reject(err);
});
});
};
export default personalization;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment