Skip to content

Commit

Permalink
feat(models): add domain model
Browse files Browse the repository at this point in the history
  • Loading branch information
ntdoJanneck committed Oct 7, 2024
1 parent 1cf99e8 commit eab9fb8
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/models/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ContractBehaviors } from "../contract/Contract/behaviors/index.js";
import { AppInstallationBehaviors } from "../app/AppInstallation/behaviors/index.js";
import { ContractItemBehaviors } from "../contract/ContractItem/behaviors/index.js";
import { ArticleBehaviors } from "../article/Article/behaviors/index.js";
import { DomainBehaviors } from "../domain/Domain/behaviors/types.js";

interface Config {
defaultPaginationLimit: number;
Expand All @@ -17,6 +18,7 @@ interface Config {
server: ServerBehaviors;
customer: CustomerBehaviors;
ingress: IngressBehaviors;
domain: DomainBehaviors;
appInstallation: AppInstallationBehaviors;
};
}
Expand All @@ -31,6 +33,7 @@ export const config: Config = {
server: undefined as unknown as ServerBehaviors,
customer: undefined as unknown as CustomerBehaviors,
ingress: undefined as unknown as IngressBehaviors,
domain: undefined as unknown as DomainBehaviors,
appInstallation: undefined as unknown as AppInstallationBehaviors,
},
};
124 changes: 124 additions & 0 deletions packages/models/src/domain/Domain/Domain.ts
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]);
}
}
13 changes: 13 additions & 0 deletions packages/models/src/domain/Domain/behaviors/types.ts
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>>;
}
2 changes: 2 additions & 0 deletions packages/models/src/domain/Domain/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./Domain.js";
export * from "types.js";
17 changes: 17 additions & 0 deletions packages/models/src/domain/Domain/types.ts
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];

0 comments on commit eab9fb8

Please sign in to comment.