/
resources.ts
153 lines (131 loc) · 4.53 KB
/
resources.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { FullItem } from "../model/fullItem";
import { Item as SimpleItem, ObjectSerializer } from "../model/models";
import { Vault } from "../model/vault";
import { RequestAdapter, Response } from "./requests";
class OPResource {
protected adapter: RequestAdapter;
public constructor(adapter: RequestAdapter) {
this.adapter = adapter;
}
}
export class Vaults extends OPResource {
private basePath = "v1/vaults";
/**
* Return all vaults the Service Account has permission to view.
*/
public async list(): Promise<Vault[]> {
const { data } = await this.adapter.sendRequest(
"get",
`${this.basePath}/`,
);
return ObjectSerializer.deserialize(data, "Array<Vault>");
}
/**
* Fetch basic information about all items in specified Vault
*
* @param vaultId
*/
public async listItems(vaultId: string): Promise<SimpleItem[]> {
const { data } = await this.adapter.sendRequest(
"get",
`${this.basePath}/${vaultId}/items`,
);
return ObjectSerializer.deserialize(data, "Array<Item>");
}
/**
* Get metadata about a single vault
*
* @param vaultId
*/
public async getVault(vaultId: string): Promise<Vault> {
const { data } = await this.adapter.sendRequest(
"get",
`${this.basePath}/${vaultId}`,
);
return ObjectSerializer.deserialize(data, "Vault");
}
}
export class Items extends OPResource {
private basePath = (vaultId: string, itemId?: string): string =>
itemId && typeof itemId !== "undefined"
? `v1/vaults/${vaultId}/items/${itemId}`
: `v1/vaults/${vaultId}/items/`;
public async create(vaultId: string, item: FullItem): Promise<FullItem> {
item.vault = Object.assign(item.vault || {}, {
id: vaultId
});
const { data } = await this.adapter.sendRequest(
"post",
this.basePath(vaultId),
{
data: ObjectSerializer.serialize(item, "FullItem"),
},
);
return ObjectSerializer.deserialize(data, "FullItem");
}
public async update(vaultId, item: FullItem): Promise<FullItem> {
const { data } = await this.adapter.sendRequest(
"put",
this.basePath(vaultId, item.id),
{ data: ObjectSerializer.serialize(item, "FullItem") },
);
return ObjectSerializer.deserialize(data, "FullItem");
}
public async get(vaultId: string, opts: GetItemOptions): Promise<FullItem> {
if (!(opts.itemId || opts.title) || (opts.itemId && opts.title)) {
throw TypeError("Items.get() requires itemId or title");
}
const { data } = opts.itemId
? await this.getById(vaultId, opts.itemId)
: await this.getByTitle(vaultId, opts.title);
return ObjectSerializer.deserialize(data, "FullItem");
}
public async delete(vaultId: string, itemId: string): Promise<Response> {
return await this.adapter.sendRequest(
"delete",
this.basePath(vaultId, itemId),
);
}
private async getById(vaultId: string, itemId: string): Promise<Response> {
return await this.adapter.sendRequest(
"get",
this.basePath(vaultId, itemId),
);
}
/**
* Searches for an Item with a case-sensitive, exact match on title.
* If found, queries for complete item details and returns result.
*
* @param {string} vaultId
* @param {string} title
* @returns {Promise<FullItem>}
* @private
*/
private async getByTitle(
vaultId: string,
title: string,
): Promise<Response> {
const queryPath = `${this.basePath(
vaultId,
)}?filter=title eq "${title}"`;
const { data } = await this.adapter.sendRequest("get", queryPath);
if (!data?.length) {
return Promise.reject({
status: 404,
message: "No Items found with title",
});
}
if (data.length > 1) {
return Promise.reject({
status: 400,
message:
"Found multiple Items with given title. Provide a more specific Item title",
});
}
return this.getById(data[0].vault.id, data[0].id);
}
}
// Either itemId OR title must be supplied
type GetItemOptions =
| { itemId: string; title?: never }
| { title: string; itemId?: never };