Skip to content

Commit

Permalink
Merge pull request #366 from vishalkhoje/feature/add-new-page-integra…
Browse files Browse the repository at this point in the history
…tion

feat: New integration post page added
  • Loading branch information
ageddesi authored Oct 21, 2023
2 parents 738f451 + d30ac7d commit 3a3b01c
Show file tree
Hide file tree
Showing 3 changed files with 264 additions and 0 deletions.
1 change: 1 addition & 0 deletions marketing-site/src/components/header.astro
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const repoInfo = await repoInfoResponse.json()
<div class="flex justify-center items-center space-x-8 my-4">
<a href="https://api.mocked-api.dev/" target="_blank" class="text-base text-white hover:text-pink-500"> Api Docs </a>
<a href="/blog" class="text-base text-white hover:text-pink-500"> Blog </a>
<a href="/integration" class="text-base text-white hover:text-pink-500"> Integration </a>
<a href="https://github.com/ageddesi/Mocked-API" target="_blank" class="text-base text-pink-500 hover:text-pink-600 border-2 p-2 rounded-xl pr-1 border-pink-500 hover:border-pink-600"> Repo <span class="bg-pink-500 p-1 rounded-lg text-white">🌟{repoInfo.watchers}</span> </a>
<!--<a href="/examples" class="text-base text-white hover:text-pink-500"> Examples </a>-->
</div>
Expand Down
95 changes: 95 additions & 0 deletions marketing-site/src/pages/integration.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
import Header from "../components/header.astro";
import Footer from "../components/footer.astro";
// Use Astro.glob() to fetch all posts, and then sort them by date.
const posts = (await Astro.glob("./integration/*.{md,mdx}")).sort(
(a, b) =>
new Date(b.frontmatter.pubDate).valueOf() -
new Date(a.frontmatter.pubDate).valueOf()
);
---

<!doctype html>
<html lang="en">
<head>
<style>
ul {
list-style-type: none;
padding: unset;
}
ul li {
display: flex;
}
ul li time {
flex: 0 0 130px;
font-style: italic;
color: #595959;
}
ul li a:visited {
color: #8e32dc;
}
</style>
</head>
<body class="bg-gray-800 overflow-x-hidden">
<Header />
<main>
<div class="relative px-4 pt-8 pb-20 sm:px-6 lg:px-8 lg:pt-16 lg:pb-28">
<div class="absolute inset-0">
<div
class="h-1/3 bg-gradient-to-b from-indigo-700 to-gray-800 sm:h-2/3"
>
</div>
</div>
<div class="relative mx-auto max-w-7xl">
<div class="text-center">
<h2
class="text-4xl font-bold tracking-tight text-pink-400 sm:text-4xl"
>
The Mocked Integration
</h2>
<p class="mx-auto mt-3 max-w-3xl text-xl text-gray-200 sm:mt-4">
Updates from the world of mocked-api.
</p>
</div>
<div
class="mx-auto mt-12 grid max-w-lg gap-5 lg:max-w-none lg:grid-cols-3"
>
{posts.map((post) => (
<div class="flex flex-col overflow-hidden rounded-lg shadow-lg bg-gradient-to-b from-indigo-700 to-purple-800 p-4">
<div class="flex-shrink-0">
<img class="mb-4" src={post.frontmatter.heroImage} alt="">
</div>
<div class="flex flex-1 flex-col justify-between bg-white p-6 border-r-pink-300 border-4">
<div class="flex-1">
<p class="text-sm font-medium text-indigo-600">
{post.frontmatter.category}
</p>
<div class="mt-2 block">
<p class="text-xl font-semibold text-gray-900">{post.frontmatter.title}</p>
<p class="mt-3 text-base text-gray-500">{post.frontmatter.summary}</p>
</div>
</div>
<div class="mt-6 flex items-center">
<p class="text-sm font-medium text-gray-900">
<a href="#" class="hover:underline">{post.frontmatter.author}</a>
</p>
<div class="flex space-x-1 text-sm text-gray-500 ml-2">
{post.frontmatter.pubDate}
</div>
</div>
<a href={post.url} class="m-auto">
<button class="bg-blue-500 w-60 m-auto mt-4 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
Read more
</button>
</a>
</div>
</div>
))}
</div>
</div>
</div>
</main>
<Footer />
</body>
</html>
168 changes: 168 additions & 0 deletions marketing-site/src/pages/integration/add-api-endpoint.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
---
layout: ../../layouts/BlogPost.astro
title: "Add an API endpoint"
pubDate: "01th October 2023"
heroImage: "/Hfest-Logo-2-Color-Manga.png"
category: "news"
summary: "We are suppy happy to be apart of hacktoberfest this year..."
author: "Aaron Rackley"
---

