|
2 | 2 | import * as nestSwagger from '@nestjs/swagger';
|
3 | 3 | import { ApiHeaderOptions } from '@nestjs/swagger';
|
4 | 4 |
|
5 |
| -export enum HttpRequestHeaderKeysEnum { |
6 |
| - AUTHORIZATION = 'Authorization', |
7 |
| - USER_AGENT = 'User-Agent', |
8 |
| - CONTENT_TYPE = 'Content-Type', |
9 |
| - SENTRY_TRACE = 'Sentry-Trace', |
10 |
| - NOVU_ENVIRONMENT_ID = 'Novu-Environment-Id', |
11 |
| - NOVU_API_VERSION = 'Novu-API-Version', |
12 |
| - NOVU_USER_AGENT = 'Novu-User-Agent', |
13 |
| - BYPASS_TUNNEL_REMINDER = 'Bypass-Tunnel-Reminder', |
14 |
| -} |
15 |
| -testHttpHeaderEnumValidity(HttpRequestHeaderKeysEnum); |
16 |
| - |
17 | 5 | export enum HttpResponseHeaderKeysEnum {
|
18 | 6 | CONTENT_TYPE = 'Content-Type',
|
19 | 7 | RATELIMIT_REMAINING = 'RateLimit-Remaining',
|
@@ -54,22 +42,20 @@ export type DeepRequired<T> = T extends object
|
54 | 42 | /**
|
55 | 43 | * Transform S to CONSTANT_CASE.
|
56 | 44 | */
|
57 |
| -export type ConvertToConstantCase<S extends string> = |
58 |
| - S extends `${infer T}-${infer U}` |
59 |
| - ? `${Uppercase<T>}_${ConvertToConstantCase<U>}` |
60 |
| - : Uppercase<S>; |
| 45 | +export type ConvertToConstantCase<S extends string> = S extends `${infer T}-${infer U}` |
| 46 | + ? `${Uppercase<T>}_${ConvertToConstantCase<U>}` |
| 47 | + : Uppercase<S>; |
61 | 48 |
|
62 | 49 | /**
|
63 | 50 | * Validate that S is in Http-Header-Case, and return S if valid, otherwise never.
|
64 | 51 | */
|
65 |
| -export type ValidateHttpHeaderCase<S extends string> = |
66 |
| - S extends `${infer U}-${infer V}` |
67 |
| - ? U extends Capitalize<U> |
68 |
| - ? `${U}-${ValidateHttpHeaderCase<V>}` |
69 |
| - : never |
70 |
| - : S extends Capitalize<S> |
71 |
| - ? `${S}` // necessary to cast to string literal type for non-hyphenated enum validation |
72 |
| - : never; |
| 52 | +export type ValidateHttpHeaderCase<S extends string> = S extends `${infer U}-${infer V}` |
| 53 | + ? U extends Capitalize<U> |
| 54 | + ? `${U}-${ValidateHttpHeaderCase<V>}` |
| 55 | + : never |
| 56 | + : S extends Capitalize<S> |
| 57 | + ? `${S}` // necessary to cast to string literal type for non-hyphenated enum validation |
| 58 | + : never; |
73 | 59 |
|
74 | 60 | /**
|
75 | 61 | * Helper function to test that Header enum keys and values match correct format.
|
@@ -102,14 +88,11 @@ export type ValidateHttpHeaderCase<S extends string> =
|
102 | 88 | export function testHttpHeaderEnumValidity<
|
103 | 89 | TEnum extends IConstants,
|
104 | 90 | TValue extends TEnum[keyof TEnum] & string,
|
105 |
| - IConstants = Record< |
106 |
| - ConvertToConstantCase<TValue>, |
107 |
| - ValidateHttpHeaderCase<TValue> |
108 |
| - >, |
| 91 | + IConstants = Record<ConvertToConstantCase<TValue>, ValidateHttpHeaderCase<TValue>>, |
109 | 92 | >(
|
110 | 93 | testEnum: TEnum &
|
111 | 94 | Record<
|
112 | 95 | Exclude<keyof TEnum, keyof IConstants>,
|
113 | 96 | ['Key must be the CONSTANT_CASED version of the Capital-Cased value']
|
114 |
| - >, |
| 97 | + > |
115 | 98 | ) {}
|
0 commit comments