Skip to content

Commit 74cc571

Browse files
Merge #686
686: Add index.d.ts file for typescript compatible r=bidoubiwa a=hirohe # Pull Request ## Related issue Fixes #479 ## What does this PR do? - add `index.d.ts` types declare file - add dependencies related to typescript and eslint config - update .eslintrc.js file, with typescript related configs - replace `no-unused-vars` with ``@typescript-eslint/no-unused-vars`` [refer](https://typescript-eslint.io/rules/no-unused-vars/) there's some definitions unclear in `index.d.ts` - ~`queryHook` dont know the arguments definitons~ - ~`handleSelected` dont know the arguments definitons~ - all methods of class DocsSearchBar should be 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: hirohe <[email protected]> Co-authored-by: hirohe <[email protected]>
2 parents 206315d + 0f429b7 commit 74cc571

File tree

4 files changed

+283
-3
lines changed

4 files changed

+283
-3
lines changed

.eslintrc.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ module.exports = {
1010
plugins: ['prettier', 'cypress'],
1111
rules: {
1212
'no-console': 0,
13-
'no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
1413
'eslint-comments/disable-enable-pair': ['error', { allowWholeFile: true }],
1514
'prettier/prettier': [
1615
'error',
@@ -19,4 +18,34 @@ module.exports = {
1918
},
2019
],
2120
},
21+
overrides: [
22+
{
23+
files: ['**/*.ts'],
24+
extends: [
25+
'algolia',
26+
'algolia/jest',
27+
'plugin:prettier/recommended',
28+
'plugin:cypress/recommended',
29+
'plugin:@typescript-eslint/recommended',
30+
],
31+
plugins: ['prettier', 'cypress', '@typescript-eslint'],
32+
rules: {
33+
'no-console': 0,
34+
'@typescript-eslint/no-unused-vars': [
35+
'error',
36+
{ argsIgnorePattern: '^_' },
37+
],
38+
'eslint-comments/disable-enable-pair': [
39+
'error',
40+
{ allowWholeFile: true },
41+
],
42+
'prettier/prettier': [
43+
'error',
44+
{
45+
trailingComma: 'all',
46+
},
47+
],
48+
},
49+
},
50+
],
2251
}

index.d.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/* eslint camelcase: ["off"] */
2+
3+
import { SearchResponse, SearchParams, Hits, Config } from 'meilisearch'
4+
5+
interface HitResponse {
6+
anchor: string
7+
content: string
8+
hierarchy_lvl0: string | null
9+
hierarchy_lvl1: string | null
10+
hierarchy_lvl2: string | null
11+
hierarchy_lvl3: string | null
12+
hierarchy_lvl4: string | null
13+
hierarchy_lvl5: string | null
14+
hierarchy_lvl6: string | null
15+
objectID: string
16+
url: string
17+
[key: string]: unknown
18+
}
19+
20+
interface DefaultFormattedHit {
21+
isLvl0: boolean
22+
isLvl1: boolean
23+
isLbl2: boolean
24+
isLvl1EmptyOrDuplicate: boolean
25+
isCategoryHeader: boolean
26+
isSubCategoryHeader: boolean
27+
isTextOrSubcategoryNonEmpty: boolean
28+
subcategory: string | null
29+
text: string | null
30+
title: string | null
31+
url: string | null
32+
}
33+
34+
interface AutoCompleteInput {
35+
open: () => void
36+
close: () => void
37+
destroy: () => void
38+
getWrapper: () => Element
39+
getVal: () => string
40+
setVal: (val: string) => void
41+
}
42+
43+
interface Options<FormattedHit = DefaultFormattedHit> {
44+
hostUrl: string
45+
apiKey: string
46+
indexUid: string
47+
inputSelector?: string
48+
inputElement?: Element
49+
debug?: boolean
50+
meilisearchOptions?: SearchParams
51+
queryDataCallback?: (data?: SearchResponse) => void
52+
autocompleteOptions?: {
53+
ariaLabel?: string
54+
cssClasses?: {
55+
prefix?: string
56+
root?: string
57+
}
58+
keyboardShortcuts?: (string | number)[]
59+
}
60+
transformData?: (hits: Hits<HitResponse>) => Hits<FormattedHit>
61+
queryHook?: (query: string) => string
62+
handleSelected?: (
63+
input: AutoCompleteInput,
64+
event: Event,
65+
suggestion: FormattedHit,
66+
datasetNumber: number,
67+
context: {
68+
selectionMethod: 'click' | 'blur' | 'enterKey' | 'tabKey' | string
69+
},
70+
) => void
71+
enhancedSearchInput?: boolean
72+
layout?: 'column' | 'simple'
73+
enableDarkMode?: boolean | 'auto'
74+
clientAgents?: Config['clientAgents']
75+
}
76+
77+
declare class DocsSearchBar<FormattedHit> {
78+
constructor(options: Options<FormattedHit>)
79+
}
80+
81+
declare global {
82+
interface Window {
83+
docsSearchBar: <FormattedHit = DefaultFormattedHit>(
84+
options: Options<FormattedHit>,
85+
) => void
86+
}
87+
}
88+
89+
export default function docsSearchBar<FormattedHit = DefaultFormattedHit>(
90+
options: Options<FormattedHit>,
91+
): DocsSearchBar<FormattedHit>

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"search-as-you-type"
1010
],
1111
"main": "dist/npm/index.js",
12+
"typings": "./index.d.ts",
1213
"scripts": {
1314
"build": "./scripts/build",
1415
"build:css": "./scripts/build-css",
@@ -36,6 +37,8 @@
3637
"@babel/core": "^7.20.2",
3738
"@babel/preset-env": "^7.19.4",
3839
"@babel/register": "^7.18.9",
40+
"@typescript-eslint/eslint-plugin": "^5.44.0",
41+
"@typescript-eslint/parser": "^5.44.0",
3942
"autoprefixer": "^9.8.8",
4043
"babel-eslint": "^10.1.0",
4144
"babel-jest": "^27.0.2",
@@ -47,6 +50,7 @@
4750
"eslint": "^7.21.0",
4851
"eslint-config-algolia": "^16.0.0",
4952
"eslint-config-prettier": "^8.1.0",
53+
"eslint-config-typescript": "^3.0.0",
5054
"eslint-plugin-cypress": "^2.12.1",
5155
"eslint-plugin-eslint-comments": "^3.2.0",
5256
"eslint-plugin-import": "^2.22.0",
@@ -59,6 +63,7 @@
5963
"pretty-bytes-cli": "^2.0.0",
6064
"sass": "^1.56.1",
6165
"sinon": "^14.0.2",
66+
"typescript": "^4.9.3",
6267
"uglify-js": "^3.17.4",
6368
"webpack": "^5.75.0",
6469
"webpack-cli": "^5.0.0"

0 commit comments

Comments
 (0)