Archived
1
0
Fork 0
This repository has been archived on 2024-02-06. You can view files and clone it, but cannot push or open issues or pull requests.
api-ts-gen/lib/CodePrinter.ts
supmiku39 75674df502 ClientAPI, ServerAPI interface, Schema TS codegen
ClientAPI: use Axios
ServerAPI: use @koa/router
FullDate: wrapped Date only class
APIPromise: enhanced Promise on api response

application/json only(multipart/*, image/*, ... are not supported)
get, post, put, delete, patch only
2020-04-05 00:57:34 +09:00

42 lines
967 B
TypeScript

interface WriteStream {
write(s: string): void;
on(s: string, callback: () => void): void;
end(): void;
}
export class StringStream implements WriteStream {
private content = ''
write(s: string) {
this.content += s;
}
on() {}
end() {}
toString(): string {
return this.content;
}
}
export class CodePrinter {
private cIndent = 0;
constructor(
private writeStream: WriteStream = new StringStream(),
private indentString: string = ' ',
) {}
writeln(s: string = '', dIndent: number = 0) {
if (dIndent < 0) this.cIndent = Math.max(0, this.cIndent + dIndent);
this.write(`${this.indentString.repeat(this.cIndent) + s}\n`);
if (dIndent > 0) this.cIndent += dIndent;
}
write(s: string) {
this.writeStream.write(s);
}
tab(x: number) {
this.cIndent += x;
}
end(): Promise<void> {
return new Promise(rsv => {
this.writeStream.on('finish', rsv);
this.writeStream.end();
});
}
}