Created
August 22, 2015 13:57
-
-
Save anonymous/9804ee91ecf673245116 to your computer and use it in GitHub Desktop.
Meteor REST API MeteorChef dynamic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
API = { | |
handleRequest: function( context, resource, method ) { | |
var connection = API.connection( context.request, resource ); | |
if ( !connection.error ) { | |
API.methods[ 'execute' ][ method ]( context, connection, resource ); | |
} else { | |
API.utility.response( context, 401, connection ); | |
} | |
}, | |
connection: function( request, resource ) { | |
var getRequestContents = API.utility.getRequestContents( request ), | |
apiKey = getRequestContents.api_key, | |
validUser = API.authentication( apiKey, resource ); | |
if ( validUser ) { | |
delete getRequestContents.api_key; | |
return { createdBy: validUser, data: getRequestContents }; | |
} else { | |
return { error: 401, message: "Invalid API key." }; | |
} | |
}, | |
authentication: function( apiKey, resource ) { | |
var collectionName = AppFilters.formSchemaName(resource.app +' '+ resource.class); | |
var getUser = eval(collectionName).findOne( { "_id": apiKey }, { fields: { "createdBy": 1 } } ); //fields: { "owner": 1 } | |
if ( getUser ) { | |
return getUser.createdBy; //getuser.owner | |
} else { | |
return false; | |
} | |
}, | |
methods: { | |
execute: { | |
GET: function( context, connection, resource ) { | |
var hasQuery = API.utility.hasData( connection.data ); | |
if ( hasQuery ) { | |
connection.data.createdBy = connection.createdBy; | |
// console.log('connect data', connection.data); | |
var collectionName = AppFilters.formSchemaName(resource.app +' '+ resource.class); | |
var getRecords = eval(collectionName).find( connection.data ).fetch(); | |
if ( getRecords.length > 0 ) { | |
API.utility.response( context, 200, getRecords ); | |
} else { | |
API.utility.response( context, 404, { error: 404, message: "No records found." } ); | |
} | |
} else { | |
var getRecords = eval(collectionName).find( { "createdBy": connection.createdBy } ).fetch(); | |
API.utility.response( context, 200, getRecords ); | |
} | |
}, | |
POST: function( context, connection ) {}, | |
PUT: function( context, connection ) {}, | |
DELETE: function( context, connection ) {} | |
} | |
}, | |
resources: {}, | |
utility: { | |
getRequestContents: function( request ) { | |
switch( request.method ) { | |
case "GET": | |
return request.query; | |
case "POST": | |
case "PUT": | |
case "DELETE": | |
return request.body; | |
} | |
}, | |
hasData: function( data ) { | |
return Object.keys( data ).length > 0 ? true : false; | |
}, | |
response: function( context, statusCode, data ) { | |
context.response.setHeader( 'Content-Type', 'application/json' ); | |
context.response.statusCode = statusCode; | |
context.response.end( JSON.stringify( data[0] ) ); | |
}, | |
validate: function( data, pattern ) { | |
return Match.test( data, pattern ); | |
} | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Transforms string like "flow profiles" into "FlowProfiles" | |
* used to form collection / schema names for eval. | |
*/ | |
AppFilters.formSchemaName = function(string) | |
{ | |
String.prototype.schema = function(){ | |
return this.toLowerCase().replace( /\b\w/g, function (m) { | |
return m.toUpperCase(); | |
}).replace(/\s/g, ''); | |
}; | |
return string.schema(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Router.route( '/api/v1/:app?/:class?', function() { | |
this.response.setHeader( 'Access-Control-Allow-Origin', '*' ); | |
if ( this.request.method === "OPTIONS" ) { | |
this.response.setHeader( 'Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept' ); | |
this.response.setHeader( 'Access-Control-Allow-Methods', 'POST, PUT, GET, DELETE, OPTIONS' ); | |
this.response.end( 'Set OPTIONS.' ); | |
} else { | |
// API.handleRequest( this, 'pizzas', this.request.method ); | |
API.handleRequest( this, {app: this.params.app, class: this.params.class}, this.request.method ); | |
} | |
}, { where: 'server' } ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment