1.1.2 • Published 6 months ago

@node-in-layers/rest-api v1.1.2

Weekly downloads
-
License
ISC
Repository
-
Last release
6 months ago

REST API - A Node In Layers Package

This package contains useful out-of-the-box tools for building REST APIS rapidly.

It includes boilerplate code for starting a modern production JSON REST API Express server, as well as code for making model based apis, with minimal boilerplate.

How to Install

npm install @node-in-layers/rest-api@latest

How To Use

There are three main components of this express package.

  • Model Based Express Controller
  • Model Based Router Controller
  • Custom Layer - express

Model Based Express Controller

A model based express controller is an express controller that can wrap around a model from the functional-models model system. It connects to the CRUDS functionality inside @node-in-layers/db for you (create, retrieve, update, delete, search). This pairs very well with the model based express router, although is not required.

Model Based Express Router

The model based express router is an express router that provides routing to a CRUDS controller. This handles all the pathing and takes this format:

Create:

PUT: /namespace/my-model-name

Retrieve:

GET: /namespace/my-model-name/:id

Update:

PUT: /namespace/my-model-name/:id

Delete:

DELETE: /namespace/my-model-name/:id

Search:

POST: /namespace/my-model-name

A Quick note on search

The search endpoint is a POST instead of a GET because it uses a JSON body for the search. The JSON body is a JSONified version of the OrmSearch object from functional-models.

Url Prefix

You can append a prefix to the url by adding it as an argument to the router. This can be useful for adding a service level name before all of your models.

Custom Layer - "express"

This package adds a custom layer called "express". This layer is intended to be a composite layer with the entries layer. This is for systems that use express as the final top layer. One example would be a REST API Backend. This custom layer, adds a number of functions that are useful for setting up an express server. (Setting routes, adding middleware, starting the server with listen).

To add your own routes and middleware you need to create a express layer (such as a file named express.ts that has a create() function in it).

How To Use

In order to use the express layer, you need to add it as a composite layer inside the configuration file, as well as this app itself.

// Example config.dev.mjs
import { CoreNamespace } from '@node-in-layers/core/index.js'
import { DataNamespace } from '@node-in-layers/data/index.js'
import { RestApiNamespace } from '@node-in-layers/rest-api/index.js'
import { peteSdkLogger } from './dist/logging.js'

const core = {
  apps: await Promise.all([
    import(`@node-in-layers/data/index.js`),
    // Import here
    import(`@node-in-layers/rest-api/express/index.js`),
  ]),
  // NOTE: The composite layer with entries.
  layerOrder: ['services', 'features', ['entries', 'express']],
  logging: {
    logLevel: 'debug',
    logFormat: 'full',
  },
  modelFactory: '@node-in-layers/data',
  modelCruds: true,
}

const data = {
  databases: {
    default: {
      datastoreType: 'memory',
    },
  },
}

const express = {
  port: 8000,
  urlPrefix: '/my-service/',
}

export default () => ({
  environment: 'dev-low',
  systemName: 'my-system',
  [CoreNamespace.root]: core,
  [DataNamespace.root]: data,
  [RestApiNamespace.express]: express,
})

Model CRUDS

In order to CRUDS functionality to the REST interface you need to create this express layer in your app, and use the express functions provided by this library. A useful function called expressModels can be used that automatically adds all model CRUDS from your namespace.

Example Use

We want to create a model and provide CRUDS routes to it. The easiest way to do this is to create our There is a helper method on the custom express layer called #addModel() that makes this really easy.

// /src/your-app/express.ts
import { FeaturesContext } from '@node-in-layers/core/index.js'
import {
  ExpressContext,
  expressModels,
} from '@node-in-layers/rest-api/index.js'

const express = {
  create: (context: ExpressContext & FeaturesContext) => {
    // Automatically adds all cruds models feature functions to express.
    expressModels('your-namespace')(context)
  },
}

export { express }

Troubleshooting

Model endpoints aren't working

Make sure that your app has the following layers (even if they are only an empty create):

  1. Services - Can be empty
  2. Features - Can be empty
  3. Express - See above
  4. modelCruds is set to true - See core configurations.
1.1.2

6 months ago

1.1.1

6 months ago

1.1.0

6 months ago

1.0.20

6 months ago

1.0.19

6 months ago

1.0.18

6 months ago

1.0.17

6 months ago

1.0.16

6 months ago

1.0.15

6 months ago

1.0.14

6 months ago

1.0.13

6 months ago

1.0.12

6 months ago

1.0.11

6 months ago

1.0.10

7 months ago

1.0.9

7 months ago

1.0.8

7 months ago

1.0.7

7 months ago

1.0.6

7 months ago

1.0.4

8 months ago

1.0.0

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago