From 9bfc63bff95ba64a84d40c25343d217f38c6d78e Mon Sep 17 00:00:00 2001 From: seven Date: Sat, 29 Jul 2023 02:55:09 +0800 Subject: [PATCH] fix create index request method --- package-lock.json | 4 ++-- package.json | 2 +- src/engineClient.ts | 14 +++++++++----- tests/engine.test.ts | 4 ++-- tests/utils/common.ts | 2 +- 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7899e62..952006f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@geek-fun/jest-search", - "version": "1.0.4", + "version": "1.0.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@geek-fun/jest-search", - "version": "1.0.4", + "version": "1.0.5", "license": "MIT", "dependencies": { "debug": "^4.3.4", diff --git a/package.json b/package.json index 963e5a1..fdaee01 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@geek-fun/jest-search", - "version": "1.0.4", + "version": "1.0.5", "main": "dist/src/index.js", "types": "dist/src/index.d.ts", "description": "Jest preset for running tests with local search platform", diff --git a/src/engineClient.ts b/src/engineClient.ts index 711a645..5dd38e9 100644 --- a/src/engineClient.ts +++ b/src/engineClient.ts @@ -23,9 +23,9 @@ export const createClient = ( return { status: res.status, data }; }; - const post = async (path: string, body?: unknown): Promise<{ status: number; data: T }> => { + const put = async (path: string, body?: unknown): Promise<{ status: number; data: T }> => { const res = await fetch(`${host}:${port}${path}`, { - method: 'POST', + method: 'PUT', headers, body: JSON.stringify(body), }); @@ -48,12 +48,14 @@ export const createClient = ( }; const createIndex = async ({ name, body, mappings }: IndexBody) => { debug(`creating index: ${name}`); - const { status, data } = await post( + const { status, data } = await put( engine === EngineType.ZINCSEARCH ? '/api/index' : `/${name}`, engine === EngineType.ZINCSEARCH ? { name, mappings } : body ); if (status !== 200) { - throw new Error(`failed to create index: ${name}, status: ${status}, data: ${data}`); + throw new Error( + `failed to create index: ${name}, status: ${status}, data: ${JSON.stringify(data)}` + ); } }; const deleteIndex = async ({ name }: IndexBody) => { @@ -62,7 +64,9 @@ export const createClient = ( engine === EngineType.ZINCSEARCH ? `/api/index/${name}` : `/${name}` ); if (status !== 200) { - throw new Error(`failed to delete index: ${name}, status: ${status}, response: ${data}`); + throw new Error( + `failed to delete index: ${name}, status: ${status}, response: ${JSON.stringify(data)}` + ); } }; return { heartbeat, createIndex, deleteIndex }; diff --git a/tests/engine.test.ts b/tests/engine.test.ts index 64951dd..d468d4b 100644 --- a/tests/engine.test.ts +++ b/tests/engine.test.ts @@ -47,10 +47,10 @@ describe('integration test for elasticsearch and opensearch', () => { expect(inspect).toMatchObject({ status: 200, name: nodeName, - cluster_name: clusterName, + clusterName, version, }); - expect(mapping).toEqual({ books: { status: 200, mappings: indexes[0].body.mappings } }); + expect(mapping).toEqual({ books: { mappings: indexes[0].body.mappings }, status: 200 }); }); }); }); diff --git a/tests/utils/common.ts b/tests/utils/common.ts index 80849e4..ac2210f 100644 --- a/tests/utils/common.ts +++ b/tests/utils/common.ts @@ -47,5 +47,5 @@ export const fetchMapping = async ( ); const data = await response.json(); - return { status: response.status, mapping: data }; + return { status: response.status, ...data }; };