Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): allow middleware props to be optional when specified in global config #458

Merged
merged 9 commits into from
May 28, 2024
8 changes: 4 additions & 4 deletions src/types/middlewares.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { BuiltinDriverName, BuiltinDriverOptions } from 'unstorage'
export type RequestSizeLimiter = {
maxRequestSizeInBytes: number;
maxUploadFileRequestInBytes: number;
maxRequestSizeInBytes?: number;
maxUploadFileRequestInBytes?: number;
throwError?: boolean;
};

export type RateLimiter<T extends BuiltinDriverName = BuiltinDriverName> = {
tokensPerInterval: number;
interval: string | number;
tokensPerInterval?: number;
interval?: string | number;
driver?: {
[K in T]: {
name: K;
Expand Down
12 changes: 9 additions & 3 deletions src/types/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import type { SecurityHeaders } from './headers'
import type { AllowedHTTPMethods, BasicAuth, RateLimiter, RequestSizeLimiter, XssValidator, CorsOptions } from './middlewares'
import type { HookResult } from '@nuxt/schema'


type RequiredWithoutThrowError<T> = Omit<T, 'throwError'>;
type OptionalThrowError<T> = Pick<T, 'throwError'>;
type QualifiedConfig<T> = Required<RequiredWithoutThrowError<T>> & Partial<OptionalThrowError<T>>;

export type Ssg = {
meta?: boolean;
hashScripts?: boolean;
Expand All @@ -30,9 +35,10 @@ export interface ModuleOptions {
}

export type NuxtSecurityRouteRules = Partial<
Omit<ModuleOptions, 'csrf' | 'basicAuth' | 'rateLimiter' | 'ssg' >
& { rateLimiter: Omit<RateLimiter, 'driver'> | false }
Omit<ModuleOptions, 'csrf' | 'basicAuth' | 'rateLimiter' | 'ssg' | 'requestSizeLimiter' >
& { rateLimiter: QualifiedConfig<Omit<RateLimiter, 'driver'>> | false }
& { ssg: Omit<Ssg, 'exportToPresets'> | false }
& { requestSizeLimiter: QualifiedConfig<RequestSizeLimiter> | false }
>

declare module 'nuxt/schema' {
Expand Down Expand Up @@ -96,4 +102,4 @@ declare module 'h3' {
};
}
}
}
}
Loading