diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index f79be49de..3a1ebb85b 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -700,6 +700,8 @@ authorization_header_1: |- const client = new MeiliSearch({ host: 'http://localhost:7700', apiKey: 'masterKey' }) client.getKeys() tenant_token_guide_generate_sdk_1: |- + import { generateTenantToken } from 'meilisearch/token' + const searchRules = { patient_medical_records: { filter: 'user_id = 1' @@ -709,7 +711,7 @@ tenant_token_guide_generate_sdk_1: |- const apiKeyUid = '85c3c2f9-bdd6-41f1-abd8-11fcf80e0f76' const expiresAt = new Date('2025-12-20') // optional - const token = await client.generateTenantToken(apiKeyUid, searchRules, { + const token = await generateTenantToken(apiKeyUid, searchRules, { apiKey: apiKey, expiresAt: expiresAt, }) diff --git a/package.json b/package.json index 5081885b0..38095a5f7 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,12 @@ "import": "./dist/bundles/meilisearch.mjs", "require": "./dist/bundles/meilisearch.cjs", "default": "./dist/bundles/meilisearch.umd.js" + }, + "./token": { + "types": "./dist/types/token.d.ts", + "import": "./dist/bundles/token.mjs", + "require": "./dist/bundles/token.cjs", + "default": "./dist/bundles/token.cjs" } }, "sideEffects": false, diff --git a/rollup.config.js b/rollup.config.js index 45577aa6b..88e3b4221 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -25,16 +25,22 @@ const PLUGINS = [ }), ]; +const INDEX_INPUT = "src/index.ts"; +const TOKEN_INPUT = "src/token.ts"; + +const INDEX_EXPORTS = pkg.exports["."]; +const TOKEN_EXPORTS = pkg.exports["./token"]; + module.exports = [ - // browser-friendly UMD build + // Index { - input: "src/browser.ts", // directory to transpilation of typescript + input: INDEX_INPUT, output: { name: "window", extend: true, file: getOutputFileName( // will add .min. in filename if in production env - resolve(ROOT, pkg.jsdelivr), + resolve(ROOT, INDEX_EXPORTS.browser), env === "production", ), format: "umd", @@ -67,36 +73,49 @@ module.exports = [ }), // nodePolyfills json(), - env === "production" ? terser() : {}, // will minify the file in production mode + env === "production" ? terser() : {}, ], }, + { + input: INDEX_INPUT, + output: [ + { + file: INDEX_EXPORTS.import, + exports: "named", + format: "es", + sourcemap: env === "production", + }, + ], + plugins: PLUGINS, + }, + { + input: INDEX_INPUT, + output: { + file: INDEX_EXPORTS.require, + exports: "named", + format: "cjs", + sourcemap: env === "production", + }, + plugins: PLUGINS, + }, - // ES module (for bundlers) build. + // Token { - input: "src/index.ts", + input: TOKEN_INPUT, output: [ { - file: getOutputFileName( - resolve(ROOT, pkg.module), - env === "production", - ), + file: TOKEN_EXPORTS.import, exports: "named", format: "es", - sourcemap: env === "production", // create sourcemap for error reporting in production mode + sourcemap: env === "production", }, ], plugins: PLUGINS, }, - // Common JS build (Node). - // Compatible only in a nodeJS environment. { - input: "src/index.ts", + input: TOKEN_INPUT, output: { - file: getOutputFileName( - // will add .min. in filename if in production env - resolve(ROOT, pkg.main), - env === "production", - ), + file: TOKEN_EXPORTS.require, exports: "named", format: "cjs", sourcemap: env === "production", // create sourcemap for error reporting in production mode diff --git a/src/browser.ts b/src/browser.ts deleted file mode 100644 index 601aae1e6..000000000 --- a/src/browser.ts +++ /dev/null @@ -1,7 +0,0 @@ -export * from "./types"; -export * from "./errors"; -export * from "./indexes"; -import { MeiliSearch } from "./clients/browser-client"; - -export { MeiliSearch, MeiliSearch as Meilisearch }; -export default MeiliSearch; diff --git a/src/clients/browser-client.ts b/src/clients/browser-client.ts deleted file mode 100644 index 738323430..000000000 --- a/src/clients/browser-client.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { Config } from "../types"; -import { Client } from "./client"; - -class MeiliSearch extends Client { - constructor(config: Config) { - super(config); - } -} - -export { MeiliSearch }; diff --git a/src/clients/node-client.ts b/src/clients/node-client.ts deleted file mode 100644 index 168549422..000000000 --- a/src/clients/node-client.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { Client } from "./client"; -import { Config, TokenSearchRules, TokenOptions } from "../types"; -import { Token } from "../token"; - -class MeiliSearch extends Client { - tokens: Token; - - constructor(config: Config) { - super(config); - this.tokens = new Token(config); - } - - /** - * Generate a tenant token - * - * @param apiKeyUid - The uid of the api key used as issuer of the token. - * @param searchRules - Search rules that are applied to every search. - * @param options - Token options to customize some aspect of the token. - * @returns The token in JWT format. - */ - async generateTenantToken( - apiKeyUid: string, - searchRules: TokenSearchRules, - options?: TokenOptions, - ): Promise { - if (typeof window === "undefined") { - return await this.tokens.generateTenantToken( - apiKeyUid, - searchRules, - options, - ); - } - return await super.generateTenantToken(apiKeyUid, searchRules, options); - } -} -export { MeiliSearch }; diff --git a/src/index.ts b/src/index.ts index e2671c0db..101f8f14d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,7 @@ export * from "./errors"; export * from "./indexes"; export * from "./enqueued-task"; export * from "./task"; -import { MeiliSearch } from "./clients/node-client"; +import { MeiliSearch } from "./meilisearch"; export { MeiliSearch, MeiliSearch as Meilisearch }; export default MeiliSearch; diff --git a/src/clients/client.ts b/src/meilisearch.ts similarity index 92% rename from src/clients/client.ts rename to src/meilisearch.ts index 0cef642a9..f28ee00e7 100644 --- a/src/clients/client.ts +++ b/src/meilisearch.ts @@ -5,7 +5,7 @@ * Copyright: 2019, MeiliSearch */ -import { Index } from "../indexes"; +import { Index } from "./indexes"; import { KeyCreation, Config, @@ -16,8 +16,6 @@ import { Stats, Version, ErrorStatusCode, - TokenSearchRules, - TokenOptions, TasksQuery, WaitOptions, KeyUpdate, @@ -34,12 +32,12 @@ import { MultiSearchResponse, SearchResponse, FederatedMultiSearchParams, -} from "../types"; -import { HttpRequests } from "../http-requests"; -import { TaskClient, Task } from "../task"; -import { EnqueuedTask } from "../enqueued-task"; +} from "./types"; +import { HttpRequests } from "./http-requests"; +import { TaskClient, Task } from "./task"; +import { EnqueuedTask } from "./enqueued-task"; -class Client { +export class MeiliSearch { config: Config; httpRequest: HttpRequests; tasks: TaskClient; @@ -470,29 +468,4 @@ class Client { return new EnqueuedTask(task); } - - /// - /// TOKENS - /// - - /** - * Generate a tenant token - * - * @param apiKeyUid - The uid of the api key used as issuer of the token. - * @param searchRules - Search rules that are applied to every search. - * @param options - Token options to customize some aspect of the token. - * @returns The token in JWT format. - */ - generateTenantToken( - _apiKeyUid: string, - _searchRules: TokenSearchRules, - _options?: TokenOptions, - ): Promise { - const error = new Error(); - error.message = `Meilisearch: failed to generate a tenant token. Generation of a token only works in a node environment \n ${error.stack}.`; - - return Promise.reject(error); - } } - -export { Client }; diff --git a/src/token.ts b/src/token.ts index b8c421798..4ed29e8f6 100644 --- a/src/token.ts +++ b/src/token.ts @@ -1,4 +1,4 @@ -import { Config, TokenSearchRules, TokenOptions } from "./types"; +import { TokenSearchRules, TokenOptions } from "./types"; import { MeiliSearchError } from "./errors"; import { validateUuid4 } from "./utils"; @@ -51,14 +51,15 @@ function createHeader() { * @param uid - The uid of the api key used as issuer of the token. * @param expiresAt - Date at which the token expires. */ -function validateTokenParameters(tokenParams: { +function validateTokenParameters({ + searchRules, + apiKeyUid, + expiresAt, +}: { searchRules: TokenSearchRules; - uid: string; - apiKey: string; + apiKeyUid: string; expiresAt?: Date; }) { - const { searchRules, uid, apiKey, expiresAt } = tokenParams; - if (expiresAt) { if (!(expiresAt instanceof Date)) { throw new MeiliSearchError( @@ -79,19 +80,13 @@ function validateTokenParameters(tokenParams: { } } - if (!apiKey || typeof apiKey !== "string") { - throw new MeiliSearchError( - `Meilisearch: The API key used for the token generation must exist and be of type string.`, - ); - } - - if (!uid || typeof uid !== "string") { + if (!apiKeyUid || typeof apiKeyUid !== "string") { throw new MeiliSearchError( `Meilisearch: The uid of the api key used for the token generation must exist, be of type string and comply to the uuid4 format.`, ); } - if (!validateUuid4(uid)) { + if (!validateUuid4(apiKeyUid)) { throw new MeiliSearchError( `Meilisearch: The uid of your key is not a valid uuid4. To find out the uid of your key use getKey().`, ); @@ -106,53 +101,46 @@ function validateTokenParameters(tokenParams: { * @param expiresAt - Date at which the token expires. * @returns The payload encoded in base64. */ -function createPayload(payloadParams: { +function createPayload({ + searchRules, + apiKeyUid, + expiresAt, +}: { searchRules: TokenSearchRules; - uid: string; + apiKeyUid: string; expiresAt?: Date; }): string { - const { searchRules, uid, expiresAt } = payloadParams; - const payload = { searchRules, - apiKeyUid: uid, + apiKeyUid, exp: expiresAt ? Math.floor(expiresAt.getTime() / 1000) : undefined, }; return encode64(payload).replace(/=/g, ""); } -class Token { - config: Config; - - constructor(config: Config) { - this.config = config; - } - - /** - * Generate a tenant token - * - * @param apiKeyUid - The uid of the api key used as issuer of the token. - * @param searchRules - Search rules that are applied to every search. - * @param options - Token options to customize some aspect of the token. - * @returns The token in JWT format. - */ - async generateTenantToken( - apiKeyUid: string, - searchRules: TokenSearchRules, - options?: TokenOptions, - ): Promise { - const apiKey = options?.apiKey || this.config.apiKey || ""; - const uid = apiKeyUid || ""; - const expiresAt = options?.expiresAt; - - validateTokenParameters({ apiKey, uid, expiresAt, searchRules }); - - const encodedHeader = createHeader(); - const encodedPayload = createPayload({ searchRules, uid, expiresAt }); - const signature = await sign(apiKey, encodedHeader, encodedPayload); +/** + * Generate a tenant token + * + * @param apiKeyUid - The uid of the api key used as issuer of the token. + * @param searchRules - Search rules that are applied to every search. + * @param options - Token options to customize some aspect of the token. + * @returns The token in JWT format. + */ +export async function generateTenantToken( + apiKeyUid: string, + searchRules: TokenSearchRules, + { apiKey, expiresAt }: TokenOptions, +): Promise { + validateTokenParameters({ apiKeyUid, expiresAt, searchRules }); + + const encodedHeader = createHeader(); + const encodedPayload = createPayload({ + searchRules, + apiKeyUid, + expiresAt, + }); + const signature = await sign(apiKey, encodedHeader, encodedPayload); - return `${encodedHeader}.${encodedPayload}.${signature}`; - } + return `${encodedHeader}.${encodedPayload}.${signature}`; } -export { Token }; diff --git a/src/types/types.ts b/src/types/types.ts index 0713066e5..f2d0befb7 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -1110,6 +1110,6 @@ export type TokenSearchRules = | string[]; export type TokenOptions = { - apiKey?: string; + apiKey: string; expiresAt?: Date; }; diff --git a/tests/env/esm/src/index.js b/tests/env/esm/src/index.js index 96f6b2fb7..daf03c008 100644 --- a/tests/env/esm/src/index.js +++ b/tests/env/esm/src/index.js @@ -1,7 +1,8 @@ import { MeiliSearch } from '../../../../' import * as DefaultMeiliSearch from '../../../../' +import { generateTenantToken } from '../../../../dist/bundles/token.mjs' -const client = new MeiliSearch({ host:'http://localhost:7700', apiKey: 'masterKey'}) -const defaultClient = new DefaultMeiliSearch.MeiliSearch({ host:'http://localhost:7700', apiKey: 'masterKey'}) -const token = client.generateTenantToken('e489fe16-3381-431b-bee3-00430192915d', []) +const client = new MeiliSearch({ host:'http://localhost:7700', apiKey: 'masterKey' }) +const defaultClient = new DefaultMeiliSearch.MeiliSearch({ host:'http://localhost:7700', apiKey: 'masterKey' }) +const token = generateTenantToken('e489fe16-3381-431b-bee3-00430192915d', [], { apiKey: 'masterKey' }) console.log({ client, token, defaultClient }) diff --git a/tests/env/node/index.js b/tests/env/node/index.js index a945aa95d..b98be85f0 100644 --- a/tests/env/node/index.js +++ b/tests/env/node/index.js @@ -1,10 +1,10 @@ const { MeiliSearch } = require('../../../') const DefaultMeiliSearch = require('../../../') +const { generateTenantToken } = require('../../../dist/bundles/token.cjs') -const CJStest = new MeiliSearch({ host:'http://localhost:7700', apiKey: 'masterKey'}) -const DefaultCJSTest = new DefaultMeiliSearch.MeiliSearch({ host:'http://localhost:7700', apiKey: 'masterKey'}) +const CJStest = new MeiliSearch({ host:'http://localhost:7700', apiKey: 'masterKey' }) +const DefaultCJSTest = new DefaultMeiliSearch.MeiliSearch({ host:'http://localhost:7700', apiKey: 'masterKey' }) -DefaultCJSTest.generateTenantToken('e489fe16-3381-431b-bee3-00430192915d', []) // Resolved using the `main` field -CJStest.generateTenantToken('e489fe16-3381-431b-bee3-00430192915d', []) // Resolved using the `main` field +generateTenantToken('e489fe16-3381-431b-bee3-00430192915d', [], { apiKey: 'masterKey' }) console.log({ CJStest, DefaultCJSTest }) diff --git a/tests/env/typescript-browser/src/index.ts b/tests/env/typescript-browser/src/index.ts index 5c1472bf7..803be4c19 100644 --- a/tests/env/typescript-browser/src/index.ts +++ b/tests/env/typescript-browser/src/index.ts @@ -1,5 +1,6 @@ import { MeiliSearch } from '../../../../' import { IndexObject } from '../../../../src' +import { generateTenantToken } from '../../../../src/token' const config = { host: 'http://127.0.0.1:7700', @@ -21,5 +22,5 @@ function greeter(person: string) { user )} this is the list of all your indexes: \n ${uids.join(', ')}` - console.log(await client.generateTenantToken('e489fe16-3381-431b-bee3-00430192915d', [])) // Resolved using the `browser` field + console.log(await generateTenantToken('e489fe16-3381-431b-bee3-00430192915d', [], { apiKey: config.apiKey })) // Resolved using the `browser` field })() diff --git a/tests/env/typescript-node/package.json b/tests/env/typescript-node/package.json index 4d54c42ff..f2a7f80c5 100644 --- a/tests/env/typescript-node/package.json +++ b/tests/env/typescript-node/package.json @@ -5,7 +5,7 @@ "main": "src/index.js", "scripts": { "build": "tsc --project tsconfig.json", - "start": "tsc --project tsconfig.json && node dist/index.js" + "start": "tsc --project tsconfig.json && node dist/tests/env/typescript-node/src/index.js" }, "keywords": [ "demo", @@ -14,7 +14,8 @@ "author": "Charlotte Vermandel", "license": "ISC", "devDependencies": { - "typescript": "4.4.4" + "typescript": "^5.6.2", + "@types/node": "^20.16.10" }, "dependencies": {} } diff --git a/tests/env/typescript-node/src/index.ts b/tests/env/typescript-node/src/index.ts index c3698a389..0ff282d4c 100644 --- a/tests/env/typescript-node/src/index.ts +++ b/tests/env/typescript-node/src/index.ts @@ -1,14 +1,12 @@ - -// @ts-ignore import { - // @ts-ignore MeiliSearch, IndexObject, SearchResponse, Hits, Hit, SearchParams, -} from '../../../../' +} from '../../../../src' +import { generateTenantToken } from '../../../../src/token' const config = { host: 'http://127.0.0.1:7700', @@ -62,7 +60,7 @@ const indexUid = "movies" console.log(hit?._formatted?.title) }) - console.log(await client.generateTenantToken('e489fe16-3381-431b-bee3-00430192915d', [])) + console.log(await generateTenantToken('e489fe16-3381-431b-bee3-00430192915d', [], { apiKey: config.apiKey })) await index.delete() })() diff --git a/tests/env/typescript-node/tsconfig.json b/tests/env/typescript-node/tsconfig.json index 59caad00d..74d6d24d2 100644 --- a/tests/env/typescript-node/tsconfig.json +++ b/tests/env/typescript-node/tsconfig.json @@ -1,17 +1,11 @@ { "extends": "../../../tsconfig.json", "compilerOptions": { - "noEmit": false, - "baseUrl": "", - "declarationDir": "./dist/types", - "lib": [ - "ES2020", - "dom" // Added to get Request type error - ], - "module": "commonjs", - "moduleResolution": "node", - "outDir": "./dist", - "types": ["../../../dist/types"] + "noEmit": false, + "noEmitOnError": true, + "moduleResolution": "node", + "module": "commonjs", + "outDir": "./dist" }, "include": ["src/index.ts"] } diff --git a/tests/env/typescript-node/yarn.lock b/tests/env/typescript-node/yarn.lock index f4de2509b..7fd556d65 100644 --- a/tests/env/typescript-node/yarn.lock +++ b/tests/env/typescript-node/yarn.lock @@ -2,7 +2,19 @@ # yarn lockfile v1 -typescript@4.4.4: - version "4.4.4" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.4.4.tgz#2cd01a1a1f160704d3101fd5a58ff0f9fcb8030c" - integrity sha512-DqGhF5IKoBl8WNf8C1gu8q0xZSInh9j1kJJMqT3a94w1JzVaBU4EXOSMrz9yDqMT0xt3selp83fuFMQ0uzv6qA== +"@types/node@^20.16.10": + version "20.16.10" + resolved "https://registry.yarnpkg.com/@types/node/-/node-20.16.10.tgz#0cc3fdd3daf114a4776f54ba19726a01c907ef71" + integrity sha512-vQUKgWTjEIRFCvK6CyriPH3MZYiYlNy0fKiEYHWbcoWLEgs4opurGGKlebrTLqdSMIbXImH6XExNiIyNUv3WpA== + dependencies: + undici-types "~6.19.2" + +typescript@^5.6.2: + version "5.6.2" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.6.2.tgz#d1de67b6bef77c41823f822df8f0b3bcff60a5a0" + integrity sha512-NW8ByodCSNCwZeghjN3o+JX5OFH0Ojg6sadjEKY4huZ52TqbJTJnDo5+Tw98lSy63NZvi4n+ez5m2u5d4PkZyw== + +undici-types@~6.19.2: + version "6.19.8" + resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-6.19.8.tgz#35111c9d1437ab83a7cdc0abae2f26d88eda0a02" + integrity sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw== diff --git a/tests/keys.test.ts b/tests/keys.test.ts index 07547d1c0..f53c9296f 100644 --- a/tests/keys.test.ts +++ b/tests/keys.test.ts @@ -1,5 +1,5 @@ import { expect, test, describe, beforeEach, afterAll } from "vitest"; -import MeiliSearch from "../src/browser"; +import MeiliSearch from "../src"; import { ErrorStatusCode } from "../src/types"; import { clearAllIndexes, diff --git a/tests/token.test.ts b/tests/token.test.ts index 3dd10fc44..ef5c4a970 100644 --- a/tests/token.test.ts +++ b/tests/token.test.ts @@ -9,6 +9,7 @@ import { HOST, } from "./utils/meilisearch-test-utils"; import { createHmac } from "node:crypto"; +import { generateTenantToken } from "../src/token"; import MeiliSearch from "../src"; const HASH_ALGORITHM = "HS256"; @@ -44,7 +45,7 @@ describe.each([{ permission: "Admin" }])( const client = await getClient(permission); const apiKey = await getKey(permission); const { uid } = await client.getKey(apiKey); - const token = await client.generateTenantToken(uid, [], {}); + const token = await generateTenantToken(uid, [], { apiKey }); const [header64] = token.split("."); // header @@ -57,7 +58,7 @@ describe.each([{ permission: "Admin" }])( const client = await getClient(permission); const apiKey = await getKey(permission); const { uid } = await client.getKey(apiKey); - const token = await client.generateTenantToken(uid, [], {}); + const token = await generateTenantToken(uid, [], { apiKey }); const [header64, payload64, signature64] = token.split("."); // signature @@ -75,7 +76,7 @@ describe.each([{ permission: "Admin" }])( const client = await getClient(permission); const apiKey = await getKey(permission); const { uid } = await client.getKey(apiKey); - const token = await client.generateTenantToken(uid, [], {}); + const token = await generateTenantToken(uid, [], { apiKey }); const [_, payload64] = token.split("."); // payload @@ -90,7 +91,7 @@ describe.each([{ permission: "Admin" }])( const client = await getClient(permission); const apiKey = await getKey(permission); const { uid } = await client.getKey(apiKey); - const token = await client.generateTenantToken(uid, [UID]); + const token = await generateTenantToken(uid, [UID], { apiKey }); const [_, payload64] = token.split("."); // payload @@ -105,7 +106,7 @@ describe.each([{ permission: "Admin" }])( const client = await getClient(permission); const apiKey = await getKey(permission); const { uid } = await client.getKey(apiKey); - const token = await client.generateTenantToken(uid, { [UID]: {} }); + const token = await generateTenantToken(uid, { [UID]: {} }, { apiKey }); const [_, payload64] = token.split("."); // payload @@ -120,7 +121,7 @@ describe.each([{ permission: "Admin" }])( const apiKey = await getKey(permission); const { uid } = await client.getKey(apiKey); - const token = await client.generateTenantToken(uid, ["*"]); + const token = await generateTenantToken(uid, ["*"], { apiKey }); const searchClient = new MeiliSearch({ host: HOST, apiKey: token }); @@ -138,8 +139,7 @@ describe.each([{ permission: "Admin" }])( actions: ["search"], indexes: [UID], }); - const client = await getClient(permission); - const token = await client.generateTenantToken(uid, ["*"], { + const token = await generateTenantToken(uid, ["*"], { apiKey: key, }); @@ -154,7 +154,8 @@ describe.each([{ permission: "Admin" }])( const date = new Date("December 17, 4000 03:24:00"); const apiKey = await getKey(permission); const { uid } = await client.getKey(apiKey); - const token = await client.generateTenantToken(uid, ["*"], { + const token = await generateTenantToken(uid, ["*"], { + apiKey, expiresAt: date, }); @@ -176,7 +177,7 @@ describe.each([{ permission: "Admin" }])( const date = new Date("December 17, 2000 03:24:00"); await expect( - client.generateTenantToken(uid, ["*"], { expiresAt: date }), + generateTenantToken(uid, ["*"], { apiKey, expiresAt: date }), ).rejects.toThrow( `Meilisearch: The expiresAt field must be a date in the future.`, ); @@ -186,9 +187,7 @@ describe.each([{ permission: "Admin" }])( const client = await getClient(permission); const apiKey = await getKey(permission); const { uid } = await client.getKey(apiKey); - const token = await client.generateTenantToken(uid, { - [UID]: null, - }); + const token = await generateTenantToken(uid, { [UID]: null }, { apiKey }); const searchClient = new MeiliSearch({ host: HOST, apiKey: token }); @@ -208,9 +207,11 @@ describe.each([{ permission: "Admin" }])( const client = await getClient(permission); const apiKey = await getKey(permission); const { uid } = await client.getKey(apiKey); - const token = await client.generateTenantToken(uid, { - [UID]: { filter: "id = 2" }, - }); + const token = await generateTenantToken( + uid, + { [UID]: { filter: "id = 2" } }, + { apiKey }, + ); const searchClient = new MeiliSearch({ host: HOST, apiKey: token }); @@ -224,7 +225,7 @@ describe.each([{ permission: "Admin" }])( const client = await getClient(permission); const apiKey = await getKey(permission); const { uid } = await client.getKey(apiKey); - const token = await client.generateTenantToken(uid, []); + const token = await generateTenantToken(uid, [], { apiKey }); const searchClient = new MeiliSearch({ host: HOST, apiKey: token }); @@ -238,7 +239,7 @@ describe.each([{ permission: "Admin" }])( const client = await getClient(permission); const apiKey = await getKey(permission); const { uid } = await client.getKey(apiKey); - const token = await client.generateTenantToken(uid, { misc: null }); + const token = await generateTenantToken(uid, { misc: null }, { apiKey }); const searchClient = new MeiliSearch({ host: HOST, apiKey: token }); @@ -255,32 +256,10 @@ describe.each([{ permission: "Admin" }])( const date = new Date("December 17, 2000 03:24:00"); await expect( - client.generateTenantToken( - uid, - {}, - { - expiresAt: date, - }, - ), + generateTenantToken(uid, {}, { apiKey, expiresAt: date }), ).rejects.toThrow( `Meilisearch: The expiresAt field must be a date in the future.`, ); }); - - test(`${permission} key: Creates tenant token with wrong uid type throws an error`, async () => { - const client = await getClient(permission); - - await expect(client.generateTenantToken("1234", ["*"])).rejects.toThrow( - `Meilisearch: The uid of your key is not a valid uuid4. To find out the uid of your key use getKey().`, - ); - }); - - test(`${permission} key: Creates a tenant token with no api key in client and in parameters throws an error`, async () => { - const client = new MeiliSearch({ host: HOST }); - - await expect(client.generateTenantToken("123", [])).rejects.toThrow( - `Meilisearch: The API key used for the token generation must exist and be of type string.`, - ); - }); }, );