Skip to content

Commit

Permalink
can we do this?
Browse files Browse the repository at this point in the history
  • Loading branch information
kwhitley committed Mar 28, 2024
1 parent e3e2ada commit 47ea498
Show file tree
Hide file tree
Showing 32 changed files with 194 additions and 116 deletions.
22 changes: 22 additions & 0 deletions example/types/content.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { IRequestStrict, IRequest, RequestHandler } from 'types'
import { IttyRouter } from 'IttyRouter'
import { withContent } from 'withContent'
import { HasContent } from 'types'

type User = {
user: string
}

const router = IttyRouter()

router
// upstream request sees the request as IRequest (default), so anything goes
.post<HasContent<User>>('/', withContent, ({ content }) => {
content.user
})

.get('/pollution', (request) => {
request.content.user
})

export default router
3 changes: 2 additions & 1 deletion example/types/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IRequestStrict, IRequest, IttyRouter, RequestHandler } from 'IttyRouter'
import { IRequestStrict, IRequest, RequestHandler } from 'types'
import { IttyRouter } from 'IttyRouter'

type UserRequest = {
user: string
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@
"require": "./text.js",
"types": "./text.d.ts"
},
"./types": {
"import": "./types.mjs",
"require": "./types.js",
"types": "./types.d.ts"
},
"./webp": {
"import": "./webp.mjs",
"require": "./webp.js",
Expand Down
1 change: 0 additions & 1 deletion src/IttyRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export const IttyRouter = <
.replace(/\./g, '\\.') // dot in path
.replace(/(\/?)\*/g, '($1.*)?') // wildcard
}/*$`),
// @ts-expect-error - fiddly
handlers, // embed handlers
path, // embed clean route path
]
Expand Down
1 change: 0 additions & 1 deletion src/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export const Router = <
.replace(/\./g, '\\.') // dot in path
.replace(/(\/?)\*/g, '($1.*)?') // wildcard
}/*$`),
// @ts-expect-error - fiddly
handlers, // embed handlers
path, // embed clean route path
]
Expand Down
2 changes: 1 addition & 1 deletion src/cors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IRequest } from 'IttyRouter'
import { IRequest } from './types'

export type CorsOptions = {
credentials?: true
Expand Down
10 changes: 2 additions & 8 deletions src/createResponse.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
export interface ResponseFormatter {
(body?: any, options?: ResponseInit): Response
}

export interface BodyTransformer {
(body: any): string
}
import { ResponseFormatter } from './types'

export const createResponse =
(
format = 'text/plain; charset=utf-8',
transform?: BodyTransformer
transform?: (body: any) => any,
): ResponseFormatter =>
(body, { ...options } = {}) => {
if (body === undefined || body instanceof Response) return body
Expand Down
15 changes: 2 additions & 13 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import { json } from './json'

interface ErrorLike extends Error {
status?: number
[any: string]: any
}

export type ErrorBody = string | object

export interface ErrorFormatter {
(statusCode?: number, body?: ErrorBody): Response
(error: ErrorLike): Response
}
import { ErrorFormatter } from './types'

const getMessage = (code: number): string => ({
400: 'Bad Request',
Expand All @@ -20,7 +9,7 @@ const getMessage = (code: number): string => ({
500: 'Internal Server Error',
})[code] || 'Unknown Error'

export const error: ErrorFormatter = (a = 500, b?: ErrorBody) => {
export const error: ErrorFormatter = (a = 500, b?) => {
// handle passing an Error | StatusError directly in
if (a instanceof Error) {
const { message, ...err } = a
Expand Down
83 changes: 0 additions & 83 deletions src/types.d.ts

This file was deleted.

26 changes: 26 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// routers
export * from './types/AutoRouterOptions'
export * from './types/CustomRoutes'
export * from './types/ErrorHandler'
export * from './types/IRequest'
export * from './types/IRequestStrict'
export * from './types/IttyRouterOptions'
export * from './types/IttyRouterType'
export * from './types/RequestHandler'
export * from './types/ResponseHandler'
export * from './types/RequestLike'
export * from './types/ResponseHandler'
export * from './types/Route'
export * from './types/RouteEntry'
export * from './types/RouterOptions'
export * from './types/RouterType'

// withContent
export * from './types/HasContent'

// createResponse
export * from './types/ResponseFormatter'

// errors
export * from './types/ErrorFormatter'

8 changes: 8 additions & 0 deletions src/types/AutoRouterOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { RequestHandler } from './RequestHandler'
import { ResponseHandler } from './ResponseHandler'
import { RouterOptions } from './RouterOptions'

export type AutoRouterOptions = {
missing?: RequestHandler
format?: ResponseHandler
} & RouterOptions
5 changes: 5 additions & 0 deletions src/types/CustomRoutes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Route } from './Route'

export type CustomRoutes<R = Route> = {
[key: string]: R
}
11 changes: 11 additions & 0 deletions src/types/ErrorFormatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
interface ErrorLike extends Error {
status?: number
[any: string]: any
}

type ErrorBody = string | object

export interface ErrorFormatter {
(statusCode?: number, body?: ErrorBody): Response
(error: ErrorLike): Response
}
4 changes: 4 additions & 0 deletions src/types/ErrorHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { IRequest } from './IRequest'

export type ErrorHandler<ErrorType = Error, RequestType = IRequest, Args extends any[] = any[]> =
(response: ErrorType, request: RequestType, ...args: Args) => any
1 change: 1 addition & 0 deletions src/types/GenericTraps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type GenericTraps = Record<string, any>
5 changes: 5 additions & 0 deletions src/types/HasContent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { IRequestStrict } from './IRequestStrict'

export type HasContent<ContentType> = {
content: ContentType
} & IRequestStrict
4 changes: 4 additions & 0 deletions src/types/IRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { GenericTraps } from './GenericTraps'
import { IRequestStrict } from './IRequestStrict'

export type IRequest = IRequestStrict & GenericTraps
12 changes: 12 additions & 0 deletions src/types/IRequestStrict.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export type IRequestStrict = {
method: string
url: string
route: string
params: {
[key: string]: string
}
query: {
[key: string]: string | string[] | undefined
}
proxy?: any
} & Request
6 changes: 6 additions & 0 deletions src/types/IttyRouterOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { RouteEntry } from './RouteEntry'

export type IttyRouterOptions = {
base?: string
routes?: RouteEntry[]
} & Record<string, any>
19 changes: 19 additions & 0 deletions src/types/IttyRouterType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { CustomRoutes } from './CustomRoutes'
import { IRequest } from './IRequest'
import { RequestLike } from './RequestLike'
import { Route } from './Route'
import { RouteEntry } from './RouteEntry'

export type IttyRouterType<R = IRequest, A extends any[] = any[], Output = any> = {
__proto__: IttyRouterType<R>
routes: RouteEntry[]
fetch: <Args extends any[] = A>(request: RequestLike, ...extra: Args) => Promise<Output>
all: Route<R, A>
delete: Route<R, A>
get: Route<R, A>
head: Route<R, A>
options: Route<R, A>
patch: Route<R, A>
post: Route<R, A>
put: Route<R, A>
} & CustomRoutes<Route<R, A>>
4 changes: 4 additions & 0 deletions src/types/RequestHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { IRequest } from './IRequest'

export type RequestHandler<R = IRequest, Args extends Array<any> = any[]> =
(request: R, ...args: Args) => any
6 changes: 6 additions & 0 deletions src/types/RequestLike.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { GenericTraps } from './GenericTraps'

export type RequestLike = {
method: string
url: string
} & GenericTraps
3 changes: 3 additions & 0 deletions src/types/ResponseFormatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface ResponseFormatter {
(body?: any, options?: ResponseInit): Response
}
4 changes: 4 additions & 0 deletions src/types/ResponseHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { IRequest } from './IRequest'

export type ResponseHandler<ResponseType = Response, RequestType = IRequest, Args extends any[] = any[]> =
(response: ResponseType & any, request: RequestType & any, ...args: Args) => any
8 changes: 8 additions & 0 deletions src/types/Route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { IRequest } from './IRequest'
import { IttyRouterType } from './IttyRouterType'
import { RequestHandler } from './RequestHandler'

export type Route<R = IRequest, A extends Array<any> = any[]> = <RequestType = R, Args extends Array<any> = A>(
path: string,
...handlers: RequestHandler<RequestType, Args>[]
) => IttyRouterType<RequestType, Args>
8 changes: 8 additions & 0 deletions src/types/RouteEntry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { RequestHandler } from './RequestHandler'

export type RouteEntry = [
httpMethod: string,
match: RegExp,
handlers: RequestHandler[],
path?: string,
]
10 changes: 10 additions & 0 deletions src/types/RouterOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ErrorHandler } from './ErrorHandler'
import { IttyRouterOptions } from './IttyRouterOptions'
import { RequestHandler } from './RequestHandler'
import { ResponseHandler } from './ResponseHandler'

export type RouterOptions = {
before?: RequestHandler<any>[]
catch?: ErrorHandler
finally?: ResponseHandler[]
} & IttyRouterOptions
11 changes: 11 additions & 0 deletions src/types/RouterType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ErrorHandler } from './ErrorHandler'
import { IRequest } from './IRequest'
import { IttyRouterType } from './IttyRouterType'
import { RequestHandler } from './RequestHandler'
import { ResponseHandler } from './ResponseHandler'

export type RouterType<R = IRequest, Args extends any[] = any[]> = {
before?: RequestHandler<any>[]
catch?: ErrorHandler
finally?: ResponseHandler[]
} & IttyRouterType<R, Args>
6 changes: 1 addition & 5 deletions src/withContent.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { IRequest, IRequestStrict } from './IttyRouter'

export type HasContent<ContentType> = {
content: ContentType
} & IRequestStrict
import { IRequest } from './types'

// withContent - embeds any request body as request.content
export const withContent = async (request: IRequest): Promise<void> => {
Expand Down
2 changes: 1 addition & 1 deletion src/withCookies.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IRequest } from './IttyRouter'
import { IRequest } from './types'

type KVPair = [string, string?]

Expand Down
2 changes: 1 addition & 1 deletion src/withParams.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IRequest } from './IttyRouter'
import { IRequest } from './types'

export const withParams = (request: IRequest): void => {
request.proxy = new Proxy(request.proxy || request, {
Expand Down
3 changes: 2 additions & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export default defineConfig({
'**/example/**',
'**/lib/**',
'src/index.ts',
'src/types.d.ts',
'src/types',
'src/types.ts',
'src/websocket.ts',
],
},
Expand Down

0 comments on commit 47ea498

Please sign in to comment.