/
Permissions.ts
117 lines (107 loc) · 4.02 KB
/
Permissions.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { ClientContext } from "../../ClientContext";
import { getIdFromLink, getPathFromLink, isResourceValid, ResourceType } from "../../common";
import { SqlQuerySpec } from "../../queryExecutionContext";
import { QueryIterator } from "../../queryIterator";
import { FeedOptions, RequestOptions } from "../../request";
import { Resource } from "../Resource";
import { User } from "../User";
import { Permission } from "./Permission";
import { PermissionBody } from "./PermissionBody";
import { PermissionDefinition } from "./PermissionDefinition";
import { PermissionResponse } from "./PermissionResponse";
/**
* Use to create, replace, query, and read all Permissions.
*
* @see {@link Permission} to read, replace, or delete a specific permission by id.
*/
export class Permissions {
/**
* @hidden
* @param user The parent {@link User}.
*/
constructor(public readonly user: User, private readonly clientContext: ClientContext) {}
/**
* Query all permissions.
* @param query Query configuration for the operation. See {@link SqlQuerySpec} for more info on how to configure a query.
* @param options
*/
public query(query: SqlQuerySpec, options?: FeedOptions): QueryIterator<any>;
/**
* Query all permissions.
* @param query Query configuration for the operation. See {@link SqlQuerySpec} for more info on how to configure a query.
* @param options
*/
public query<T>(query: SqlQuerySpec, options?: FeedOptions): QueryIterator<T>;
public query<T>(query: SqlQuerySpec, options?: FeedOptions): QueryIterator<T> {
const path = getPathFromLink(this.user.url, ResourceType.permission);
const id = getIdFromLink(this.user.url);
return new QueryIterator(this.clientContext, query, options, innerOptions => {
return this.clientContext.queryFeed({
path,
resourceType: ResourceType.permission,
resourceId: id,
resultFn: result => result.Permissions,
query,
options: innerOptions
});
});
}
/**
* Read all permissions.
* @param options
* @example Read all permissions to array.
* ```typescript
* const {body: permissionList} = await user.permissions.readAll().toArray();
* ```
*/
public readAll(options?: FeedOptions): QueryIterator<PermissionDefinition & Resource> {
return this.query(undefined, options);
}
/**
* Create a permission.
*
* A permission represents a per-User Permission to access a specific resource
* e.g. Item or Container.
* @param body Represents the body of the permission.
*/
public async create(body: PermissionDefinition, options?: RequestOptions): Promise<PermissionResponse> {
const err = {};
if (!isResourceValid(body, err)) {
throw err;
}
const path = getPathFromLink(this.user.url, ResourceType.permission);
const id = getIdFromLink(this.user.url);
const response = await this.clientContext.create<PermissionDefinition, PermissionBody>({
body,
path,
resourceType: ResourceType.permission,
resourceId: id,
options
});
const ref = new Permission(this.user, response.result.id, this.clientContext);
return new PermissionResponse(response.result, response.headers, response.code, ref);
}
/**
* Upsert a permission.
*
* A permission represents a per-User Permission to access a
* specific resource e.g. Item or Container.
*/
public async upsert(body: PermissionDefinition, options?: RequestOptions): Promise<PermissionResponse> {
const err = {};
if (!isResourceValid(body, err)) {
throw err;
}
const path = getPathFromLink(this.user.url, ResourceType.permission);
const id = getIdFromLink(this.user.url);
const response = await this.clientContext.upsert<PermissionDefinition, PermissionBody>({
body,
path,
resourceType: ResourceType.permission,
resourceId: id,
options
});
const ref = new Permission(this.user, response.result.id, this.clientContext);
return new PermissionResponse(response.result, response.headers, response.code, ref);
}
}