From acba4b206692b526146a1b457816e9bdfd9df982 Mon Sep 17 00:00:00 2001 From: Diego van Haaster Date: Wed, 12 Jun 2024 01:02:54 -0300 Subject: [PATCH] Fix __parser and collection --- .../src/lib/models/collection.ts | 3 +- .../src/lib/resources/resource.ts | 114 ++++++++---------- 2 files changed, 52 insertions(+), 65 deletions(-) diff --git a/projects/angular-odata/src/lib/models/collection.ts b/projects/angular-odata/src/lib/models/collection.ts index 8f0bb3af..ac515dd0 100644 --- a/projects/angular-odata/src/lib/models/collection.ts +++ b/projects/angular-odata/src/lib/models/collection.ts @@ -3,7 +3,6 @@ import { finalize, map, switchMap } from 'rxjs/operators'; import { DEFAULT_VERSION } from '../constants'; import { ODataHelper } from '../helper'; import { - EntityKey, ODataActionOptions, ODataEntitiesAnnotations, ODataEntityAnnotations, @@ -1019,7 +1018,7 @@ export class ODataCollection> options: ODataFunctionOptions = {} ): Observable | ODataCollection> | null> { const resource = this.resource(); - if (!(resource instanceof ODataEntityResource)) + if (!(resource instanceof ODataEntitySetResource)) return throwError( () => new Error( diff --git a/projects/angular-odata/src/lib/resources/resource.ts b/projects/angular-odata/src/lib/resources/resource.ts index 6de702a2..c682d818 100644 --- a/projects/angular-odata/src/lib/resources/resource.ts +++ b/projects/angular-odata/src/lib/resources/resource.ts @@ -30,8 +30,8 @@ import { ODataOptions } from './types'; export type EntityKey = | { - readonly [P in keyof T]?: T[P]; - } + readonly [P in keyof T]?: T[P]; + } | QueryCustomType | string | number; @@ -171,8 +171,8 @@ export class ODataResource { return test === 'path' ? otherPath === selfPath : test === 'params' - ? Types.isEqual(selfParams, otherParams) - : otherPath === selfPath && Types.isEqual(selfParams, otherParams); + ? Types.isEqual(selfParams, otherParams) + : otherPath === selfPath && Types.isEqual(selfParams, otherParams); } pathAndParams( @@ -230,7 +230,7 @@ export class ODataResource { }); } - private __serializeParser( + private __parser( value: any, options?: ParserOptions, resourceType?: string @@ -249,22 +249,10 @@ export class ODataResource { return undefined; } - private __deserializeParser( - value: any, - options?: ParserOptions, - resourceType?: string - ): Parser | undefined { - const type = - options !== undefined && Types.isPlainObject(value) - ? ODataHelper[options.version || DEFAULT_VERSION].type(value) - : resourceType; - return (type !== undefined) ? this.api.parserForType(type) : undefined; - } - deserialize(value: any, options?: ParserOptions): any { const resourceType = this.incomingType(); const _d = (value: any, options?: ParserOptions) => { - const parser = this.__deserializeParser(value, options, resourceType); + const parser = this.__parser(value, options, resourceType); return parser !== undefined ? parser.deserialize(value, options) : value; }; return Array.isArray(value) @@ -275,7 +263,7 @@ export class ODataResource { serialize(value: any, options?: ParserOptions): any { const resourceType = this.outgoingType(); const _s = (value: any, options?: ParserOptions) => { - const parser = this.__serializeParser(value, options, resourceType); + const parser = this.__parser(value, options, resourceType); return parser !== undefined ? parser.serialize(value, options) : value; }; return Array.isArray(value) @@ -286,7 +274,7 @@ export class ODataResource { encode(value: any, options?: ParserOptions): any { const resourceType = this.outgoingType(); const _e = (value: any, options?: ParserOptions) => { - const parser = this.__serializeParser(value, options, resourceType); + const parser = this.__parser(value, options, resourceType); return parser !== undefined ? parser.encode(value, options) : value; }; return Array.isArray(value) @@ -325,7 +313,7 @@ export class ODataResource { f: (q: ODataPathSegmentsHandler, s?: ODataStructuredType) => void ) { const type = this.outgoingType(); - f(new ODataPathSegmentsHandler(this.pathSegments), + f(new ODataPathSegmentsHandler(this.pathSegments), type !== undefined ? this.api.structuredType(type) : undefined ); return this; @@ -340,7 +328,7 @@ export class ODataResource { f: (q: ODataQueryOptionsHandler, s?: ODataStructuredType) => void ) { const type = this.outgoingType(); - f(new ODataQueryOptionsHandler(this.queryOptions), + f(new ODataQueryOptionsHandler(this.queryOptions), type !== undefined ? this.api.structuredType(type) : undefined ); return this; @@ -351,7 +339,7 @@ export class ODataResource { builder: ApplyExpressionBuilder, current?: ApplyExpression ) => ApplyExpression, - {type, fields}: {type?: string, fields?: { [P in keyof R]?: StructuredTypeFieldConfig }} = {}): ODataResource { + { type, fields }: { type?: string, fields?: { [P in keyof R]?: StructuredTypeFieldConfig } } = {}): ODataResource { const query = this.cloneQuery(); const handler = new ODataQueryOptionsHandler(query); handler.apply(opts); @@ -393,14 +381,14 @@ export class ODataResource { options: ODataOptions & { etag?: string; responseType?: - | 'arraybuffer' - | 'blob' - | 'json' - | 'text' - | 'value' - | 'property' - | 'entity' - | 'entities'; + | 'arraybuffer' + | 'blob' + | 'json' + | 'text' + | 'value' + | 'property' + | 'entity' + | 'entities'; withCount?: boolean; bodyQueryOptions?: QueryOption[]; } = {} @@ -412,14 +400,14 @@ export class ODataResource { body: any, options: ODataOptions & { responseType?: - | 'arraybuffer' - | 'blob' - | 'json' - | 'text' - | 'value' - | 'property' - | 'entity' - | 'entities'; + | 'arraybuffer' + | 'blob' + | 'json' + | 'text' + | 'value' + | 'property' + | 'entity' + | 'entities'; withCount?: boolean; } = {} ): Observable { @@ -431,14 +419,14 @@ export class ODataResource { options: ODataOptions & { etag?: string; responseType?: - | 'arraybuffer' - | 'blob' - | 'json' - | 'text' - | 'value' - | 'property' - | 'entity' - | 'entities'; + | 'arraybuffer' + | 'blob' + | 'json' + | 'text' + | 'value' + | 'property' + | 'entity' + | 'entities'; withCount?: boolean; } = {} ): Observable { @@ -450,14 +438,14 @@ export class ODataResource { options: ODataOptions & { etag?: string; responseType?: - | 'arraybuffer' - | 'blob' - | 'json' - | 'text' - | 'value' - | 'property' - | 'entity' - | 'entities'; + | 'arraybuffer' + | 'blob' + | 'json' + | 'text' + | 'value' + | 'property' + | 'entity' + | 'entities'; withCount?: boolean; } = {} ): Observable { @@ -468,14 +456,14 @@ export class ODataResource { options: ODataOptions & { etag?: string; responseType?: - | 'arraybuffer' - | 'blob' - | 'json' - | 'text' - | 'value' - | 'property' - | 'entity' - | 'entities'; + | 'arraybuffer' + | 'blob' + | 'json' + | 'text' + | 'value' + | 'property' + | 'entity' + | 'entities'; withCount?: boolean; } = {} ): Observable {