Skip to content

Refactor key types #1917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 24 additions & 55 deletions src/meilisearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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";
Expand Down Expand Up @@ -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<KeysResults> {
const keys = await this.httpRequest.get<KeysResults>({
/** {@link https://www.meilisearch.com/docs/reference/api/keys#get-all-keys} */
async getKeys(listApiKeys?: ListApiKeys): Promise<KeyViewList> {
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<Key> {
return await this.httpRequest.get<Key>({
/** {@link https://www.meilisearch.com/docs/reference/api/keys#get-one-key} */
async getKey(keyOrUid: string): Promise<KeyView> {
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<Key> {
return await this.httpRequest.post<Key>({
/** {@link https://www.meilisearch.com/docs/reference/api/keys#create-a-key} */
async createKey(createApiKey: CreateApiKey): Promise<KeyView> {
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<Key> {
return await this.httpRequest.patch<Key>({
/** {@link https://www.meilisearch.com/docs/reference/api/keys#update-a-key} */
async updateKey(
keyOrUid: string,
patchApiKey: PatchApiKey,
): Promise<KeyView> {
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<void> {
await this.httpRequest.delete({ path: `keys/${keyOrUid}` });
}
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./keys.js";
export * from "./task_and_batch.js";
export * from "./token.js";
export * from "./types.js";
87 changes: 87 additions & 0 deletions src/types/keys.ts
Original file line number Diff line number Diff line change
@@ -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<T> = {
results: T[];
offset: number;
limit: number;
total: number;
};

/** {@link https://www.meilisearch.com/docs/reference/api/keys#response} */
export type KeyViewList = PaginationView<KeyView>;

/**
* {@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;
};
34 changes: 0 additions & 34 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,40 +697,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<Key[]> & {};

/*
** version
*/
Expand Down
14 changes: 7 additions & 7 deletions tests/keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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(adminKey?.createdAt).toBeTypeOf("string");
expect(adminKey).toHaveProperty("updatedAt");
expect(searchKey?.updatedAt).toBeInstanceOf(Date);
expect(adminKey?.updatedAt).toBeTypeOf("string");
});

test(`${permission} key: get keys with pagination`, async () => {
Expand Down Expand Up @@ -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();
Expand All @@ -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, {
Expand All @@ -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);
Expand Down