Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add index.d.ts file for typescript compatible #686

Merged
merged 7 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 30 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module.exports = {
plugins: ['prettier', 'cypress'],
rules: {
'no-console': 0,
'no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
'eslint-comments/disable-enable-pair': ['error', { allowWholeFile: true }],
'prettier/prettier': [
'error',
Expand All @@ -19,4 +18,34 @@ module.exports = {
},
],
},
overrides: [
{
files: ['**/*.ts'],
extends: [
'algolia',
'algolia/jest',
'plugin:prettier/recommended',
'plugin:cypress/recommended',
'plugin:@typescript-eslint/recommended',
],
plugins: ['prettier', 'cypress', '@typescript-eslint'],
rules: {
'no-console': 0,
'@typescript-eslint/no-unused-vars': [
'error',
{ argsIgnorePattern: '^_' },
],
'eslint-comments/disable-enable-pair': [
'error',
{ allowWholeFile: true },
],
'prettier/prettier': [
'error',
{
trailingComma: 'all',
},
],
},
},
],
}
91 changes: 91 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* eslint camelcase: ["off"] */

import { SearchResponse, SearchParams, Hits, Config } from 'meilisearch'

interface HitResponse {
anchor: string
content: string
hierarchy_lvl0: string | null
hierarchy_lvl1: string | null
hierarchy_lvl2: string | null
hierarchy_lvl3: string | null
hierarchy_lvl4: string | null
hierarchy_lvl5: string | null
hierarchy_lvl6: string | null
objectID: string
url: string
[key: string]: unknown
}

interface DefaultFormattedHit {
isLvl0: boolean
isLvl1: boolean
isLbl2: boolean
isLvl1EmptyOrDuplicate: boolean
isCategoryHeader: boolean
isSubCategoryHeader: boolean
isTextOrSubcategoryNonEmpty: boolean
subcategory: string | null
text: string | null
title: string | null
url: string | null
}

interface AutoCompleteInput {
open: () => void
close: () => void
destroy: () => void
getWrapper: () => Element
getVal: () => string
setVal: (val: string) => void
}

interface Options<FormattedHit = DefaultFormattedHit> {
hostUrl: string
apiKey: string
indexUid: string
inputSelector?: string
inputElement?: Element
debug?: boolean
meilisearchOptions?: SearchParams
queryDataCallback?: (data?: SearchResponse) => void
autocompleteOptions?: {
ariaLabel?: string
cssClasses?: {
prefix?: string
root?: string
}
keyboardShortcuts?: (string | number)[]
}
transformData?: (hits: Hits<HitResponse>) => Hits<FormattedHit>
queryHook?: (query: string) => string
handleSelected?: (
input: AutoCompleteInput,
event: Event,
suggestion: FormattedHit,
datasetNumber: number,
context: {
selectionMethod: 'click' | 'blur' | 'enterKey' | 'tabKey' | string
},
) => void
enhancedSearchInput?: boolean
layout?: 'column' | 'simple'
enableDarkMode?: boolean | 'auto'
clientAgents?: Config['clientAgents']
}

declare class DocsSearchBar<FormattedHit> {
constructor(options: Options<FormattedHit>)
}

declare global {
interface Window {
docsSearchBar: <FormattedHit = DefaultFormattedHit>(
options: Options<FormattedHit>,
) => void
}
}

export default function docsSearchBar<FormattedHit = DefaultFormattedHit>(
options: Options<FormattedHit>,
): DocsSearchBar<FormattedHit>
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"search-as-you-type"
],
"main": "dist/npm/index.js",
"typings": "./index.d.ts",
"scripts": {
"build": "./scripts/build",
"build:css": "./scripts/build-css",
Expand Down Expand Up @@ -36,6 +37,8 @@
"@babel/core": "^7.20.2",
"@babel/preset-env": "^7.19.4",
"@babel/register": "^7.18.9",
"@typescript-eslint/eslint-plugin": "^5.44.0",
"@typescript-eslint/parser": "^5.44.0",
"autoprefixer": "^9.8.8",
"babel-eslint": "^10.1.0",
"babel-jest": "^27.0.2",
Expand All @@ -47,6 +50,7 @@
"eslint": "^7.21.0",
"eslint-config-algolia": "^16.0.0",
"eslint-config-prettier": "^8.1.0",
"eslint-config-typescript": "^3.0.0",
"eslint-plugin-cypress": "^2.12.1",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-import": "^2.22.0",
Expand All @@ -59,6 +63,7 @@
"pretty-bytes-cli": "^2.0.0",
"sass": "^1.56.1",
"sinon": "^14.0.2",
"typescript": "^4.9.3",
"uglify-js": "^3.17.4",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.0"
Expand Down