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

New decorator @TypedHeaders() #473

Closed
samchon opened this issue Jul 22, 2023 · 0 comments
Closed

New decorator @TypedHeaders() #473

samchon opened this issue Jul 22, 2023 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@samchon
Copy link
Owner

samchon commented Jul 22, 2023

Implement a new decorator @TypeHeaders(), which can take variables from header.

Also, TypedHeaders() can have only one object type, and each properties must be atomic or array of atomic type like below.

export interface IHeaders {
    Authorization: string;
    ["X-some-values"]: number[];
    ["X-something-allow"]: boolean;
}

Some may have doubts about the header parameters. Looking at API of headers property of express or fastify, which can parse header variables, only string typed property values are allowed. However, above interface IHeaders has numeber and boolean types. How it can be possible? Isn't it something wrong?

The answer is such number and boolean typed properties would be automatically casted, by analyzing the DTO structure in the compilation level, as other decorator functions like @TypedHeaders() could.

@samchon samchon added the enhancement New feature or request label Jul 22, 2023
@samchon samchon self-assigned this Jul 22, 2023
samchon added a commit that referenced this issue Jul 31, 2023
Close #473 - new decorator `@TypedHeaders()`
samchon added a commit that referenced this issue Aug 1, 2023
From now on, `@nestia/fetcher` and `@nestia/sdk` will support `@TypedHeaders()`. In the `@nestia/fetcher` case, it will enhance `IConnection.headers` type to accept atomic and atomic array types. The other `@nestia/sdk` case, it will also support the `@TypedHeaders()` in SDK and Mockup simulator level.

```typescript
export interface IConnection<Headers extends object = {}> {
    headers?: Record<string, IConnection.HeaderValue> &
        IConnection.Headerify<Headers>;
}
export namespace IConnection {
    export type HeaderValue =
        | string
        | boolean
        | number
        | bigint
        | string
        | Array<boolean>
        | Array<number>
        | Array<bigint>
        | Array<number>
        | Array<string>;

    export type Headerify<T extends object> = {
        [P in keyof T]?: T[P] extends HeaderValue | undefined
            ? T[P] | undefined
            : never;
    };
}
```
samchon added a commit that referenced this issue Aug 1, 2023
Enhance #473 - support `@TypedHeaders()` in `fetcher` and `sdk`
samchon added a commit that referenced this issue Aug 2, 2023
Enhanced `IConnection.Headerify` type of `@nestia/fetcher` to be much stronger. It will validate special cases whether each property allows Array type or not through their key name. For example, "set-cookie" type must be Array type, and `Authorization` must be atomic type.

```typescript
export namespace IConnection {
    export type Headerify<T extends object> = {
        [P in keyof T]?: T[P] extends HeaderValue | undefined
            ? P extends string
                ? Lowercase<P> extends "set-cookie"
                    ? T[P] extends Array<HeaderValue>
                        ? T[P] | undefined
                        : never
                    : Lowercase<P> extends
                          | "age"
                          | "authorization"
                          | "content-length"
                          | "content-type"
                          | "etag"
                          | "expires"
                          | "from"
                          | "host"
                          | "if-modified-since"
                          | "if-unmodified-since"
                          | "last-modified"
                          | "location"
                          | "max-forwards"
                          | "proxy-authorization"
                          | "referer"
                          | "retry-after"
                          | "server"
                          | "user-agent"
                    ? T[P] extends Array<HeaderValue>
                        ? never
                        : T[P] | undefined
                    : T[P] | undefined
                : never
            : never;
}
```
samchon added a commit that referenced this issue Aug 2, 2023
Enhance #473 - `IConnection.Headerify` to be much stronger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant