Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

path-proxy

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

path-proxy - npm Package Compare versions

Comparing version 0.0.0 to 0.1.0

2

package.json
{
"name": "path-proxy",
"version": "0.0.0",
"version": "0.1.0",
"description": "A path proxy object constructor",

@@ -5,0 +5,0 @@ "main": "index.js",

# path-proxy
path-proxy is based on my work on the [Node client for the Heroku API][heroku_client].
Its purpose is to, given a base constructor function and an array of paths (most
likely for an API), build a network of proxy object constructors on top of that
base constructor.
Given an group of paths (say, from an API schema), you might need to create a
set of proxy objects for interacting with those paths. This is the situation I
found myself in while working on the [Node client for the Heroku API][heroku_client].
## Example:
Given a set of paths and a base constructor function, path-proxy will create a
network of logical proxy objects based on the paths and attach it to the
constructor's prototype.
## Install
```sh
npm install path-proxy --save
```
## Usage
```javascript
var pathProxy = require('path-proxy');
function Client () {
}
function ApiClient() {}
Client.prototype.sayHello = function () {
return "hello";
}
pathProxy.proxy(Client, [
pathProxy.proxy(ApiClient, [
"/foo",
"/foo/{id}/bar",
"/foo/{id}/bar/{id}/baz"
"/foo/{id}/bar"
]);
var client = new Client(),
baz = client.foo("a").bar("b").baz();
var client = new ApiClient();
client.foo("qux").bar();
```
// `baz` is a proxy object for "/foo/a/bar/b/baz". It also has access to
// `client` through its `base` parameter.
baz.params; // ["a", "b"]
baz.path; // "/foo/a/bar/b/baz"
baz.base.sayHello(); // "hello"
This may not appear all that useful—they're mostly just empty functions—until you
start mucking around with their prototypes:
```javascript
var BarProxy = pathProxy.pathProxy(ApiClient, "/foo/{id}/bar");
BarProxy.prototype.sayHello = function () {
console.log("hello");
};
client.foo("qux").bar().sayHello(); // Logs "hello".
```
Proxy constructor functions can also be fetched individually:
They also have access to a few useful attributes:
```javascript
var qux = pathProxy.pathProxy(Client, "/qux");
var baz = client.foo("qux").bar("baz");
baz.params; // ["qux", "baz"]
baz.pathSegments; // ["foo", "qux", "bar", "baz"]
baz.path; // "/foo/qux/bar/baz"
```
qux.prototype.foo = function () {
return "foo";
}
And can access the instance of the base constructor they're based off of:
client.qux().foo(); // "foo"
```javascript
ApiClient.prototype.delete = function (path, callback) {
var message = this.name + " deleted at " + path;
callback(message);
};
var client = new ApiClient();
client.name = "Jonathan";
BarProxy.prototype.delete = function (callback) {
this.base.delete(this.path, callback);
};
// This:
client.foo("qux").bar("baz").delete(function (message) {
// message == "Jonathan deleted at /foo/qux/bar/baz"
});
// Is equivalent to this:
client.delete("/foo/qux/bar/baz", function (message) {
// message == "Jonathan deleted at /foo/qux/bar/baz"
});
```
Note that **this project is still extremely unstable**. As in, don't be surprised
at awful things like force-pushes to master.
## The End
That's all there is for now. This is might prove to be a useful tool for working
on documented API clients in the future. For now, don't rely on it. There are
no tests, and it's more of a fun experiment at the moment
[heroku_client]: https://github.com/heroku/node-heroku-client

@@ -6,32 +6,26 @@ var pathProxy = require('./index');

Client.prototype.sayHello = function () {
console.log('hello');
Client.prototype.delete = function (path, callback) {
var message = this.name + " deleted at " + path;
callback(message)
}
pathProxy.proxy(Client, [
"/apps",
"/apps/{id}",
"/apps/{id}/dynos"
]);
var BarProxy = pathProxy.pathProxy(Client, "/foo/{id}/bar/{id}");
var client = new Client();
client.name = "Jonathan";
client = new Client();
client.hi = function () {
BarProxy.prototype.delete = function (callback) {
this.base.delete(this.path, callback);
}
apps = client.apps("foo");
// console.log(apps.pathSegments);
dynos = apps.dynos()
// console.log(apps.pathSegments);
// console.log(dynos.path);
console.log(dynos.params);
console.log(dynos.pathSegments);
console.log(client.hi());
// baz = pathProxy.pathProxy(Client, "/foo/{id}/bar/{baz}");
// console.log(baz());
// console.log(client.apps().dynos("bar").pathSegments);
// console.log(client.apps().dynos("bar").path);
// console.log(client.apps("foo").pathSegments);
// console.log(client.apps("foo").pathSegments);
// console.log(client.apps("foo").path);
// client.apps("foo").base.sayHello()
// console.log(Client.prototype.apps);
// This:
client.foo("qux").bar("baz").delete(function (message) {
console.log(message);
// message == "Jonathan deleted at /foo/qux/bar/baz"
});
// Is equivalent to this:
client.delete("/foo/qux/bar/baz", function (message) {
console.log(message);
// message == "Jonathan deleted at /foo/qux/bar/baz"
});
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