diff --git a/clients/ts-sdk/openapi.json b/clients/ts-sdk/openapi.json index 8cbae0d098..41328db0d5 100644 --- a/clients/ts-sdk/openapi.json +++ b/clients/ts-sdk/openapi.json @@ -5162,6 +5162,174 @@ ] } }, + "/api/organization/api_key": { + "get": { + "tags": [ + "Organization" + ], + "summary": "Get Organization Api Keys", + "description": "Get the api keys which belong to the organization. The actual api key values are not returned, only the ids, names, and creation dates.", + "operationId": "get_organization_api_keys", + "parameters": [ + { + "name": "TR-Organization", + "in": "header", + "description": "The organization id to use for the request.", + "required": true, + "schema": { + "type": "string", + "format": "uuid" + } + } + ], + "responses": { + "200": { + "description": "JSON body representing the api_key for the organization", + "content": { + "application/json": { + "schema": { + "type": "array", + "items": { + "$ref": "#/components/schemas/ApiKeyRespBody" + } + } + } + } + }, + "400": { + "description": "Service error relating to creating api_key for the organization", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ErrorResponseBody" + } + } + } + } + }, + "security": [ + { + "ApiKey": [ + "readonly" + ] + } + ] + }, + "post": { + "tags": [ + "Organization" + ], + "summary": "Create Organization Api Key", + "description": "Create a new api key for the organization. Successful response will contain the newly created api key.", + "operationId": "create_organization_api_key", + "parameters": [ + { + "name": "TR-Organization", + "in": "header", + "description": "The organization id to use for the request.", + "required": true, + "schema": { + "type": "string", + "format": "uuid" + } + } + ], + "requestBody": { + "description": "JSON request payload to create a new organization api key", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/CreateApiKeyReqPayload" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "JSON body representing the api_key for the organization", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/CreateApiKeyResponse" + } + } + } + }, + "400": { + "description": "Service error relating to creating api_key for the organization", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ErrorResponseBody" + } + } + } + } + }, + "security": [ + { + "ApiKey": [ + "readonly" + ] + } + ] + } + }, + "/api/organization/api_key/{api_key_id}": { + "delete": { + "tags": [ + "Organization" + ], + "summary": "Delete Organization Api Key", + "description": "Delete an api key for the auth'ed organization.", + "operationId": "delete_organization_api_key", + "parameters": [ + { + "name": "api_key_id", + "in": "path", + "description": "The id of the api key to delete", + "required": true, + "schema": { + "type": "string", + "format": "uuid" + } + }, + { + "name": "TR-Organization", + "in": "header", + "description": "The organization id to use for the request.", + "required": true, + "schema": { + "type": "string", + "format": "uuid" + } + } + ], + "responses": { + "204": { + "description": "Confirmation that the api key was deleted" + }, + "400": { + "description": "Service error relating to creating api_key for the organization", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ErrorResponseBody" + } + } + } + } + }, + "security": [ + { + "ApiKey": [ + "readonly" + ] + } + ] + } + }, "/api/organization/update_dataset_configs": { "post": { "tags": [ @@ -6163,100 +6331,6 @@ ] } }, - "/api/user/api_key": { - "post": { - "tags": [ - "User" - ], - "summary": "Create User Api Key", - "description": "Create a new api key for the auth'ed user. Successful response will contain the newly created api key. If a write role is assigned the api key will have permission level of the auth'ed user who calls this endpoint.", - "operationId": "create_user_api_key", - "requestBody": { - "description": "JSON request payload to create a new user api key", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/CreateApiKeyReqPayload" - } - } - }, - "required": true - }, - "responses": { - "200": { - "description": "JSON body representing the api_key for the user", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/CreateApiKeyResponse" - } - } - } - }, - "400": { - "description": "Service error relating to creating api_key for the user", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/ErrorResponseBody" - } - } - } - } - }, - "security": [ - { - "ApiKey": [ - "readonly" - ] - } - ] - } - }, - "/api/user/api_key/{api_key_id}": { - "delete": { - "tags": [ - "User" - ], - "summary": "Delete User Api Key", - "description": "Delete an api key for the auth'ed user.", - "operationId": "delete_user_api_key", - "parameters": [ - { - "name": "api_key_id", - "in": "path", - "description": "The id of the api key to delete", - "required": true, - "schema": { - "type": "string", - "format": "uuid" - } - } - ], - "responses": { - "204": { - "description": "Confirmation that the api key was deleted" - }, - "400": { - "description": "Service error relating to creating api_key for the user", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/ErrorResponseBody" - } - } - } - } - }, - "security": [ - { - "ApiKey": [ - "readonly" - ] - } - ] - } - }, "/metrics": { "post": { "tags": [ @@ -6395,7 +6469,7 @@ "type": "object", "required": [ "id", - "user_id", + "organization_id", "name", "role", "created_at", @@ -6420,12 +6494,9 @@ "name": { "type": "string" }, - "organization_ids": { - "type": "array", - "items": { - "type": "string" - }, - "nullable": true + "organization_id": { + "type": "string", + "format": "uuid" }, "role": { "type": "integer", @@ -6434,10 +6505,6 @@ "updated_at": { "type": "string", "format": "date-time" - }, - "user_id": { - "type": "string", - "format": "uuid" } }, "example": { @@ -6447,12 +6514,9 @@ ], "id": "e3e3e3e3-e3e3-e3e3-e3e3-e3e3e3e3e3e3", "name": "Trieve", - "organization_ids": [ - "o1o1o1o1-o1o1-o1o1-o1o1-o1o1o1o1o1o1" - ], + "organization_id": "e3e3e3e3-e3e3-e3e3-e3e3-e3e3e3e3e3e3", "role": 1, - "updated_at": "2021-01-01 00:00:00.000", - "user_id": "e3e3e3e3-e3e3-e3e3-e3e3-e3e3e3e3e3e3" + "updated_at": "2021-01-01 00:00:00.000" } }, "AuthQuery": { @@ -8273,7 +8337,7 @@ "type": "string", "format": "uuid" }, - "description": "The dataset ids which the api key will have access to. If not provided or empty, the api key will have access to all datasets the auth'ed user has access to. If both dataset_ids and organization_ids are provided, the api key will have access to the intersection of the datasets and organizations.", + "description": "The dataset ids which the api key will have access to. If not provided or empty, the api key will have access to all datasets in the dataset.", "nullable": true }, "default_params": { @@ -8293,26 +8357,17 @@ "type": "string", "description": "The name which will be assigned to the new api key." }, - "organization_ids": { - "type": "array", - "items": { - "type": "string", - "format": "uuid" - }, - "description": "The organization ids which the api key will have access to. If not provided or empty, the api key will have access to all organizations the auth'ed user has access to.", - "nullable": true - }, "role": { "type": "integer", "format": "int32", - "description": "The role which will be assigned to the new api key. Either 0 (read), 1 (read and write at the level of the currently auth'ed user). The auth'ed user must have a role greater than or equal to the role being assigned which means they must be an admin (1) or owner (2) of the organization to assign write permissions with a role of 1." + "description": "The role which will be assigned to the new api key. Either 0 (read), 1 (Admin) or 2 (Owner). The auth'ed user must have a role greater than or equal to the role being assigned." }, "scopes": { "type": "array", "items": { "type": "string" }, - "description": "The routes which the api key will have access to. If not provided or empty, the api key will have access to all routes the auth'ed user has access to. Specify the routes as a list of strings. For example, [\"GET /api/dataset\", \"POST /api/dataset\"].", + "description": "The routes which the api key will have access to. If not provided or empty, the api key will have access to all routes. Specify the routes as a list of strings. For example, [\"GET /api/dataset\", \"POST /api/dataset\"].", "nullable": true } } diff --git a/clients/ts-sdk/src/functions/index.ts b/clients/ts-sdk/src/functions/index.ts index 0fbcf774ef..3712086f85 100644 --- a/clients/ts-sdk/src/functions/index.ts +++ b/clients/ts-sdk/src/functions/index.ts @@ -7,6 +7,7 @@ import * as fileMethods from "./file/index"; import * as eventsMethods from "./events/index"; import * as datasetsMethods from "./datasets/index"; import * as userMethods from "./user/index"; +import * as organizationMethods from "./organization/index"; export default { ...chunkMethods, @@ -18,4 +19,5 @@ export default { ...eventsMethods, ...datasetsMethods, ...userMethods, + ...organizationMethods, }; diff --git a/clients/ts-sdk/src/functions/organization/index.ts b/clients/ts-sdk/src/functions/organization/index.ts new file mode 100644 index 0000000000..21d1194355 --- /dev/null +++ b/clients/ts-sdk/src/functions/organization/index.ts @@ -0,0 +1,53 @@ +/** + * This includes all the functions you can use to communicate with our organization endpoint + * + * @module Organization Methods + */ + +import { TrieveSDK } from "../../sdk"; +import { CreateApiKeyReqPayload, CreateApiKeyResponse } from "../../types.gen"; + +export async function createOrganizationApiKey( + /** @hidden */ + this: TrieveSDK, + props: CreateApiKeyReqPayload, + signal?: AbortSignal, +): Promise { + if (!this.organizationId) { + throw new Error( + "Organization ID is required to create Organization API key", + ); + } + + return this.trieve.fetch( + "/api/organization/api_key", + "post", + { + data: props, + organizationId: this.organizationId, + }, + signal, + ); +} + +export async function deleteOrganizationApiKey( + /** @hidden */ + this: TrieveSDK, + apiKeyId: string, + signal?: AbortSignal, +): Promise { + if (!this.organizationId) { + throw new Error( + "Organization ID is required to delete Organization API key", + ); + } + return this.trieve.fetch( + "/api/organization/api_key/{api_key_id}", + "delete", + { + apiKeyId, + organizationId: this.organizationId, + }, + signal, + ); +} diff --git a/clients/ts-sdk/src/functions/organization/organization.test.ts b/clients/ts-sdk/src/functions/organization/organization.test.ts new file mode 100644 index 0000000000..e5d69ab5c2 --- /dev/null +++ b/clients/ts-sdk/src/functions/organization/organization.test.ts @@ -0,0 +1,134 @@ +import { describe, beforeAll, test, expectTypeOf, expect } from "vitest"; +import { TRIEVE } from "../../__tests__/constants"; +import { TrieveSDK } from "../../sdk"; +import { CreateApiKeyResponse, ReturnQueuedChunk } from "../../types.gen"; + +describe("Organization Tests", async () => { + let trieve: TrieveSDK; + beforeAll(() => { + trieve = TRIEVE; + }); + + test("create an api key and verify it works", async () => { + const apiKeyResponse = await trieve.createOrganizationApiKey({ + role: 1, + name: "test suite key", + }); + + expectTypeOf(apiKeyResponse).toEqualTypeOf(); + + const newTrieve = new TrieveSDK({ + apiKey: apiKeyResponse.api_key, + datasetId: trieve.datasetId, + }); + + const queuedChunk = await newTrieve.createChunk({ + chunk_html: "testing hello world", + tracking_id: "1234", + tag_set: ["test"], + }); + + expectTypeOf(queuedChunk).toEqualTypeOf(); + + newTrieve.deleteChunkByTrackingId({ + trackingId: "1234", + }); + }); + + test("create an expired api key and verify it does not work", async () => { + const apiKeyResponse = await trieve.createOrganizationApiKey({ + expires_at: new Date(new Date().setDate(new Date().getDate() - 1)) + .toISOString() + .slice(0, 19) + .replace("T", " "), + role: 1, + name: "test suite key", + }); + + expectTypeOf(apiKeyResponse).toEqualTypeOf(); + + let errorOccurred = false; + + const newTrieve = new TrieveSDK({ + apiKey: apiKeyResponse.api_key, + datasetId: trieve.datasetId, + }); + + try { + await newTrieve.createChunk({ + chunk_html: "testing hello world", + tracking_id: "should_never_work", + tag_set: ["test"], + }); + + newTrieve.deleteChunkByTrackingId({ + trackingId: "should_never_work", + }); + } catch (e) { + errorOccurred = true; + } + + expect(errorOccurred).toBe(true); + }); + + test("create an api key with a filter for test and verify it excludes chunks without the tag", async () => { + const apiKeyResponse = await trieve.createOrganizationApiKey({ + role: 1, + name: "test suite key", + default_params: { + filters: { + must: [ + { + field: "tag_set", + match_all: ["test"], + }, + ], + }, + }, + }); + + expectTypeOf(apiKeyResponse).toEqualTypeOf(); + + const newTrieve = new TrieveSDK({ + apiKey: apiKeyResponse.api_key, + datasetId: trieve.datasetId, + }); + + const queuedChunks = await newTrieve.createChunk([ + { + chunk_html: "testing hello world", + tracking_id: "not_test", + tag_set: ["not_test"], + }, + { + chunk_html: "testing hello world", + tracking_id: "test", + tag_set: ["test"], + }, + ]); + + expectTypeOf(queuedChunks).toEqualTypeOf(); + + await new Promise((r) => setTimeout(r, 10000)); + + const chunksResp = await newTrieve.scroll({ + page_size: 100, + filters: { + must: [ + { + field: "tag_set", + match_all: ["not_test"], + }, + ], + }, + }); + + for (const chunk of chunksResp.chunks) { + expect(chunk.tag_set).toContain("test"); + } + + newTrieve.deleteChunkByTrackingId({ + trackingId: "1234", + }); + }); +}); diff --git a/clients/ts-sdk/src/functions/user/index.ts b/clients/ts-sdk/src/functions/user/index.ts index e447308102..c71d5b3263 100644 --- a/clients/ts-sdk/src/functions/user/index.ts +++ b/clients/ts-sdk/src/functions/user/index.ts @@ -6,8 +6,6 @@ import { TrieveSDK } from "../../sdk"; import { - CreateApiKeyReqPayload, - CreateApiKeyResponse, UpdateUserOrgRoleReqPayload, } from "../../types.gen"; @@ -32,38 +30,3 @@ export async function updateUserRole( ); } -export async function createUserApiKey( - /** @hidden */ - this: TrieveSDK, - props: CreateApiKeyReqPayload, - signal?: AbortSignal -): Promise { - if (!this.organizationId) { - throw new Error("Organization ID is required to create user API key"); - } - - return this.trieve.fetch( - "/api/user/api_key", - "post", - { - data: props, - }, - signal - ); -} - -export async function deleteUserApiKey( - /** @hidden */ - this: TrieveSDK, - apiKeyId: string, - signal?: AbortSignal -): Promise { - return this.trieve.fetch( - "/api/user/api_key/{api_key_id}", - "delete", - { - apiKeyId, - }, - signal - ); -} \ No newline at end of file diff --git a/clients/ts-sdk/src/functions/user/user.test.ts b/clients/ts-sdk/src/functions/user/user.test.ts index 3621fd68d4..91ac280440 100644 --- a/clients/ts-sdk/src/functions/user/user.test.ts +++ b/clients/ts-sdk/src/functions/user/user.test.ts @@ -1,6 +1,5 @@ -import { beforeAll, describe, expect, expectTypeOf } from "vitest"; +import { beforeAll, describe, expectTypeOf } from "vitest"; import { TrieveSDK } from "../../sdk"; -import { CreateApiKeyResponse, ReturnQueuedChunk } from "../../types.gen"; import { TRIEVE } from "../../__tests__/constants"; import { test } from "../../__tests__/utils"; @@ -9,127 +8,11 @@ describe("User Tests", async () => { beforeAll(() => { trieve = TRIEVE; }); - - test("create an api key and verify it works", async () => { - const apiKeyResponse = await trieve.createUserApiKey({ - role: 1, - name: "test suite key", - }); - - expectTypeOf(apiKeyResponse).toEqualTypeOf(); - - const newTrieve = new TrieveSDK({ - apiKey: apiKeyResponse.api_key, - datasetId: trieve.datasetId, - }); - - const queuedChunk = await newTrieve.createChunk({ - chunk_html: "testing hello world", - tracking_id: "1234", - tag_set: ["test"], - }); - - expectTypeOf(queuedChunk).toEqualTypeOf(); - - newTrieve.deleteChunkByTrackingId({ - trackingId: "1234", - }); - }); - - test("create an expired api key and verify it does not work", async () => { - const apiKeyResponse = await trieve.createUserApiKey({ - expires_at: new Date(new Date().setDate(new Date().getDate() - 1)) - .toISOString() - .slice(0, 19) - .replace("T", " "), - role: 1, - name: "test suite key", - }); - - expectTypeOf(apiKeyResponse).toEqualTypeOf(); - - let errorOccurred = false; - - const newTrieve = new TrieveSDK({ - apiKey: apiKeyResponse.api_key, - datasetId: trieve.datasetId, - }); - - try { - await newTrieve.createChunk({ - chunk_html: "testing hello world", - tracking_id: "should_never_work", - tag_set: ["test"], - }); - - newTrieve.deleteChunkByTrackingId({ - trackingId: "should_never_work", - }); - } catch (e) { - errorOccurred = true; - } - - expect(errorOccurred).toBe(true); - }); - - test("create an api key with a filter for test and verify it excludes chunks without the tag", async () => { - const apiKeyResponse = await trieve.createUserApiKey({ + test("updateUserRole", async () => { + const data = await trieve.updateUserRole({ role: 1, - name: "test suite key", - default_params: { - filters: { - must: [ - { - field: "tag_set", - match_all: ["test"], - }, - ], - }, - }, }); - expectTypeOf(apiKeyResponse).toEqualTypeOf(); - - const newTrieve = new TrieveSDK({ - apiKey: apiKeyResponse.api_key, - datasetId: trieve.datasetId, - }); - - const queuedChunks = await newTrieve.createChunk([ - { - chunk_html: "testing hello world", - tracking_id: "not_test", - tag_set: ["not_test"], - }, - { - chunk_html: "testing hello world", - tracking_id: "test", - tag_set: ["test"], - }, - ]); - - expectTypeOf(queuedChunks).toEqualTypeOf(); - - await new Promise((r) => setTimeout(r, 10000)); - - const chunksResp = await newTrieve.scroll({ - page_size: 100, - filters: { - must: [ - { - field: "tag_set", - match_all: ["not_test"], - }, - ], - }, - }); - - for (const chunk of chunksResp.chunks) { - expect(chunk.tag_set).toContain("test"); - } - - newTrieve.deleteChunkByTrackingId({ - trackingId: "1234", - }); + expectTypeOf(data).toBeVoid(); }); }); diff --git a/clients/ts-sdk/src/types.gen.ts b/clients/ts-sdk/src/types.gen.ts index 36472e14ba..8644352ffb 100644 --- a/clients/ts-sdk/src/types.gen.ts +++ b/clients/ts-sdk/src/types.gen.ts @@ -52,10 +52,9 @@ export type ApiKeyRespBody = { dataset_ids?: Array<(string)> | null; id: string; name: string; - organization_ids?: Array<(string)> | null; + organization_id: string; role: number; updated_at: string; - user_id: string; }; export type AuthQuery = { @@ -619,7 +618,7 @@ export type CrawlShopifyOptions = { export type CreateApiKeyReqPayload = { /** - * The dataset ids which the api key will have access to. If not provided or empty, the api key will have access to all datasets the auth'ed user has access to. If both dataset_ids and organization_ids are provided, the api key will have access to the intersection of the datasets and organizations. + * The dataset ids which the api key will have access to. If not provided or empty, the api key will have access to all datasets in the dataset. */ dataset_ids?: Array<(string)> | null; default_params?: ((ApiKeyRequestParams) | null); @@ -632,15 +631,11 @@ export type CreateApiKeyReqPayload = { */ name: string; /** - * The organization ids which the api key will have access to. If not provided or empty, the api key will have access to all organizations the auth'ed user has access to. - */ - organization_ids?: Array<(string)> | null; - /** - * The role which will be assigned to the new api key. Either 0 (read), 1 (read and write at the level of the currently auth'ed user). The auth'ed user must have a role greater than or equal to the role being assigned which means they must be an admin (1) or owner (2) of the organization to assign write permissions with a role of 1. + * The role which will be assigned to the new api key. Either 0 (read), 1 (Admin) or 2 (Owner). The auth'ed user must have a role greater than or equal to the role being assigned. */ role: number; /** - * The routes which the api key will have access to. If not provided or empty, the api key will have access to all routes the auth'ed user has access to. Specify the routes as a list of strings. For example, ["GET /api/dataset", "POST /api/dataset"]. + * The routes which the api key will have access to. If not provided or empty, the api key will have access to all routes. Specify the routes as a list of strings. For example, ["GET /api/dataset", "POST /api/dataset"]. */ scopes?: Array<(string)> | null; }; @@ -4224,6 +4219,41 @@ export type UpdateOrganizationData = { export type UpdateOrganizationResponse = (Organization); +export type GetOrganizationApiKeysData = { + /** + * The organization id to use for the request. + */ + trOrganization: string; +}; + +export type GetOrganizationApiKeysResponse = (Array); + +export type CreateOrganizationApiKeyData = { + /** + * JSON request payload to create a new organization api key + */ + requestBody: CreateApiKeyReqPayload; + /** + * The organization id to use for the request. + */ + trOrganization: string; +}; + +export type CreateOrganizationApiKeyResponse = (CreateApiKeyResponse); + +export type DeleteOrganizationApiKeyData = { + /** + * The id of the api key to delete + */ + apiKeyId: string; + /** + * The organization id to use for the request. + */ + trOrganization: string; +}; + +export type DeleteOrganizationApiKeyResponse = (void); + export type UpdateAllOrgDatasetConfigsData = { /** * The organization data that you want to create @@ -4437,24 +4467,6 @@ export type UpdateUserData = { export type UpdateUserResponse = (void); -export type CreateUserApiKeyData = { - /** - * JSON request payload to create a new user api key - */ - requestBody: CreateApiKeyReqPayload; -}; - -export type CreateUserApiKeyResponse = (CreateApiKeyResponse); - -export type DeleteUserApiKeyData = { - /** - * The id of the api key to delete - */ - apiKeyId: string; -}; - -export type DeleteUserApiKeyResponse = (void); - export type GetMetricsResponse = (string); export type $OpenApiTs = { @@ -5694,6 +5706,49 @@ export type $OpenApiTs = { }; }; }; + '/api/organization/api_key': { + get: { + req: GetOrganizationApiKeysData; + res: { + /** + * JSON body representing the api_key for the organization + */ + 200: Array; + /** + * Service error relating to creating api_key for the organization + */ + 400: ErrorResponseBody; + }; + }; + post: { + req: CreateOrganizationApiKeyData; + res: { + /** + * JSON body representing the api_key for the organization + */ + 200: CreateApiKeyResponse; + /** + * Service error relating to creating api_key for the organization + */ + 400: ErrorResponseBody; + }; + }; + }; + '/api/organization/api_key/{api_key_id}': { + delete: { + req: DeleteOrganizationApiKeyData; + res: { + /** + * Confirmation that the api key was deleted + */ + 204: void; + /** + * Service error relating to creating api_key for the organization + */ + 400: ErrorResponseBody; + }; + }; + }; '/api/organization/update_dataset_configs': { post: { req: UpdateAllOrgDatasetConfigsData; @@ -5967,36 +6022,6 @@ export type $OpenApiTs = { }; }; }; - '/api/user/api_key': { - post: { - req: CreateUserApiKeyData; - res: { - /** - * JSON body representing the api_key for the user - */ - 200: CreateApiKeyResponse; - /** - * Service error relating to creating api_key for the user - */ - 400: ErrorResponseBody; - }; - }; - }; - '/api/user/api_key/{api_key_id}': { - delete: { - req: DeleteUserApiKeyData; - res: { - /** - * Confirmation that the api key was deleted - */ - 204: void; - /** - * Service error relating to creating api_key for the user - */ - 400: ErrorResponseBody; - }; - }; - }; '/metrics': { post: { res: { diff --git a/frontends/dashboard/src/components/ApiKeyGenerateModal.tsx b/frontends/dashboard/src/components/ApiKeyGenerateModal.tsx index 74a328d80a..62338d96bd 100644 --- a/frontends/dashboard/src/components/ApiKeyGenerateModal.tsx +++ b/frontends/dashboard/src/components/ApiKeyGenerateModal.tsx @@ -6,7 +6,6 @@ import { JSX, Show, useContext, - createResource, } from "solid-js"; import { Dialog, @@ -17,11 +16,7 @@ import { DisclosureButton, DisclosurePanel, } from "terracotta"; -import { - DatasetAndUsage, - fromI32ToUserRole, - SetUserApiKeyResponse, -} from "shared/types"; +import { SetUserApiKeyResponse } from "shared/types"; import { UserContext } from "../contexts/UserContext"; import { createToast } from "./ShowToasts"; import { @@ -32,7 +27,6 @@ import { import { Item, MultiSelect } from "./MultiSelect"; import { JsonInput, Tooltip } from "shared/ui"; import { ApiRoutes, RouteScope } from "./Routes"; -import { Organization } from "trieve-ts-sdk"; import { z } from "zod"; import { chunkFilterSchema, @@ -51,10 +45,8 @@ export const ApiKeyGenerateModal = (props: { const [apiKey, setApiKey] = createSignal(""); const [name, setName] = createSignal(""); - const [role, setRole] = createSignal(1); + const [role, setRole] = createSignal(0); const [generated, setGenerated] = createSignal(false); - const organizations = createMemo(() => userContext?.user?.()?.orgs ?? []); - const [selectedOrgs, setSelectedOrgs] = createSignal([]); const [selectedDatasetIds, setSelectedDatasetIds] = createSignal([]); const [selectedRoutes, setSelectedRoutes] = createSignal([]); const [defaultParams, setDefaultParams] = @@ -81,44 +73,23 @@ export const ApiKeyGenerateModal = (props: { }) .strict(); - const [datasetsAndUsages] = createResource( - selectedOrgs, - async (selected) => { - const datasetsAndUsages: Item[] = []; - const resolved = await Promise.all( - selected.map((org) => - fetch(`${apiHost}/dataset/organization/${org.id}`, { - credentials: "include", - headers: { - "TR-Organization": org.id, - }, - }), - ), - ); - for (const res of resolved) { - if (res.ok) { - const data = (await res.json()) as unknown as DatasetAndUsage[]; - const mapped = data.map((d) => ({ - name: d.dataset.name, - id: d.dataset.id, - })); - datasetsAndUsages.push(...mapped); - } - } - - return datasetsAndUsages; - }, - { initialValue: [] }, - ); + const datasetItems: Item[] = + userContext.orgDatasets()?.map((dataset) => { + return { + id: dataset.dataset.id, + name: dataset.dataset.name, + }; + }) ?? []; const generateApiKey = () => { if (role() !== 0 && !role()) return; - void fetch(`${apiHost}/user/api_key`, { + void fetch(`${apiHost}/organization/api_key`, { credentials: "include", method: "POST", headers: { "Content-Type": "application/json", + "TR-Organization": userContext.selectedOrg().id, }, body: JSON.stringify({ name: name(), @@ -127,10 +98,6 @@ export const ApiKeyGenerateModal = (props: { selectedDatasetIds().length > 0 ? selectedDatasetIds().map((d) => d.id) : undefined, - organization_ids: - selectedOrgs().length > 0 - ? selectedOrgs().map((org) => org.id) - : undefined, scopes: selectedRoutes().length > 0 ? selectedRoutes() @@ -262,13 +229,15 @@ export const ApiKeyGenerateModal = (props: { }} value={role()} > - - {(currentRole) => ( - - )} + = 2}> + + + = 1}> + @@ -314,21 +283,6 @@ export const ApiKeyGenerateModal = (props: { } /> -
- - { - setSelectedOrgs(selected); - }} - /> -