Skip to content

Commit 2d2c7f2

Browse files
Merge #1503
1503: Feat/native fetch with nitro support r=bidoubiwa a=icyJoseph # Pull Request This PR brings back the conditional fetch loading. And it also makes sure that the SDK still supports nitro-apps. ## Related issue Fixes #1487 ## What does this PR do? - Conditionally load cross-fetch, only when global fetch is not defined ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: Joseph Chamochumbi <[email protected]> Co-authored-by: Joseph <[email protected]>
2 parents ed73bdf + 0e43e97 commit 2d2c7f2

File tree

8 files changed

+4008
-3
lines changed

8 files changed

+4008
-3
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"test:env:node-ts": "yarn --cwd tests/env/typescript-node start",
4747
"test:env:nodejs": "yarn build && node tests/env/node/index.js && node tests/env/node/getting_started.js",
4848
"test:env:esm": "yarn --cwd tests/env/esm && yarn --cwd tests/env/esm start",
49+
"test:env:nitro-app": "yarn build && yarn --cwd tests/env/nitro-app test",
4950
"size": "node scripts/file-size ./dist/bundles/meilisearch.esm.min.js ./dist/bundles/meilisearch.umd.min.js",
5051
"style": "yarn lint",
5152
"style:fix": "yarn lint:fix",

src/errors/meilisearch-communication-error.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import 'cross-fetch/polyfill'
21
import { FetchError } from '../types'
32

43
class MeiliSearchCommunicationError extends Error {

src/http-requests.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'cross-fetch/polyfill'
2-
31
import { Config, EnqueuedTaskObject } from './types'
42
import { PACKAGE_VERSION } from './package-version'
53

@@ -120,6 +118,10 @@ class HttpRequests {
120118
body?: any
121119
config?: Record<string, any>
122120
}) {
121+
if (typeof fetch === 'undefined') {
122+
require('cross-fetch/polyfill')
123+
}
124+
123125
const constructURL = new URL(url, this.url)
124126
if (params) {
125127
const queryParams = new URLSearchParams()

tests/env/nitro-app/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
*.log*
3+
.nitro
4+
.cache
5+
.output

tests/env/nitro-app/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"private": true,
3+
"scripts": {
4+
"test": "yarn build && jest",
5+
"dev": "npx nitropack dev",
6+
"build": "npx nitropack build",
7+
"preview": "node .output/server/index.mjs"
8+
},
9+
"dependencies": {
10+
"meilisearch": "file:../../../",
11+
"nitropack": "latest"
12+
},
13+
"devDependencies": {
14+
"jest": "29.5.0"
15+
}
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { MeiliSearch } from 'meilisearch'
2+
3+
const meilisearch = new MeiliSearch({
4+
host: 'http://localhost:7700',
5+
apiKey: 'masterKey',
6+
})
7+
8+
export default defineEventHandler(async () => {
9+
try {
10+
const health = await meilisearch.isHealthy()
11+
12+
return { health }
13+
} catch (error) {
14+
throw createError({
15+
statusCode: 500,
16+
statusMessage: 'Unexpected Error',
17+
})
18+
}
19+
})
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const { spawn } = require('child_process')
2+
3+
let server
4+
5+
beforeAll(() => {
6+
server = spawn('node', ['.output/server/index.mjs'])
7+
})
8+
9+
afterAll(() => {
10+
server.kill()
11+
})
12+
13+
describe('Meilisearch JS w/ Nitro App Server Browser test', () => {
14+
it('Should have created an index and displayed it', async () => {
15+
await new Promise((next) => {
16+
server.stdout.on('data', () => {
17+
next()
18+
server.stdout.removeAllListeners('data')
19+
})
20+
})
21+
const response = await fetch('http://[::]:3000')
22+
const data = await response.json()
23+
24+
expect(data.health).toBe(true)
25+
})
26+
})

tests/env/nitro-app/yarn.lock

Lines changed: 3937 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)