Skip to content

Commit 9b8f2fa

Browse files
committed
source code
0 parents  commit 9b8f2fa

38 files changed

+4356
-0
lines changed

.editorconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true
6+

.github/workflows/code-quality.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Code Quality
2+
3+
on:
4+
push:
5+
branches:
6+
- '*'
7+
8+
jobs:
9+
check:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout Code
14+
uses: actions/checkout@v4
15+
16+
- name: Set up Node.js
17+
uses: actions/setup-node@v4
18+
with:
19+
node-version: "22"
20+
21+
- name: Install Dependencies
22+
run: npm ci
23+
24+
- name: Lint
25+
run: npm run lint
26+
27+
- name: Format
28+
run: npm run format

.github/workflows/release.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
branches: [main]
6+
7+
jobs:
8+
deploy:
9+
runs-on: ubuntu-latest
10+
environment: production
11+
12+
steps:
13+
- name: Checkout Repository
14+
uses: actions/checkout@v4
15+
16+
- name: Deploy to Vercel
17+
env:
18+
VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
19+
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
20+
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
21+
run: npx vercel deploy --token $VERCEL_TOKEN --prod --confirm

.gitignore

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# Dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# Testing
9+
/coverage
10+
11+
# Production
12+
build
13+
dist
14+
15+
# Misc
16+
.DS_Store
17+
*.pem
18+
19+
# Debug
20+
npm-debug.log*
21+
yarn-debug.log*
22+
yarn-error.log*
23+
24+
# Local ENV files
25+
.env.local
26+
.env.development.local
27+
.env.test.local
28+
.env.production.local
29+
30+
# Vercel
31+
.vercel
32+
33+
# Turborepo
34+
.turbo
35+
36+
# typescript
37+
*.tsbuildinfo
38+
39+
# .env
40+
.env

.husky/pre-commit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
npm run lint
2+
npm run format

.vscode/extensions.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"recommendations": [
3+
"editorconfig.editorconfig",
4+
"dbaeumer.vscode-eslint",
5+
"esbenp.prettier-vscode"
6+
]
7+
}

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"editor.defaultFormatter": "esbenp.prettier-vscode",
3+
"editor.codeActionsOnSave": {
4+
"source.fixAll.eslint": "explicit"
5+
}
6+
}

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Sample Posts
2+
3+
Sample Posts is an API that provides sample content for our [front-end challenge][].
4+
5+
## Usage
6+
7+
If you're looking for sample content, you can request it directly from our production endpoint:
8+
9+
👉 [https://sample-posts.coderockr.com/api/posts][]
10+
11+
📷 [https://sample-posts.coderockr.com/posts/post_1.jpg][] (for images)
12+
13+
If you wish to customize or modify the API behaviors or content, follow the Getting Started guide to run it locally.
14+
15+
## Getting Started
16+
17+
Before start make sure that you have [Node.js 22][] and [Vercel CLI][] installed and configured.
18+
19+
1. Clone this repository
20+
2. Install the project dependencies `npm install`
21+
3. Run it `vercel dev`
22+
23+
[front-end challenge]: https://github.com/Coderockr/frontend-test
24+
[https://sample-posts.coderockr.com/api/posts]: https://sample-posts.coderockr.com/api/posts
25+
[https://sample-posts.coderockr.com/posts/post_1.jpg]: https://sample-posts.coderockr.com/posts/post_1.jpg
26+
[Node.js 22]: https://nodejs.org
27+
[Vercel CLI]: https://vercel.com/docs/cli

api/posts.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { VercelRequest, VercelResponse } from "@vercel/node";
2+
import { readFileSync } from "node:fs";
3+
import { join } from "node:path";
4+
import { allowCors } from "../cors";
5+
6+
const handler = async (req: VercelRequest, res: VercelResponse) => {
7+
if (req.method != "GET") {
8+
res.status(405).end();
9+
return;
10+
}
11+
12+
const content = readFileSync(join(process.cwd(), "posts.json"), "utf8");
13+
const posts = JSON.parse(content);
14+
15+
res.setHeader("Content-Type", "application/json");
16+
res.status(200).json(posts);
17+
};
18+
19+
export default allowCors(handler);

cors.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { VercelRequest, VercelResponse } from "@vercel/node";
2+
3+
const allowCors =
4+
(fn: (req: VercelRequest, res: VercelResponse) => Promise<void>) =>
5+
async (req: VercelRequest, res: VercelResponse) => {
6+
res.setHeader("Access-Control-Allow-Credentials", "true");
7+
res.setHeader("Access-Control-Allow-Origin", "*");
8+
9+
res.setHeader(
10+
"Access-Control-Allow-Methods",
11+
"GET,OPTIONS,PATCH,DELETE,POST,PUT",
12+
);
13+
14+
res.setHeader(
15+
"Access-Control-Allow-Headers",
16+
"X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version",
17+
);
18+
19+
if (req.method === "OPTIONS") {
20+
res.status(200).end();
21+
return;
22+
}
23+
24+
return await fn(req, res);
25+
};
26+
27+
export { allowCors };

0 commit comments

Comments
 (0)