Skip to content

Commit 31c3841

Browse files
committed
feat: update toJSON method
1 parent a6b0f12 commit 31c3841

File tree

4 files changed

+44
-24
lines changed

4 files changed

+44
-24
lines changed

.changeset/hip-webs-invite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@arkts/image-manager": minor
3+
---
4+
5+
feat: update toJSON method

src/images/image.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,27 @@ export interface BaseImage {
2424
createDownloader(signal?: AbortSignal): Promise<ImageDownloader<this> | RequestUrlError>
2525
}
2626

27+
export namespace BaseImage {
28+
export interface Stringifiable {
29+
imageType: ImageType
30+
arch: 'arm' | 'x86' | (string & {})
31+
path: string
32+
checksum: string
33+
fsPath: string
34+
version: string
35+
apiVersion: string
36+
targetOS: string
37+
targetVersion: string
38+
deviceType: DeviceType | (string & {})
39+
snakecaseDeviceType: SnakecaseDeviceType | (string & {})
40+
}
41+
}
42+
2743
export type Image = LocalImage | RemoteImage
2844
export type ImageType = 'local' | 'remote'
2945

30-
export abstract class ImageBase<T> implements BaseImage, Stringifiable<T> {
46+
export abstract class ImageBase<T extends BaseImage.Stringifiable> implements BaseImage, Stringifiable<T> {
3147
abstract imageType: ImageType
32-
abstract toJSON(): T
3348

3449
constructor(
3550
private readonly response: any,
@@ -125,4 +140,20 @@ export abstract class ImageBase<T> implements BaseImage, Stringifiable<T> {
125140
return new RequestUrlError(error.message, error.code, error)
126141
}
127142
}
143+
144+
toJSON(): T {
145+
return {
146+
imageType: this.imageType,
147+
arch: this.getArch(),
148+
path: this.getPath(),
149+
checksum: this.getChecksum(),
150+
fsPath: this.getFsPath(),
151+
version: this.getVersion(),
152+
apiVersion: this.getApiVersion(),
153+
targetOS: this.getTargetOS(),
154+
targetVersion: this.getTargetVersion(),
155+
deviceType: this.getDeviceType(),
156+
snakecaseDeviceType: this.getSnakecaseDeviceType(),
157+
} as T
158+
}
128159
}

src/images/local-image.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,9 @@ export interface LocalImage extends BaseImage, Stringifiable<LocalImage.Stringif
1818
}
1919

2020
export namespace LocalImage {
21-
export interface Stringifiable {
21+
export interface Stringifiable extends Omit<BaseImage.Stringifiable, 'imageType'> {
2222
imageType: 'local'
23-
path: string
24-
version: string
25-
apiVersion: string
26-
targetVersion: string
27-
checksum: string
23+
executablePath: string
2824
}
2925
}
3026

@@ -105,12 +101,8 @@ export class LocalImageImpl extends ImageBase<LocalImage.Stringifiable> implemen
105101

106102
toJSON(): LocalImage.Stringifiable {
107103
return {
108-
imageType: this.imageType,
109-
path: this.getPath(),
110-
version: this.getVersion(),
111-
apiVersion: this.getApiVersion(),
112-
targetVersion: this.getTargetVersion(),
113-
checksum: this.getChecksum(),
104+
...super.toJSON(),
105+
executablePath: this.getExecutablePath(),
114106
}
115107
}
116108
}

src/images/remote-image.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,15 @@ export interface RemoteImage extends BaseImage, Stringifiable<RemoteImage.String
77
}
88

99
export namespace RemoteImage {
10-
export interface Stringifiable {
10+
export interface Stringifiable extends Omit<BaseImage.Stringifiable, 'imageType'> {
1111
imageType: 'remote'
12-
path: string
13-
apiVersion: string
14-
checksum: string
1512
}
1613
}
1714

1815
export class RemoteImageImpl extends ImageBase<RemoteImage.Stringifiable> implements RemoteImage {
1916
imageType = 'remote' as const
2017

2118
toJSON(): RemoteImage.Stringifiable {
22-
return {
23-
imageType: this.imageType,
24-
path: this.getPath(),
25-
apiVersion: this.getApiVersion(),
26-
checksum: this.getChecksum(),
27-
}
19+
return super.toJSON()
2820
}
2921
}

0 commit comments

Comments
 (0)