Skip to content

Instantly share code, notes, and snippets.

@pluveto
Last active April 10, 2022 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pluveto/0ad8401fdd884cce3b2805e8296a78d6 to your computer and use it in GitHub Desktop.
Save pluveto/0ad8401fdd884cce3b2805e8296a78d6 to your computer and use it in GitHub Desktop.
import fs from 'fs';
import stream from 'stream';
interface Exportable {
export(): Promise<stream.Readable>;
}
interface CloudExportable {
exportCloud(provider: CloudProvider): Promise<void>;
}
interface FileExportable {
exportLocal(file: string): Promise<void>;
}
interface CloudProvider {
GetWriteStream(): Promise<stream.Writable>;
}
class AzureFileShareProvider implements CloudProvider {
public constructor(private readonly connectionString: string) { }
GetWriteStream(): Promise<stream.Writable> {
throw new Error('Method not implemented.');
}
}
abstract class UniversalExporter implements Exportable, FileExportable, CloudExportable {
public async exportLocal(file: string): Promise<void> {
const readStream = await this.export();
const writeStream = fs.createWriteStream(file);
readStream.pipe(writeStream);
}
public async exportCloud(provider: CloudProvider): Promise<void> {
const readStream = await this.export();
// upload to cloud
// ...
}
public export(): Promise<stream.Readable> {
throw new Error('To be override');
}
}
class TextFile extends UniversalExporter {
public async export(): Promise<stream.Readable> {
const readable = new stream.Readable();
// readable.pipe(...some txt convert)
return readable;
}
}
class ExcelFile extends UniversalExporter {
public async export(): Promise<stream.Readable> {
const readable = new stream.Readable();
// readable.pipe(...some excel convert)
return readable;
}
}
const textFile: UniversalExporter = new TextFile();
textFile.exportLocal('file.txt');
const excelFile: UniversalExporter = new ExcelFile();
excelFile.exportCloud(new AzureFileShareProvider('connectionString'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment