diff --git a/sdk-core/src/core/core-object.ts b/sdk-core/src/core/core-object.ts index a7b1994..2416f31 100644 --- a/sdk-core/src/core/core-object.ts +++ b/sdk-core/src/core/core-object.ts @@ -50,14 +50,29 @@ export abstract class CoreObject { /** * shortcut for easier chained construction of Include Queries */ - public include(...objects: Array<(typeof CoreObject) | string>): Array { - return objects.map((object: typeof CoreObject | string) => { - if (typeof object === 'string' || object instanceof String) { - return object; + public static include(...objects: Array<(typeof CoreObject) | Array>): Array { + const data: Array> = objects.map>((object: typeof CoreObject | Array) => { + if (Array.isArray(object)) { + return object.map((object: string) => { + return `${this.type}.${object}`; + }); } return `${this.type}.${object.type}`; }); + + const consolidatedData: Array = new Array(); + + data.forEach((object: string | Array) => { + if (Array.isArray(object)) { + consolidatedData.push(...object); + } + else { + consolidatedData.push(object); + } + }); + + return consolidatedData; } /** diff --git a/sdk-core/src/core/query/core-query.ts b/sdk-core/src/core/query/core-query.ts index 3e688a7..8903cf3 100644 --- a/sdk-core/src/core/query/core-query.ts +++ b/sdk-core/src/core/query/core-query.ts @@ -58,16 +58,29 @@ export abstract class CoreQuery, U extends CoreObjectAtt return this; } - public include(...objects: Array<(typeof CoreObject) | string>): this { - const data: Array = objects.map((object: typeof CoreObject | string) => { - if (typeof object === 'string' || object instanceof String) { - return object; + public include(...objects: Array<(typeof CoreObject) | Array>): this { + const data: Array> = objects.map>((object: typeof CoreObject | Array) => { + if (Array.isArray(object)) { + return object.map((object: string) => { + return `${this.instance.type}.${object}`; + }); } return `${this.instance.type}.${object.type}`; }); - this._queries.push(new IncludeQuery(data)); + const consolidatedData: Array = new Array(); + + data.forEach((object: string | Array) => { + if (Array.isArray(object)) { + consolidatedData.push(...object); + } + else { + consolidatedData.push(object); + } + }); + + this._queries.push(new IncludeQuery(consolidatedData)); return this; } @@ -101,24 +114,8 @@ export abstract class CoreQuery, U extends CoreObjectAtt protected async _Fetch(url: string, type: QueryFetchType): Promise> { const results: Array = new Array(); - const queries: Array = this._queries; - - // generate all the query parameters to the final URL (if any) - // some requests (like POST) will ignore certain queries as they are - // not processable in the provided context - if (queries.length > 0) { - url += '?'; - - queries.forEach((query: Query) => { - url += `${query.toString()}&`; - }); - - // remove the last & keyword - url = url.slice(0, -1); - } - // encode the full url to safely escape all characters (like whitespaces) - const encodedURL: string = encodeURI(url); + const encodedURL: string = encodeURI(url + this.toString()); // proceed with generating the request - for anything other than GET we need to generate a payload // this payload is generated from non-null values of the object attributes @@ -127,4 +124,24 @@ export abstract class CoreQuery, U extends CoreObjectAtt // return the final results which might contain 0 or more objects (depending on the request) return results; } + + /** + * Generates the url sequence of all queries and returns + */ + public toString(): string { + const queries: Array = this._queries; + + if (queries.length <= 0) { + return ''; + } + + let url: string = '?'; + + queries.forEach((query: Query) => { + url += `${query.toString()}&`; + }); + + // remove the last & keyword + return url.slice(0, -1); + } } \ No newline at end of file diff --git a/sdk-core/src/generator/generator.ts b/sdk-core/src/generator/generator.ts index 1a5f042..3a37233 100644 --- a/sdk-core/src/generator/generator.ts +++ b/sdk-core/src/generator/generator.ts @@ -50,7 +50,9 @@ export class Generator { } private static generateIndexFile(schemas: Array): string { - let output: string = ''; + let output: string = '/*\n * Warning: Do Not Edit - Auto Generated via @plattar/sdk-core\n */\n\n'; + + output += 'export { Service, ServiceConfig, ServiceAuth } from "@plattar/sdk-core";\n'; schemas.forEach((schema) => { output += `export * from "./schemas/${schema.name}";\n`;