Skip to content

Commit 31cde75

Browse files
authored
Add readObjectByName method for Vault API (#1424)
## Description This new endpoint matches the response of the existing ID-based read method. Names have enforced uniqueness. ## Documentation Does this require changes to the WorkOS Docs? E.g. the [API Reference](https://workos.com/docs/reference) or code snippets need updates. ``` [x] Yes ``` If yes, link a related docs PR and add a docs maintainer as a reviewer. Their approval is required. workos/workos#49220
1 parent 0e60589 commit 31cde75

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

src/vault/vault-live-test.spec.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,38 @@ describe.skip('Vault Live Test', () => {
7070
});
7171
});
7272

73+
it('Reads objects by name', async () => {
74+
const objectName = `${objectPrefix}-nazca`;
75+
const newObject = await workos.vault.createObject({
76+
name: objectName,
77+
value: 'Suri 10-15 micron',
78+
context: { fiber: 'Alpalca' },
79+
});
80+
81+
const expectedMetadata = {
82+
id: expect.any(String),
83+
context: {
84+
fiber: 'Alpalca',
85+
},
86+
environmentId: expect.any(String),
87+
keyId: expect.any(String),
88+
updatedAt: expect.any(Date),
89+
updatedBy: {
90+
id: expect.any(String),
91+
name: expect.any(String),
92+
},
93+
versionId: expect.any(String),
94+
};
95+
96+
const objectValue = await workos.vault.readObjectByName(objectName);
97+
expect(objectValue).toStrictEqual({
98+
id: newObject.id,
99+
name: objectName,
100+
value: 'Suri 10-15 micron',
101+
metadata: expectedMetadata,
102+
});
103+
});
104+
73105
it('Fails to create objects with the same name', async () => {
74106
const objectName = `${objectPrefix}-lima`;
75107
await workos.vault.createObject({
@@ -173,7 +205,7 @@ describe.skip('Vault Live Test', () => {
173205
const newObject = await workos.vault.createObject({
174206
name: objectName,
175207
value: 'Qiviut 11-13 micron',
176-
context: { fiber: 'Musk Ox' },
208+
context: { fiber: 'MuskOx' },
177209
});
178210

179211
const objectDescription = await workos.vault.describeObject({
@@ -183,7 +215,7 @@ describe.skip('Vault Live Test', () => {
183215
const expectedMetadata = {
184216
id: expect.any(String),
185217
context: {
186-
fiber: 'Musk Ox',
218+
fiber: 'MuskOx',
187219
},
188220
environmentId: expect.any(String),
189221
keyId: expect.any(String),
@@ -213,7 +245,7 @@ describe.skip('Vault Live Test', () => {
213245
await workos.vault.createObject({
214246
name: objectName,
215247
value: 'Qiviut 11-13 micron',
216-
context: { fiber: 'Musk Ox' },
248+
context: { fiber: 'MuskOx' },
217249
});
218250
objectNames.push(objectName);
219251
}
@@ -296,7 +328,7 @@ describe.skip('Vault Live Test', () => {
296328
const aad = 'seq1';
297329
const encrypted = await workos.vault.encrypt(data, keyContext, aad);
298330
await expect(() => workos.vault.decrypt(encrypted)).rejects.toThrow(
299-
'unable to authenticate data',
331+
'The operation failed for an operation-specific reason',
300332
);
301333
});
302334
});

src/vault/vault.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,32 @@ describe('Vault', () => {
125125
});
126126
});
127127

128+
describe('readObjectByName', () => {
129+
it('reads an object by name', async () => {
130+
const objectName = 'lima';
131+
const objectId = 'secret1';
132+
fetchOnce({
133+
id: objectId,
134+
metadata: {
135+
id: objectId,
136+
context: { emporer: 'groove' },
137+
environment_id: 'environment_d',
138+
key_id: 'key1',
139+
updated_at: '2025-03-11T02:18:54.250931Z',
140+
updated_by: { id: 'key_xxx', name: 'Local Test Key' },
141+
version_id: 'version1',
142+
},
143+
name: objectName,
144+
value: 'Pull the lever Gronk',
145+
});
146+
const resource = await workos.vault.readObjectByName(objectName);
147+
expect(fetchURL()).toContain(`/vault/v1/kv/name/${objectName}`);
148+
expect(fetchMethod()).toBe('GET');
149+
expect(resource.name).toBe(objectName);
150+
expect(resource.id).toBe(objectId);
151+
});
152+
});
153+
128154
describe('listSecrets', () => {
129155
it('gets a paginated list of secrets', async () => {
130156
fetchOnce({

src/vault/vault.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ export class Vault {
114114
return deserializeObject(data);
115115
}
116116

117+
async readObjectByName(name: string): Promise<VaultObject> {
118+
const { data } = await this.workos.get<ReadObjectResponse>(
119+
`/vault/v1/kv/name/${encodeURIComponent(name)}`,
120+
);
121+
return deserializeObject(data);
122+
}
123+
117124
async describeObject(options: ReadObjectOptions): Promise<VaultObject> {
118125
const { data } = await this.workos.get<ReadObjectResponse>(
119126
`/vault/v1/kv/${encodeURIComponent(options.id)}/metadata`,

0 commit comments

Comments
 (0)