mirror of
https://github.com/typicode/json-server.git
synced 2025-07-24 18:55:57 +08:00
Update deps and allow any header in CORS (#1611)
This commit is contained in:
1698
package-lock.json
generated
1698
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
38
package.json
38
package.json
@ -18,7 +18,7 @@
|
||||
"dev": "tsx watch src/bin.ts fixtures/db.json",
|
||||
"build": "rm -rf lib && tsc",
|
||||
"test": "node --import tsx/esm --test src/*.test.ts",
|
||||
"lint": "eslint src --ext .ts --ignore-path .gitignore",
|
||||
"lint": "eslint src",
|
||||
"prepare": "husky",
|
||||
"prepublishOnly": "npm run build"
|
||||
},
|
||||
@ -30,34 +30,34 @@
|
||||
"url": "git+https://github.com/typicode/json-server.git"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9.9.0",
|
||||
"@sindresorhus/tsconfig": "^5.0.0",
|
||||
"@tailwindcss/typography": "^0.5.13",
|
||||
"@types/node": "^20.14.1",
|
||||
"@typicode/eslint-config": "^2.0.0",
|
||||
"concurrently": "^8.2.2",
|
||||
"eslint": "^8.57.0",
|
||||
"@eslint/js": "^9.11.0",
|
||||
"@sindresorhus/tsconfig": "^6.0.0",
|
||||
"@tailwindcss/typography": "^0.5.15",
|
||||
"@types/node": "^22.5.5",
|
||||
"concurrently": "^9.0.1",
|
||||
"eslint": "^9.11.0",
|
||||
"get-port": "^7.1.0",
|
||||
"globals": "^15.9.0",
|
||||
"husky": "^9.0.11",
|
||||
"husky": "^9.1.6",
|
||||
"tempy": "^3.1.0",
|
||||
"tsx": "^4.11.2",
|
||||
"type-fest": "^4.18.3",
|
||||
"typescript": "^5.4.5",
|
||||
"typescript-eslint": "^8.1.0"
|
||||
"tsx": "^4.19.1",
|
||||
"type-fest": "^4.26.1",
|
||||
"typescript": "^5.6.2",
|
||||
"typescript-eslint": "^8.6.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@tinyhttp/app": "^2.2.3",
|
||||
"@tinyhttp/cors": "^2.0.0",
|
||||
"@tinyhttp/app": "^2.4.0",
|
||||
"@tinyhttp/cors": "^2.0.1",
|
||||
"@tinyhttp/logger": "^2.0.0",
|
||||
"chalk": "^5.3.0",
|
||||
"chokidar": "^3.6.0",
|
||||
"chokidar": "^4.0.1",
|
||||
"dot-prop": "^9.0.0",
|
||||
"eta": "^3.4.0",
|
||||
"eta": "^3.5.0",
|
||||
"inflection": "^3.0.0",
|
||||
"json5": "^2.2.3",
|
||||
"lowdb": "^7.0.1",
|
||||
"milliparsec": "^2.3.0",
|
||||
"milliparsec": "^4.0.0",
|
||||
"sirv": "^2.0.4",
|
||||
"sort-on": "^6.0.0"
|
||||
"sort-on": "^6.1.0"
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ import { createApp } from './app.js'
|
||||
import { Data } from './service.js'
|
||||
|
||||
type Test = {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
|
||||
method: HTTPMethods
|
||||
url: string
|
||||
statusCode: number
|
||||
|
55
src/app.ts
55
src/app.ts
@ -37,9 +37,18 @@ export function createApp(db: Low<Data>, options: AppOptions = {}) {
|
||||
.forEach((dir) => app.use(sirv(dir, { dev: !isProduction })))
|
||||
|
||||
// CORS
|
||||
app.use(cors()).options('*', cors())
|
||||
app
|
||||
.use((req, res, next) => {
|
||||
return cors({
|
||||
allowedHeaders: req.headers['access-control-request-headers']
|
||||
?.split(',')
|
||||
.map((h) => h.trim()),
|
||||
})(req, res, next)
|
||||
})
|
||||
.options('*', cors())
|
||||
|
||||
// Body parser
|
||||
// @ts-expect-error expected
|
||||
app.use(json())
|
||||
|
||||
app.get('/', (_req, res) =>
|
||||
@ -48,24 +57,28 @@ export function createApp(db: Low<Data>, options: AppOptions = {}) {
|
||||
|
||||
app.get('/:name', (req, res, next) => {
|
||||
const { name = '' } = req.params
|
||||
const query = Object.fromEntries(Object.entries(req.query)
|
||||
.map(([key, value]) => {
|
||||
if (['_start', '_end', '_limit', '_page', '_per_page'].includes(key) && typeof value === 'string') {
|
||||
return [key, parseInt(value)]
|
||||
} else {
|
||||
return [key, value]
|
||||
}
|
||||
})
|
||||
.filter(([_, value]) => !Number.isNaN(value))
|
||||
const query = Object.fromEntries(
|
||||
Object.entries(req.query)
|
||||
.map(([key, value]) => {
|
||||
if (
|
||||
['_start', '_end', '_limit', '_page', '_per_page'].includes(key) &&
|
||||
typeof value === 'string'
|
||||
) {
|
||||
return [key, parseInt(value)]
|
||||
} else {
|
||||
return [key, value]
|
||||
}
|
||||
})
|
||||
.filter(([, value]) => !Number.isNaN(value)),
|
||||
)
|
||||
res.locals['data'] = service.find(name, query)
|
||||
next()
|
||||
next?.()
|
||||
})
|
||||
|
||||
app.get('/:name/:id', (req, res, next) => {
|
||||
const { name = '', id = '' } = req.params
|
||||
res.locals['data'] = service.findById(name, id, req.query)
|
||||
next()
|
||||
next?.()
|
||||
})
|
||||
|
||||
app.post('/:name', async (req, res, next) => {
|
||||
@ -73,7 +86,7 @@ export function createApp(db: Low<Data>, options: AppOptions = {}) {
|
||||
if (isItem(req.body)) {
|
||||
res.locals['data'] = await service.create(name, req.body)
|
||||
}
|
||||
next()
|
||||
next?.()
|
||||
})
|
||||
|
||||
app.put('/:name', async (req, res, next) => {
|
||||
@ -81,7 +94,7 @@ export function createApp(db: Low<Data>, options: AppOptions = {}) {
|
||||
if (isItem(req.body)) {
|
||||
res.locals['data'] = await service.update(name, req.body)
|
||||
}
|
||||
next()
|
||||
next?.()
|
||||
})
|
||||
|
||||
app.put('/:name/:id', async (req, res, next) => {
|
||||
@ -89,7 +102,7 @@ export function createApp(db: Low<Data>, options: AppOptions = {}) {
|
||||
if (isItem(req.body)) {
|
||||
res.locals['data'] = await service.updateById(name, id, req.body)
|
||||
}
|
||||
next()
|
||||
next?.()
|
||||
})
|
||||
|
||||
app.patch('/:name', async (req, res, next) => {
|
||||
@ -97,7 +110,7 @@ export function createApp(db: Low<Data>, options: AppOptions = {}) {
|
||||
if (isItem(req.body)) {
|
||||
res.locals['data'] = await service.patch(name, req.body)
|
||||
}
|
||||
next()
|
||||
next?.()
|
||||
})
|
||||
|
||||
app.patch('/:name/:id', async (req, res, next) => {
|
||||
@ -105,13 +118,17 @@ export function createApp(db: Low<Data>, options: AppOptions = {}) {
|
||||
if (isItem(req.body)) {
|
||||
res.locals['data'] = await service.patchById(name, id, req.body)
|
||||
}
|
||||
next()
|
||||
next?.()
|
||||
})
|
||||
|
||||
app.delete('/:name/:id', async (req, res, next) => {
|
||||
const { name = '', id = '' } = req.params
|
||||
res.locals['data'] = await service.destroyById(name, id, req.query['_dependent'])
|
||||
next()
|
||||
res.locals['data'] = await service.destroyById(
|
||||
name,
|
||||
id,
|
||||
req.query['_dependent'],
|
||||
)
|
||||
next?.()
|
||||
})
|
||||
|
||||
app.use('/:name', (req, res) => {
|
||||
|
@ -447,7 +447,7 @@ export class Service {
|
||||
const item = items.find((item) => item['id'] === id)
|
||||
if (item === undefined) return
|
||||
const index = items.indexOf(item)
|
||||
items.splice(index, 1)[0]
|
||||
items.splice(index, 1)
|
||||
|
||||
nullifyForeignKey(this.#db, name, id)
|
||||
const dependents = ensureArray(dependent)
|
||||
|
Reference in New Issue
Block a user