0.39.2 • Published 28 days ago

ferns-api v0.39.2

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
28 days ago

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.

Coming soon:

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

Getting started

To install:

yarn add 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 by running yarn build then running node dist/example.js in /ferns-api; while running, you can use a mongoDB client such as Compass to view collections.

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
0.39.2

28 days ago

0.39.1

30 days ago

0.39.0

1 month ago

0.38.2

1 month ago

0.38.3

1 month ago

0.38.1

1 month ago

0.38.0

2 months ago

0.37.1

2 months ago

0.37.0

2 months ago

0.36.1

3 months ago

0.36.0

3 months ago

0.35.4

3 months ago

0.35.3

4 months ago

0.35.2

4 months ago

0.35.1

4 months ago

0.35.0

4 months ago

0.34.0

5 months ago

0.33.10

6 months ago

0.33.7

6 months ago

0.33.6

6 months ago

0.33.5

6 months ago

0.33.4

7 months ago

0.33.13

6 months ago

0.33.12

6 months ago

0.33.9

6 months ago

0.33.11

6 months ago

0.33.8

6 months ago

0.33.3

7 months ago

0.33.2

7 months ago

0.33.1

7 months ago

0.33.0

8 months ago

0.32.1

8 months ago

0.32.0

9 months ago

0.30.0

10 months ago

0.29.0

10 months ago

0.29.3

10 months ago

0.29.2

10 months ago

0.29.1

10 months ago

0.31.0

9 months ago

0.28.5

10 months ago

0.28.4

11 months ago

0.28.3

12 months ago

0.27.2

1 year ago

0.27.1

1 year ago

0.27.0

1 year ago

0.28.1

1 year ago

0.28.0

1 year ago

0.28.2

1 year ago

0.26.1

1 year ago

0.26.0

1 year ago

0.25.0

1 year ago

0.24.1

1 year ago

0.24.0

1 year ago

0.23.1

1 year ago

0.23.0

1 year ago

0.21.0

1 year ago

0.20.1

1 year ago

0.20.0

1 year ago

0.19.0

2 years ago

0.19.1

2 years ago

0.18.3

2 years ago

0.21.3

1 year ago

0.21.2

1 year ago

0.22.0

1 year ago

0.21.1

1 year ago

0.20.2

1 year ago

0.18.1

2 years ago

0.18.2

2 years ago

0.16.0

2 years ago

0.17.0

2 years ago

0.18.0

2 years ago

0.10.0

2 years ago

0.11.0

2 years ago

0.9.0

2 years ago

0.7.2

2 years ago

0.6.3

2 years ago

0.12.0

2 years ago

0.11.1

2 years ago

0.8.0

2 years ago

0.7.1

2 years ago

0.13.0

2 years ago

0.7.4

2 years ago

0.14.0

2 years ago

0.7.3

2 years ago

0.15.0

2 years ago

0.7.0

2 years ago

0.6.2

2 years ago

0.6.1

2 years ago

0.6.0

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago