Skip to content

Commit

Permalink
- Added full support for new api-core module that allows separate inp…
Browse files Browse the repository at this point in the history
…ut/output schema definitions for endpoints

- Added support for new Endpoint format from api-core
  • Loading branch information
DavidArayan committed Feb 2, 2024
1 parent c75898b commit bc4ac60
Show file tree
Hide file tree
Showing 11 changed files with 437 additions and 171 deletions.
6 changes: 3 additions & 3 deletions sdk-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
"homepage": "https://www.plattar.com",
"dependencies": {},
"devDependencies": {
"typescript": "^5.2.2",
"@plattar/api-core": "^1.163.5",
"@types/node": "^18.16.0",
"typescript": "^5.3.3",
"@plattar/api-core": "^1.166.2",
"@types/node": "^20.11.13",
"madge": "^6.1.0"
},
"publishConfig": {
Expand Down
24 changes: 12 additions & 12 deletions sdk-core/src/core/query/core-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ export abstract class CoreQuery<T extends CoreObject<U>, U extends CoreObjectAtt
/**
* Performs the primary request and returns the responses as an array
*/
protected async _Fetch(url: string, type: QueryFetchType): Promise<Array<T>> {
return CoreQuery.fetch<T>(this.service, this.instance, encodeURI(`${url}${this._queries.length > 0 ? `?${this.toString()}` : ''}`), type, this._abort.signal);
protected async _Fetch<Output extends CoreObject<U>, U extends CoreObjectAttributes>(output: Output, url: string, type: QueryFetchType): Promise<Array<Output>> {
return CoreQuery.fetch<T, Output>(this.service, this.instance, output, encodeURI(`${url}${this._queries.length > 0 ? `?${this.toString()}` : ''}`), type, this._abort.signal);
}

/**
Expand All @@ -172,8 +172,8 @@ export abstract class CoreQuery<T extends CoreObject<U>, U extends CoreObjectAtt
return url.slice(0, -1);
}

public static async fetch<T extends CoreObject<CoreObjectAttributes>>(service: Service, instance: T, encodedURL: string, type: QueryFetchType, abort?: AbortSignal): Promise<Array<T>> {
const results: Array<T> = new Array<T>();
public static async fetch<Input extends CoreObject<CoreObjectAttributes>, Output extends CoreObject<CoreObjectAttributes>>(service: Service, input: Input, output: Output, encodedURL: string, type: QueryFetchType, abort?: AbortSignal): Promise<Array<Output>> {
const results: Array<Output> = new Array<Output>();

if (!fetch) {
CoreError.init({
Expand Down Expand Up @@ -238,7 +238,7 @@ export abstract class CoreQuery<T extends CoreObject<U>, U extends CoreObjectAtt
case 'POST':
case 'PUT':
case 'PATCH':
request.body = JSON.stringify(instance.payload);
request.body = JSON.stringify(input.payload);
break;
}

Expand Down Expand Up @@ -352,16 +352,16 @@ export abstract class CoreQuery<T extends CoreObject<U>, U extends CoreObjectAtt
const object: any = listRecords[0];

// add the first object to the instance
cache.set(object.id, instance);
cache.set(object.id, output);

// construct the first object
instance.setFromAPI({
output.setFromAPI({
object: object,
includes: includesMap,
cache: cache
});

results.push(instance);
results.push(output);

// begin construction of every other instance
for (let i = 1; i < listRecords.length; i++) {
Expand All @@ -388,7 +388,7 @@ export abstract class CoreQuery<T extends CoreObject<U>, U extends CoreObjectAtt
cache: cache
});

results.push(<T>objectInstance);
results.push(<Output>objectInstance);
}
}
else {
Expand All @@ -406,16 +406,16 @@ export abstract class CoreQuery<T extends CoreObject<U>, U extends CoreObjectAtt
const cache: Map<string, CoreObject<CoreObjectAttributes>> = new Map<string, CoreObject<CoreObjectAttributes>>();

// add the first object to the instance
cache.set(record.id, instance);
cache.set(record.id, output);

// construct the first object
instance.setFromAPI({
output.setFromAPI({
object: record,
includes: includesMap,
cache: cache
});

results.push(instance);
results.push(output);
}
}
catch (err: any) {
Expand Down
3 changes: 2 additions & 1 deletion sdk-core/src/core/relations/core-object-relations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,10 @@ export class CoreObjectRelations {
}

const connection: Service = service || Service.default;
const newObjectInstance = objectType.newInstance();

// otherwise we need to fetch and cache the relations directly
const results: Array<T> = await CoreQuery.fetch(connection, objectType.newInstance(), `${connection.url}/${this._instance.type}/${this._instance.id}/${objectType.type}`, 'GET');
const results: Array<T> = await CoreQuery.fetch<T, T>(connection, <T>newObjectInstance, <T>newObjectInstance, `${connection.url}/${this._instance.type}/${this._instance.id}/${objectType.type}`, 'GET');

// add the results into the cache
this.cache.put(objectType.type, results);
Expand Down
22 changes: 16 additions & 6 deletions sdk-core/src/generator/generator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { CoreController } from "@plattar/api-core";
import { CoreController, ObjectSchema } from "@plattar/api-core";
import { GeneratedProject, PackageJsonVars, Project } from "./generators/project";
import { GeneratedSchema, Schema } from "./generators/schema";
import { Schema } from "./generators/schema";
import fs from "fs";
import { SchemaList } from "./generators/schema-list";
import { EndpointMapping, SchemaCollection } from "./generators/schema-collection";

export interface GeneratorData {
readonly controllers: Array<typeof CoreController>;
Expand All @@ -13,11 +15,19 @@ export class Generator {
public static async generate(data: GeneratorData): Promise<void> {
// generate the project files
const project: GeneratedProject = Project.generate(data.package);
const schemas: Array<GeneratedSchema> = new Array<GeneratedSchema>();

// generate the schema source files
const schemas: SchemaList = new SchemaList();
const collections: SchemaCollection = new SchemaCollection();

// add all schemas into the collections pool
data.controllers.forEach((controller: typeof CoreController) => {
schemas.push(Schema.generate(<CoreController>(new (<any>controller)())));
collections.push(<CoreController>(new (<any>controller)()));
});

// generate the schema source files
// each schema can generate multiple schema files based on endpoints
collections.forEach((schema: typeof ObjectSchema, endpoints: Array<EndpointMapping>) => {
schemas.push(new Schema().generate(schema, endpoints));
});

const outputDir: string = `./${data.output}/${data.package.name}-sdk`;
Expand Down Expand Up @@ -56,7 +66,7 @@ export class Generator {
]));
}

private static generateIndexFile(files: Array<{ dir: string, schemas: Array<GeneratedSchema> }>): string {
private static generateIndexFile(files: Array<{ dir: string, schemas: SchemaList }>): string {
let output: string = '/*\n * Warning: Do Not Edit - Auto Generated via @plattar/sdk-core\n */\n\n';

output += `export { Service, ServiceConfig, ServiceAuth, ServiceOptions, ServiceAuthType, ServiceErrorHandler, ServiceErrorListener, CoreError } from '@plattar/sdk-core';\n`;
Expand Down
74 changes: 74 additions & 0 deletions sdk-core/src/generator/generators/schema-collection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { CoreController, EndpointMount, ObjectSchema } from "@plattar/api-core";

export interface EndpointMapping {
readonly controller: CoreController;
readonly mount: EndpointMount;
}

/**
* Contains a collection of Schema to Endpoint lists used for generating query functions
*/
export class SchemaCollection {
private readonly _map: Map<typeof ObjectSchema, Array<EndpointMapping>>;

public constructor() {
this._map = new Map<typeof ObjectSchema, Array<EndpointMapping>>();
}

public push(controller: CoreController): SchemaCollection {
const mounts: Array<EndpointMount> = controller.mount();
const schema: typeof ObjectSchema = controller.getSchema();

// we insert as part of the input, if the input is undefined we use the
// original schema
mounts.forEach((mount: EndpointMount) => {
this._insert(controller, mount.meta.input || schema, mount);

if (mount.meta.output) {
this._insert(controller, mount.meta.output, null);
}
});

return this;
}

private _insert(controller: CoreController, schema: typeof ObjectSchema, mount: EndpointMount | null): SchemaCollection {
const data: Array<EndpointMapping> | undefined = this._map.get(schema);

if (data) {
if (mount) {
data.push({
controller: controller,
mount: mount
});
}

return this;
}

const newData: Array<EndpointMapping> = new Array<EndpointMapping>();

if (mount) {
newData.push({
controller: controller,
mount: mount
});
}

this._map.set(schema, newData);

return this;
}

public get(key: typeof ObjectSchema): Array<EndpointMapping> | null {
const data = this._map.get(key);

return data ? data : null;
}

public forEach(fn: (schema: typeof ObjectSchema, endpoints: Array<EndpointMapping>) => void) {
this._map.forEach((value: Array<EndpointMapping>, key: typeof ObjectSchema) => {
fn(key, value);
});
}
}
30 changes: 30 additions & 0 deletions sdk-core/src/generator/generators/schema-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Schema } from "./schema";

/**
* Keeps track of all generated Schema objects ready to be written out into files
*/
export class SchemaList {
private readonly _map: Map<string, Schema>;

public constructor() {
this._map = new Map<string, Schema>();
}

public push(schema: Schema): SchemaList {
this._map.set(schema.key, schema);

return this;
}

public get(key: string): Schema | null {
const data = this._map.get(key);

return data ? data : null;
}

public forEach(fn: (schema: Schema) => void) {
this._map.forEach((value: Schema, key: string) => {
fn(value);
});
}
}
Loading

0 comments on commit bc4ac60

Please sign in to comment.