Skip to content

Commit

Permalink
Add docs, re-organize code, rename Goose* to Ferns* (#72)
Browse files Browse the repository at this point in the history
* Start adding documentation

* Add tsdoc to generate documentation as markdown

* Remove Env, Typescript handles this

* Move permissions to its own file

* Move plugins to its own file

* Move auth to its own file

* Move transformers to its own file

* Move permission tests to their own file

* Finish documenting GooseRESTOptions

* Remove docs directory, this will be generated by Netlify

* Move more transformers, rename GooseRest to Ferns

* Update Readme, use the right typedoc version
  • Loading branch information
joshgachnang authored Jul 27, 2022
1 parent 5067458 commit ed93382
Show file tree
Hide file tree
Showing 19 changed files with 1,682 additions and 1,375 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist/
node_modules/
.idea/
docs/
103 changes: 103 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# ferns-api

This library attempts to make creating REST APIs much easier with Express and Mongoose.
Most REST APIs wind up being a lot of boilerplate, so this tries to cut that down without turning
into a full blown framework of its own. This library is inspired by the
[Django-REST-Framework](https://www.django-rest-framework.org).

### Coming soon:

A frontend library to consume these APIs with Redux Toolkit Query.

## Getting started

To install:

yarn install ferns-api

## Usage

Assuming we have a model:

const foodSchema = new Schema<Food>({
name: String,
hidden: {type: Boolean, default: false},
ownerId: {type: "ObjectId", ref: "User"},
});
export const FoodModel = model("Food", foodSchema);

We can expose this model as an API like this:

import express from "express";
import {fernsRouter, Permissions} from "ferns-api";

const app = express();
app.use(
"/foods",
fernsRouter(UserModel, {
permissions: {
list: [Permissions.IsAny],
create: [Permissions.IsAuthenticated],
read: [Permissions.IsAny],
update: [Permissions.IsOwner],
delete: [Permissions.IsAdmin],
},
})
);

Now we can perform operations on the Food model in a standard REST way. We've also added some permissioning.

# Gets a list of foods. Anyone can do this without being authenticated.
GET /foods
{
data: [{_id: "62c86d787c7e2db0bf286acd", name: "Carrots", hidden: false, ownerId: "62c44d9f003d9f8ee8cc9256"}],
more: false,
page: 1,
limit: 100
}

# Get a specific food. Anyone can do this.
GET /foods/62c86d787c7e2db0bf286acd
{_id: "62c86d787c7e2db0bf286acd", name: "Carrots", hidden: false, ownerId: "62c44d9f003d9f8ee8cc9256"}

# Creates a new food. Only authenticated users are allowed to do this.
POST /foods {name: "Broccoli", ownerId: "62c44d9f003d9f8ee8cc9256"}
{_id: "62c86d787c7e2db0bf286000", name: "Broccoli", hidden: false, ownerId: "62c44d9f003d9f8ee8cc9256"}

# Updates an existing food. Only the owner of the food can do this, otherwise an error code is returned.
PATCH /foods/62c86d787c7e2db0bf286acd {name: "Peas And Carrots"}
{_id: "62c86d787c7e2db0bf286acd", name: "Peas And Carrots", hidden: false, ownerId: "62c44d9f003d9f8ee8cc9256"}

# Deletes an existing food. Only admins are allowed to do this (users with `user.admin` set to true).
DELETE /foods/62c86d787c7e2db0bf286acd

You can create your own permissions functions. Check permissions.ts for some examples of how to write them.

## Example

To test out how the API works, you can look at and run [example.ts].

## Dev

To continuously compile the package:

yarn dev

To run tests, linting, and fixing up lint issues:

yarn lint
yarn lintfix
yarn test

To see how your changes will affect the docs:

yarn docs
cd docs/
npx http-server

A lot of dev may require using yarn link. You'll want to keep the `yarn dev` window running to continuously compile:

yarn link
cd $your-api-repo
yarn link ferns-api

2 changes: 0 additions & 2 deletions README.me

This file was deleted.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"dev": "tsc -w",
"lint": "eslint \"src/**/*.ts*\"",
"lintfix": "eslint --fix \"src/**/*.ts*\"",
"test": "jest -i"
"test": "jest -i",
"docs": "typedoc --out docs src/index.ts"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -85,6 +86,7 @@
"sinon": "^14.0.0",
"supertest": "^6.1.6",
"ts-jest": "^28.0.5",
"typedoc": "~0.23.0",
"typescript": "4.1.5"
},
"prettier": {
Expand Down
Loading

0 comments on commit ed93382

Please sign in to comment.