/
Permission.ts
85 lines (77 loc) · 2.84 KB
/
Permission.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { ClientContext } from "../../ClientContext";
import { createPermissionUri, getIdFromLink, getPathFromLink, isResourceValid, ResourceType } from "../../common";
import { RequestOptions } from "../../request/RequestOptions";
import { User } from "../User";
import { PermissionBody } from "./PermissionBody";
import { PermissionDefinition } from "./PermissionDefinition";
import { PermissionResponse } from "./PermissionResponse";
/**
* Use to read, replace, or delete a given {@link Permission} by id.
*
* @see {@link Permissions} to create, upsert, query, or read all Permissions.
*/
export class Permission {
/**
* Returns a reference URL to the resource. Used for linking in Permissions.
*/
public get url() {
return createPermissionUri(this.user.database.id, this.user.id, this.id);
}
/**
* @hidden
* @param user The parent {@link User}.
* @param id The id of the given {@link Permission}.
*/
constructor(public readonly user: User, public readonly id: string, private readonly clientContext: ClientContext) {}
/**
* Read the {@link PermissionDefinition} of the given {@link Permission}.
* @param options
*/
public async read(options?: RequestOptions): Promise<PermissionResponse> {
const path = getPathFromLink(this.url);
const id = getIdFromLink(this.url);
const response = await this.clientContext.read<PermissionDefinition & PermissionBody>({
path,
resourceType: ResourceType.permission,
resourceId: id,
options
});
return new PermissionResponse(response.result, response.headers, response.code, this);
}
/**
* Replace the given {@link Permission} with the specified {@link PermissionDefinition}.
* @param body The specified {@link PermissionDefinition}.
* @param options
*/
public async replace(body: PermissionDefinition, options?: RequestOptions): Promise<PermissionResponse> {
const err = {};
if (!isResourceValid(body, err)) {
throw err;
}
const path = getPathFromLink(this.url);
const id = getIdFromLink(this.url);
const response = await this.clientContext.replace<PermissionDefinition & PermissionBody>({
body,
path,
resourceType: ResourceType.permission,
resourceId: id,
options
});
return new PermissionResponse(response.result, response.headers, response.code, this);
}
/**
* Delete the given {@link Permission}.
* @param options
*/
public async delete(options?: RequestOptions): Promise<PermissionResponse> {
const path = getPathFromLink(this.url);
const id = getIdFromLink(this.url);
const response = await this.clientContext.delete<PermissionDefinition & PermissionBody>({
path,
resourceType: ResourceType.permission,
resourceId: id,
options
});
return new PermissionResponse(response.result, response.headers, response.code, this);
}
}