-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Oleg Dovger
committed
Feb 29, 2024
1 parent
5b5fbf4
commit cd7e9b9
Showing
7 changed files
with
372 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,6 @@ npm_debug.log* | |
# vscode | ||
.vscode | ||
|
||
# idea | ||
.idea | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore | ||
|
||
# Logs | ||
|
||
logs | ||
_.log | ||
npm-debug.log_ | ||
yarn-debug.log* | ||
yarn-error.log* | ||
lerna-debug.log* | ||
.pnpm-debug.log* | ||
|
||
# Caches | ||
|
||
.cache | ||
|
||
# Diagnostic reports (https://nodejs.org/api/report.html) | ||
|
||
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json | ||
|
||
# Runtime data | ||
|
||
pids | ||
_.pid | ||
_.seed | ||
*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
|
||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
|
||
coverage | ||
*.lcov | ||
|
||
# nyc test coverage | ||
|
||
.nyc_output | ||
|
||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) | ||
|
||
.grunt | ||
|
||
# Bower dependency directory (https://bower.io/) | ||
|
||
bower_components | ||
|
||
# node-waf configuration | ||
|
||
.lock-wscript | ||
|
||
# Compiled binary addons (https://nodejs.org/api/addons.html) | ||
|
||
build/Release | ||
|
||
# Dependency directories | ||
|
||
node_modules/ | ||
jspm_packages/ | ||
bun.lockb | ||
|
||
# Snowpack dependency directory (https://snowpack.dev/) | ||
|
||
web_modules/ | ||
|
||
# TypeScript cache | ||
|
||
*.tsbuildinfo | ||
|
||
# Optional npm cache directory | ||
|
||
.npm | ||
|
||
# Optional eslint cache | ||
|
||
.eslintcache | ||
|
||
# Optional stylelint cache | ||
|
||
.stylelintcache | ||
|
||
# Microbundle cache | ||
|
||
.rpt2_cache/ | ||
.rts2_cache_cjs/ | ||
.rts2_cache_es/ | ||
.rts2_cache_umd/ | ||
|
||
# Optional REPL history | ||
|
||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
|
||
*.tgz | ||
|
||
# Yarn Integrity file | ||
|
||
.yarn-integrity | ||
|
||
# dotenv environment variable files | ||
|
||
.env | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
.env.local | ||
|
||
# parcel-bundler cache (https://parceljs.org/) | ||
|
||
.parcel-cache | ||
|
||
# Next.js build output | ||
|
||
.next | ||
out | ||
|
||
# Nuxt.js build / generate output | ||
|
||
.nuxt | ||
dist | ||
|
||
# Gatsby files | ||
|
||
# Comment in the public line in if your project uses Gatsby and not Next.js | ||
|
||
# https://nextjs.org/blog/next-9-1#public-directory-support | ||
|
||
# public | ||
|
||
# vuepress build output | ||
|
||
.vuepress/dist | ||
|
||
# vuepress v2.x temp and cache directory | ||
|
||
.temp | ||
|
||
# Docusaurus cache and generated files | ||
|
||
.docusaurus | ||
|
||
# Serverless directories | ||
|
||
.serverless/ | ||
|
||
# FuseBox cache | ||
|
||
.fusebox/ | ||
|
||
# DynamoDB Local files | ||
|
||
.dynamodb/ | ||
|
||
# TernJS port file | ||
|
||
.tern-port | ||
|
||
# Stores VSCode versions used for testing VSCode extensions | ||
|
||
.vscode-test | ||
|
||
# yarn v2 | ||
|
||
.yarn/cache | ||
.yarn/unplugged | ||
.yarn/build-state.yml | ||
.yarn/install-state.gz | ||
.pnp.* | ||
|
||
# IntelliJ based IDEs | ||
.idea | ||
|
||
# Finder (MacOS) folder config | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# openapi-backend-bun | ||
|
||
To install dependencies: | ||
|
||
```bash | ||
bun install | ||
``` | ||
|
||
To run: | ||
|
||
```bash | ||
bun run index.ts | ||
``` | ||
|
||
This project was created using `bun init` in bun v1.0.28. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import {$} from 'bun'; | ||
import axios from 'axios'; | ||
import type { AxiosInstance } from 'axios'; | ||
import type { Subprocess } from 'bun'; | ||
import {test, expect, describe, beforeAll, afterAll} from "bun:test"; | ||
|
||
async function waitForPort(port: number, retryInterval = 1000, timeout = 30000) { | ||
const start = Date.now(); | ||
|
||
while (true) { | ||
try { | ||
// Attempt to fetch from the server | ||
await fetch(`http://localhost:${port}`); | ||
// If successful, the port is open; break out of the loop | ||
console.log(`Port ${port} is now open.`); | ||
break; | ||
} catch (e) { | ||
// If there's an error (likely connection refused), the port isn't open yet | ||
if (Date.now() - start > timeout) { | ||
// If we've exceeded our timeout, throw an error | ||
throw new Error(`Timeout waiting for port ${port}`); | ||
} | ||
// Wait for `retryInterval` milliseconds before trying again | ||
await new Promise(resolve => setTimeout(resolve, retryInterval)); | ||
} | ||
} | ||
} | ||
|
||
describe('bun-ts example', () => { | ||
let proc: Subprocess<"ignore", "pipe", "inherit">; | ||
let client: AxiosInstance; | ||
|
||
beforeAll(async () => { | ||
client = axios.create({ baseURL: 'http://localhost:9000', validateStatus: () => true }); | ||
proc = Bun.spawn(["bun", "run", "index.ts"]); | ||
proc.unref(); | ||
|
||
await waitForPort(9000); | ||
}); | ||
|
||
afterAll(async () => { | ||
proc.kill(); | ||
}); | ||
|
||
test('GET /pets returns 200 with matched operation', async () => { | ||
const res = await client.get('/pets'); | ||
expect(res.status).toBe(200); | ||
expect(res.data).toEqual({ operationId: 'getPets' }); | ||
}); | ||
|
||
test('GET /pets/1 returns 200 with matched operation', async () => { | ||
const res = await client.get('/pets/1'); | ||
expect(res.status).toBe(200); | ||
expect(res.data).toEqual({ operationId: 'getPetById' }); | ||
}); | ||
|
||
test('GET /pets/1a returns 400 with validation error', async () => { | ||
const res = await client.get('/pets/1a'); | ||
expect(res.status).toBe(400); | ||
expect(res.data).toHaveProperty('err'); | ||
}); | ||
|
||
test('GET /unknown returns 404', async () => { | ||
const res = await client.get('/unknown'); | ||
expect(res.status).toBe(404); | ||
expect(res.data).toHaveProperty('err'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import OpenAPIBackend from "openapi-backend"; | ||
import {type Serve} from "bun"; | ||
|
||
const api = new OpenAPIBackend({ | ||
definition: { | ||
openapi: '3.0.1', | ||
info: { | ||
title: 'My API', | ||
version: '1.0.0', | ||
}, | ||
paths: { | ||
'/pets': { | ||
get: { | ||
operationId: 'getPets', | ||
responses: { | ||
200: { description: 'ok' }, | ||
}, | ||
}, | ||
}, | ||
'/pets/{id}': { | ||
get: { | ||
operationId: 'getPetById', | ||
responses: { | ||
200: { description: 'ok' }, | ||
}, | ||
}, | ||
parameters: [ | ||
{ | ||
name: 'id', | ||
in: 'path', | ||
required: true, | ||
schema: { | ||
type: 'integer', | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
handlers: { | ||
getPets: async (c, req, res) => Response.json({ operationId: c.operation.operationId }), | ||
getPetById: async (c, req, res) => Response.json({ operationId: c.operation.operationId }), | ||
validationFail: async (c, req, res) => Response.json({ err: c.validation.errors }, {status: 400}), | ||
notFound: async (c, req, res) => Response.json({ err: 'not found' }, {status: 404}), | ||
}, | ||
}); | ||
|
||
api.init(); | ||
|
||
export default { | ||
port: 9000, | ||
fetch(req) { | ||
const {pathname, search} = new URL(req.url); | ||
|
||
return api.handleRequest({ | ||
path: pathname, | ||
query: search, | ||
method: req.method, | ||
headers: req.headers.toJSON(), | ||
body: req.body | ||
}, req, new Response(null, {status: 200})); | ||
}, | ||
} satisfies Serve; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "openapi-backend-bun", | ||
"module": "index.ts", | ||
"type": "module", | ||
"scripts": { | ||
"dev": "bun run index.ts --watch" | ||
}, | ||
"devDependencies": { | ||
"@types/bun": "latest", | ||
"@types/wait-on": "^5.3.4", | ||
"axios": "^1.6.7", | ||
"wait-on": "^7.2.0" | ||
}, | ||
"peerDependencies": { | ||
"typescript": "^5.0.0" | ||
}, | ||
"dependencies": { | ||
"openapi-backend": "^5.10.6" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"compilerOptions": { | ||
// Enable latest features | ||
"lib": ["ESNext", "DOM"], | ||
"target": "ESNext", | ||
"module": "ESNext", | ||
"moduleDetection": "force", | ||
"jsx": "react-jsx", | ||
"allowJs": true, | ||
|
||
// Bundler mode | ||
"moduleResolution": "bundler", | ||
"allowImportingTsExtensions": true, | ||
"verbatimModuleSyntax": true, | ||
"noEmit": true, | ||
|
||
// Best practices | ||
"strict": true, | ||
"skipLibCheck": true, | ||
"noFallthroughCasesInSwitch": true, | ||
|
||
// Some stricter flags (disabled by default) | ||
"noUnusedLocals": false, | ||
"noUnusedParameters": false, | ||
"noPropertyAccessFromIndexSignature": false | ||
} | ||
} |