Skip to content

Commit

Permalink
Merge pull request #6 from michealroberts/refactor/dependencies/h3
Browse files Browse the repository at this point in the history
refactor: Upgraded to h3 => v1.8.0 dependency in nitro-cors.
  • Loading branch information
michealroberts authored Aug 28, 2023
2 parents 533ccfa + ad4c8cd commit 877d98c
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 28 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"test": "vitest"
},
"dependencies": {
"h3": "^1.6.6",
"h3": "^1.8.0",
"ufo": "^1.1.2"
},
"devDependencies": {
Expand Down
66 changes: 52 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 16 additions & 3 deletions src/corsEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,25 @@

/*****************************************************************************************************************/

import { type EventHandler, type H3CorsOptions, defineEventHandler, handleCors } from 'h3'
import {
type EventHandler,
type EventHandlerRequest,
type EventHandlerResponse,
type H3CorsOptions,
defineEventHandler,
handleCors
} from 'h3'

/*****************************************************************************************************************/

export const corsEventHandler = <T>(handler: EventHandler<T>, options: H3CorsOptions) => {
return defineEventHandler(async event => {
export const corsEventHandler = <
TRequest extends EventHandlerRequest,
TResponse extends EventHandlerResponse
>(
handler: EventHandler<TRequest, TResponse>,
options: H3CorsOptions
): EventHandler<EventHandlerRequest, TResponse> => {
return defineEventHandler(event => {
handleCors(event, options)
return handler(event)
})
Expand Down
13 changes: 9 additions & 4 deletions src/defineCORSEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import {
type EventHandler,
type EventHandlerRequest,
type EventHandlerResponse,
type H3CorsOptions,
getHeaders,
setHeader,
Expand All @@ -19,11 +21,14 @@ import { useCORS } from './internals/utils'

/*****************************************************************************************************************/

export const defineCORSEventHandler = <T extends any>(
handler: EventHandler<T>,
export const defineCORSEventHandler = <
TRequest extends EventHandlerRequest,
TResponse extends EventHandlerResponse
>(
handler: EventHandler<TRequest, TResponse>,
options: H3CorsOptions
) => {
return defineEventHandler(async event => {
): EventHandler<EventHandlerRequest, TResponse> => {
return defineEventHandler(event => {
useCORS(event, options)

const { origin } = getHeaders(event)
Expand Down
8 changes: 3 additions & 5 deletions src/internals/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,19 @@ import {
type H3CorsOptions,
appendCorsHeaders,
appendCorsPreflightHeaders,
getMethod,
isPreflightRequest,
sendNoContent
} from 'h3'

/*****************************************************************************************************************/

export const useCORS = (event: H3Event, options: H3CorsOptions): boolean => {
const method = getMethod(event)

const { methods = [] } = options

const methodIsAllowed = (Array.isArray(methods) && methods.includes(method)) || methods == '*'
const methodIsAllowed =
(Array.isArray(methods) && methods.includes(event.method)) || methods == '*'

const methodIsOptions = method === 'OPTIONS'
const methodIsOptions = event.method === 'OPTIONS'

// If the method is not allowed, return:
if (!methodIsAllowed && !methodIsOptions) {
Expand Down
40 changes: 39 additions & 1 deletion tests/defineCORSEventHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

/*****************************************************************************************************************/

import { describe, expect, it, suite } from 'vitest'
import { describe, expect, expectTypeOf, it, suite } from 'vitest'

import { type EventHandler, type EventHandlerRequest } from 'h3'

import { defineCORSEventHandler } from '../src'

Expand All @@ -17,5 +19,41 @@ suite('nitro-cors eventHandler', () => {
it('should be defined', () => {
expect(defineCORSEventHandler).toBeDefined()
})

it('should return the correct event handler types when specified', async () => {
const handler = defineCORSEventHandler(
async _event => {
return {
cors: true
}
},
{
origin: '*',
methods: '*'
}
)

expectTypeOf(handler).toEqualTypeOf<
EventHandler<EventHandlerRequest, Promise<{ cors: boolean }>>
>()
})

it('should return the correct event handler types when specified', () => {
const handler = defineCORSEventHandler(
_event => {
return {
cors: true
}
},
{
origin: '*',
methods: '*'
}
)

expectTypeOf(handler).toEqualTypeOf<EventHandler<EventHandlerRequest, { cors: boolean }>>()
})
})
})

/*****************************************************************************************************************/

0 comments on commit 877d98c

Please sign in to comment.