Skip to content

Commit

Permalink
feat: add eslint and prettier
Browse files Browse the repository at this point in the history
feat: add vercel server route

fix(ci): add tsup config for vercel

fix(ci): setup the api folder as the vercel convention

fix(ci): copy vercel config after build

refactor: add prefix to /api to all routes

fix(ci): change output as the root directory for vercel build

fix(ci): rewrite all routes to vercel api folder

refactor: remove vercel deployment
  • Loading branch information
jellydn committed Sep 24, 2023
1 parent c95908e commit eae5856
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 93 deletions.
7 changes: 7 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
extends: ["productsway/typescript"],
rules: {
"@typescript-eslint/naming-convention": "off",
},
ignorePatterns: ["dist", ".eslintrc.cjs", "vite.config.ts"],
};
6 changes: 6 additions & 0 deletions .prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
importOrder: ["^@core/(.*)$", "^@server/(.*)$", "^@fastify/(.*)$", "^[./]"],
importOrderSeparation: true,
importOrderSortSpecifiers: true,
plugins: ["@trivago/prettier-plugin-sort-imports"],
};
71 changes: 37 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,25 @@ This repository provides a guide to set up continuous integration for a [Fastify
## Usage

1. **Clone the Repository**
```bash
git clone https://github.com/jellydn/stepci-demo.git
```


```bash
git clone https://github.com/jellydn/stepci-demo.git
```

2. **Install Dependencies**
```bash
bun install
```


```bash
bun install
```

3. **Run Tests Locally**
```bash
bun run start
bun run test:e2e
```

[![Demo](https://i.gyazo.com/10b1e6a520bac9044e4db5d5faacebb4.gif)](https://gyazo.com/10b1e6a520bac9044e4db5d5faacebb4)
```bash
bun run start
bun run test:e2e
```

[![Demo](https://i.gyazo.com/10b1e6a520bac9044e4db5d5faacebb4.gif)](https://gyazo.com/10b1e6a520bac9044e4db5d5faacebb4)

## Get Started

Expand All @@ -35,35 +38,35 @@ Run your tests using either Node, Docker, or GitHub Actions.
### Node

- **Run Workflow via CLI**
```bash
npx stepci run workflow.yml
```
```bash
npx stepci run workflow.yml
```

### Docker

- **Run Workflow via Docker**
```bash
docker run \
-v "$(pwd)"/tests:/tests \
ghcr.io/stepci/stepci \
tests/workflow.yml
```
```bash
docker run \
-v "$(pwd)"/tests:/tests \
ghcr.io/stepci/stepci \
tests/workflow.yml
```

### GitHub Actions

- **Automated Workflow on Push**
```yaml
on: [push]
jobs:
api_test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Step CI Action
uses: stepci/stepci@main
with:
workflow: "workflow.yml"
```
```yaml
on: [push]
jobs:
api_test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Step CI Action
uses: stepci/stepci@main
with:
workflow: "workflow.yml"
```
## Resources
Expand Down
3 changes: 3 additions & 0 deletions app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import startServer from "./server";

startServer().catch(console.error);
Binary file modified bun.lockb
Binary file not shown.
22 changes: 15 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
{
"name": "stepci-demo",
"type": "module",
"module": "index.ts",
"module": "app.ts",
"scripts": {
"dev": "tsx watch index.ts",
"build": "tsup index.ts --clean --format esm",
"dev": "tsx watch app.ts",
"build": "tsup app.ts --clean --format esm,cjs",
"ci": "run-p --race test:*",
"test:server": "NODE_DEVELOPMENT=test tsx index.ts",
"format": "prettier --write .",
"lint": "eslint . --ext .ts,.tsx",
"test:server": "NODE_DEVELOPMENT=test tsx app.ts",
"test:e2e": "wait-on -l tcp:3000 && stepci run workflow.yml",
"generate": "stepci generate ./specs/openapi.yaml ./specs/generated.yaml",
"start": "tsx index.ts",
"start:prod": "node dist/index.js",
"vercel-build": "tsup index.ts --clean --format esm --env.NODE_ENV production --env.DEPLOYMENT_ENV vercel --out-dir api"
"start": "tsx app.ts",
"start:prod": "node dist/app.js"
},
"dependencies": {
"@fastify/cors": "8.4.0",
Expand All @@ -20,11 +21,18 @@
"fastify": "4.23.2"
},
"devDependencies": {
"@trivago/prettier-plugin-sort-imports": "^4.2.0",
"@typescript-eslint/eslint-plugin": "^6.7.2",
"@typescript-eslint/parser": "^6.7.2",
"bun-types": "latest",
"eslint": "^8.50.0",
"eslint-config-productsway": "^1.3.0",
"npm-run-all": "4.1.5",
"prettier": "^3.0.3",
"stepci": "2.6.8",
"tsup": "7.2.0",
"tsx": "3.12.10",
"typescript": "^5.2.2",
"wait-on": "7.0.1"
},
"peerDependencies": {
Expand Down
43 changes: 19 additions & 24 deletions index.ts → server.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
// Import the framework and instantiate it
import Fastify, {
FastifyInstance,
FastifyReply,
FastifyRequest,
RouteShorthandOptions,
import fastify, {
type FastifyInstance,
type FastifyReply,
type FastifyRequest,
type RouteShorthandOptions,
} from "fastify";

import cors from "@fastify/cors";
import swagger from "@fastify/swagger";
import swaggerUI from "@fastify/swagger-ui";

const server: FastifyInstance = Fastify({
logger: process.env.NODE_DEVELOPMENT === "test" ? false : true,
const server: FastifyInstance = fastify({
logger: process.env.NODE_DEVELOPMENT !== "test",
});

// Register plugins
await server.register(cors);
void server.register(cors);

await server.register(swagger, {
void server.register(swagger, {
swagger: {
info: {
title: "Test swagger",
Expand All @@ -34,7 +35,7 @@ await server.register(swagger, {
},
});

await server.register(swaggerUI, {
void server.register(swaggerUI, {
routePrefix: "/documentation",
uiConfig: {
docExpansion: "full",
Expand All @@ -44,16 +45,13 @@ await server.register(swaggerUI, {
});

// Health check
server.get("/", async (_request, _reply) => {
return { ok: true };
});

server.get("/api", async (_request, _reply) => ({ ok: true }));

// Declare a route
server.get<{
Querystring: { name: string };
}>("/hello", async function handler(request) {
const name = request.query.name;
}>("/api/hello", async (request) => {
const { name } = request.query;
return { hello: name ?? "world" };
});

Expand All @@ -73,11 +71,11 @@ const opts: RouteShorthandOptions = {
};

server.get(
"/ping",
"/api/ping",
opts,
async (_request: FastifyRequest, _reply: FastifyReply) => {
return { pong: "it worked!" };
}
async (_request: FastifyRequest, _reply: FastifyReply) => ({
pong: "it worked!",
}),
);

async function startServer() {
Expand All @@ -94,9 +92,6 @@ async function startServer() {
server.log.error(err);
process.exit(1);
}
};

if (process.env.DEPLOYMENT_ENV !== 'vercel') startServer();
}

export default startServer;

6 changes: 3 additions & 3 deletions specs/generated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ tests:
name: Default
steps:
- http:
url: /
url: /api
method: GET
schema:
type: object
properties:
ok: bool
- http:
url: /hello
url: /api/hello
method: GET
check:
status: 200
schema:
type: object
description: Successful response with a JSON object
- http:
url: /ping
url: /api/ping
method: GET
check:
status: 200
Expand Down
6 changes: 3 additions & 3 deletions specs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ externalDocs:
servers:
- url: http://localhost:3000
paths:
/:
/api:
get:
responses:
200:
description: Default Response
content: {}
/hello:
/api/hello:
get:
responses:
200:
Expand All @@ -25,7 +25,7 @@ paths:
schema:
type: string
description: Successful response with a JSON object
/ping:
/api/ping:
get:
responses:
200:
Expand Down
19 changes: 6 additions & 13 deletions specs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"definitions": {},
"paths": {
"/": {
"/api": {
"get": {
"responses": {
"200": {
Expand All @@ -16,7 +16,7 @@
}
}
},
"/hello": {
"/api/hello": {
"get": {
"responses": {
"200": {
Expand All @@ -25,7 +25,7 @@
}
}
},
"/ping": {
"/api/ping": {
"get": {
"responses": {
"200": {
Expand All @@ -44,16 +44,9 @@
}
},
"host": "localhost:3000",
"schemes": [
"http",
"https"
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"schemes": ["http", "https"],
"consumes": ["application/json"],
"produces": ["application/json"],
"externalDocs": {
"url": "https://swagger.io",
"description": "Find more info here"
Expand Down
8 changes: 0 additions & 8 deletions vercel.json

This file was deleted.

2 changes: 1 addition & 1 deletion workflow.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "1.1"
name: Api Check
env:
host: localhost:3000
host: localhost:3000/api
tests:
ping:
steps:
Expand Down

0 comments on commit eae5856

Please sign in to comment.