Skip to content

Commit

Permalink
Merge branch 'main' of github.com:meilisearch/meilisearch-js into bum…
Browse files Browse the repository at this point in the history
…p-meilisearch-v0.26.0
  • Loading branch information
bidoubiwa committed Mar 10, 2022
2 parents 639fc0a + efac18e commit b693195
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 210 deletions.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@
]
},
"dependencies": {
"cross-fetch": "^3.1.4"
"cross-fetch": "^3.1.5"
},
"devDependencies": {
"@babel/preset-env": "^7.16.7",
"@rollup/plugin-babel": "^5.3.0",
"@rollup/plugin-commonjs": "21.0.1",
"@babel/preset-env": "^7.16.11",
"@rollup/plugin-babel": "^5.3.1",
"@rollup/plugin-commonjs": "21.0.2",
"@rollup/plugin-json": "^4.0.2",
"@rollup/plugin-node-resolve": "13.1.3",
"@types/jest": "^27.4.0",
"@types/jest": "^27.4.1",
"@types/prettier": "^2.2.3",
"@typescript-eslint/eslint-plugin": "2.34.0",
"@typescript-eslint/parser": "2.34.0",
Expand All @@ -99,11 +99,11 @@
"nodemon": "^2.0.15",
"prettier": "^2.2.1",
"pretty-bytes": "^5.6.0",
"rollup": "^2.67.0",
"rollup": "^2.70.0",
"rollup-plugin-terser": "^7.0.0",
"rollup-plugin-typescript2": "^0.31.2",
"shx": "^0.3.2",
"ts-jest": "^26.5.6",
"typescript": "4.5.4"
"typescript": "4.6.2"
}
}
30 changes: 22 additions & 8 deletions src/lib/http-requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,23 @@ import 'cross-fetch/polyfill'

import { Config, EnqueuedTask } from '../types'

import { httpResponseErrorHandler, httpErrorHandler } from '../errors'
import {
MeiliSearchError,
httpResponseErrorHandler,
httpErrorHandler,
} from '../errors'

import { addTrailingSlash, addProtocolIfNotPresent } from './utils'

function constructHostURL(host: string): string {
try {
host = addProtocolIfNotPresent(host)
host = addTrailingSlash(host)
return host
} catch (e) {
throw new MeiliSearchError('The provided host is not valid.')
}
}

class HttpRequests {
headers: Record<string, any>
Expand All @@ -14,14 +30,12 @@ class HttpRequests {
if (config.apiKey) {
this.headers['Authorization'] = `Bearer ${config.apiKey}`
}
this.url = new URL(config.host)
}

static addTrailingSlash(url: string): string {
if (!url.endsWith('/')) {
url += '/'
try {
const host = constructHostURL(config.host)
this.url = new URL(host)
} catch (e) {
throw new MeiliSearchError('The provided host is not valid.')
}
return url
}

async request({
Expand Down
3 changes: 0 additions & 3 deletions src/lib/meilisearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
Result,
} from '../types'
import { HttpRequests } from './http-requests'
import { addProtocolIfNotPresent } from './utils'
import { TaskClient } from './task'

class MeiliSearch {
Expand All @@ -37,8 +36,6 @@ class MeiliSearch {
* @param {Config} config Configuration object
*/
constructor(config: Config) {
config.host = addProtocolIfNotPresent(config.host)
config.host = HttpRequests.addTrailingSlash(config.host)
this.config = config
this.httpRequest = new HttpRequests(config)
this.tasks = new TaskClient(config)
Expand Down
14 changes: 13 additions & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,16 @@ function addProtocolIfNotPresent(host: string): string {
return host
}

export { sleep, removeUndefinedFromObject, addProtocolIfNotPresent }
function addTrailingSlash(url: string): string {
if (!url.endsWith('/')) {
url += '/'
}
return url
}

export {
sleep,
removeUndefinedFromObject,
addProtocolIfNotPresent,
addTrailingSlash,
}
30 changes: 6 additions & 24 deletions tests/client_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
getClient,
config,
MeiliSearch,
MASTER_KEY,
BAD_HOST,
} from './meilisearch-test-utils'

Expand All @@ -28,29 +27,6 @@ afterAll(() => {
return clearAllIndexes(config)
})

describe.skip('Tests on client methods w/ master key', () => {
beforeEach(() => {
return clearAllIndexes(config)
})

test(`Master key: Should get keys`, async () => {
const client = new MeiliSearch({
...config,
apiKey: MASTER_KEY,
})
const keys = await client.getKeys()

expect(keys).toHaveProperty(
'private',
'8dcbb482663333d0280fa9fedf0e0c16d52185cb67db494ce4cd34da32ce2092'
)
expect(keys).toHaveProperty(
'public',
'3b3bf839485f90453acc6159ba18fbed673ca88523093def11a9b4f4320e44a5'
)
})
})

describe.each([
{ permission: 'Master' },
{ permission: 'Private' },
Expand Down Expand Up @@ -168,6 +144,12 @@ describe.each([
const health = await client.isHealthy()
expect(health).toBe(false)
})

test(`${permission} key: Empty string host should throw an error`, () => {
expect(() => {
new MeiliSearch({ host: '' })
}).toThrow('The provided host is not valid.')
})
})

describe.each([{ permission: 'Master' }, { permission: 'Private' }])(
Expand Down
1 change: 0 additions & 1 deletion tests/unit_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ test(`Client handles host URL with domain and path and no trailing slash`, () =>
const client = new MeiliSearch({
host: customHost,
})
expect(client.config.host).toBe(customHost + '/')
expect(client.httpRequest.url.href).toBe(customHost + '/')
})
Loading

0 comments on commit b693195

Please sign in to comment.