Skip to content

Commit 25ed1a5

Browse files
Merge pull request #2 from VisualGuruz/hapi-plugin
Hapi plugin
2 parents f925845 + 18e309b commit 25ed1a5

File tree

11 files changed

+81
-68
lines changed

11 files changed

+81
-68
lines changed

index.js

+25-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
1-
module.exports = {
2-
server: require('./lib/server'),
3-
serializers: require('./lib/serializers'),
4-
decorator: require('./lib/decorator')
1+
var pkg = require('./package.json'),
2+
server = require('./lib/server'),
3+
decorator = require('./lib/decorator');
4+
5+
module.exports = function (orm) {
6+
var plugin = {
7+
register: function register (plugin, options, next) {
8+
9+
console.info(plugin);
10+
11+
// Setup the server
12+
server(orm, plugin);
13+
14+
next();
15+
}
16+
};
17+
18+
// Decorate the ORM
19+
orm.decorate(decorator);
20+
21+
// Add the attributes to the plugin
22+
plugin.register.attributes = {pkg: pkg}
23+
24+
// Return the plugin
25+
return plugin;
526
};

lib/serializers/hal.js

+20-19
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
var _ = require('underscore');
22

3-
var baseUrl, api = {};
3+
module.exports = function (request) {
4+
console.info(request);
45

5-
module.exports = function (url) {
6-
baseUrl = url;
6+
var api = {
7+
serialize: function serialize(obj) {
8+
var baseUrl = request.server.info.uri,
9+
data = obj.getRawData(),
10+
links = {
11+
'_links': {
12+
'self': {
13+
'href': baseUrl+obj.getSelfLink()
14+
}
15+
}
16+
};
717

8-
return api;
9-
};
18+
return _.extend({}, data, links);
19+
},
1020

11-
api.serialize = function serialize(obj) {
12-
var data = obj.getRawData(),
13-
links = {
14-
'_links': {
15-
'self': {
16-
'href': baseUrl+obj.getSelfLink()
17-
}
18-
}
19-
};
21+
type: function type() {
22+
return 'application/json';
23+
}
24+
};
2025

21-
return _.extend({}, data, links);
26+
return api;
2227
};
23-
24-
api.type = function type() {
25-
return 'application/json';
26-
};

lib/server/handlers/base.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ var _ = require('underscore'),
22
Hapi = require('hapi'),
33
queryParser = require('./../query');
44

5-
var orm, serializer, parsedSchema;
5+
var orm, parsedSchema;
66

7-
module.exports = function (ormModule, serializerModule) {
7+
module.exports = function (ormModule) {
88
orm = ormModule;
9-
serializer = serializerModule;
109

1110
return handler;
1211
};

lib/server/handlers/delete.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function handler (request, reply) {
2323
// Get the base model
2424
model = orm.factory(modelSchema.getName());
2525

26-
if (primaryKey !== '') {
26+
if ( ! _.isEmpty(primaryKey)) {
2727
// We're updating something!
2828
model.find(primaryKey).then(function (data) {
2929
// Found nothing! Bail!

lib/server/handlers/get.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@ var _ = require('underscore'),
33
queryParser = require('./../query'),
44
RSVP = require('rsvp');
55

6-
var orm, serializer;
6+
var orm, Serializer;
77

88
module.exports = function (ormModule, serializerModule) {
99
orm = ormModule;
10-
serializer = serializerModule;
10+
Serializer = serializerModule;
1111

1212
return handler;
1313
};
1414

1515
function handler (request, reply) {
1616
var modelSchema = orm.getSchema().getModel(request.params.model),
1717
primaryKey = request.params.id,
18-
model;
18+
model,
19+
serializer = Serializer(request);
1920

2021
// Make sure the model exists, throw 404 if not
2122
if ( ! modelSchema)
@@ -26,7 +27,7 @@ function handler (request, reply) {
2627
model = orm.factory(modelSchema.getName());
2728

2829
// Load the model
29-
if (primaryKey !== '') {
30+
if ( ! _.isEmpty(primaryKey)) {
3031
model.find(primaryKey).then(function (data) {
3132
if ( ! data)
3233
return RSVP.Promise.cast(reply(Hapi.error.notFound(error.message)));

lib/server/handlers/options.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function handler (request, reply) {
2222
// Get the base model
2323
model = orm.factory(modelSchema.getName());
2424

25-
if (primaryKey !== '') {
25+
if ( ! _.isEmpty(primaryKey)) {
2626
// We're updating something!
2727
model.find(primaryKey).then(function (data) {
2828
if ( ! data)

lib/server/handlers/post.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@ var _ = require('underscore'),
22
Hapi = require('hapi'),
33
RSVP = require('rsvp');
44

5-
var orm, serializer;
5+
var orm, Serializer;
66

77
module.exports = function (ormModule, serializerModule) {
88
orm = ormModule;
9-
serializer = serializerModule;
9+
Serializer = serializerModule;
1010

1111
return handler;
1212
};
1313

1414
function handler (request, reply) {
1515
var modelSchema = orm.getSchema().getModel(request.params.model),
1616
primaryKey = request.params.id,
17-
model;
17+
model,
18+
serializer = Serializer(request);
1819

1920
if ( ! modelSchema)
2021
return reply(Hapi.error.notFound());
@@ -23,7 +24,7 @@ function handler (request, reply) {
2324
// Get the base model
2425
model = orm.factory(modelSchema.getName());
2526

26-
if (primaryKey !== '') {
27+
if ( ! _.isEmpty(primaryKey)) {
2728
// We're updating something!
2829
model.find(primaryKey).then(function (data) {
2930
if ( ! data)

lib/server/handlers/put.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@ var _ = require('underscore'),
22
Hapi = require('hapi'),
33
RSVP = require('rsvp');
44

5-
var orm, serializer;
5+
var orm, Serializer;
66

77
module.exports = function (ormModule, serializerModule) {
88
orm = ormModule;
9-
serializer = serializerModule;
9+
Serializer = serializerModule;
1010

1111
return handler;
1212
};
1313

1414
function handler (request, reply) {
1515
var modelSchema = orm.getSchema().getModel(request.params.model),
1616
primaryKey = request.params.id,
17-
model;
17+
model,
18+
serializer = Serializer(request);
1819

1920
if ( ! modelSchema)
2021
return reply(Hapi.error.notFound());
@@ -23,7 +24,7 @@ function handler (request, reply) {
2324
// Get the base model
2425
model = orm.factory(modelSchema.getName());
2526

26-
if (primaryKey !== '') {
27+
if ( ! _.isEmpty(primaryKey)) {
2728
// We're updating something!
2829
model.find(primaryKey).then(function (data) {
2930
var ret;

lib/server/handlers/schema.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var _ = require('underscore'),
22
Hapi = require('hapi');
33

4-
var orm, serializer;
4+
var orm;
55

66
module.exports = function (ormModule) {
77
orm = ormModule;

lib/server/index.js

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
var Hapi = require('hapi'),
2-
Intaglio = require('intaglio');
1+
var Intaglio = require('intaglio'),
2+
serializer = require('../serializers/hal');
33

4-
module.exports = function (orm, host, port, serializer, options) {
4+
module.exports = function (orm, plugin) {
55
Intaglio.utils.assert('`orm` is required!', orm !== undefined);
6-
Intaglio.utils.assert('`host` is required!', host !== undefined);
7-
Intaglio.utils.assert('`port` is required!', port !== undefined);
8-
Intaglio.utils.assert('`serializer` is required!', serializer !== undefined);
6+
Intaglio.utils.assert('`plugin` is required!', plugin !== undefined);
97

10-
var server = Hapi.createServer(host, port, options),
11-
routes = [
8+
var routes = [
129
{
1310
path: '/api/{model}/{id?}',
1411
method: 'GET',
@@ -62,7 +59,7 @@ module.exports = function (orm, host, port, serializer, options) {
6259
{
6360
path: '/api',
6461
method: 'GET',
65-
handler: require('./handlers/base')(orm, serializer)
62+
handler: require('./handlers/base')(orm)
6663
},
6764
{
6865
path: '/schema',
@@ -71,7 +68,7 @@ module.exports = function (orm, host, port, serializer, options) {
7168
}
7269
];
7370

74-
server.route(routes);
71+
plugin.route(routes);
7572

76-
return server;
73+
return plugin;
7774
};

test/index.js

+8-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var Intaglio = require('intaglio'),
2-
rest = require('../index');
2+
rest = require('../index'),
3+
Hapi = require('hapi');
34

45
var mysqlRepository = new Intaglio.repositories.mysql({
56
host: "192.168.33.10",
@@ -12,9 +13,12 @@ var mysqlRepository = new Intaglio.repositories.mysql({
1213

1314

1415
ORM.create(mysqlRepository).then(function (orm) {
15-
var server;
16-
// Decorate the orm
17-
orm.decorate(rest.decorator);
16+
var plugin = rest(orm),
17+
server = Hapi.createServer('localhost', 8080);
18+
19+
server.pack.register(plugin, function (err) {
20+
console.error(err);
21+
});
1822

1923
orm.extend('deployLocation', {
2024
preGetHook: function () {
@@ -43,18 +47,6 @@ ORM.create(mysqlRepository).then(function (orm) {
4347
}
4448
});
4549

46-
server = rest.server(orm, 'localhost', 8080, rest.serializers.hal('http://localhost:8080'));
47-
48-
server.route({
49-
method: '*',
50-
path: '/{path*}',
51-
handler: {
52-
directory: {
53-
path: 'public', listing: false, index: true
54-
}
55-
}
56-
});
57-
5850
server.start();
5951

6052
console.info('Ready!');

0 commit comments

Comments
 (0)