api-clients-automation icon indicating copy to clipboard operation
api-clients-automation copied to clipboard

javascript: certain types aren't generic

Open Haroenv opened this issue 1 year ago • 6 comments

Description

  • SearchResponse, SearchResponses should take a single generic THit
  • HighlightResult and SnippetResults should take a single generic THit

SnippetResults and HighlightResult should also be recursive as in v4

declare type SnippetMatch = {
    readonly value: string;
    readonly matchLevel: 'none' | 'partial' | 'full';
};

export declare type SnippetResult<THit> = THit extends string | number ? SnippetMatch : {
    [KAttribute in keyof THit]: SnippetResult<THit[KAttribute]>;
};


declare type HighlightMatch = {
    readonly value: string;
    readonly matchLevel: 'none' | 'partial' | 'full';
    readonly matchedWords: readonly string[];
    readonly fullyHighlighted?: boolean;
};

export declare type HighlightResult<THit> = THit extends string | number ? HighlightMatch : {
    [KAttribute in keyof THit]?: HighlightResult<THit[KAttribute]>;
};

export declare type Hit<THit> = THit & {
    readonly objectID: string;
    readonly _highlightResult?: HighlightResult<THit>;
    readonly _snippetResult?: SnippetResult<THit>;
    readonly _rankingInfo?: RankingInfo;
    readonly _distinctSeqID?: number;
};

Haroenv avatar Jul 20 '22 16:07 Haroenv

Thanks! This is being worked on for Java and JavaScript here: https://github.com/algolia/api-clients-automation/pull/829

shortcuts avatar Jul 20 '22 16:07 shortcuts

Should the hit be overridden by the generic ? like hits: T[] and it's up to the user to extend its type with Hit ?

millotp avatar Jul 20 '22 16:07 millotp

Should the hit be overridden by the generic ?

It should be type Hits<T> = Array<Hit<T>> and type Hit<T = Record<string, any>> = T & { ...baseProps }

shortcuts avatar Jul 20 '22 16:07 shortcuts

Okay so the generic logic will be exclusively for hits, it would be hard to make it work with other generic type

millotp avatar Jul 20 '22 16:07 millotp

Is there a plan for Highlight and Snippet to be correct as well?

Haroenv avatar Sep 07 '22 08:09 Haroenv

Not for now as we have other priorities, but if you have an implementation in mind feel free to contribute!

shortcuts avatar Sep 07 '22 08:09 shortcuts