Skip to content

Commit

Permalink
4.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
chasenlehara committed Mar 14, 2018
1 parent 25dd841 commit 02ebafa
Show file tree
Hide file tree
Showing 11 changed files with 970 additions and 0 deletions.
13 changes: 13 additions & 0 deletions dist/amd/can-connect-feathers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*can-connect-feathers@4.0.0#can-connect-feathers*/
define([
'require',
'exports',
'module',
'./service/service',
'./session/session'
], function (require, exports, module) {
module.exports = {
service: require('./service/service'),
session: require('./session/session')
};
});
71 changes: 71 additions & 0 deletions dist/amd/service/service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*can-connect-feathers@4.0.0#service/service*/
define([
'require',
'exports',
'module',
'can-connect'
], function (require, exports, module) {
var connect = require('can-connect');
function getIdProp(Model) {
var algebraIdProp;
var algebraClause = Model.algebra && Model.algebra.clauses && Model.algebra.clauses.id;
if (algebraClause) {
algebraIdProp = Object.keys(algebraClause)[0];
}
if (!algebraIdProp && !Model.idProp) {
throw new Error('An idProp was not set in the Model for ' + Model + '. Things may not work as expected.');
}
return algebraIdProp || Model.idProp;
}
module.exports = connect.behavior('data/feathers-service', function (base) {
var helpURL = 'https://canjs.com/doc/can-connect-feathers.html';
if (!this.feathersService) {
throw new Error('You must provide a feathersService to the feathers-service behavior: ' + helpURL);
}
var service = this.feathersService;
return {
init: function () {
base.init.apply(this, arguments);
var self = this;
service.on('created', function (message) {
self.createInstance(message);
});
service.on('updated', function (message) {
self.updateInstance(message);
});
service.on('patched', function (message) {
self.updateInstance(message);
});
service.on('removed', function (message) {
self.destroyInstance(message);
});
},
getListData: function (params) {
return service.find({ query: params });
},
getData: function (params) {
var id = null;
var idProp = getIdProp(this);
if (typeof params === 'string' || typeof params === 'number') {
id = params;
params = {};
} else if (params && typeof params[idProp] !== 'undefined') {
id = params[idProp];
delete params[idProp];
}
return service.get(id, params);
},
createData: function (data) {
return service.create(data);
},
updateData: function (instance) {
var idProp = getIdProp(this);
return service.update(instance[idProp], instance);
},
destroyData: function (instance) {
var idProp = getIdProp(this);
return service.remove(instance[idProp]);
}
};
});
});
117 changes: 117 additions & 0 deletions dist/amd/session/session.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*can-connect-feathers@4.0.0#session/session*/
define([
'require',
'exports',
'module',
'can-connect',
'feathers-errors',
'feathers-authentication-popups',
'jwt-decode',
'../utils/utils',
'../utils/utils',
'../utils/utils',
'can-observation',
'./storage'
], function (require, exports, module) {
var connect = require('can-connect');
var errors = require('feathers-errors');
var authAgent = require('feathers-authentication-popups').authAgent;
var decode = require('jwt-decode');
var payloadIsValid = require('../utils/utils').payloadIsValid;
var hasValidToken = require('../utils/utils').hasValidToken;
var convertLocalAuthData = require('../utils/utils').convertLocalAuthData;
var Observation = require('can-observation');
var zoneStorage = require('./storage');
module.exports = connect.behavior('data/feathers-session', function (base) {
var helpURL = 'https://canjs.com/doc/can-connect-feathers.html';
var feathersClient = this.feathersClient;
if (!feathersClient) {
throw new Error('You must provide a feathersClient instance to the feathers-session behavior. See ' + helpURL);
}
if (!this.Map) {
throw new Error('You must provide a Map instance to the feathers-session behavior. See ' + helpURL);
}
if (!feathersClient.passport) {
throw new Error('You must register the feathers-authentication-client plugin before using the feathers-session behavior. See ' + helpURL);
}
var options = feathersClient.passport.options;
var Session = this.Map;
Object.defineProperty(Session, 'current', {
get: function () {
Observation.add(Session, 'current');
if (zoneStorage.getItem('can-connect-feathers-session') === undefined) {
zoneStorage.removeItem('can-connect-feathers-session');
Session.get().then(function (session) {
zoneStorage.setItem('can-connect-feathers-session', session);
Session.dispatch('current', [session]);
}).catch(function (error) {
zoneStorage.setItem('can-connect-feathers-session', null);
Session.dispatch('current', [null]);
if (!error.className || error.className.indexOf('not-authenticated') < 0) {
return Promise.reject(error);
}
});
}
return zoneStorage.getItem('can-connect-feathers-session');
}
});
Session.on('created', function (ev, session) {
zoneStorage.setItem('can-connect-feathers-session', session);
Session.dispatch('current', [session]);
});
Session.on('destroyed', function () {
zoneStorage.removeItem('can-connect-feathers-session');
Session.dispatch('current', [
undefined,
zoneStorage.getItem('can-connect-feathers-session')
]);
});
return {
init: function () {
base.init.apply(this, arguments);
var connection = this;
authAgent.on('login', function (token) {
try {
var payload = decode(token);
if (!payloadIsValid(payload)) {
throw new Error('invalid token');
}
} catch (error) {
throw new Error('An invalid token was received through the feathers-authentication-popups authAgent');
}
feathersClient.authenticate({
strategy: 'jwt',
accessToken: token
}).then(function (data) {
var payload = decode(data.accessToken);
connection.createInstance(payload);
});
});
},
createData: function (data) {
var requestData = convertLocalAuthData(data);
return feathersClient.authenticate(requestData).then(function (response) {
return response.accessToken ? decode(response.accessToken) : response;
});
},
getData: function () {
return new Promise(function (resolve, reject) {
var tokenLocation = options.tokenKey || options.cookie;
if (hasValidToken(tokenLocation) && !window.doneSsr) {
feathersClient.authenticate().then(function (data) {
var payload = decode(data.accessToken);
return resolve(payload);
}).catch(reject);
} else {
reject(new errors.NotAuthenticated('Not Authenticated'));
}
});
},
destroyData: function (session) {
return feathersClient.logout().then(function () {
return session;
});
}
};
});
});
25 changes: 25 additions & 0 deletions dist/amd/session/storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*can-connect-feathers@4.0.0#session/storage*/
define(function (require, exports, module) {
module.exports = {
data: {},
getStore: function () {
if (window.doneSsr) {
var CanZone = window.CanZone || undefined;
return typeof CanZone === 'undefined' ? this.data : CanZone.current.data;
}
return this.data;
},
setItem: function (prop, value) {
var store = this.getStore();
store[prop] = value;
},
getItem: function (prop) {
var store = this.getStore();
return store[prop];
},
removeItem: function (prop) {
var store = this.getStore();
delete store[prop];
}
};
});
64 changes: 64 additions & 0 deletions dist/amd/utils/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*can-connect-feathers@4.0.0#utils/utils*/
define([
'require',
'exports',
'module',
'jwt-decode',
'can-util/js/assign'
], function (require, exports, module) {
var decode = require('jwt-decode');
var assign = require('can-util/js/assign');
function readCookie(name) {
var nameEQ = name + '=';
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) === 0) {
return c.substring(nameEQ.length, c.length);
}
}
return null;
}
function getStoredToken(storageLocation) {
var token = readCookie(storageLocation);
if (!token && (window && window.localStorage || window.sessionStorage)) {
token = window.sessionStorage.getItem(storageLocation) || window.localStorage.getItem(storageLocation);
}
return token;
}
function payloadIsValid(payload) {
return payload && payload.exp * 1000 > new Date().getTime();
}
function hasValidToken(storageLocation) {
var token = getStoredToken(storageLocation);
if (token) {
try {
var payload = decode(token);
return payloadIsValid(payload);
} catch (error) {
return false;
}
}
return false;
}
function convertLocalAuthData(originalData) {
var data = assign({}, originalData);
if (data && data.strategy === 'local' && data.user) {
Object.keys(data.user).forEach(function (key) {
data[key] = data.user[key];
});
delete data.user;
}
return data;
}
module.exports = {
readCookie: readCookie,
getStoredToken: getStoredToken,
hasValidToken: hasValidToken,
payloadIsValid: payloadIsValid,
convertLocalAuthData: convertLocalAuthData
};
});
5 changes: 5 additions & 0 deletions dist/cjs/can-connect-feathers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*can-connect-feathers@4.0.0#can-connect-feathers*/
module.exports = {
service: require('./service/service.js'),
session: require('./session/session.js')
};
64 changes: 64 additions & 0 deletions dist/cjs/service/service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*can-connect-feathers@4.0.0#service/service*/
var connect = require('can-connect');
function getIdProp(Model) {
var algebraIdProp;
var algebraClause = Model.algebra && Model.algebra.clauses && Model.algebra.clauses.id;
if (algebraClause) {
algebraIdProp = Object.keys(algebraClause)[0];
}
if (!algebraIdProp && !Model.idProp) {
throw new Error('An idProp was not set in the Model for ' + Model + '. Things may not work as expected.');
}
return algebraIdProp || Model.idProp;
}
module.exports = connect.behavior('data/feathers-service', function (base) {
var helpURL = 'https://canjs.com/doc/can-connect-feathers.html';
if (!this.feathersService) {
throw new Error('You must provide a feathersService to the feathers-service behavior: ' + helpURL);
}
var service = this.feathersService;
return {
init: function () {
base.init.apply(this, arguments);
var self = this;
service.on('created', function (message) {
self.createInstance(message);
});
service.on('updated', function (message) {
self.updateInstance(message);
});
service.on('patched', function (message) {
self.updateInstance(message);
});
service.on('removed', function (message) {
self.destroyInstance(message);
});
},
getListData: function (params) {
return service.find({ query: params });
},
getData: function (params) {
var id = null;
var idProp = getIdProp(this);
if (typeof params === 'string' || typeof params === 'number') {
id = params;
params = {};
} else if (params && typeof params[idProp] !== 'undefined') {
id = params[idProp];
delete params[idProp];
}
return service.get(id, params);
},
createData: function (data) {
return service.create(data);
},
updateData: function (instance) {
var idProp = getIdProp(this);
return service.update(instance[idProp], instance);
},
destroyData: function (instance) {
var idProp = getIdProp(this);
return service.remove(instance[idProp]);
}
};
});
Loading

0 comments on commit 02ebafa

Please sign in to comment.