-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1cf99e8
commit eab9fb8
Showing
5 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { classes } from "polytype"; | ||
import { | ||
DataModel, | ||
ListDataModel, | ||
ListQueryModel, | ||
ReferenceModel, | ||
} from "../../base/index.js"; | ||
import { | ||
DomainData, | ||
DomainListItemData, | ||
DomainListQueryData, | ||
DomainListQueryModelData, | ||
} from "./types.js"; | ||
import { provideReact } from "../../react/provideReact.js"; | ||
import { config } from "../../config/config.js"; | ||
import assertObjectFound from "../../base/assertObjectFound.js"; | ||
|
||
export class Domain extends ReferenceModel { | ||
public static ofId(id: string): Domain { | ||
return new Domain(id); | ||
} | ||
|
||
public static find = provideReact( | ||
async (id: string): Promise<DomainDetailed | undefined> => { | ||
const data = await config.behaviors.domain.find(id); | ||
if (data !== undefined) { | ||
return new DomainDetailed(data); | ||
} | ||
}, | ||
); | ||
|
||
public static get = provideReact( | ||
async (id: string): Promise<DomainDetailed> => { | ||
const domain = await this.find(id); | ||
assertObjectFound(domain, this, id); | ||
return domain; | ||
}, | ||
); | ||
|
||
public static query = (query: DomainListQueryModelData = {}) => { | ||
return new DomainListQuery(query); | ||
}; | ||
} | ||
|
||
export class DomainCommon extends classes(DataModel<DomainData>, Domain) { | ||
public readonly id: string; | ||
public readonly hostname: string; | ||
|
||
public constructor(data: DomainData) { | ||
super([data], [data.domainId]); | ||
this.hostname = data.domain; | ||
this.id = data.domainId; | ||
} | ||
} | ||
|
||
export class DomainDetailed extends classes( | ||
DomainCommon, | ||
DataModel<DomainData>, | ||
) { | ||
public constructor(data: DomainData) { | ||
super([data], [data]); | ||
} | ||
} | ||
|
||
export class DomainListItem extends classes( | ||
DomainCommon, | ||
DataModel<DomainListItemData>, | ||
) { | ||
public constructor(data: DomainListItemData) { | ||
super([data]); | ||
} | ||
} | ||
|
||
export class DomainListQuery extends ListQueryModel<DomainListQueryData> { | ||
public constructor(data: DomainListQueryModelData = {}) { | ||
super(data); | ||
} | ||
|
||
public refine(query: DomainListQueryModelData) { | ||
return new DomainListQuery({ | ||
...this.query, | ||
...query, | ||
}); | ||
} | ||
|
||
public execute = provideReact(async () => { | ||
const { ...query } = this.query; | ||
const { items, totalCount } = await config.behaviors.domain.query({ | ||
limit: config.defaultPaginationLimit, | ||
...query, | ||
}); | ||
|
||
return new DomainList( | ||
this.query, | ||
items.map((d) => new DomainListItem(d)), | ||
totalCount, | ||
); | ||
}, [this.queryId]); | ||
|
||
public getTotalCount = provideReact(async () => { | ||
const { totalCount } = await this.refine({ limit: 1 }).execute(); | ||
return totalCount; | ||
}, [this.queryId]); | ||
|
||
public findOneAndOnly = provideReact(async () => { | ||
const { items, totalCount } = await this.refine({ limit: 1 }).execute(); | ||
if (totalCount === 1) { | ||
return items[0]; | ||
} | ||
}, [this.queryId]); | ||
} | ||
|
||
export class DomainList extends classes( | ||
DomainListQuery, | ||
ListDataModel<DomainListItem>, | ||
) { | ||
public constructor( | ||
query: DomainListQueryData, | ||
domains: DomainListItem[], | ||
totalCount: number, | ||
) { | ||
super([query], [domains, totalCount]); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { | ||
DomainData, | ||
DomainListItemData, | ||
DomainListQueryData, | ||
} from "../types.js"; | ||
import { QueryResponseData } from "../../../base/index.js"; | ||
|
||
export interface DomainBehaviors { | ||
find: (id: string) => Promise<DomainData>; | ||
query: ( | ||
query?: DomainListQueryData, | ||
) => Promise<QueryResponseData<DomainListItemData>>; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./Domain.js"; | ||
export * from "types.js"; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { MittwaldAPIV2 } from "@mittwald/api-client"; | ||
import { Project } from "../../project/index.js"; | ||
|
||
export type DomainListQueryData = | ||
MittwaldAPIV2.Paths.V2Domains.Get.Parameters.Query; | ||
|
||
export type DomainListQueryModelData = Omit< | ||
DomainListQueryData, | ||
"projectId" | ||
> & { | ||
project?: Project; | ||
}; | ||
|
||
export type DomainData = MittwaldAPIV2.Operations.DomainGetDomain.ResponseData; | ||
|
||
export type DomainListItemData = | ||
MittwaldAPIV2.Operations.DomainListDomains.ResponseData[number]; |