diff --git a/sdk-core/src/core/service.ts b/sdk-core/src/core/service.ts index 331f629..09da423 100644 --- a/sdk-core/src/core/service.ts +++ b/sdk-core/src/core/service.ts @@ -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 */ @@ -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) { @@ -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; } /** diff --git a/sdk-core/src/generator/generator.ts b/sdk-core/src/generator/generator.ts index cab2e4e..72baab1 100644 --- a/sdk-core/src/generator/generator.ts +++ b/sdk-core/src/generator/generator.ts @@ -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; @@ -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); @@ -45,11 +43,6 @@ 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 @@ -57,10 +50,6 @@ export class Generator { { dir: 'schemas', schemas: schemas - }, - { - dir: 'core', - schemas: [serviceSchema] } ])); } @@ -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 - } - } } \ No newline at end of file