Add an API endpoint
===================

This example will use `users` as a new TAG for OpenAPI, meaning we will have dedicated endpoints to get some mocked users or create new ones.

How to add an endpoint
----------------------

Endpoints should be created in a `<TOPIC>-routes.ts` file under `./modules/<TOPIC>/api` folder.
We assume `<TOPIC>` as a new TAG on OpenAPI, meaning a different category with dedicated endpoints like `address`, `countries`, `currency`, and son on... We will use `users` topic in our examples.

Example on how to create and endpoint for a new topic:

- 1 - Create a new folder with the topic name, under `./modules` folder, like `./modules/users`
- 2 - Create a new file with mocked data, under `./modules/users/data`, like `./modules/users/data/users.ts`.
- 3 - Create an `api` folder to store the `routes` file, like `./modules/users/api/users-routes.ts`
- 4 - Create different endpoints for users. Example:

```javascript
import usersList from "../data/users";

module.exports = function (app: core.Express) {
app.get("/users", (req: Request, res: Response) => {
res.json({
users: usersList,
});
});
};
```

Note: This endpoint will fetch all mocked users stored in `./modules/users/data/users.ts`

How to write tests
----------------------

For each module you create you will also need to create a tests folder, Inside this folder there should be a `api` and `utils`
(if you create any utils) folder.

The utils tests are fairly simple jests tests, the `api` routes tests are slightly different where you will need to add

```javascript
import request from "supertest";
import app from "path/to/app";
```

to your tests and make a request in your test instead of calling a normal function.

How to run tests
----------------------

run `npm test`
run `npm run test:watch` to run the tests in watch mode.

How to add OpenAPI comments
----------------------

For each endpoint you should do an OpenAPI comment, this way, you will make sure your endpoint will be reflected in swagger, as well as the response schema and type are correct.
To describe describe an endpoint as an OpenAPI comment, you should use the yaml structure like this:

- 1 - Define the path
- 2 - Define the http method referent to that path
- 3 - Define the tag in order to group all endpoints referent to the same TAG (in this case, all `users` endpoints will be grouped under the tag `Users`)
- 4 - Define a brief summary of you endpoint
- 5 - Define the response types for the endpoint (200 - OK ; 404 - Not found, etc...)
- 6 - Define the schema of the response

Note: You can define the properties of each schema or reuse a schema that already exists

Each OpenAPI comment should start with `@openapi` in order to be read by swagger and reflected on it.

Example:

```javascript
/**
* @openapi
* '/users':
* get:
* tags:
* - Users
* summary: Obtain a list of all users
* responses:
* '200':
* description: OK
* schema:
* type: array
* items:
* type: object
* properties:
* email:
* type: string
* example: example@example.com
* gender:
* type: string
* example: male
* username:
* type: string
* example: user00000
* first_name:
* type: string
* example: John
* last_name:
* type: string
* example: Doe
* title:
* type: string
* example: mr
*/
```

This describe the get endpoint that we did [here](#how-to-add-an-endpoint).

After doing the OpenAPI comment:

```javascript
import usersList from "../data/users";

module.exports = function (app: core.Express) {
/**
* @openapi
* '/users':
* get:
* tags:
* - Users
* summary: Obtain a list of all users
* responses:
* '200':
* description: OK
* schema:
* type: array
* items:
* type: object
* properties:
* email:
* type: string
* example: example@example.com
* gender:
* type: string
* example: male
* username:
* type: string
* example: user00000
* first_name:
* type: string
* example: John
* last_name:
* type: string
* example: Doe
* title:
* type: string
* example: mr
*/
app.get("/users", (req: Request, res: Response) => {
res.json({
users: usersList,
});
});
};
```

0 comments on commit 3a3b01c

Please sign in to comment.