Skip to content

Commit

Permalink
Merge #1488
Browse files Browse the repository at this point in the history
1488: Add conditional loading for cross-fetch/polyfill - favour native fetch r=bidoubiwa a=icyJoseph

# Pull Request

## Related issue
Fixes #1487 

## What does this PR do?

Load `cross-fetch/polyfill` only if `fetch` is `undefined`.

## 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)? #1487 was opened by me though...
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?



Co-authored-by: Joseph Chamochumbi <[email protected]>
  • Loading branch information
meili-bors[bot] and icyJoseph authored May 22, 2023
2 parents 7950449 + f457cd8 commit b319cc6
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"test:ci": "yarn test --ci",
"test:env": "yarn build && yarn test:env:nodejs && yarn test:env:esm && yarn test:env:node-ts",
"test:env:node-ts": "yarn --cwd tests/env/typescript-node start",
"test:env:nodejs": "node tests/env/node/index.js",
"test:env:nodejs": "yarn build && node tests/env/node/index.js && node tests/env/node/getting_started.js",
"test:env:esm": "yarn --cwd tests/env/esm && yarn --cwd tests/env/esm start",
"size": "node scripts/file-size ./dist/bundles/meilisearch.esm.min.js ./dist/bundles/meilisearch.umd.min.js",
"style": "yarn lint",
Expand Down
1 change: 0 additions & 1 deletion src/errors/meilisearch-communication-error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'cross-fetch/polyfill'
import { FetchError } from '../types'

class MeiliSearchCommunicationError extends Error {
Expand Down
6 changes: 4 additions & 2 deletions src/http-requests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'cross-fetch/polyfill'

import { Config, EnqueuedTaskObject } from './types'
import { PACKAGE_VERSION } from './package-version'

Expand Down Expand Up @@ -120,6 +118,10 @@ class HttpRequests {
body?: any
config?: Record<string, any>
}) {
if (typeof fetch === 'undefined') {
require('cross-fetch/dist/node-polyfill')
}

const constructURL = new URL(url, this.url)
if (params) {
const queryParams = new URLSearchParams()
Expand Down
3 changes: 2 additions & 1 deletion tests/env/node/getting_started.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const { MeiliSearch } = require('../../../dist/bundles/meilisearch.umd.js')
// If the index 'movies' does not exist, MeiliSearch creates it when you first add the documents.
await index.updateFilterableAttributes([
'director',
'genres'
'genres',
'id'
])

let response = await index.addDocuments(dataset)
Expand Down

0 comments on commit b319cc6

Please sign in to comment.