Last active
April 10, 2022 15:01
-
-
Save pluveto/0ad8401fdd884cce3b2805e8296a78d6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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