New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

cat-nap

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cat-nap - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

index.js

181

endpoint.js

@@ -1,28 +0,7 @@

const jsonFetch = require('./jsonFetch');
const inflection = require('inflection');
const _ = require('lodash');
const Server = require('./server');
// const inflection = require('inflection');
jsonFetch.config({
credentials: 'include'
});
const Model = require('./model');
/** Private instance methods */
const addPath = function(path, schemaPath) {
const sym = Symbol(path);
this[symbols.paths].set(path, sym);
Object.defineProperty(this, path, {
get: () => {
return this[sym];
}
});
};
const buildSchema = function() {
for(let [path, schemaPath] of this.constructor.schema) {
addPath.call(this, path, schemaPath);
}
}
/** /end private instance methods */
/** private statics */

@@ -36,54 +15,20 @@ const buildFromArray = function(arr, parent) {

return new this(obj, parent);
};
};
/** end private statics */
const symbols = {
paths: Symbol('paths'),
dirtyList: Symbol('dirtyList'),
parent: Symbol('parent'),
cache: Symbol('cache')
};
class Endpoint {
constructor(obj, parent) {
this[symbols.parent] = parent;
// this is a private hashmap of strings for a path to their symbols
this[symbols.paths] = new Map();
// set of paths that have changed
this[symbols.dirtyList] = new Set();
buildSchema.call(this);
this.merge(obj);
constructor(obj, parent={}) {
this.parent = parent;
this.data = new this.constructor.model(obj, parent.model);
}
get parentDoc() {
return this[symbols.parent];
get id() {
return this.data[primaryIdentifier];
}
merge(changes) {
Object.keys(changes)
.forEach(key => {
const change = changes[key];
let keySym = this[symbols.paths].get(key);
if(!keySym) {
if(!this.constructor.schema.strict) {
addPath.call(this,key);
keySym = this[symbols.paths].get(key);
} else { return; }
}
this[keySym] = change;
this[symbols.dirtyList].add(key)
});
return this;
}
get primaryIdentifier() {
return this[this.constructor.schema.primary];
}
get parentUrl() {
if(!!this[symbols.parent]) {
return this[symbols.parent].url;
if(!!this.parent) {
return this.parent.url;
} else {

@@ -97,5 +42,5 @@ return this.constructor.path;

if(this.constructor.parents) {
subRoute = this.constructor.parents.get(this[symbols.parent].constructor);
subRoute = this.constructor.parents.get(this.parent.constructor);
}
return [this.parentUrl,subRoute,this.primaryIdentifier]
return [this.parentUrl,subRoute,this.id]
.filter(part => !!part)

@@ -105,38 +50,16 @@ .join('/');

get dirtyValues() {
const obj = {};
for(let key of this[symbols.dirtyList]) {
obj[key] = this[key];
}
return _.merge({}, obj);
}
get values() {
return _.merge({},this.reduce((holdOver, key, val) => {
holdOver[key] = val;
return holdOver;
}, {}));
}
reduce(fn, startingValue) {
for(let [str, sym] of this[symbols.paths]) {
startingValue = fn.call(this, startingValue, str, this[sym])
}
return startingValue;
}
update(changes={}) {
if(!this.primaryIdentifier) { throw new Error('cannot update document without id. try calling create'); }
this.merge(changes);
const diff = this.dirtyValues;
this[symbols.dirtyList].clear();
console.log(this.url)
return jsonFetch.put(this.url, { body: diff })
.then(resp => this.merge(resp));
if(!this.id) { throw new Error('cannot update document without id. try calling create'); }
this.data.merge(changes);
const diff = this.data.dirtyValues;
return this.constructor.server.put(this.url, diff)
.then(resp => this.data.merge(resp))
.then(() => this.data.clearDirtyList());
}
create() {
if(!!this.primaryIdentifier) { throw new Error('cannot recreate document that already has identifier'); }
return this.constructor.create(this.values)
.then(resp => this.merge(resp))
if(!!this.id) { throw new Error('cannot recreate document that already has identifier'); }
return this.constructor.create(this.data.values)
.then(resp => this.data.merge(resp))
.then(() => this.data.clearDirtyList());
}

@@ -148,15 +71,21 @@

static find(query={}) {
if(!!this[symbols.parent]) {
throw new Error(`cannot find on nested route`)
if(!!this.parent) {
throw new Error(`cannot find on nested route`);
}
return jsonFetch.get(this.path)
return this.server.get(this.path)
.then(buildFromArray.bind(this));
}
static delete(url) {
return this.server.delete(url);
}
static findById(id) {
if(!!this[symbols.parent]) {
throw new Error(`cannot find by id on nested route`)
if(!!this.parent) {
throw new Error(`cannot find by id on nested route`);
}
return jsonFetch.get(`${this.path}/${id}`)
return this.server.get(`${this.path}/${id}`)
.then(buildFromObj.bind(this));

@@ -166,30 +95,30 @@ }

static create(obj={}) {
if(!!this[symbols.parent]) {
if(!!this.parent) {
throw new Error(`cannot create ${this.name} with create method because ${this.name} is a subclass. call create from a parent.`)
}
return jsonFetch.post(this.path, { body: obj })
return this.server.post(this.path, obj)
.then(buildFromObj.bind(this));
}
static nest(otherClass, route) {
route = route || inflection.pluralize(otherClass.name).toLowerCase();
// parents is a map of the class to its mounted route
otherClass.parents = otherClass.parents || new Map();
otherClass.parents.set(this, route);
// static nest(otherClass, route) {
// route = route || inflection.pluralize(otherClass.name).toLowerCase();
// // parents is a map of the class to its mounted route
// otherClass.parents = otherClass.parents || new Map();
// otherClass.parents.set(this, route);
const pluralName = inflection.pluralize(otherClass.name);
const singularName = otherClass.name;
this.prototype[`get${pluralName}`] = function() {
return jsonFetch.get(`${this.url}/${route}`)
.then(resArr => buildFromArray.call(otherClass, resArr, this));
};
// const pluralName = inflection.pluralize(otherClass.name);
// const singularName = otherClass.name;
// this.prototype[`get${pluralName}`] = function() {
// return this.server.get(`${this.url}/${route}`)
// .then(resArr => buildFromArray.call(otherClass, resArr, this));
// };
this.prototype[`create${singularName}`] = function(obj={}) {
return jsonFetch.post(`${this.url}/${route}`, { body: obj })
.then(res => buildFromObj.call(otherClass, res, this))
}
}
// this.prototype[`create${singularName}`] = function(obj={}) {
// return this.server.post(`${this.url}/${route}`, obj )
// .then(res => buildFromObj.call(otherClass, res, this))
// }
// }
}
module.exports = Endpointend;
module.exports = Endpoint;
module.exports.Schema = require('./schema');

@@ -1,37 +0,37 @@

const _ = require('lodash');
class EndpointCache {
constructor() {
// map of lamdas => cache maps
this.data = new Map();
// const _ = require('lodash');
// class EndpointCache {
// constructor() {
// // map of lamdas => cache maps
// this.data = new Map();
// map of labels => lamdas
this.lamdas = new Map();
}
// // map of labels => lamdas
// this.lamdas = new Map();
// }
createCache(label, lamda, unique=false) {
this.lamdas.set(label, lamda);
this.data.set(lamda, new Map());
this.records.forEach(record => {
this.cacheDoc(lamda, doc, unique)
});
// createCache(label, lamda, unique=false) {
// this.lamdas.set(label, lamda);
// this.data.set(lamda, new Map());
// this.records.forEach(record => {
// this.cacheDoc(lamda, doc, unique)
// });
}
// }
cacheDoc(lamda, doc, unique) {
const cache = this.data.get(lamda);
const bucket = lamda.call(this, record);
if(unique) {
cache.set(bucket, record);
} else {
if(!cache.has(bucket)) {
cache.set(bucket, new Set());
}
cache.get(bucket).add(record);
}
}
// cacheDoc(lamda, doc, unique) {
// const cache = this.data.get(lamda);
// const bucket = lamda.call(this, record);
// if(unique) {
// cache.set(bucket, record);
// } else {
// if(!cache.has(bucket)) {
// cache.set(bucket, new Set());
// }
// cache.get(bucket).add(record);
// }
// }
add(doc) {
// add(doc) {
}
}
// }
// }
'use strict';
const jsonFetch = require('./jsonFetch');
jsonFetch.config({
headers: {
Authorization: 'Bearer foobar.foobar.foobar'
}
});
global.fetch = require('node-fetch');
const RestClient = require('./restClient');
const Server = require('./server');
const server = new Server('http://localhost:4001');
// server.config({
// headers: {
// Authorization: 'Bearer foobar.foobar.foobar'
// }
// });
class Cohort extends RestClient {}
Cohort.path = 'https://learn.fullstackacademy.com/api/cohorts';
Cohort.schema = new RestClient.Schema({
_id: { type: String, primary: true, validators: ['required'] },
name: { type: String, validators: ['required'] }
const Model = require('./model');
class Problem extends Model {
};
const Schema = require('./schema');
Problem.schema = new Schema({
id: { type: Number, primary: true, validators: ['required'] },
title: { type: String, validators: ['required'] }
}, {

@@ -24,7 +30,28 @@ // this makes it impossible to add things not in the schema

Cohort.findById('572d1079306d520300575068')
.then(cohort => console.log(cohort.json))
.catch(console.error);
const Endpoint = require('./endpoint');
class ProblemEndpoint extends Endpoint {}
ProblemEndpoint.model = Problem;
ProblemEndpoint.server = server;
ProblemEndpoint.path = '/api/problems';
server.post('/api/sessions', { email: 'email@email.com', password: 'this is a test'})
.then(jwt => {
console.log(jwt);
server.config({ headers: { Authorization: jwt} })
})
.then(() => {
return ProblemEndpoint.create({
title: 'foobar'
})
})
.then(console.log).catch(console.error);
// Problem.findById(1)
// .then(cohort => console.log(cohort.json))
// .catch(console.error);
// Cohort.find()
// .then(console.log)
// .catch(console.error);
{
"name": "cat-nap",
"version": "0.0.2",
"version": "0.0.3",
"description": "",
"main": "restClient.js",
"main": "index.js",
"scripts": {

@@ -17,2 +17,3 @@ "test": "./node_modules/.bin/mocha"

"inflection": "^1.10.0",
"json-fetcher": "^1.0.3",
"lodash": "^4.12.0",

@@ -19,0 +20,0 @@ "node-fetch": "^1.5.2",

@@ -10,3 +10,2 @@ 'use strict';

describe('constructor', () => {
it('is a map', () => assert(schema instanceof Map));

@@ -13,0 +12,0 @@ it('makes schema paths with what is passed', () => {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc