Skip to content

Commit

Permalink
- Consolidated Service, there is no need to generate an upper class f…
Browse files Browse the repository at this point in the history
…rom it as each package is its own seperate instance
  • Loading branch information
DavidArayan committed Dec 4, 2023
1 parent 8335fc7 commit ec6a2f8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 52 deletions.
27 changes: 9 additions & 18 deletions sdk-core/src/core/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,6 @@ export interface ServiceConfig {
readonly auth?: ServiceAuth | null;
}

/**
* Static container used for holding the default service, since sdk-core is used
* in multiple projects, using an anti-pattern can become troublesome for state
* storage
*/
export interface ServiceStaticContainer {
service: Service | null;
}

/**
* Locked down, immutable version of ServiceConfig with defaults already set
*/
Expand All @@ -79,7 +70,9 @@ export interface LockedServiceConfig {
/**
* Allows configuration of a connection service to an api-core based backend service
*/
export abstract class Service {
export class Service {
private static _defaultServiceInstance: Service | null;

private readonly _config: LockedServiceConfig;

public constructor(config: ServiceConfig) {
Expand Down Expand Up @@ -114,23 +107,21 @@ export abstract class Service {
/**
* Configure a new default service
*/
public static config(_config: ServiceConfig): Service {
throw new Error('Service.config is not implemented correctly, contact admin');
public static config(config: ServiceConfig): Service {
Service._defaultServiceInstance = new Service(config);

return Service._defaultServiceInstance;
}

/**
* Returns the default service object
*/
public static get default(): Service {
if (!this.container.service) {
if (!Service._defaultServiceInstance) {
throw new Error('Service.default is not configured, use Service.config() to set a new default');
}

return this.container.service;
}

public static get container(): ServiceStaticContainer {
throw new Error('Service.container is not implemented correctly, contact admin');
return Service._defaultServiceInstance;
}

/**
Expand Down
34 changes: 0 additions & 34 deletions sdk-core/src/generator/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { CoreController } from "@plattar/api-core";
import { GeneratedProject, PackageJsonVars, Project } from "./generators/project";
import { GeneratedSchema, Schema } from "./generators/schema";
import fs from "fs";
import { Util } from "./generators/util";

export interface GeneratorData {
readonly controllers: Array<typeof CoreController>;
Expand All @@ -25,7 +24,6 @@ export class Generator {

// ensure project folder exists
fs.mkdirSync(`${outputDir}/src/schemas`, { recursive: true });
fs.mkdirSync(`${outputDir}/src/core`, { recursive: true });

// write the .npmignore file
await fs.promises.writeFile(`${outputDir}/${project.npmIgnore.fname}`, project.npmIgnore.data);
Expand All @@ -45,22 +43,13 @@ export class Generator {
allSchemas.push(fs.promises.writeFile(`${outputDir}/src/schemas/${schema.fname}`, schema.data));
});

const serviceSchema = this.generateServiceFile(data);

// write the service file
allSchemas.push(fs.promises.writeFile(`${outputDir}/src/core/${serviceSchema.fname}`, serviceSchema.data));

await Promise.all(allSchemas);

// write the index.ts file
await fs.promises.writeFile(`${outputDir}/src/index.ts`, this.generateIndexFile([
{
dir: 'schemas',
schemas: schemas
},
{
dir: 'core',
schemas: [serviceSchema]
}
]));
}
Expand All @@ -78,27 +67,4 @@ export class Generator {

return output;
}

public static generateServiceFile(data: GeneratorData): GeneratedSchema {
const className: string = `Connection`;

let output: string = `import { Service, ServiceConfig, ServiceStaticContainer } from '@plattar/sdk-core';\n\n`;

output += `export class ${className} extends Service {\n`;
output += `\tprivate static readonly serviceContainer: ServiceStaticContainer = {service:null}\n`;
output += `\tpublic static override get container(): ServiceStaticContainer {\n`;
output += `\t\treturn this.serviceContainer;\n`;
output += `\t}\n`;
output += `\tpublic static override config(config: ServiceConfig): ${className} {\n`;
output += `\t\tthis.container.service = new ${className}(config);\n`;
output += `\t\treturn <${className}>this.container.service;\n`;
output += `\t}\n`;
output += '}\n';

return {
name: `connection`,
fname: `connection.ts`,
data: output
}
}
}

0 comments on commit ec6a2f8

Please sign in to comment.