From eca893c2b6ed1dd0b1813f97234ae28ae2f73045 Mon Sep 17 00:00:00 2001 From: Luke Butler Date: Fri, 9 Jun 2017 20:07:37 -0400 Subject: [PATCH] Init Code --- README.md | 43 ++++++++++++++++++++++++++++++++++++++++++- index.js | 2 ++ package.json | 10 ++++++++++ src/index.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ yarn.lock | 7 +++++++ 5 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 index.js create mode 100644 package.json create mode 100644 src/index.js create mode 100644 yarn.lock diff --git a/README.md b/README.md index 038adac..4962487 100644 --- a/README.md +++ b/README.md @@ -1 +1,42 @@ -# feathers-versionate \ No newline at end of file + +# feathers-versionate + +## About +> Service nesting for feathersjs + +Nests services under configurable base paths, and provides easy methods to access those services. + +## Getting Started + +Install the module + +NPM: `npm install feathers-versionate --save` + +Yarn: `yarn add feathers-versionate` + +```js +var feathers = require('feathers'); +var versionate = require('feathers-versionate'); +var memory = require('feathers-memory'); + +const app = feathers(); +// Configure versionate +app.configure(versionate()); +// Register a base-path "/api/v2", and provide access to it via `app.v2` +app.versionate.register('v2', '/api/v2/'); +// Now you can use `app.v2` to create and access services under the registered path! +app.v2.use('/users', memory); // http://localhost:3030/api/v2/users +``` + +## Documentation + +`feathers-versionate` is just a wrapper around app.use and app.service for applying + +* `app.versionate.register(name, basePath)` + +## Release History + +__0.1.0__ + +- Initial release +- Support for app.use and app.service \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..fa43e41 --- /dev/null +++ b/index.js @@ -0,0 +1,2 @@ +'use strict'; +module.exports = require('./src'); diff --git a/package.json b/package.json new file mode 100644 index 0000000..6b73a9f --- /dev/null +++ b/package.json @@ -0,0 +1,10 @@ +{ + "name": "feathers-versionate", + "description": "feathersjs service nesting utility", + "version": "0.1.0", + "main": "index.js", + "license": "MIT", + "dependencies": { + "uberproto": "^1.2.0" + } +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..8b8f229 --- /dev/null +++ b/src/index.js @@ -0,0 +1,52 @@ +'use strict'; +const path = require('path'); +const Proto = require('uberproto'); + +const service = function (name, basePath, subItem = false) { + const app = this; + + // Relay use to app.use with basePath applied + const use = function () { + const args = Array.from(arguments); + const subPath = args.shift(); + app.use(path.join(basePath, subPath), ...args); + } + + // Relay service to app.service with basePath applied + const service = function (subPath) { + return app.service(path.join(basePath, subPath)); + } + + // Create an object that will be added to `app` + const newService = {}; + newService[name] = { + use, + service + }; + + // Nest object within `vs` if flag set to true + if (subItem === true) { + newService = { + versionate: newService + }; + } + + // Return the new service's function methods + return newService; +} + +module.exports = function () { + return function () { + const app = this; + Proto.mixin({ + versionate: { + register: function () { + // Create new service + const newService = service.apply(app, Array.from(arguments)); + // Add new service access method to app + Proto.mixin(newService, app) + } + } + }, app) + } +} diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..7f7b088 --- /dev/null +++ b/yarn.lock @@ -0,0 +1,7 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +uberproto@^1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/uberproto/-/uberproto-1.2.0.tgz#61d4eab024f909c4e6ea52be867c4894a4beeb76"