Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
DJankauskas committed Jul 28, 2023
2 parents db4d492 + 511adb2 commit 491a938
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 456 deletions.
170 changes: 17 additions & 153 deletions docs/docs/express/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,56 +19,24 @@ Use this plugin to serve a Stainless API in a Next.js app.
npm i --save 'stainless-api/stl-api#express-0.0.2'
```

## Creating Express Applications and Routers
## Creating an Express Router

### Stainless API -> Express Application
### From an entire Stainless API

```ts
stlExpressAPI(
api: AnyAPIDescription,
options?: AddEndpointsToExpressOptions
): Application
```

```ts
import { stlExpressAPI } from "@stl-api/express";
import api from "./api";

export const app = stlExpressAPI(api);
```

#### Options

See [`AddEndpointsToExpressOptions`](#addendpointstoexpressoptions).

#### Notes

`stlExpressAPI` is provided as a convenience; for production use
cases you'll probably want to use more fine-grained methods below
that enable greater customization.

`stlExpressAPI` installs default json, text and raw middleware on the app:

```ts
app.use(express.json());
app.use(express.text());
app.use(express.raw());
```

### Stainless API -> Express Router

```ts
stlExpressAPIRouter(
apiRouter(
api: AnyAPIDescription,
options?: AddEndpointsToExpressOptions
): Router
```

```ts
import { stlExpressAPIRouter } from "@stl-api/express";
import { apiRouter } from "@stl-api/express";
import express from "express";
import api from "./api";

export const router = stlExpressAPIRouter(api);
const app = express();
app.use(apiRouter(api));
```

#### Options
Expand All @@ -77,32 +45,34 @@ See [`AddEndpointsToExpressOptions`](#addendpointstoexpressoptions).

#### Notes

`stlExpressAPIRouter` is provided as a convenience; for production use
`apiRouter` is provided as a convenience; for production use
cases you'll probably want to use more fine-grained methods below
that enable greater customization.

`stlExpressAPIRouter` installs default json, text and raw middleware on the router:
`apiRouter` installs default json, text and raw middleware on the router:

```ts
router.use(express.json());
router.use(express.text());
router.use(express.raw());
```

### Stainless Resource -> Express Router
### From a Stainless Resource

```ts
stlExpressResourceRouter(
resourceRouter(
resource: Pick<AnyResourceConfig, "actions" | "namespacedResources">,
options?: AddEndpointsToExpressOptions & RouterOptions
): Router
```

```ts
import { stlExpressResourceRouter } from "@stl-api/express";
import { resourceRouter } from "@stl-api/express";
import express from "express";
import posts from "./posts";

export const router = stlExpressResourceRouter(posts);
const app = express();
app.use(resourceRouter(posts));
```

#### Options
Expand All @@ -111,124 +81,18 @@ See [`AddEndpointsToExpressOptions`](#addendpointstoexpressoptions).

#### Notes

`stlExpressResourceRouter` is provided as a convenience; for production use
`resourceRouter` is provided as a convenience; for production use
cases you'll probably want to use more fine-grained methods below
that enable greater customization.

`stlExpressResourceRouter` installs default json, text and raw middleware on the router:
`resourceRouter` installs default json, text and raw middleware on the router:

```ts
router.use(express.json());
router.use(express.text());
router.use(express.raw());
```

## Registering Stainless API Endpoints with Express

### Register an entire Stainless API

```ts
addStlAPIToExpress(
router: Application | Router,
api: AnyAPIDescription,
options?: AddEndpointsToExpressOptions
): void
```

```ts
import express from "express";
import { addStlAPIToExpress } from "@stl-api/express";
import api from "./api";

export const app = express();
// you'll need to specify body parser middleware, e.g.:
app.use(express.json());
app.use(express.text());
app.use(express.raw());

addStlAPIToExpress(app, api);
```

#### Options

See [`AddEndpointsToExpressOptions`](#addendpointstoexpressoptions).

#### Notes

By default `addStlAPIToExpress` will handle all errors on API routes.
If you want to customize error handling you may want to pass the
`handleErrors: false` and/or `addMethodNotAllowedHandlers: false`
options.

### Register a Stainless Resource

```ts
addStlResourceToExpress(
router: Application | Router,
resource: Pick<AnyResourceConfig, "actions" | "namespacedResources">,
options?: AddEndpointsToExpressOptions
): void
```

```ts
import express from "express";
import { addStlResourceToExpress } from "@stl-api/express";
import posts from "./posts";

export const app = express();
// you'll need to specify body parser middleware, e.g.:
app.use(express.json());
app.use(express.text());
app.use(express.raw());

addStlResourceToExpress(app, posts);
```

#### Options

See [`AddEndpointsToExpressOptions`](#addendpointstoexpressoptions).

#### Notes

By default `addStlResourceToExpress` will handle all errors on API routes.
If you want to customize error handling you may want to pass the
`handleErrors: false` and/or `addMethodNotAllowedHandlers: false`
options.

### Register a single Stainless API Endpoint

```ts
addStlEndpointToExpress(
router: Application | Router,
endpoint: AnyEndpoint,
options?: AddToExpressOptions
): void
```

```ts
import express from "express";
import { addStlEndpointToExpress } from "@stl-api/express";
import retrievePosts from "./posts/retrieve";

export const app = express();
// you'll need to specify body parser middleware, e.g.:
app.use(express.json());
app.use(express.text());
app.use(express.raw());

addStlEndpointToExpress(app, retrievePosts);
```

#### Options

See [`AddToExpressOptions`](#addtoexpressoptions).

#### Notes

By default `addStlEndpointToExpress` will handle all errors on the route.
If you want to customize error handling you may want to pass the
`handleErrors: false` option.

## Lower-level APIs

### Create an Express request handler
Expand Down
Loading

0 comments on commit 491a938

Please sign in to comment.