Comparing version 0.0.1 to 0.0.2
@@ -6,3 +6,3 @@ 'use strict'; | ||
headers: { | ||
Authorization: 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfaWQiOiI1NDdkZWJkNWU3M2M3MDBiMDA5NTk4ZWQiLCJpYXQiOjE0NjI1NzIxMTIsImV4cCI6MTQ2MzE3NjkxMn0.Re4VCn9Q1ZhYLRXP3DNJg5k3iEUmocfP8DjLlOvta8E' | ||
Authorization: 'Bearer foobar.foobar.foobar' | ||
} | ||
@@ -9,0 +9,0 @@ }); |
@@ -29,3 +29,4 @@ | ||
exports[method] = (url, options={}) => { | ||
options[method] = method.toUpperCase(); | ||
options.method = method.toUpperCase(); | ||
console.log(options) | ||
return jsonFetch(url, options); | ||
@@ -32,0 +33,0 @@ } |
{ | ||
"name": "cat-nap", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "", | ||
"main": "restClient.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "./node_modules/.bin/mocha" | ||
}, | ||
@@ -16,2 +16,3 @@ "author": "", | ||
"dependencies": { | ||
"inflection": "^1.10.0", | ||
"lodash": "^4.12.0", | ||
@@ -18,0 +19,0 @@ "node-fetch": "^1.5.2", |
const jsonFetch = require('./jsonFetch'); | ||
const inflection = require('inflection'); | ||
const _ = require('lodash'); | ||
jsonFetch.config({ | ||
@@ -9,28 +11,46 @@ credentials: 'include' | ||
paths: Symbol('paths'), | ||
getters: Symbol('getters'), | ||
dirtyList: Symbol('dirtyList') | ||
}; | ||
/** 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 */ | ||
const buildFromArray = function(arr, parent) { | ||
return arr.map(buildFromObj.bind(this)); | ||
}; | ||
const buildFromObj = function(obj, parent) { | ||
return new this(obj, parent); | ||
}; | ||
/** end private statics */ | ||
class RestClient { | ||
constructor(obj) { | ||
// Object.assign(this, obj); | ||
constructor(obj, parent) { | ||
this.parent = parent; | ||
// this is a private hashmap of strings for a path to their symbols | ||
this[symbols.paths] = new Map(); | ||
this[symbols.getters] = new Map(); | ||
// set of paths that have changed | ||
this[symbols.dirtyList] = new Set(); | ||
this.buildSchema(); | ||
buildSchema.call(this); | ||
this.merge(obj); | ||
} | ||
addPath(path, schemaPath) { | ||
const sym = Symbol(path); | ||
this[symbols.paths].set(path, sym); | ||
Object.defineProperty(this, path, { | ||
get: () => { | ||
return this[sym]; | ||
} | ||
}); | ||
} | ||
merge(changes) { | ||
@@ -41,5 +61,5 @@ Object.keys(changes) | ||
let keySym = this[symbols.paths].get(key); | ||
if(!keySym && ) { | ||
if(!keySym) { | ||
if(!this.constructor.schema.strict) { | ||
this.addPath(key); | ||
addPath.call(this,key); | ||
keySym = this[symbols.paths].get(key); | ||
@@ -51,7 +71,15 @@ } else { return; } | ||
}); | ||
return this; | ||
} | ||
buildSchema() { | ||
for(let [path, schemaPath] of this.constructor.schema) { | ||
this.addPath(path, schemaPath); | ||
get primaryIdentifier() { | ||
return this[this.constructor.schema.primary]; | ||
} | ||
get parentUrl() { | ||
if(this.parent) { | ||
return this.parent.url; | ||
} else { | ||
return this.constructor.path; | ||
} | ||
@@ -61,3 +89,10 @@ } | ||
get url() { | ||
return this.constructor.path + '/' + this[this.constructor.schema.primary] | ||
let subRoute; | ||
if(this.constructor.parents) { | ||
subroute = this.constructor.parents.get(this.parent.constructor); | ||
} | ||
return [this.parentUrl,subRoute,this.primaryIdentifier] | ||
.filter(part => !!part) | ||
.join('/'); | ||
} | ||
@@ -87,17 +122,17 @@ | ||
// map(fn) { | ||
// return this.reduce((previous, strKey, current) => fn(strKey, current)); | ||
// } | ||
forEach(fn) { | ||
this.map(fn.bind(this)); | ||
} | ||
update(changes={}) { | ||
this.merge(changes) | ||
if(!this.primaryIdentifier) { throw new Error('cannot update document without id. try calling create'); } | ||
this.merge(changes); | ||
const diff = this.dirtyValues; | ||
this.dirtyList.clear(); | ||
return this.constructor.put(this.url, { body: diff }); | ||
return this.constructor.put(this.url, { body: diff }) | ||
.then(resp => this.merge(resp)); | ||
} | ||
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)) | ||
} | ||
delete() { | ||
@@ -107,25 +142,44 @@ return this.constructor.delete(this.url); | ||
static buildFromArray(arr) { | ||
console.log(arr); | ||
return arr.map(this.buildFromObj.bind(this)); | ||
} | ||
static buildFromObj(obj) { | ||
return new this(obj); | ||
} | ||
static find(query) { | ||
static find(query={}) { | ||
if(!!this.parent) { | ||
throw new Error(`cannot find on nested route`) | ||
} | ||
return jsonFetch.get(this.path) | ||
.then(this.buildFromArray.bind(this)); | ||
.then(buildFromArray.bind(this)); | ||
} | ||
static findById(id) { | ||
if(!!this.parent) { | ||
throw new Error(`cannot find by id on nested route`) | ||
} | ||
return jsonFetch.get(`${this.path}/${id}`) | ||
.then(this.buildFromObj.bind(this)); | ||
.then(buildFromObj.bind(this)); | ||
} | ||
static create(obj) { | ||
static create(obj={}) { | ||
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 }) | ||
.then(this.buildFromObj.bind(this)); | ||
.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); | ||
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)); | ||
}; | ||
this.prototype[`create${singularName}`] = function(obj={}) { | ||
return jsonFetch.post(`${this.url}/${route}`, { body: obj }) | ||
.then(res => buildFromObj.call(otherClass, res, this)) | ||
} | ||
} | ||
} | ||
@@ -132,0 +186,0 @@ |
@@ -5,5 +5,6 @@ 'use strict'; | ||
class Schema extends Map { | ||
class Schema { | ||
constructor(props={}, config={}) { | ||
super(); | ||
this.data = new Map(); | ||
this[Symbol.iterator] = this.data[Symbol.iterator].bind(this.data); | ||
Object.keys(props).forEach(key => this.set(key, props[key])); | ||
@@ -15,6 +16,10 @@ Object.assign(this, config); | ||
if(val.primary) { this.primary = key; } | ||
super.set(key, new SchemaPath(val)); | ||
this.data.set(key, new SchemaPath(val)); | ||
} | ||
get(key) { | ||
return this.data.get(key); | ||
} | ||
} | ||
module.exports = Schema; |
@@ -0,1 +1,2 @@ | ||
'use strict'; | ||
const jsonFetch = require('../jsonFetch'); | ||
@@ -2,0 +3,0 @@ const fetch = require('../fetch'); |
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
19395
20
604
2
4
+ Addedinflection@^1.10.0
+ Addedinflection@1.13.4(transitive)