Skip to content

Commit 80eb5c1

Browse files
densumeshskeptrunedev
authored andcommitted
feature: scope api keys to orgs
1 parent 1a1f797 commit 80eb5c1

21 files changed

Lines changed: 1018 additions & 827 deletions

File tree

clients/ts-sdk/openapi.json

Lines changed: 212 additions & 131 deletions
Large diffs are not rendered by default.

clients/ts-sdk/src/functions/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as fileMethods from "./file/index";
77
import * as eventsMethods from "./events/index";
88
import * as datasetsMethods from "./datasets/index";
99
import * as userMethods from "./user/index";
10+
import * as organizationMethods from "./organization/index";
1011

1112
export default {
1213
...chunkMethods,
@@ -18,4 +19,5 @@ export default {
1819
...eventsMethods,
1920
...datasetsMethods,
2021
...userMethods,
22+
...organizationMethods,
2123
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* This includes all the functions you can use to communicate with our organization endpoint
3+
*
4+
* @module Organization Methods
5+
*/
6+
7+
import { TrieveSDK } from "../../sdk";
8+
import { CreateApiKeyReqPayload, CreateApiKeyResponse } from "../../types.gen";
9+
10+
export async function createOrganizationApiKey(
11+
/** @hidden */
12+
this: TrieveSDK,
13+
props: CreateApiKeyReqPayload,
14+
signal?: AbortSignal,
15+
): Promise<CreateApiKeyResponse> {
16+
if (!this.organizationId) {
17+
throw new Error(
18+
"Organization ID is required to create Organization API key",
19+
);
20+
}
21+
22+
return this.trieve.fetch(
23+
"/api/organization/api_key",
24+
"post",
25+
{
26+
data: props,
27+
organizationId: this.organizationId,
28+
},
29+
signal,
30+
);
31+
}
32+
33+
export async function deleteOrganizationApiKey(
34+
/** @hidden */
35+
this: TrieveSDK,
36+
apiKeyId: string,
37+
signal?: AbortSignal,
38+
): Promise<void> {
39+
if (!this.organizationId) {
40+
throw new Error(
41+
"Organization ID is required to delete Organization API key",
42+
);
43+
}
44+
return this.trieve.fetch(
45+
"/api/organization/api_key/{api_key_id}",
46+
"delete",
47+
{
48+
apiKeyId,
49+
organizationId: this.organizationId,
50+
},
51+
signal,
52+
);
53+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import { describe, beforeAll, test, expectTypeOf, expect } from "vitest";
2+
import { TRIEVE } from "../../__tests__/constants";
3+
import { TrieveSDK } from "../../sdk";
4+
import { CreateApiKeyResponse, ReturnQueuedChunk } from "../../types.gen";
5+
6+
describe("Organization Tests", async () => {
7+
let trieve: TrieveSDK;
8+
beforeAll(() => {
9+
trieve = TRIEVE;
10+
});
11+
12+
test("create an api key and verify it works", async () => {
13+
const apiKeyResponse = await trieve.createOrganizationApiKey({
14+
role: 1,
15+
name: "test suite key",
16+
});
17+
18+
expectTypeOf(apiKeyResponse).toEqualTypeOf<CreateApiKeyResponse>();
19+
20+
const newTrieve = new TrieveSDK({
21+
apiKey: apiKeyResponse.api_key,
22+
datasetId: trieve.datasetId,
23+
});
24+
25+
const queuedChunk = await newTrieve.createChunk({
26+
chunk_html: "testing hello world",
27+
tracking_id: "1234",
28+
tag_set: ["test"],
29+
});
30+
31+
expectTypeOf(queuedChunk).toEqualTypeOf<ReturnQueuedChunk>();
32+
33+
newTrieve.deleteChunkByTrackingId({
34+
trackingId: "1234",
35+
});
36+
});
37+
38+
test("create an expired api key and verify it does not work", async () => {
39+
const apiKeyResponse = await trieve.createOrganizationApiKey({
40+
expires_at: new Date(new Date().setDate(new Date().getDate() - 1))
41+
.toISOString()
42+
.slice(0, 19)
43+
.replace("T", " "),
44+
role: 1,
45+
name: "test suite key",
46+
});
47+
48+
expectTypeOf(apiKeyResponse).toEqualTypeOf<CreateApiKeyResponse>();
49+
50+
let errorOccurred = false;
51+
52+
const newTrieve = new TrieveSDK({
53+
apiKey: apiKeyResponse.api_key,
54+
datasetId: trieve.datasetId,
55+
});
56+
57+
try {
58+
await newTrieve.createChunk({
59+
chunk_html: "testing hello world",
60+
tracking_id: "should_never_work",
61+
tag_set: ["test"],
62+
});
63+
64+
newTrieve.deleteChunkByTrackingId({
65+
trackingId: "should_never_work",
66+
});
67+
} catch (e) {
68+
errorOccurred = true;
69+
}
70+
71+
expect(errorOccurred).toBe(true);
72+
});
73+
74+
test("create an api key with a filter for test and verify it excludes chunks without the tag", async () => {
75+
const apiKeyResponse = await trieve.createOrganizationApiKey({
76+
role: 1,
77+
name: "test suite key",
78+
default_params: {
79+
filters: {
80+
must: [
81+
{
82+
field: "tag_set",
83+
match_all: ["test"],
84+
},
85+
],
86+
},
87+
},
88+
});
89+
90+
expectTypeOf(apiKeyResponse).toEqualTypeOf<CreateApiKeyResponse>();
91+
92+
const newTrieve = new TrieveSDK({
93+
apiKey: apiKeyResponse.api_key,
94+
datasetId: trieve.datasetId,
95+
});
96+
97+
const queuedChunks = await newTrieve.createChunk([
98+
{
99+
chunk_html: "testing hello world",
100+
tracking_id: "not_test",
101+
tag_set: ["not_test"],
102+
},
103+
{
104+
chunk_html: "testing hello world",
105+
tracking_id: "test",
106+
tag_set: ["test"],
107+
},
108+
]);
109+
110+
expectTypeOf(queuedChunks).toEqualTypeOf<ReturnQueuedChunk>();
111+
112+
await new Promise((r) => setTimeout(r, 10000));
113+
114+
const chunksResp = await newTrieve.scroll({
115+
page_size: 100,
116+
filters: {
117+
must: [
118+
{
119+
field: "tag_set",
120+
match_all: ["not_test"],
121+
},
122+
],
123+
},
124+
});
125+
126+
for (const chunk of chunksResp.chunks) {
127+
expect(chunk.tag_set).toContain("test");
128+
}
129+
130+
newTrieve.deleteChunkByTrackingId({
131+
trackingId: "1234",
132+
});
133+
});
134+
});

clients/ts-sdk/src/functions/user/index.ts

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
import { TrieveSDK } from "../../sdk";
88
import {
9-
CreateApiKeyReqPayload,
10-
CreateApiKeyResponse,
119
UpdateUserOrgRoleReqPayload,
1210
} from "../../types.gen";
1311

@@ -32,38 +30,3 @@ export async function updateUserRole(
3230
);
3331
}
3432

35-
export async function createUserApiKey(
36-
/** @hidden */
37-
this: TrieveSDK,
38-
props: CreateApiKeyReqPayload,
39-
signal?: AbortSignal
40-
): Promise<CreateApiKeyResponse> {
41-
if (!this.organizationId) {
42-
throw new Error("Organization ID is required to create user API key");
43-
}
44-
45-
return this.trieve.fetch(
46-
"/api/user/api_key",
47-
"post",
48-
{
49-
data: props,
50-
},
51-
signal
52-
);
53-
}
54-
55-
export async function deleteUserApiKey(
56-
/** @hidden */
57-
this: TrieveSDK,
58-
apiKeyId: string,
59-
signal?: AbortSignal
60-
): Promise<void> {
61-
return this.trieve.fetch(
62-
"/api/user/api_key/{api_key_id}",
63-
"delete",
64-
{
65-
apiKeyId,
66-
},
67-
signal
68-
);
69-
}
Lines changed: 4 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { beforeAll, describe, expect, expectTypeOf } from "vitest";
1+
import { beforeAll, describe, expectTypeOf } from "vitest";
22
import { TrieveSDK } from "../../sdk";
3-
import { CreateApiKeyResponse, ReturnQueuedChunk } from "../../types.gen";
43
import { TRIEVE } from "../../__tests__/constants";
54
import { test } from "../../__tests__/utils";
65

@@ -9,127 +8,11 @@ describe("User Tests", async () => {
98
beforeAll(() => {
109
trieve = TRIEVE;
1110
});
12-
13-
test("create an api key and verify it works", async () => {
14-
const apiKeyResponse = await trieve.createUserApiKey({
15-
role: 1,
16-
name: "test suite key",
17-
});
18-
19-
expectTypeOf(apiKeyResponse).toEqualTypeOf<CreateApiKeyResponse>();
20-
21-
const newTrieve = new TrieveSDK({
22-
apiKey: apiKeyResponse.api_key,
23-
datasetId: trieve.datasetId,
24-
});
25-
26-
const queuedChunk = await newTrieve.createChunk({
27-
chunk_html: "testing hello world",
28-
tracking_id: "1234",
29-
tag_set: ["test"],
30-
});
31-
32-
expectTypeOf(queuedChunk).toEqualTypeOf<ReturnQueuedChunk>();
33-
34-
newTrieve.deleteChunkByTrackingId({
35-
trackingId: "1234",
36-
});
37-
});
38-
39-
test("create an expired api key and verify it does not work", async () => {
40-
const apiKeyResponse = await trieve.createUserApiKey({
41-
expires_at: new Date(new Date().setDate(new Date().getDate() - 1))
42-
.toISOString()
43-
.slice(0, 19)
44-
.replace("T", " "),
45-
role: 1,
46-
name: "test suite key",
47-
});
48-
49-
expectTypeOf(apiKeyResponse).toEqualTypeOf<CreateApiKeyResponse>();
50-
51-
let errorOccurred = false;
52-
53-
const newTrieve = new TrieveSDK({
54-
apiKey: apiKeyResponse.api_key,
55-
datasetId: trieve.datasetId,
56-
});
57-
58-
try {
59-
await newTrieve.createChunk({
60-
chunk_html: "testing hello world",
61-
tracking_id: "should_never_work",
62-
tag_set: ["test"],
63-
});
64-
65-
newTrieve.deleteChunkByTrackingId({
66-
trackingId: "should_never_work",
67-
});
68-
} catch (e) {
69-
errorOccurred = true;
70-
}
71-
72-
expect(errorOccurred).toBe(true);
73-
});
74-
75-
test("create an api key with a filter for test and verify it excludes chunks without the tag", async () => {
76-
const apiKeyResponse = await trieve.createUserApiKey({
11+
test("updateUserRole", async () => {
12+
const data = await trieve.updateUserRole({
7713
role: 1,
78-
name: "test suite key",
79-
default_params: {
80-
filters: {
81-
must: [
82-
{
83-
field: "tag_set",
84-
match_all: ["test"],
85-
},
86-
],
87-
},
88-
},
8914
});
9015

91-
expectTypeOf(apiKeyResponse).toEqualTypeOf<CreateApiKeyResponse>();
92-
93-
const newTrieve = new TrieveSDK({
94-
apiKey: apiKeyResponse.api_key,
95-
datasetId: trieve.datasetId,
96-
});
97-
98-
const queuedChunks = await newTrieve.createChunk([
99-
{
100-
chunk_html: "testing hello world",
101-
tracking_id: "not_test",
102-
tag_set: ["not_test"],
103-
},
104-
{
105-
chunk_html: "testing hello world",
106-
tracking_id: "test",
107-
tag_set: ["test"],
108-
},
109-
]);
110-
111-
expectTypeOf(queuedChunks).toEqualTypeOf<ReturnQueuedChunk>();
112-
113-
await new Promise((r) => setTimeout(r, 10000));
114-
115-
const chunksResp = await newTrieve.scroll({
116-
page_size: 100,
117-
filters: {
118-
must: [
119-
{
120-
field: "tag_set",
121-
match_all: ["not_test"],
122-
},
123-
],
124-
},
125-
});
126-
127-
for (const chunk of chunksResp.chunks) {
128-
expect(chunk.tag_set).toContain("test");
129-
}
130-
131-
newTrieve.deleteChunkByTrackingId({
132-
trackingId: "1234",
133-
});
16+
expectTypeOf(data).toBeVoid();
13417
});
13518
});

0 commit comments

Comments
 (0)