GithubHelp home page GithubHelp logo

aiir / pharrell Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 38 KB

Opinionated HTTP framework for Node.js to make the creation of CRUDL APIs as simple as possible

Home Page: https://aiir.com/

License: MIT License

Makefile 0.96% JavaScript 98.46% Shell 0.58%
crud crudl javascript lambda nodejs

pharrell's People

Contributors

andybee avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

pharrell's Issues

Add support for Google Cloud Functions

Why end the serverless love at AWS Lambda?

See if there's a neat/simple way to introduce support for Cloud Functions too so a Pharrell based app can be portable across local server, AWS Lambda and Google Cloud Functions.

When `list()` returns 0 results, prev/next links always null

The prev/next pagination links in a list response are auto generated based on the size and position of the list results.

When a list result has 0 total results, the prev/next links will always be null, even if we request a page number outside the range.

Even with 0 results, we should enforce there being a single page of results and any out-of-range request should define a prev link to page 1.

Remove Koa core?

As the core of Pharrell is essentially one piece of middleware and #1 is related to removing the unnecessary network-stack-to-feed-Lambda implementation, it may ultimately make more sense for Koa compose to merge the few bits together and map straight in to the Node.js HTTP stack directly via the listen method.

I'm not sure how much "magic" Koa is doing for us behind the scenes that make this too much effort. This will probably be clearer once attempting to remove the Lambda handler library for #1.

Invalid JSON body triggers internal server error

The body parser throws on invalid JSON and so gets caught and returned as an internal server error.

Invalid JSON should be treated as a 400 Bad Request error as per the Deliveroo API Design: "the entity cannot be created with the information in the request body".

Improve Lambda handling

Currently the Lambda handling is solely reliant on the AWS-supplied package that provides a wrapper around the Node.js HTTP library (running it via a socket) and melding the Lambda event and context callback methods in to it.

Whilst that works and provides a simple way to wrap up any Node.js HTTP/Express/Koa app, it might be nicer longer term to remove the dependency, avoid the internal network-stack-through-a-socket implementation and instead create a Koa-compatiable context object from the Lambda event, use koa-compose to create the middleware stack, and then feed the result back via the newly introduce Node 8.10 Lambda async handling.

Introduce endpoint middleware

A use case has come up where we wish to secure endpoints on the API.

Because the current approach of Pharrell abstracts endpoints away from any of usual request/response model of HTTP, we cannot implement this inside an endpoint method.

One way of introducing this would be to add the concept of both endpoints and the app adding middleware. These middlewares would need to be Koa-like (e.g. receive the context) to unlock the potential for doing clever things. In realistic terms, these would just hook in to Koa the same way the router does.

Make errors more specific where possible

Because of the way the middleware layering is currently implemented, sending a malformed JSON body to a specific result throws an exception at the bodyParser middleware and gets caught and returned as an error with the generic key. Ideally, we'd only attempt parsing on the methods that allow JSON bodies and return a specific key.

Don't enforce requirement for specific methods on an endpoint

Currently an endpoint has to support lookup, read and list functions to be mountable.

It's conceivable that an endpoint might be "write only" (e.g. not implement lookup, read or list), so this logic potentially causes issues.

The only real enforcement we should have at the mounting stage is that at least 1 supported function is implemented, so there's a point to having the endpoint mounted in the first place.

Another potential issue this highlights is that, by having a helper read function in the underlying Endpoint class, you can never not have a read function in your Endpoint subclass. A better approach would be to build this functionality in to the router so it can simulate the behaviour of a read method when only lookup is present.

Enforce validation on Endpoints at point of mounting

When performing a request on an endpoint that hits a non-existent method, an error is thrown and an internal server error is returned to the user.

As the issue exists from the outset of the application, if we enforce the validation check as part of the mount function we could catch and exit earlier and avoid the potential of deploying an application with implementation errors on the endpoints.

Relax requirement for `create()` return to include an `id` property

create() has a requirement for (and generates an internal server error if) the returned object to include an id property so that the middleware can generate a _links object for the response with a self URL reference.

It is possible for a specific endpoint to not want this behaviour, particularly if it's doing something unusual which means no typical list() and read() responses. Whilst that can be handled by throwing forbidden or not found errors, the validation cannot be circumvented.

Application middleware should run after routing, but before Endpoint middleware

Application middleware is currently being attached to the parent Koa instance, so runs before the router.

This means, for security middleware, you can receive a forbidden for a 404 route.

If all middleware execution is handled by the router, maintaining the relative order (app before endpoint) we could handle 404s ahead of executing middleware.

Allow method chaining from App

The Endpoint class' mount and use functions both return the Endpoint instance to allow easy method chaining.

It would be consistent to do the same with mount and use on App, too.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.