Skip to content

Commit

Permalink
Updating docs and tutorial (#343)
Browse files Browse the repository at this point in the history
* Updating docs and tutorial

* action.yml updated

* folder path

* remove Check Markdown links
  • Loading branch information
dannysheridan authored Jul 6, 2022
1 parent 4b50686 commit 0c29a2b
Show file tree
Hide file tree
Showing 21 changed files with 77 additions and 85 deletions.
11 changes: 0 additions & 11 deletions .github/workflows/action.yml

This file was deleted.

11 changes: 8 additions & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

> **Fern is a framework for building APIs.** You can think of it as an alternative to OpenAPI (f.k.a Swagger).
<a href="https://www.loom.com/share/c892f4a9fc674c4bb42fb31d395d9ebf">
<p>Fern TypeScript Server Tutorial</p>
<img style="max-width:300px;" src="https://cdn.loom.com/sessions/thumbnails/c892f4a9fc674c4bb42fb31d395d9ebf-1657127975624-with-play.gif">
</a>

## How does it work?

![Overview diagram](assets/diagrams/overview-diagram.png)
Expand Down Expand Up @@ -30,12 +35,12 @@ With an API definition, development projects that involve multiple engineers/tea

## What is a Fern API Definition?

A [**Fern API Definition**](markdown_files/definition.md#what-is-a-fern-api-definition) is a set of YAML files that describe your API.
A [**Fern API Definition**](_/definition.md#what-is-a-fern-api-definition) is a set of YAML files that describe your API.

## What are Fern Generators?

[**Generators**](markdown_files/generators.md) convert a Fern API Definition into clients, servers, and documentation.
[**Generators**](_/generators.md) convert a Fern API Definition into clients, servers, and documentation.

## How is this different than OpenAPI?

Fern is optimized for [kick-ass codegen](markdown_files/faq.md#_1-how-is-fern-different-than-openapi-fka-swagger).
Fern is optimized for [kick-ass codegen](_/comparison.md#_1-how-is-fern-different-than-openapi-fka-swagger).
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions docs/_/comparison.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# How is Fern different than OpenAPI (f.k.a Swagger)?

- **Higher-quality code generation**: Fern is more restrictive in what you can model, enabling idiomatic, easy-to-use code generation.

- **Protocol agnostic**: Fern lets you define RESTful services alongside WebSocket subscriptions.

- **Ease of use**: Fern runs code generators remotely and manages publishing packages to registries (e.g., NPM, Maven, PyPI). You get a dependency that you can start using right away.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions docs/markdown_files/generators.md → docs/_/generators.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,15 @@
| | |
| OpenAPI | converts a Fern Definition to an [OpenAPI Spec](https://swagger.io/resources/open-api/) | fern add openapi | <!-- markdown-link-check-disable-line --> [fern-openapi](https://github.com/fern-api/fern-openapi) |
| | |

## How does remote code generation work?

Code generators run remotely in the cloud. They take a set of Fern API Definition YAML files, run the generators listed in `.fernrc.yml, and produce generated files as an output. Files can output locally (i.e., seen in the file system of your IDE) or remotely (i.e., in a package manager like NPM, Maven, or PyPI).

### Generating clients

![client generators](../assets/diagrams/frontend-diagram.png)

### Generating servers

![server generators](../assets/diagrams/backend-diagram.png)
File renamed without changes.
90 changes: 45 additions & 45 deletions docs/markdown_files/imdb.md → docs/_/imdb.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<!-- markdownlint-disable MD033 -->

# Tutorial: IMDb
# TypeScript Server Tutorial: IMDb

This guide uses an example API for IMDb (the International Movie Database) that introduces you to using Fern. We'll generate a TypeScript server and a Postman Collection to implement and test our API.

<a href="https://www.loom.com/share/c892f4a9fc674c4bb42fb31d395d9ebf">
<p>Video walkthrough</p>
<img style="max-width:300px;" src="https://cdn.loom.com/sessions/thumbnails/c892f4a9fc674c4bb42fb31d395d9ebf-1657127975624-with-play.gif">
</a>

## Step 0: Prerequisites

- Install [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm)
Expand Down Expand Up @@ -32,6 +37,10 @@ In the root of your backend repo, run:
fern init
```

When it asks you for your `organization`, just write `imdb`.

In the `.fernrc.yml`, let's change the name of our api from `api` to `imdb-api`.

<br>
<details>
<summary>What happens:</summary>
Expand Down Expand Up @@ -62,7 +71,7 @@ fern add typescript
<details>
<summary>What happens:</summary>

`.fernrc.yml` will now list two generators:
`.fernrc.yml` will now list a generator:

```diff
name: api
Expand All @@ -80,13 +89,18 @@ fern add typescript

## Step 4: Run the generator

```bash
fern generate
```

In the terminal, you'll see `Published @imdb-fern/imdb-api-server@x.x.x` which we'll add as a dependency. By default, Fern publishes dependencies to a private registry.

```bash
# Teach npm about the Fern private registry
npm config set --location project @imdb-fern:registry https://npm-dev.buildwithfern.com/

npm install @imdb-fern/api-server@x.x.x
# Your version may be different, but this version will also work
npm install @imdb-fern/imdb-api-server@0.0.19
```

## Step 5: Implement the server
Expand All @@ -95,54 +109,34 @@ We'll create a new file `server.ts` at the root of our project. This will be a s

```ts
// server.ts
import { GetMovieErrorBody, MovieId } from "@imdb-fern/api-server/model";
import { MoviesService } from "@imdb-fern/api-server/services";
import { GetMovieErrorBody, MovieId } from "@imdb-fern/imdb-api-server/model";
import { MoviesService } from "@imdb-fern/imdb-api-server/services";
import express from "express";

const app = express();

app.use(
MoviesService.expressMiddleware({
// TODO
createMovie: () => {
return {
ok: true,
body: MovieId.of("iron-man-3"),
};
},

getMovie: () => {
return {
ok: false,
error: GetMovieErrorBody.NotFoundError(),
};
},
})
);

console.log("Listening for requests...");
app.listen(8080);
```

Our IDE will give us red lines indicating that we need to add missing properties.

![server.ts error message](../assets/tutorial/server.ts%20error%20message.png)

You can implement the missing methods, for example:

```diff
// server.ts
import express from "express";
const app = express();

app.use(MoviesService.expressMiddleware({
+ createMovie: (request) => {
+ console.log(request);
+ return {
+ ok: true,
+ body: MovieId.of("iron-man-3")
+ }
+ },

+ getMovie: () => {
+ return {
+ ok: false,
+ error: GetMovieErrorBody.NotFoundError()
+ }
+ }
}))

console.log("Listening for requests...");
app.listen(8080);
```

## Step 6: Run the server

```bash
Expand All @@ -151,23 +145,29 @@ npx ts-node server.ts

In the terminal, you should see `Listening for requests...`

## Step 6: Add the Postman generator
## Step 7: Add the Postman generator

In another terminal, let's run:

```bash
fern add postman
fern generate
```

In the `api/` folder you'll see `collection.json` that we'll import to Postman.
In the `api/` folder you'll see `generated-postman.json` that we'll import to Postman.

## Step 8: Hit the server from Postman

Open Postman and File -> Import `api/generated-postman.json`.

## Step 7: Hit the server from Postman
Select the `createMovie` endpoint and hit `Send`. You should get **`iron-man-3`** back from your server.

Open Postman and File -> Import `api/generated-postman/collection.json`.
![createMovie-postman](../assets/tutorial/createMovie-postman.png)

Select the `createMovie` endpoint and hit `Send`. You should get a response back from your server **`iron-man-3`**.
Select the `getMovie` endpoint and hit `Send`. As expected, we get a 404 response back.

![postman-testing](../assets/tutorial/postman-testing.png)
![getMovie-postman](../assets/tutorial/getMovie-postman.png)

## Step 8: Celebrate
## Step 9: Celebrate 🎉

You've successfully implemented a simple IMDb server using Fern. You're invited to join our [Discord](https://discord.gg/JkkXumPzcG).
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

- Getting started

- [Tutorial: IMDb](markdown_files/imdb.md)
- [CLI](markdown_files/cli.md)
- [Tutorial: IMDb](_/imdb.md)
- [CLI](_/cli.md)

- Concepts

- [Fern API Definition](markdown_files/definition.md)
- [Generators](markdown_files/generators.md)
- [Fern API Definition](_/definition.md)
- [Generators](_/generators.md)

- Reference
p
- [FAQs](markdown_files/faq.md)
- [Comparison with OpenAPI](_/comparison.md)
- [Join the Discord](https://discord.gg/JkkXumPzcG)
Binary file added docs/assets/tutorial/createMovie-postman.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/tutorial/getMovie-postman.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/assets/tutorial/postman-testing.png
Binary file not shown.
Binary file removed docs/assets/tutorial/server.ts error message.png
Binary file not shown.
21 changes: 0 additions & 21 deletions docs/markdown_files/faq.md

This file was deleted.

0 comments on commit 0c29a2b

Please sign in to comment.