Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions .eslintrc.json

This file was deleted.

2 changes: 1 addition & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"recommendations": [
"dbaeumer.vscode-eslint"
"biomejs.biome"
]
}
63 changes: 63 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"$schema": "https://biomejs.dev/schemas/2.4.13/schema.json",
"vcs": {
"enabled": true,
"clientKind": "git",
"useIgnoreFile": true
},
"files": {
"includes": ["packages/*/src/**/*.ts", "examples/*.ts", "scripts/**/*.ts", "scripts/**/*.js"]
},
"formatter": {
"enabled": true,
"indentStyle": "space",
"indentWidth": 2,
"lineWidth": 120
},
"javascript": {
"formatter": {
"quoteStyle": "single",
"semicolons": "asNeeded",
"trailingCommas": "none",
"arrowParentheses": "always"
}
},
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"suspicious": {
"noExplicitAny": "off",
"noAssignInExpressions": "off"
},
"style": {
"noParameterAssign": "off",
"useNodejsImportProtocol": "off"
},
"correctness": {
"noUnusedVariables": {
"level": "error",
"options": {
"ignoreRestSiblings": true
}
}
}
}
},
"overrides": [
{
"includes": ["**/*.test.ts"],
"linter": {
"rules": {
"suspicious": {
"noExplicitAny": "off"
},
"correctness": {
"noUnusedVariables": "off",
"noUnusedFunctionParameters": "off"
}
}
}
}
]
}
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zhttp/docs",
"private": true,
"version": "2.0.1-refactor-remove-cjs-support.0",
"version": "2.0.1-chore-migrate-to-biome.1",
"scripts": {
"fill-code-snippets": "npx embedme ./pages/**/*.*",
"prebuild": "npm run fill-code-snippets",
Expand Down
45 changes: 19 additions & 26 deletions examples/basic-usage.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import { apiResponse, controller, get, openapiController, Server, zApiOutput } from '@zhttp/core'
import { z } from 'zod'
import {
Server,
controller,
get,
zApiOutput,
apiResponse,
openapiController
} from '@zhttp/core'

// You can optionally add OAS info to a Zod schema using zodSchema.openapi(...).
// If this schema is used in the input or output of an endpoint, the info
// will be included in the generated openapi spec.

const zHelloResponse = zApiOutput(z.object({
greeting: z.string().openapi({ example: 'Hello Joske!' })
})).openapi('HelloResponse')
const zHelloResponse = zApiOutput(
z.object({
greeting: z.string().openapi({ example: 'Hello Joske!' })
})
).openapi('HelloResponse')

const helloController = controller('Hello')
.description('This controller says hello to everyone')
const helloController = controller('Hello').description('This controller says hello to everyone')

helloController.endpoint(
get('/hello')
Expand All @@ -35,19 +29,18 @@ helloController.endpoint(
})
)

