From b277ace533fb47afc0823ecc0f0a6c20cf3c4873 Mon Sep 17 00:00:00 2001 From: David Arayan Date: Fri, 8 Dec 2023 14:20:15 +1100 Subject: [PATCH] - Fixed a bug with object construction where Object.entries() would try to use undefined for objects with no relationships - URL Constructor will now only use `?` if there are queries to compile --- sdk-core/src/core/core-object.ts | 38 +++++++++++++++------------ sdk-core/src/core/query/core-query.ts | 2 +- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/sdk-core/src/core/core-object.ts b/sdk-core/src/core/core-object.ts index 5f381a9..43d3efb 100644 --- a/sdk-core/src/core/core-object.ts +++ b/sdk-core/src/core/core-object.ts @@ -146,27 +146,31 @@ export abstract class CoreObject { // assign new keys to our attributes // NOTE: this could probably be optimized by using attributes directly instead of deep-copy - for (const [key, value] of Object.entries(data.object.attributes)) { - ((this._attributes))[key] = value; + if (data.object.attributes) { + for (const [key, value] of Object.entries(data.object.attributes)) { + ((this._attributes))[key] = value; + } } // we need to build the relationships of this object from the records section // which includes all the records from any include query - for (const [_key, value] of Object.entries(data.object.relationships)) { - const relationRecord: any = (value).data; - - // check if the object exists in the includes section - the value - // can either be a single object or an array - // this only contains id or type but not the full record - if (Array.isArray(relationRecord)) { - const arrayRecord: Array = relationRecord; - - arrayRecord.forEach((record: any) => { - this._CreateRecord(data, record); - }); - } - else { - this._CreateRecord(data, relationRecord); + if (data.object.relationships) { + for (const [_key, value] of Object.entries(data.object.relationships)) { + const relationRecord: any = (value).data; + + // check if the object exists in the includes section - the value + // can either be a single object or an array + // this only contains id or type but not the full record + if (Array.isArray(relationRecord)) { + const arrayRecord: Array = relationRecord; + + arrayRecord.forEach((record: any) => { + this._CreateRecord(data, record); + }); + } + else { + this._CreateRecord(data, relationRecord); + } } } } diff --git a/sdk-core/src/core/query/core-query.ts b/sdk-core/src/core/query/core-query.ts index a32af81..e4ebfcc 100644 --- a/sdk-core/src/core/query/core-query.ts +++ b/sdk-core/src/core/query/core-query.ts @@ -138,7 +138,7 @@ export abstract class CoreQuery, U extends CoreObjectAtt * Performs the primary request and returns the responses as an array */ protected async _Fetch(url: string, type: QueryFetchType): Promise> { - return CoreQuery.fetch(this.service, this.instance, encodeURI(`${url}?${this.toString()}`), type, this._abort.signal); + return CoreQuery.fetch(this.service, this.instance, encodeURI(`${url}${this._queries.length > 0 ? `?${this.toString()}` : ''}`), type, this._abort.signal); } /**