Skip to content

Commit 792ce67

Browse files
Merge #1839
1839: Improve playgrounds r=Strift a=flevi29 # Pull Request ## What does this PR do? - refactor playgrounds, so that it uses the base `package.json` Vite dependency, modernize it according to https://vite.new/vanilla ## 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: F. Levi <[email protected]>
2 parents 7b6698f + 4b36fec commit 792ce67

File tree

14 files changed

+106
-489
lines changed

14 files changed

+106
-489
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,4 @@ package
132132
.idea
133133
dist_default_export_in_index
134134
no_default_export_in_index
135+
playgrounds/javascript/yarn.lock

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ const client = new MeiliSearch({
132132

133133
## 🚀 Getting started
134134

135+
Take a look at the [playground](./playgrounds/javascript/src/meilisearch.ts) for a concrete example.
136+
135137
### Add documents <!-- omit in toc -->
136138

137139
```js

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"url": "https://github.com/meilisearch/meilisearch-js"
4040
},
4141
"scripts": {
42-
"playground:javascript": "yarn --cwd ./playgrounds/javascript && yarn --cwd ./playgrounds/javascript dev",
42+
"playground:javascript": "vite serve playgrounds/javascript --open",
4343
"build:docs": "typedoc",
4444
"build": "vite build && tsc -p tsconfig.build.json && vite --mode production-umd build",
4545
"postbuild": "node scripts/build.js",

playgrounds/javascript/.gitignore

Lines changed: 0 additions & 24 deletions
This file was deleted.

playgrounds/javascript/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Meilisearch JavaScript playground
22

3-
This is an example vanilla JavaScript project using [Vite](https://vite.dev) and the [Yarn](https://classic.yarnpkg.com/en/docs/install) package manager.
3+
This is an example vanilla JavaScript project using [Vite](https://vite.dev) and
4+
the [Yarn](https://classic.yarnpkg.com/en/docs/install) package manager.
45

56
## Development
67

playgrounds/javascript/index.html

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
11
<!doctype html>
22
<html lang="en">
33
<head>
4-
<meta charset="utf-8" />
5-
<meta
6-
name="viewport"
7-
content="width=device-width, initial-scale=1, shrink-to-fit=no"
8-
/>
9-
<meta name="theme-color" content="#000000" />
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
106
<title>Meilisearch + Vite</title>
117
</head>
128
<body>
13-
<h1>Meilisearch + Vite</h1>
14-
<h2 class="errors_title">Errors:</h2>
15-
<div class="errors"></div>
16-
17-
<h2>Movies index:</h2>
18-
<div class="indexes"></div>
19-
<h2>Search response:</h2>
20-
<div class="hits"></div>
21-
<script src="https://cdn.jsdelivr.net/npm/instantsearch.js@4"></script>
22-
<script type="module" src="/src/app.js"></script>
9+
<div id="app"></div>
10+
<script type="module" src="/src/main.ts"></script>
2311
</body>
2412
</html>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "meilisearch-js-playground",
3-
"version": "0.2.0",
4-
"description": "Meilisearch playground written with Vite",
3+
"private": true,
4+
"version": "0.0.0",
55
"type": "module",
66
"scripts": {
77
"dev": "vite",
88
"build": "vite build",
99
"preview": "vite preview"
1010
},
1111
"devDependencies": {
12-
"vite": "^6.0.9"
12+
"vite": "^6.0.11"
1313
}
1414
}

playgrounds/javascript/src/app.css

Lines changed: 0 additions & 51 deletions
This file was deleted.

playgrounds/javascript/src/app.js

Lines changed: 0 additions & 60 deletions
This file was deleted.

playgrounds/javascript/src/main.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import "./style.css";
2+
import { addDocuments, getAllHits, getSearchResponse } from "./meilisearch.js";
3+
4+
document.querySelector<HTMLDivElement>("#app")!.innerHTML = `
5+
<div>
6+
<h1>Meilisearch + Vite</h1>
7+
8+
<h2>Documents:</h2>
9+
<div id="hits" style="white-space: break-spaces"> - </div>
10+
11+
<h2>Search response:</h2>
12+
<div id="response" style="white-space: break-spaces"> - </div>
13+
14+
<h2>Errors:</h2>
15+
<div id="errors">None</div>
16+
</div>
17+
`;
18+
19+
function getErrorMessage(error: unknown): string {
20+
if (!(error instanceof Error)) {
21+
return JSON.stringify(error);
22+
}
23+
24+
const message = String(error);
25+
26+
if (error.cause === undefined) {
27+
return message;
28+
}
29+
30+
return `${message}\nCaused by ${getErrorMessage(error.cause)}`;
31+
}
32+
33+
try {
34+
await addDocuments();
35+
await getAllHits(document.querySelector<HTMLDivElement>("#hits")!);
36+
await getSearchResponse(document.querySelector<HTMLDivElement>("#response")!);
37+
} catch (error) {
38+
console.error(error);
39+
document.querySelector<HTMLDivElement>("#errors")!.innerText =
40+
getErrorMessage(error);
41+
}

0 commit comments

Comments
 (0)