import { QComplexCollectionPath, QComplexPath, QEntityCollectionPath, QEntityPath, QFilterExpression, QFlatComplexPath, QOrderByExpression, QSearchTerm, QSelectExpression, QueryObjectModel } from "@odata2ts/odata-query-objects"; /** * Extracts the wrapped entity from QEntityPath, QEntityCollectionPath, QComplexPath, QComplexCollectionPath * or QFlatComplexPath */ export type EntityExtractor = QProp extends QEntityPath ? ET : QProp extends QEntityCollectionPath ? ET : QProp extends QComplexPath ? ET : QProp extends QComplexCollectionPath ? ET : QProp extends QFlatComplexPath ? ET : never; /** * Extracts all keys from a property (Q*Path), but only for the given types */ export type ExtractPropertyNamesOfType = { [Key in keyof QPath]: QPath[Key] extends QPathTypes ? Key : never; }[keyof QPath]; /** * Retrieves all property names which are expandable via a plain `$expand`, * i.e. props of type QEntityPath and QEntityCollectionPath. * * Complex-typed properties are deliberately excluded: per the OData V4 ABNF, an `expandPath` can never terminate * on a bare complex property (only `*`, a stream property, or a navigation property are valid terminal segments), * so a plain `$expand=ComplexProp` is not valid V4 syntax. Use `expanding()` instead (see NestingType). */ export type ExpandType = ExtractPropertyNamesOfType | QEntityCollectionPath>; /** * Retrieves all property names which are valid `expanding()` targets, i.e. entity/entity-collection navigation * properties (rendered as `$expand=Prop(...)`) as well as complex/complex-collection properties (rendered as * `$select=Prop(...)`, since complex types are always inline and never need `$expand` in V4). */ export type NestingType = ExtractPropertyNamesOfType | QEntityCollectionPath | QComplexPath | QComplexCollectionPath | QFlatComplexPath>; /** * Retrieves all valid `select()` targets: any property name of the entity, a raw `QSelectExpression`, or the * literal wildcard `"*"` (OData `$select=*` - "all structural properties"). Deliberately NOT reused by * `expand()`/`expanding()` (see ExpandType/NestingType) - `$expand=*` is a distinct, unsupported system query * option and must stay unreachable from expand's public surface. */ export type SelectType = keyof Q | "*" | QSelectExpression; export type Nullable = null | undefined; export type NullableParam = OptionType | Nullable; export type NullableParamList = Array; /** * The nested builder type passed into an `expanding()` callback depends on whether the expanded property * is a to-one (single model) or to-many (collection) property — entity or complex, it doesn't matter which: * a to-one target only allows select/expand/expanding, while a to-many target keeps the full set of system * query options. Whether the property itself is an entity/entity-collection (rendered as `$expand=Prop(...)`) * or a complex/complex-collection type (rendered as `$select=Prop(...)`) is resolved dynamically at runtime * by the engine — the nested builder's shape only depends on cardinality. */ export type ExpandingFunction = (Prop extends QEntityCollectionPath | QComplexCollectionPath ? (expBuilder: ExpandingCollectionQueryBuilderV4>, qObject: EntityExtractor) => void : (expBuilder: ExpandingModelQueryBuilderV4>, qObject: EntityExtractor) => void) | Nullable; export type ExpandingFunctionV2 = ((expBuilder: ExpandingQueryBuilderV2>, qObject: EntityExtractor) => void) | Nullable; export interface ODataQueryBuilderConfig { expandingBuilder?: boolean; unencoded?: boolean; } /** * Represents all possible builder operations. * However, any builder will only be composed of a subset of these operations. */ export interface ODataQueryBuilderModel { /** * Name the properties of the entity you want to select (as they are specified on the query object). * Null or undefined are allowed and will be ignored. * * This function can be called multiple times. * * The literal wildcard `"*"` selects all structural properties and can be combined with other select items, * e.g. `builder.select("*", "bestFriend")` // $select=*,bestFriend. Note: unlike V4 (where complex/navigation * content is always inlined), V2's `$select` is a pure response-shaping filter and does not by itself cause * complex or navigation content to be included - use `expand()`/`expanding()` for that in V2 as well. * * @example * builder.select("lastName", "firstName", undefined) // $select=lastName,firstName * @example * builder.select("lastName", false ? "firstName" : undefined) // $select=lastName * @example * builder.select("*", "bestFriend") // $select=*,bestFriend * @param props the property names to select as they are specified on the query object, or "*" for all * structural properties * @returns this query builder */ select: (...props: NullableParamList>) => ReturnType; /** * Specify as many filter expressions as you want by facilitating query objects. * Alternatively you can use QueryExpressions directly. * * Each passed expression is concatenated to the other ones by an and expression. * * This method can be called multiple times in order to add filters successively. * * @example * builder.filter(qPerson.lastName.eq("Smith"), qPerson.age.gt(18)) // $filter=LastName eq 'Smith' and Age gt 18 * @example * builder.filter(qPerson.lastName.eq("Smith").or(qPerson.firstName.eq("Horst")) // $filter=LastName eq 'Smith' or FirstName eq 'Horst' * @param expressions possibly multiple expressions * @returns this query builder */ filter: (...expressions: NullableParamList) => ReturnType; /** * V4 search option, where the server decides how to apply the search value. * Null or undefined are allowed and will be ignored. * * Each passed expression is concatenated to the other ones by an and expression. * * This method can be called multiple times in order to add filters successively. * * Uses system query option $search. * * @example * builder.search("term1", "this is a phrase") // $search=term1 AND "this is a phrase" * @param terms * @returns this query builder */ search: (...terms: NullableParamList) => ReturnType; /** * Simple & plain expand of attributes which are entities or entity collections. * * This method can be called multiple times. * * @example * builder.expand("address", "altAddress") // $expand=Address,AltAddress * @param props the attributes to expand * @returns this query builder */ expand: >(...props: NullableParamList) => ReturnType; /** * Expand one property, which is an entity or entity collection. * The second parameter is a callback function which receives a specialized URI builder and the proper query object * for the expanded entity. * With the help of the builder you can further select, filter, expand, etc. * * @example * builder.expanding("address", (addressBuilder, qAddress) => { * addressBuilder * .select("city") * .filter(qAddress.country.eq("IT")) * })} // $expand=address($select=City;$filter=Country eq 'IT') * @example * builder.expanding("homeAddress", (addressBuilder) => { * addressBuilder.select("city") * })} // $select=homeAddress($select=city) -- homeAddress is a complex type, not a navigation property * @param prop the name of the property which should be expanded (an entity, entity collection, complex type * or complex type collection) * @param builderFn function which receives an entity specific builder as first & the appropriate query object as second argument * @returns this query builder */ expanding: >(prop: Prop, expBuilderFn: ExpandingFunction) => ReturnType; /** * Simple group by clause for properties (no aggregate functionality yet). * Uses system query option $apply. * * Null or undefined are allowed and will be ignored. * * @example * builder.select("country").groupBy("country") // $select=Country&apply=groupby((Country)) * @param props * @returns this query builder */ groupBy: (...props: NullableParamList) => ReturnType; /** * Count the list of result items. The query response will have an appropriate count field. * * @example * builder.count() // $count * @example * builder.count(false) // $count=false * @param doCount defaults to true * @returns this query builder */ count: (doCount?: boolean) => ReturnType; /** * Limit the amount of records to retrieve. * * @example * builder.top(20) // $top=20 * @param itemsTop max amount of items to fetch */ top: (itemsTop: NullableParam) => ReturnType; /** * Skips a specified number of records, e.g. skip the first 20 items. * * @example * builder.skip(20) // $skip=20 * @param itemsToSkip number of records to skip * @returns this query builder */ skip: (itemsToSkip: NullableParam) => ReturnType; /** * Specify the sort order of the results by utilizing query objects. * * @example * builder.orderBy(qPerson.age.desc(), qPerson.name.asc()) // $orderby=Age desc,Name asc * @param expressions * @returns this query builder */ orderBy: (...expressions: NullableParamList) => ReturnType; /** * Build the final URI string. * * @example * builder.build() // Person?$select=...&$expand=... * @returns the query string including the base service & collection path */ build: () => string; } export interface V2ExpandingFunction { /** * Expand one property, which is an entity, entity collection, complex type or complex type collection. * The second parameter is a callback function which receives a specialized URI builder and the proper query object * for the expanded entity. * With the help of the builder you can further select, filter, expand, etc. * * Expand nested props of the current entity. * You can then select, expand or do further expanding. * * @example * builder.expanding("address", (expBuilder) => expBuilder.select("street", "city")) // $select=address/street,address/city&$expand=address * @example * builder.expanding("address", (expBuilder) => expBuilder.expand("country")) // $expand=address,address/country * @param prop the name of the property which should be expanded (an entity, entity collection, complex type * or complex type collection) * @param builderFn function which receives an entity specific builder as first & the appropriate query object as second argument * @returns this query builder */ expanding: >(prop: Prop, expBuilderFn: ExpandingFunctionV2) => ReturnType; } /** * V2 only: `expand` is widened to also accept complex/complex-collection properties, since (unlike V4, where * complex types are always inline) V2 requires `$expand=ComplexProp` alongside `$select=ComplexProp/Sub` for a * complex-typed property to appear in the response at all. */ export interface V2ExpandFunction { expand: >(...props: NullableParamList) => ReturnType; } type BuilderOp = "build"; type ModelOps = "select" | "expand" | BuilderOp; type CollectionOnlyOps = "filter" | "orderBy" | "count" | "skip" | "top"; type V2ModelOps = "select" | BuilderOp; type V2CollectionOps = "select" | BuilderOp | CollectionOnlyOps; type V2ExpandingOps = "select"; type V4ModelOps = ModelOps | "expanding"; type V4CollectionOps = V4ModelOps | CollectionOnlyOps | "groupBy" | "search"; type V4ModelExpandingOps = V4ModelOps; type V4CollectionExpandingOps = V4ModelOps | CollectionOnlyOps | "search"; export type V2ExpandResult = { selects: Array; expands: Array; }; /** * Contract for ODataQueryBuilder for V2, bound to a collection of models: * exposes the full set of system query options. */ export interface CollectionQueryBuilderV2 extends Pick>, V2CollectionOps>, V2ExpandingFunction>, V2ExpandFunction> { /** * Creates a new builder with the identical state (deep copy). */ clone: () => CollectionQueryBuilderV2; } /** * Contract for ODataQueryBuilder for V2, bound to a single model (EntityType or ComplexType instance): * only select/expand/expanding are meaningful. */ export interface ModelQueryBuilderV2 extends Pick>, V2ModelOps>, V2ExpandingFunction>, V2ExpandFunction> { /** * Creates a new builder with the identical state (deep copy). */ clone: () => ModelQueryBuilderV2; } export interface ExpandingQueryBuilderV2 extends Pick>, V2ExpandingOps>, V2ExpandingFunction>, V2ExpandFunction> { /** * Build result is actually a list of select and expand strings, which must be consumed manually by the * caller of the build function. */ build: () => V2ExpandResult; } /** * Contract for ODataQueryBuilder for V4, bound to a collection of models: * exposes the full set of system query options. */ export interface CollectionQueryBuilderV4 extends Pick>, V4CollectionOps> { /** * Creates a new builder with the identical state (deep copy). */ clone: () => CollectionQueryBuilderV4; } /** * Contract for ODataQueryBuilder for V4, bound to a single model (EntityType or ComplexType instance): * only select/expand/expanding are meaningful. */ export interface ModelQueryBuilderV4 extends Pick>, V4ModelOps> { /** * Creates a new builder with the identical state (deep copy). */ clone: () => ModelQueryBuilderV4; } export interface ExpandingCollectionQueryBuilderV4 extends Pick>, V4CollectionExpandingOps> { } export interface ExpandingModelQueryBuilderV4 extends Pick>, V4ModelExpandingOps> { } export {};