-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspaceScopedBasicRepository.ts
49 lines (42 loc) Β· 2.39 KB
/
spaceScopedBasicRepository.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { NewSpaceScopedResource, SpaceScopedResource } from "../spaceScopedResource";
import { Client } from "../client";
import { RouteArgs } from "../resolver";
import { ListArgs, BasicRepository } from "./basicRepository";
import { ResourceCollection } from "../resourceCollection";
import { spaceScopedRoutePrefix } from "../spaceScopedRoutePrefix";
export class SpaceScopedBasicRepository<
TExistingResource extends SpaceScopedResource,
TNewResource extends NewSpaceScopedResource,
TListArgs extends ListArgs & RouteArgs = ListArgs,
TCreateArgs extends RouteArgs = RouteArgs,
TModifyArgs extends RouteArgs = RouteArgs
> extends BasicRepository<TExistingResource, TNewResource, TListArgs, TCreateArgs, TModifyArgs> {
protected readonly spaceName: string;
constructor(client: Client, spaceName: string, baseApiPathTemplate: string, listParametersTemplate: string) {
super(client, baseApiPathTemplate, listParametersTemplate);
if (!baseApiPathTemplate.startsWith(spaceScopedRoutePrefix)) {
throw new Error("Space scoped repositories must prefix their baseApiTemplate with `spaceScopedRoutePrefix`");
}
this.spaceName = spaceName;
}
override del(resource: TExistingResource) {
return this.client
.del(`${this.baseApiPathTemplate}/${resource.Id}`, { spaceName: this.spaceName })
.then((d) => this.notifySubscribersToDataModifications(resource));
}
override create(resource: TNewResource, args?: TCreateArgs): Promise<TExistingResource> {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return super.create(resource, { spaceName: this.spaceName, ...args! });
}
override get(id: string): Promise<TExistingResource> {
return this.client.request(`${this.baseApiPathTemplate}/${id}`, { spaceName: this.spaceName });
}
override list(args?: TListArgs): Promise<ResourceCollection<TExistingResource>> {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return super.list({ spaceName: this.spaceName, ...args! });
}
override modify(resource: TExistingResource, args?: TModifyArgs): Promise<TExistingResource> {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return super.modify(resource, { spaceName: this.spaceName, ...args! });
}
}