const server = new Server({
controllers: [
helloController,
openapiController
],
middlewares: []
}, {
port: 3000,
oasInfo: {
title: 'A very cool api',
version: '1.0.0'
const server = new Server(
{
controllers: [helloController, openapiController],
middlewares: []
},
{
port: 3000,
oasInfo: {
title: 'A very cool api',
version: '1.0.0'
}
}
})
)

// eslint-disable-next-line @typescript-eslint/no-floating-promises
server.start()
13 changes: 7 additions & 6 deletions examples/concept-controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { z } from 'zod'
import { controller, get } from '@zhttp/core'
import { z } from 'zod'

export const greetingController = controller('greeting')
.description('A controller that greets the world.')
export const greetingController = controller('greeting').description('A controller that greets the world.')

greetingController.endpoint(
get('/hello', 'getGreeting')
Expand All @@ -12,9 +11,11 @@ greetingController.endpoint(
name: z.string().optional()
})
})
.response(z.object({
message: z.string()
}))
.response(
z.object({
message: z.string()
})
)
.handler(async ({ query }) => {
return {
message: `Hello ${query.name ?? 'everyone'}!`
Expand Down
2 changes: 1 addition & 1 deletion examples/concept-endpoint.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from 'zod'
import { endpoint, get } from '@zhttp/core'
import { z } from 'zod'

const zGreetingOutput = z.object({
message: z.string()
Expand Down
11 changes: 6 additions & 5 deletions examples/concept-middleware.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { type Request, type Response, type NextFunction } from 'express'
import { middleware, MiddlewareTypes } from '@zhttp/core'
import { MiddlewareTypes, middleware } from '@zhttp/core'
import type { NextFunction, Request, Response } from 'express'

export const lastVisitMiddleware = middleware({
name: 'lastVisitMiddleware',
type: MiddlewareTypes.BEFORE,
handler (req: Request, res: Response, next: NextFunction) {
handler(req: Request, res: Response, next: NextFunction) {
const now = new Date()
const lastVisitCookieValue = req.cookies.beenHereBefore
const lastVisitTime = lastVisitCookieValue != null ? new Date(String(lastVisitCookieValue)) : undefined
res.cookie('beenHereBefore', now.toISOString())
if (lastVisitTime == null) {
console.log('Seems like we\'ve got a new user 👀')
next(); return
console.log("Seems like we've got a new user 👀")
next()
return
}
const daysSinceLastVisit = (now.getTime() - lastVisitTime.getTime()) / (1000 * 60 * 60 * 24)
console.log(`It's been ${daysSinceLastVisit} days since this user last visited.`)
Expand Down
16 changes: 9 additions & 7 deletions examples/concept-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { Server } from '@zhttp/core'
import { greetingController } from './concept-controller.js'
import { lastVisitMiddleware } from './concept-middleware.js'

export const server = new Server({
controllers: [greetingController],
middlewares: [lastVisitMiddleware]
}, {
port: 8080
})
export const server = new Server(
{
controllers: [greetingController],
middlewares: [lastVisitMiddleware]
},
{
port: 8080
}
)

// eslint-disable-next-line @typescript-eslint/no-floating-promises
server.start()
10 changes: 6 additions & 4 deletions examples/concepts-errors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from 'zod'
import { controller, get } from '@zhttp/core'
import { NotFoundError } from '@zhttp/errors'
import { z } from 'zod'

// Let's presume we're talking to some sort of database
const db: any = undefined
Expand All @@ -14,9 +14,11 @@ vegetablesController.endpoint(
vegetableId: z.string().uuid()
})
})
.response(z.object({
message: z.string()
}))
.response(
z.object({
message: z.string()
})
)
.handler(async ({ params: { vegetableId } }) => {
const vegetableDetails = await db.getVegetableById(vegetableId)
if (vegetableDetails == null) {
Expand Down
4 changes: 1 addition & 3 deletions examples/direct-openapi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { server } from './concept-server.js'

console.log(
server.oasInstance.getJsonSpec()
)
console.log(server.oasInstance.getJsonSpec())
18 changes: 11 additions & 7 deletions examples/validation-errors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from 'zod'
import { controller, get } from '@zhttp/core'
import { z } from 'zod'

export const validationExampleController = controller('validationExample')

Expand All @@ -11,9 +11,11 @@ validationExampleController.endpoint(
name: z.string().min(5)
})
})
.response(z.object({
message: z.string()
}))
.response(
z.object({
message: z.string()
})
)
.handler(async ({ query }) => {
return {
message: `Hello ${query.name ?? 'everyone'}!`
Expand All @@ -28,9 +30,11 @@ validationExampleController.endpoint(
name: z.string().optional()
})
})
.response(z.object({
message: z.string()
}))
.response(
z.object({
message: z.string()
})
)
.handler(async (_input) => {
return {
thisKeyShouldntBeHere: 'noBueno'
Expand Down
Loading