From d2cbc183f69f457681d1dfccfb01bb0e3b2c3693 Mon Sep 17 00:00:00 2001 From: "F. Levi" <55688616+flevi29@users.noreply.github.com> Date: Sat, 5 Apr 2025 16:42:02 +0300 Subject: [PATCH 1/3] Refactor key types --- src/meilisearch.ts | 79 +++++++++++++---------------------------- src/types/index.ts | 1 + src/types/keys.ts | 87 ++++++++++++++++++++++++++++++++++++++++++++++ src/types/types.ts | 34 ------------------ tests/keys.test.ts | 14 ++++---- 5 files changed, 119 insertions(+), 96 deletions(-) create mode 100644 src/types/keys.ts diff --git a/src/meilisearch.ts b/src/meilisearch.ts index 25f1c4e8d..7efd7ee07 100644 --- a/src/meilisearch.ts +++ b/src/meilisearch.ts @@ -7,19 +7,14 @@ import { Index } from "./indexes.js"; import type { - KeyCreation, Config, IndexOptions, IndexObject, - Key, Health, Stats, Version, - KeyUpdate, IndexesQuery, IndexesResults, - KeysQuery, - KeysResults, IndexSwap, MultiSearchParams, FederatedMultiSearchParams, @@ -28,6 +23,11 @@ import type { ExtraRequestInit, Network, RecordAny, + CreateApiKey, + KeyView, + KeyViewList, + ListApiKeys, + PatchApiKey, } from "./types/index.js"; import { ErrorStatusCode } from "./types/index.js"; import { HttpRequests } from "./http-requests.js"; @@ -302,72 +302,41 @@ export class MeiliSearch { /// KEYS /// - /** - * Get all API keys - * - * @param parameters - Parameters to browse the indexes - * @returns Promise returning an object with keys - */ - async getKeys(parameters?: KeysQuery): Promise { - const keys = await this.httpRequest.get({ + /** {@link https://www.meilisearch.com/docs/reference/api/keys#get-all-keys} */ + async getKeys(listApiKeys?: ListApiKeys): Promise { + return await this.httpRequest.get({ path: "keys", - params: parameters, + params: listApiKeys, }); - - keys.results = keys.results.map((key) => ({ - ...key, - createdAt: new Date(key.createdAt), - updatedAt: new Date(key.updatedAt), - })); - - return keys; } - /** - * Get one API key - * - * @param keyOrUid - Key or uid of the API key - * @returns Promise returning a key - */ - async getKey(keyOrUid: string): Promise { - return await this.httpRequest.get({ + /** {@link https://www.meilisearch.com/docs/reference/api/keys#get-one-key} */ + async getKey(keyOrUid: string): Promise { + return await this.httpRequest.get({ path: `keys/${keyOrUid}`, }); } - /** - * Create one API key - * - * @param options - Key options - * @returns Promise returning a key - */ - async createKey(options: KeyCreation): Promise { - return await this.httpRequest.post({ + /** {@link https://www.meilisearch.com/docs/reference/api/keys#create-a-key} */ + async createKey(createApiKey: CreateApiKey): Promise { + return await this.httpRequest.post({ path: "keys", - body: options, + body: createApiKey, }); } - /** - * Update one API key - * - * @param keyOrUid - Key - * @param options - Key options - * @returns Promise returning a key - */ - async updateKey(keyOrUid: string, options: KeyUpdate): Promise { - return await this.httpRequest.patch({ + /** {@link https://www.meilisearch.com/docs/reference/api/keys#update-a-key} */ + async updateKey( + keyOrUid: string, + patchApiKey: PatchApiKey, + ): Promise { + return await this.httpRequest.patch({ path: `keys/${keyOrUid}`, - body: options, + body: patchApiKey, }); } - /** - * Delete one API key - * - * @param keyOrUid - Key - * @returns - */ + /** {@link https://www.meilisearch.com/docs/reference/api/keys#delete-a-key} */ async deleteKey(keyOrUid: string): Promise { await this.httpRequest.delete({ path: `keys/${keyOrUid}` }); } diff --git a/src/types/index.ts b/src/types/index.ts index 2a13bc669..fad2099bc 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,3 +1,4 @@ +export * from "./keys.js"; export * from "./task_and_batch.js"; export * from "./token.js"; export * from "./types.js"; diff --git a/src/types/keys.ts b/src/types/keys.ts new file mode 100644 index 000000000..c8d2a5a79 --- /dev/null +++ b/src/types/keys.ts @@ -0,0 +1,87 @@ +type ALL = "*"; +type GET = "get"; +type CREATE = "create"; +type UPDATE = "update"; +type DELETE = "delete"; + +/** + * {@link https://www.meilisearch.com/docs/reference/api/keys#actions} + * + * @see `meilisearch_types::keys::Action` + */ +export type Action = + | ALL + | "search" + | `documents.${ALL | "add" | GET | DELETE}` + | `indexes.${ALL | CREATE | GET | UPDATE | DELETE | "swap"}` + | `tasks.${ALL | "cancel" | DELETE | GET}` + | `settings.${ALL | GET | UPDATE}` + | `stats.${GET}` + | `metrics.${GET}` + | `dumps.${CREATE}` + | `snapshots.${CREATE}` + | "version" + | `keys.${CREATE | GET | UPDATE | DELETE}` + | `experimental.${GET | UPDATE}` + | `network.${GET | UPDATE}`; + +/** + * {@link https://www.meilisearch.com/docs/reference/api/keys#body} + * + * @see `meilisearch_types::keys::CreateApiKey` + */ +export type CreateApiKey = { + description?: string | null; + name?: string | null; + uid?: string; + actions: Action[]; + indexes: string[]; + expiresAt: string | null; +}; + +/** + * {@link https://www.meilisearch.com/docs/reference/api/keys#key-object} + * + * @see `meilisearch::routes::api_key::KeyView` + */ +export type KeyView = { + name: string | null; + description: string | null; + key: string; + uid: string; + actions: Action[]; + expiresAt: string | null; + createdAt: string; + updatedAt: string; +}; + +/** + * {@link https://www.meilisearch.com/docs/reference/api/keys#query-parameters} + * + * @see `meilisearch::routes::api_key::ListApiKeys` + */ +export type ListApiKeys = { + offset?: number; + limit?: number; +}; + +/** @see `meilisearch::routes::PaginationView` */ +export type PaginationView = { + results: T[]; + offset: number; + limit: number; + total: number; +}; + +/** {@link https://www.meilisearch.com/docs/reference/api/keys#response} */ +export type KeyViewList = PaginationView; + +/** + * {@link https://www.meilisearch.com/docs/reference/api/keys#body-1} + * + * @see `meilisearch_types::keys::PatchApiKey` + */ +export type PatchApiKey = { + description?: string | null; + name?: string | null; +}; diff --git a/src/types/types.ts b/src/types/types.ts index 9b1640f14..a31042268 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -673,40 +673,6 @@ export type Stats = { }; }; -/* - ** Keys - */ - -export type Key = { - uid: string; - description: string; - name: string | null; - key: string; - actions: string[]; - indexes: string[]; - expiresAt: Date; - createdAt: Date; - updatedAt: Date; -}; - -export type KeyCreation = { - uid?: string; - name?: string; - description?: string; - actions: string[]; - indexes: string[]; - expiresAt: Date | null; -}; - -export type KeyUpdate = { - name?: string; - description?: string; -}; - -export type KeysQuery = ResourceQuery & {}; - -export type KeysResults = ResourceResults & {}; - /* ** version */ diff --git a/tests/keys.test.ts b/tests/keys.test.ts index a1e305d07..eea712b59 100644 --- a/tests/keys.test.ts +++ b/tests/keys.test.ts @@ -54,9 +54,9 @@ describe.each([{ permission: "Master" }, { permission: "Admin" }])( expect(searchKey).toHaveProperty("indexes"); expect(searchKey).toHaveProperty("expiresAt", null); expect(searchKey).toHaveProperty("createdAt"); - expect(searchKey?.createdAt).toBeInstanceOf(Date); + expect(searchKey?.createdAt).toBeTypeOf("string"); expect(searchKey).toHaveProperty("updatedAt"); - expect(searchKey?.updatedAt).toBeInstanceOf(Date); + expect(searchKey?.updatedAt).toBeTypeOf("string"); const adminKey = keys.results.find( (key) => key.name === "Default Admin API Key", @@ -72,9 +72,9 @@ describe.each([{ permission: "Master" }, { permission: "Admin" }])( expect(adminKey).toHaveProperty("indexes"); expect(adminKey).toHaveProperty("expiresAt", null); expect(adminKey).toHaveProperty("createdAt"); - expect(searchKey?.createdAt).toBeInstanceOf(Date); + expect(searchKey?.createdAt).toBeTypeOf("string"); expect(adminKey).toHaveProperty("updatedAt"); - expect(searchKey?.updatedAt).toBeInstanceOf(Date); + expect(searchKey?.updatedAt).toBeTypeOf("string"); }); test(`${permission} key: get keys with pagination`, async () => { @@ -156,7 +156,7 @@ describe.each([{ permission: "Master" }, { permission: "Admin" }])( description: "Indexing Products API key", actions: ["documents.add"], indexes: ["products"], - expiresAt: new Date("2050-11-13T00:00:00Z"), // Test will fail in 2050 + expiresAt: new Date("2050-11-13T00:00:00Z").toISOString(), // Test will fail in 2050 }); expect(key).toBeDefined(); @@ -171,7 +171,7 @@ describe.each([{ permission: "Master" }, { permission: "Admin" }])( description: "Indexing Products API key", actions: ["documents.add"], indexes: ["products"], - expiresAt: new Date("2050-11-13T00:00:00Z"), // Test will fail in 2050 + expiresAt: new Date("2050-11-13T00:00:00Z").toISOString(), // Test will fail in 2050 }); const updatedKey = await client.updateKey(key.key, { @@ -194,7 +194,7 @@ describe.each([{ permission: "Master" }, { permission: "Admin" }])( description: "Indexing Products API key", actions: ["documents.add"], indexes: ["products"], - expiresAt: new Date("2050-11-13T00:00:00Z"), // Test will fail in 2050 + expiresAt: new Date("2050-11-13T00:00:00Z").toISOString(), // Test will fail in 2050 }); const deletedKey = await client.deleteKey(key.key); From 391e4fd3bd30b6d36380d34eb31a6a9601690ad0 Mon Sep 17 00:00:00 2001 From: "F. Levi" <55688616+flevi29@users.noreply.github.com> Date: Sun, 6 Apr 2025 11:07:20 +0300 Subject: [PATCH 2/3] Fix test --- src/types/indexes.ts | 23 +++++++++++++++++++++++ tests/keys.test.ts | 4 ++-- 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 src/types/indexes.ts diff --git a/src/types/indexes.ts b/src/types/indexes.ts new file mode 100644 index 000000000..352c0424b --- /dev/null +++ b/src/types/indexes.ts @@ -0,0 +1,23 @@ +/** @see `meilisearch::routes::indexes::ListIndexes` */ +export type ListIndexes = { + offset?: number; + limit?: number; +}; + +/** @see `meilisearch::routes::PaginationView` */ +type PaginationView = { + results: T[]; + offset: number; + limit: number; + total: number; +}; + +/** @see `meilisearch::routes::indexes::IndexView` */ +export type IndexView = { + uid: string; + createdAt: string; + updatedAt: string; + primaryKey: string | null; +}; + +export type IndexViewList = PaginationView; diff --git a/tests/keys.test.ts b/tests/keys.test.ts index eea712b59..f2220ae89 100644 --- a/tests/keys.test.ts +++ b/tests/keys.test.ts @@ -72,9 +72,9 @@ describe.each([{ permission: "Master" }, { permission: "Admin" }])( expect(adminKey).toHaveProperty("indexes"); expect(adminKey).toHaveProperty("expiresAt", null); expect(adminKey).toHaveProperty("createdAt"); - expect(searchKey?.createdAt).toBeTypeOf("string"); + expect(adminKey?.createdAt).toBeTypeOf("string"); expect(adminKey).toHaveProperty("updatedAt"); - expect(searchKey?.updatedAt).toBeTypeOf("string"); + expect(adminKey?.updatedAt).toBeTypeOf("string"); }); test(`${permission} key: get keys with pagination`, async () => { From 8cc208f92008f57d676a92ed5ff758f11c94fc4d Mon Sep 17 00:00:00 2001 From: "F. Levi" <55688616+flevi29@users.noreply.github.com> Date: Sun, 6 Apr 2025 11:24:04 +0300 Subject: [PATCH 3/3] Remove accidentally added file --- src/types/indexes.ts | 23 ----------------------- 1 file changed, 23 deletions(-) delete mode 100644 src/types/indexes.ts diff --git a/src/types/indexes.ts b/src/types/indexes.ts deleted file mode 100644 index 352c0424b..000000000 --- a/src/types/indexes.ts +++ /dev/null @@ -1,23 +0,0 @@ -/** @see `meilisearch::routes::indexes::ListIndexes` */ -export type ListIndexes = { - offset?: number; - limit?: number; -}; - -/** @see `meilisearch::routes::PaginationView` */ -type PaginationView = { - results: T[]; - offset: number; - limit: number; - total: number; -}; - -/** @see `meilisearch::routes::indexes::IndexView` */ -export type IndexView = { - uid: string; - createdAt: string; - updatedAt: string; - primaryKey: string | null; -}; - -export type IndexViewList = PaginationView;