Skip to content

Commit

Permalink
- Added CoreQuery toString() for debugging and ease of use
Browse files Browse the repository at this point in the history
- Added export of Service and subtypes from sdk-core
- Fixed the include query
  • Loading branch information
DavidArayan committed Nov 23, 2023
1 parent 4b13699 commit d8f2f57
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 27 deletions.
23 changes: 19 additions & 4 deletions sdk-core/src/core/core-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,29 @@ export abstract class CoreObject<Attributes extends CoreObjectAttributes> {
/**
* shortcut for easier chained construction of Include Queries
*/
public include(...objects: Array<(typeof CoreObject<CoreObjectAttributes>) | string>): Array<string> {
return objects.map<string>((object: typeof CoreObject<CoreObjectAttributes> | string) => {
if (typeof object === 'string' || object instanceof String) {
return <string>object;
public static include(...objects: Array<(typeof CoreObject<CoreObjectAttributes>) | Array<string>>): Array<string> {
const data: Array<string | Array<string>> = objects.map<string | Array<string>>((object: typeof CoreObject<CoreObjectAttributes> | Array<string>) => {
if (Array.isArray(object)) {
return object.map<string>((object: string) => {
return `${this.type}.${object}`;
});
}

return `${this.type}.${object.type}`;
});

const consolidatedData: Array<string> = new Array<string>();

data.forEach((object: string | Array<string>) => {
if (Array.isArray(object)) {
consolidatedData.push(...object);
}
else {
consolidatedData.push(object);
}
});

return consolidatedData;
}

/**
Expand Down
61 changes: 39 additions & 22 deletions sdk-core/src/core/query/core-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,29 @@ export abstract class CoreQuery<T extends CoreObject<U>, U extends CoreObjectAtt
return this;
}

public include(...objects: Array<(typeof CoreObject<CoreObjectAttributes>) | string>): this {
const data: Array<string> = objects.map<string>((object: typeof CoreObject<CoreObjectAttributes> | string) => {
if (typeof object === 'string' || object instanceof String) {
return <string>object;
public include(...objects: Array<(typeof CoreObject<CoreObjectAttributes>) | Array<string>>): this {
const data: Array<string | Array<string>> = objects.map<string | Array<string>>((object: typeof CoreObject<CoreObjectAttributes> | Array<string>) => {
if (Array.isArray(object)) {
return object.map<string>((object: string) => {
return `${this.instance.type}.${object}`;
});
}

return `${this.instance.type}.${object.type}`;
});

this._queries.push(new IncludeQuery(data));
const consolidatedData: Array<string> = new Array<string>();

data.forEach((object: string | Array<string>) => {
if (Array.isArray(object)) {
consolidatedData.push(...object);
}
else {
consolidatedData.push(object);
}
});

this._queries.push(new IncludeQuery(consolidatedData));

return this;
}
Expand Down Expand Up @@ -101,24 +114,8 @@ export abstract class CoreQuery<T extends CoreObject<U>, U extends CoreObjectAtt
protected async _Fetch(url: string, type: QueryFetchType): Promise<Array<T>> {
const results: Array<T> = new Array<T>();

const queries: Array<Query> = 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
Expand All @@ -127,4 +124,24 @@ export abstract class CoreQuery<T extends CoreObject<U>, 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<Query> = 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);
}
}
4 changes: 3 additions & 1 deletion sdk-core/src/generator/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ export class Generator {
}

private static generateIndexFile(schemas: Array<GeneratedSchema>): 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`;
Expand Down

0 comments on commit d8f2f57

Please sign in to comment.